feeds_parser_sitemap.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * Tests for plugins/FeedsSitemapParser.inc
  5. */
  6. /**
  7. * Test Sitemap parser.
  8. */
  9. class FeedsSitemapParserTestCase extends FeedsWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Sitemap parser',
  13. 'description' => 'Regression tests for Sitemap XML format parser.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Run tests.
  19. */
  20. public function test() {
  21. $this->createImporterConfiguration('Sitemap', 'sitemap');
  22. $this->setPlugin('sitemap', 'FeedsSitemapParser');
  23. $this->addMappings('sitemap',
  24. array(
  25. 0 => array(
  26. 'source' => 'changefreq',
  27. 'target' => 'title',
  28. ),
  29. 1 => array(
  30. 'source' => 'priority',
  31. 'target' => 'body',
  32. ),
  33. 2 => array(
  34. 'source' => 'lastmod',
  35. 'target' => 'created',
  36. ),
  37. 3 => array(
  38. 'source' => 'url',
  39. 'target' => 'url',
  40. 'unique' => TRUE,
  41. ),
  42. 4 => array(
  43. 'source' => 'url',
  44. 'target' => 'guid',
  45. 'unique' => TRUE,
  46. ),
  47. )
  48. );
  49. $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/';
  50. $nid = $this->createFeedNode('sitemap', $path . 'sitemap-example.xml', 'Testing Sitemap Parser');
  51. $this->assertText('Created 5 nodes');
  52. // Assert DB status.
  53. $count = db_query("SELECT COUNT(*) FROM {feeds_item} WHERE entity_type = 'node'")->fetchField();
  54. $this->assertEqual($count, 5, 'Accurate number of items in database.');
  55. // Check items against known content of feed.
  56. $items = db_query("SELECT * FROM {feeds_item} WHERE entity_type = 'node' AND feed_nid = :nid ORDER BY entity_id", array(':nid' => $nid));
  57. // Check first item.
  58. date_default_timezone_set('GMT');
  59. $item = $items->fetchObject();
  60. $node = node_load($item->entity_id);
  61. $this->assertEqual($node->title, 'monthly', 'Feed item 1 changefreq is correct.');
  62. $this->assertEqual($node->body[LANGUAGE_NONE][0]['value'], '0.8', 'Feed item 1 priority is correct.');
  63. $this->assertEqual($node->created, strtotime('2005-01-01'), 'Feed item 1 lastmod is correct.');
  64. $info = feeds_item_info_load('node', $node->nid);
  65. $this->assertEqual($info->url, 'http://www.example.com/', 'Feed item 1 url is correct.');
  66. $this->assertEqual($info->url, $info->guid, 'Feed item 1 guid is correct.');
  67. // Check second item.
  68. $item = $items->fetchObject();
  69. $node = node_load($item->entity_id);
  70. $this->assertEqual($node->title, 'weekly', 'Feed item 2 changefreq is correct.');
  71. $this->assertTrue(empty($node->body[LANGUAGE_NONE]), 'Feed item 2 priority is correct.');
  72. // $node->created is... recently
  73. $info = feeds_item_info_load('node', $node->nid);
  74. $this->assertEqual($info->url, 'http://www.example.com/catalog?item=12&desc=vacation_hawaii', 'Feed item 2 url is correct.');
  75. $this->assertEqual($info->url, $info->guid, 'Feed item 2 guid is correct.');
  76. // Check third item.
  77. $item = $items->fetchObject();
  78. $node = node_load($item->entity_id);
  79. $this->assertEqual($node->title, 'weekly', 'Feed item 3 changefreq is correct.');
  80. $this->assertTrue(empty($node->body[LANGUAGE_NONE]), 'Feed item 3 priority is correct.');
  81. $this->assertEqual($node->created, strtotime('2004-12-23'), 'Feed item 3 lastmod is correct.');
  82. $info = feeds_item_info_load('node', $node->nid);
  83. $this->assertEqual($info->url, 'http://www.example.com/catalog?item=73&desc=vacation_new_zealand', 'Feed item 3 url is correct.');
  84. $this->assertEqual($info->url, $info->guid, 'Feed item 3 guid is correct.');
  85. // Check fourth item.
  86. $item = $items->fetchObject();
  87. $node = node_load($item->entity_id);
  88. $this->assertEqual($node->title, '', 'Feed item 4 changefreq is correct.');
  89. $this->assertEqual($node->body[LANGUAGE_NONE][0]['value'], '0.3', 'Feed item 4 priority is correct.');
  90. $this->assertEqual($node->created, strtotime('2004-12-23T18:00:15+00:00'), 'Feed item 4 lastmod is correct.');
  91. $info = feeds_item_info_load('node', $node->nid);
  92. $this->assertEqual($info->url, 'http://www.example.com/catalog?item=74&desc=vacation_newfoundland', 'Feed item 4 url is correct.');
  93. $this->assertEqual($info->url, $info->guid, 'Feed item 1 guid is correct.');
  94. // Check fifth item.
  95. $item = $items->fetchObject();
  96. $node = node_load($item->entity_id);
  97. $this->assertEqual($node->title, '', 'Feed item 5 changefreq is correct.');
  98. $this->assertTrue(empty($node->body[LANGUAGE_NONE]), 'Feed item 5 priority is correct.');
  99. $this->assertEqual($node->created, strtotime('2004-11-23'), 'Feed item 5 lastmod is correct.');
  100. $info = feeds_item_info_load('node', $node->nid);
  101. $this->assertEqual($info->url, 'http://www.example.com/catalog?item=83&desc=vacation_usa', 'Feed item 5 url is correct.');
  102. $this->assertEqual($info->url, $info->guid, 'Feed item 5 guid is correct.');
  103. // Check for more items.
  104. $item = $items->fetchObject();
  105. $this->assertFalse($item, 'Correct number of feed items recorded.');
  106. }
  107. }