Projet

Général

Profil

Paste
Télécharger (5,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / common_syndication_parser.test @ 651307cd

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
  public static function getInfo() {
16
    return array(
17
      'name' => 'Common Syndication Parser',
18
      'description' => 'Unit tests for Common Syndication Parser.',
19
      'group' => 'Feeds',
20
    );
21
  }
22

    
23
  public function setUp() {
24
    parent::setUp(array('feeds', 'feeds_ui', 'ctools', 'job_scheduler'));
25
    feeds_include_library('common_syndication_parser.inc', 'common_syndication_parser');
26
  }
27

    
28
  /**
29
   * Dispatch tests, only use one entry point method testX to save time.
30
   */
31
  public function test() {
32
    $this->_testRSS10();
33
    $this->_testRSS2();
34
    $this->_testAtomGeoRSS();
35
    $this->_testAtomGeoRSSWithoutAuthor();
36
    $this->_testAtomEntriesWithoutBaseUrl();
37
  }
38

    
39
  /**
40
   * Test RSS 1.0.
41
   */
42
  protected function _testRSS10() {
43
    $string = $this->readFeed('magento.rss1');
44
    $feed = common_syndication_parser_parse($string);
45
    $this->assertEqual($feed['title'], 'Magento Sites Network - A directory listing of Magento Commerce stores');
46
    $this->assertEqual($feed['items'][0]['title'], 'Gezondheidswebwinkel');
47
    $this->assertEqual($feed['items'][0]['url'], 'http://www.magentosites.net/store/2010/04/28/gezondheidswebwinkel/index.html');
48
    $this->assertEqual($feed['items'][1]['url'], 'http://www.magentosites.net/store/2010/04/26/mybobinocom/index.html');
49
    $this->assertEqual($feed['items'][1]['guid'], 'http://www.magentosites.net/node/3472');
50
    $this->assertEqual($feed['items'][2]['guid'], 'http://www.magentosites.net/node/3471');
51
    $this->assertEqual($feed['items'][2]['timestamp'], 1272285294);
52
  }
53

    
54
  /**
55
   * Test RSS 2.
56
   */
57
  protected function _testRSS2() {
58
    $string = $this->readFeed('developmentseed.rss2');
59
    $feed = common_syndication_parser_parse($string);
60
    $this->assertEqual($feed['title'], 'Development Seed - Technological Solutions for Progressive Organizations');
61
    $this->assertEqual($feed['items'][0]['title'], 'Open Atrium Translation Workflow: Two Way Translation Updates');
62
    $this->assertEqual($feed['items'][1]['url'], 'http://developmentseed.org/blog/2009/oct/05/week-dc-tech-october-5th-edition');
63
    $this->assertEqual($feed['items'][1]['guid'], '973 at http://developmentseed.org');
64
    $this->assertEqual($feed['items'][2]['guid'], '972 at http://developmentseed.org');
65
    $this->assertEqual($feed['items'][2]['timestamp'], 1254493864);
66
  }
67

    
68
  /**
69
   * Test Geo RSS in Atom feed.
70
   */
71
  protected function _testAtomGeoRSS() {
72
    $string = $this->readFeed('earthquake-georss.atom');
73
    $feed = common_syndication_parser_parse($string);
74
    $this->assertEqual($feed['title'], 'USGS M2.5+ Earthquakes');
75
    $this->assertEqual($feed['items'][0]['title'], 'M 2.6, Central Alaska');
76
    $this->assertEqual($feed['items'][1]['url'], 'http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/us2010axbz.php');
77
    $this->assertEqual($feed['items'][1]['guid'], 'urn:earthquake-usgs-gov:us:2010axbz');
78
    $this->assertEqual($feed['items'][2]['guid'], 'urn:earthquake-usgs-gov:us:2010axbr');
79
    $this->assertEqual($feed['items'][2]['geolocations'][0]['name'], '-53.1979 -118.0676');
80
    $this->assertEqual($feed['items'][2]['geolocations'][0]['lat'], '-53.1979');
81
    $this->assertEqual($feed['items'][2]['geolocations'][0]['lon'], '-118.0676');
82
    $this->assertEqual($feed['items'][3]['geolocations'][0]['name'], '-43.4371 172.5902');
83
    $this->assertEqual($feed['items'][3]['geolocations'][0]['lat'], '-43.4371');
84
    $this->assertEqual($feed['items'][3]['geolocations'][0]['lon'], '172.5902');
85
  }
86

    
87
  /**
88
   * Tests parsing an Atom feed without an author.
89
   */
90
  protected function _testAtomGeoRSSWithoutAuthor() {
91
    $string = $this->readFeed('earthquake-georss-noauthor.atom');
92
    $feed = common_syndication_parser_parse($string);
93
  }
94

    
95
  /**
96
   * Tests if the base url is prepended for entries without base url.
97
   *
98
   * For example, the url in the following entry should be parsed as
99
   * 'http://www.example.com/node/123' and not as 'node/123'.
100
   * @code
101
   * <entry>
102
   *   <link href="node/123"/>
103
   * </entry>
104
   * @endcode
105
   */
106
  protected function _testAtomEntriesWithoutBaseUrl() {
107
    $string = $this->readFeed('entries-without-base-url.atom');
108
    $feed = common_syndication_parser_parse($string);
109

    
110
    // Assert that all items got the base url assigned.
111
    $expected = array(
112
      'http://www.example.com/node/1281496#comment-11669575',
113
      'http://www.example.com/node/1281496#comment-10080648',
114
      'http://www.example.com/node/1281496#comment-10062564',
115
    );
116
    foreach ($feed['items'] as $key => $item) {
117
      $this->assertEqual($expected[$key], $item['url']);
118
    }
119
  }
120

    
121
  /**
122
   * Helper to read a feed.
123
   */
124
  protected function readFeed($filename) {
125
    $feed = dirname(__FILE__) . '/feeds/' . $filename;
126
    $handle = fopen($feed, 'r');
127
    $string = fread($handle, filesize($feed));
128
    fclose($handle);
129
    return $string;
130
  }
131
}