xmlsitemap_node.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for the xmlsitemap_node module.
  5. */
  6. class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
  7. protected $normal_user;
  8. protected $nodes = array();
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'XML sitemap node',
  12. 'description' => 'Functional tests for the XML sitemap node module.',
  13. 'group' => 'XML sitemap',
  14. );
  15. }
  16. function setUp($modules = array()) {
  17. $modules[] = 'xmlsitemap_node';
  18. $modules[] = 'comment';
  19. parent::setUp($modules);
  20. $this->admin_user = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'administer content types', 'administer xmlsitemap'));
  21. $this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'access content', 'view own unpublished content'));
  22. xmlsitemap_link_bundle_settings_save('node', 'page', array('status' => 1, 'priority' => 0.5));
  23. }
  24. function testNodeSettings() {
  25. $body_field = 'body[' . LANGUAGE_NONE . '][0][value]';
  26. $node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid));
  27. $this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  28. $this->drupalLogin($this->normal_user);
  29. $this->drupalGet('node/' . $node->nid . '/edit');
  30. $this->assertNoField('xmlsitemap[status]');
  31. $this->assertNoField('xmlsitemap[priority]');
  32. $edit = array(
  33. 'title' => 'Test node title',
  34. $body_field => 'Test node body',
  35. );
  36. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  37. $this->assertText('Basic page Test node title has been updated.');
  38. $this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  39. $this->drupalLogin($this->admin_user);
  40. $this->drupalGet('node/' . $node->nid . '/edit');
  41. $this->assertField('xmlsitemap[status]');
  42. $this->assertField('xmlsitemap[priority]');
  43. $edit = array(
  44. 'xmlsitemap[status]' => 0,
  45. 'xmlsitemap[priority]' => 0.9,
  46. 'status' => TRUE,
  47. );
  48. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  49. $this->assertText('Basic page Test node title has been updated.');
  50. $this->assertSitemapLinkValues('node', $node->nid, array('access' => 1, 'status' => 0, 'priority' => 0.9, 'status_override' => 1, 'priority_override' => 1));
  51. $edit = array(
  52. 'xmlsitemap[status]' => 'default',
  53. 'xmlsitemap[priority]' => 'default',
  54. 'status' => FALSE,
  55. );
  56. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  57. $this->assertText('Basic page Test node title has been updated.');
  58. $this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  59. }
  60. /**
  61. * Test the content type settings.
  62. */
  63. function testTypeSettings() {
  64. $this->drupalLogin($this->admin_user);
  65. $node_old = $this->drupalCreateNode();
  66. $this->assertSitemapLinkValues('node', $node_old->nid, array('status' => 1, 'priority' => 0.5));
  67. $edit = array(
  68. 'xmlsitemap[status]' => 0,
  69. 'xmlsitemap[priority]' => '0.0',
  70. );
  71. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  72. $this->assertText('The content type Basic page has been updated.');
  73. $node = $this->drupalCreateNode();
  74. $this->assertSitemapLinkValues('node', $node->nid, array('status' => 0, 'priority' => 0.0));
  75. $this->assertSitemapLinkValues('node', $node_old->nid, array('status' => 0, 'priority' => 0.0));
  76. $edit = array(
  77. 'type' => 'page2',
  78. 'xmlsitemap[status]' => 1,
  79. 'xmlsitemap[priority]' => '0.5',
  80. );
  81. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  82. $this->assertText('Changed the content type of 2 posts from page to page2.');
  83. $this->assertText('The content type Basic page has been updated.');
  84. $this->assertSitemapLinkValues('node', $node->nid, array('subtype' => 'page2', 'status' => 1, 'priority' => 0.5));
  85. $this->assertSitemapLinkValues('node', $node_old->nid, array('subtype' => 'page2', 'status' => 1, 'priority' => 0.5));
  86. $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page'))), 0);
  87. $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2'))), 2);
  88. $this->drupalPost('admin/structure/types/manage/page2/delete', array(), t('Delete'));
  89. $this->assertText('The content type Basic page has been deleted.');
  90. $this->assertFalse(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2')), 'Nodes with deleted node type removed from {xmlsitemap}.');
  91. }
  92. /**
  93. * Test the import of old nodes via cron.
  94. */
  95. function testCron() {
  96. $limit = 5;
  97. variable_set('xmlsitemap_batch_limit', $limit);
  98. $nodes = array();
  99. for ($i = 1; $i <= ($limit + 1); $i++) {
  100. $node = $this->drupalCreateNode();
  101. array_push($nodes, $node);
  102. // Need to delay by one second so the nodes don't all have the same
  103. // timestamp.
  104. sleep(1);
  105. }
  106. // Clear all the node link data so we can emulate 'old' nodes.
  107. db_delete('xmlsitemap')
  108. ->condition('type', 'node')
  109. ->execute();
  110. // Run cron to import old nodes.
  111. xmlsitemap_node_cron();
  112. for ($i = 1; $i <= ($limit + 1); $i++) {
  113. $node = array_pop($nodes);
  114. if ($i <= $limit) {
  115. // The first $limit nodes should be inserted.
  116. $this->assertSitemapLinkValues('node', $node->nid, array('access' => 1, 'status' => 1, 'lastmod' => $node->changed));
  117. }
  118. else {
  119. // Any beyond $limit should not be in the sitemap.
  120. $this->assertNoSitemapLink(array('type' => 'node', 'id' => $node->nid));
  121. }
  122. }
  123. }
  124. }