1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for the common syndication parser.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test cases for common syndication parser library.
|
10
|
*
|
11
|
* @todo Break out into Drupal independent test framework.
|
12
|
* @todo Could I use DrupalUnitTestCase here?
|
13
|
*/
|
14
|
class CommonSyndicationParserTestCase extends DrupalWebTestCase {
|
15
|
|
16
|
/**
|
17
|
* {@inheritdoc}
|
18
|
*/
|
19
|
public static function getInfo() {
|
20
|
return array(
|
21
|
'name' => 'Common Syndication Parser',
|
22
|
'description' => 'Unit tests for Common Syndication Parser.',
|
23
|
'group' => 'Feeds',
|
24
|
);
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* {@inheritdoc}
|
29
|
*/
|
30
|
public function setUp() {
|
31
|
parent::setUp(array('feeds', 'feeds_ui', 'ctools', 'job_scheduler'));
|
32
|
feeds_include_library('common_syndication_parser.inc', 'common_syndication_parser');
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Dispatch tests, only use one entry point method testX to save time.
|
37
|
*/
|
38
|
public function test() {
|
39
|
$this->_testRSS10();
|
40
|
$this->_testRSS2();
|
41
|
$this->_testAtomGeoRSS();
|
42
|
$this->_testAtomGeoRSSWithoutAuthor();
|
43
|
$this->_testAtomEntriesWithoutBaseUrl();
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Test RSS 1.0.
|
48
|
*/
|
49
|
protected function _testRSS10() {
|
50
|
$string = $this->readFeed('magento.rss1');
|
51
|
$feed = common_syndication_parser_parse($string);
|
52
|
$this->assertEqual($feed['title'], 'Magento Sites Network - A directory listing of Magento Commerce stores');
|
53
|
$this->assertEqual($feed['items'][0]['title'], 'Gezondheidswebwinkel');
|
54
|
$this->assertEqual($feed['items'][0]['url'], 'http://www.magentosites.net/store/2010/04/28/gezondheidswebwinkel/index.html');
|
55
|
$this->assertEqual($feed['items'][1]['url'], 'http://www.magentosites.net/store/2010/04/26/mybobinocom/index.html');
|
56
|
$this->assertEqual($feed['items'][1]['guid'], 'http://www.magentosites.net/node/3472');
|
57
|
$this->assertEqual($feed['items'][2]['guid'], 'http://www.magentosites.net/node/3471');
|
58
|
$this->assertEqual($feed['items'][2]['timestamp'], 1272285294);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Test RSS 2.
|
63
|
*/
|
64
|
protected function _testRSS2() {
|
65
|
$string = $this->readFeed('developmentseed.rss2');
|
66
|
$feed = common_syndication_parser_parse($string);
|
67
|
$this->assertEqual($feed['title'], 'Development Seed - Technological Solutions for Progressive Organizations');
|
68
|
$this->assertEqual($feed['items'][0]['title'], 'Open Atrium Translation Workflow: Two Way Translation Updates');
|
69
|
$this->assertEqual($feed['items'][1]['url'], 'http://developmentseed.org/blog/2009/oct/05/week-dc-tech-october-5th-edition');
|
70
|
$this->assertEqual($feed['items'][1]['guid'], '973 at http://developmentseed.org');
|
71
|
$this->assertEqual($feed['items'][2]['guid'], '972 at http://developmentseed.org');
|
72
|
$this->assertEqual($feed['items'][2]['timestamp'], 1254493864);
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Test Geo RSS in Atom feed.
|
77
|
*/
|
78
|
protected function _testAtomGeoRSS() {
|
79
|
$string = $this->readFeed('earthquake-georss.atom');
|
80
|
$feed = common_syndication_parser_parse($string);
|
81
|
$this->assertEqual($feed['title'], 'USGS M2.5+ Earthquakes');
|
82
|
$this->assertEqual($feed['items'][0]['title'], 'M 2.6, Central Alaska');
|
83
|
$this->assertEqual($feed['items'][1]['url'], 'http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/us2010axbz.php');
|
84
|
$this->assertEqual($feed['items'][1]['guid'], 'urn:earthquake-usgs-gov:us:2010axbz');
|
85
|
$this->assertEqual($feed['items'][2]['guid'], 'urn:earthquake-usgs-gov:us:2010axbr');
|
86
|
$this->assertEqual($feed['items'][2]['geolocations'][0]['name'], '-53.1979 -118.0676');
|
87
|
$this->assertEqual($feed['items'][2]['geolocations'][0]['lat'], '-53.1979');
|
88
|
$this->assertEqual($feed['items'][2]['geolocations'][0]['lon'], '-118.0676');
|
89
|
$this->assertEqual($feed['items'][3]['geolocations'][0]['name'], '-43.4371 172.5902');
|
90
|
$this->assertEqual($feed['items'][3]['geolocations'][0]['lat'], '-43.4371');
|
91
|
$this->assertEqual($feed['items'][3]['geolocations'][0]['lon'], '172.5902');
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Tests parsing an Atom feed without an author.
|
96
|
*/
|
97
|
protected function _testAtomGeoRSSWithoutAuthor() {
|
98
|
$string = $this->readFeed('earthquake-georss-noauthor.atom');
|
99
|
$feed = common_syndication_parser_parse($string);
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Tests if the base url is prepended for entries without base url.
|
104
|
*
|
105
|
* For example, the url in the following entry should be parsed as
|
106
|
* 'http://www.example.com/node/123' and not as 'node/123'.
|
107
|
* @code
|
108
|
* <entry>
|
109
|
* <link href="node/123"/>
|
110
|
* </entry>
|
111
|
* @endcode
|
112
|
*/
|
113
|
protected function _testAtomEntriesWithoutBaseUrl() {
|
114
|
$string = $this->readFeed('entries-without-base-url.atom');
|
115
|
$feed = common_syndication_parser_parse($string);
|
116
|
|
117
|
// Assert that all items got the base url assigned.
|
118
|
$expected = array(
|
119
|
'http://www.example.com/node/1281496#comment-11669575',
|
120
|
'http://www.example.com/node/1281496#comment-10080648',
|
121
|
'http://www.example.com/node/1281496#comment-10062564',
|
122
|
);
|
123
|
foreach ($feed['items'] as $key => $item) {
|
124
|
$this->assertEqual($expected[$key], $item['url']);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Helper to read a feed.
|
130
|
*/
|
131
|
protected function readFeed($filename) {
|
132
|
$feed = dirname(__FILE__) . '/feeds/' . $filename;
|
133
|
$handle = fopen($feed, 'r');
|
134
|
$string = fread($handle, filesize($feed));
|
135
|
fclose($handle);
|
136
|
return $string;
|
137
|
}
|
138
|
|
139
|
}
|