Projet

Général

Profil

Paste
Télécharger (6,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / simplepie / tests / LocatorTest.php @ 41cc1b08

1
<?php
2
/**
3
 * Tests for autodiscovery
4
 *
5
 * SimplePie
6
 *
7
 * A PHP-Based RSS and Atom Feed Framework.
8
 * Takes the hard work out of managing a complete RSS/Atom solution.
9
 *
10
 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification, are
14
 * permitted provided that the following conditions are met:
15
 *
16
 *         * Redistributions of source code must retain the above copyright notice, this list of
17
 *           conditions and the following disclaimer.
18
 *
19
 *         * Redistributions in binary form must reproduce the above copyright notice, this list
20
 *           of conditions and the following disclaimer in the documentation and/or other materials
21
 *           provided with the distribution.
22
 *
23
 *         * Neither the name of the SimplePie Team nor the names of its contributors may be used
24
 *           to endorse or promote products derived from this software without specific prior
25
 *           written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
28
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
30
 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @package SimplePie
38
 * @version 1.3.1
39
 * @copyright 2004-2011 Ryan Parman, Geoffrey Sneddon, Ryan McCue
40
 * @author Ryan Parman
41
 * @author Geoffrey Sneddon
42
 * @author Ryan McCue
43
 * @link http://simplepie.org/ SimplePie
44
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45
 */
46

    
47
require_once dirname(__FILE__) . '/bootstrap.php';
48

    
49
class LocatorTest extends PHPUnit_Framework_TestCase
50
{
51
        public static function feedmimetypes()
52
        {
53
                return array(
54
                        array('application/rss+xml'),
55
                        array('application/rdf+xml'),
56
                        array('text/rdf'),
57
                        array('application/atom+xml'),
58
                        array('text/xml'),
59
                        array('application/xml'),
60
                );
61
        }
62
        /**
63
         * @dataProvider feedmimetypes
64
         */
65
        public function testAutodiscoverOnFeed($mime)
66
        {
67
                $data = new MockSimplePie_File('http://example.com/feed.xml');
68
                $data->headers['content-type'] = $mime;
69

    
70
                $locator = new SimplePie_Locator($data, 0, null, false);
71

    
72
                $registry = new SimplePie_Registry();
73
                $registry->register('File', 'MockSimplePie_File');
74
                $locator->set_registry($registry);
75

    
76
                $feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all);
77
                $this->assertEquals($feed, $data);
78
        }
79

    
80
        public function testInvalidMIMEType()
81
        {
82
                $data = new MockSimplePie_File('http://example.com/feed.xml');
83
                $data->headers['content-type'] = 'application/pdf';
84

    
85
                $locator = new SimplePie_Locator($data, 0, null, false);
86

    
87
                $registry = new SimplePie_Registry();
88
                $registry->register('File', 'MockSimplePie_File');
89
                $locator->set_registry($registry);
90

    
91
                $feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all);
92
                $this->assertEquals($feed, null);
93
        }
94

    
95
        public function testDirectNoDOM()
96
        {
97
                $data = new MockSimplePie_File('http://example.com/feed.xml');
98

    
99
                $registry = new SimplePie_Registry();
100
                $locator = new SimplePie_Locator($data, 0, null, false);
101
                $locator->dom = null;
102
                $locator->set_registry($registry);
103

    
104
                $this->assertTrue($locator->is_feed($data));
105
                $this->assertEquals($locator->find(SIMPLEPIE_LOCATOR_ALL, $found), $data);
106
        }
107

    
108
        /**
109
         * @expectedException SimplePie_Exception
110
         */
111
        public function testFailDiscoveryNoDOM()
112
        {
113
                $data = new MockSimplePie_File('http://example.com/feed.xml');
114
                $data->headers['content-type'] = 'text/html';
115
                $data->body = '<!DOCTYPE html><html><body>Hi!</body></html>';
116

    
117
                $registry = new SimplePie_Registry();
118
                $locator = new SimplePie_Locator($data, 0, null, false);
119
                $locator->dom = null;
120
                $locator->set_registry($registry);
121

    
122
                $this->assertFalse($locator->is_feed($data));
123
                $this->assertFalse($locator->find(SIMPLEPIE_LOCATOR_ALL, $found));
124
        }
125

    
126
        /**
127
         * Tests from Firefox
128
         *
129
         * Tests are used under the LGPL license, see file for license
130
         * information
131
         */
132
        public static function firefoxtests()
133
        {
134
                $data = array(
135
                        array(new SimplePie_File(dirname(__FILE__) . '/data/fftests.html'))
136
                );
137
                foreach ($data as &$row)
138
                {
139
                        $row[0]->headers = array('content-type' => 'text/html');
140
                        $row[0]->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
141
                        $row[0]->url = 'http://example.com/';
142
                }
143

    
144
                return $data;
145
        }
146

    
147
        /**
148
         * @dataProvider firefoxtests
149
         */
150
        public function test_from_file($data)
151
        {
152
                $locator = new SimplePie_Locator($data, 0, null, false);
153

    
154
                $registry = new SimplePie_Registry();
155
                $registry->register('File', 'MockSimplePie_File');
156
                $locator->set_registry($registry);
157

    
158
                $expected = array();
159
                $document = new DOMDocument();
160
                $document->loadHTML($data->body);
161
                $xpath = new DOMXPath($document);
162
                foreach ($xpath->query('//link') as $element)
163
                {
164
                        $expected[] = 'http://example.com' . $element->getAttribute('href');
165
                }
166
                //$expected = SimplePie_Misc::get_element('link', $data->body);
167

    
168
                $feed = $locator->find(SIMPLEPIE_LOCATOR_ALL, $all);
169
                $this->assertFalse($locator->is_feed($data), 'HTML document not be a feed itself');
170
                $this->assertInstanceOf('MockSimplePie_File', $feed);
171
                $success = array_filter($expected, array(get_class(), 'filter_success'));
172

    
173
                $found = array_map(array(get_class(), 'map_url_file'), $all);
174
                $this->assertEquals($success, $found);
175
        }
176

    
177
        protected static function filter_success($url)
178
        {
179
                return (stripos($url, 'bogus') === false);
180
        }
181

    
182
        protected static function map_url_file($file)
183
        {
184
                return $file->url;
185
        }
186
}
187

    
188
/**
189
 * Acts as a fake feed request
190
 */
191
class MockSimplePie_File extends SimplePie_File
192
{
193
        public function __construct($url)
194
        {
195
                $this->url = $url;
196
                $this->headers = array(
197
                        'content-type' => 'application/atom+xml'
198
                );
199
                $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
200
                $this->body = '<?xml charset="utf-8"?><feed />';
201
                $this->status_code = 200;
202
        }
203
}