xmlsitemap_node.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // $Id: xmlsitemap_node.test,v 1.7 2010/01/24 07:21:22 davereid Exp $
  3. /**
  4. * @file
  5. * Unit tests for the xmlsitemap_node module.
  6. */
  7. class XMLSitemapNodeTestHelper extends XMLSitemapTestHelper {
  8. protected $admin_user;
  9. protected $normal_user;
  10. protected $nodes = array();
  11. function setUp() {
  12. parent::setUp('xmlsitemap', 'xmlsitemap_node', 'comment');
  13. $this->admin_user = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'administer content types', 'administer xmlsitemap'));
  14. $this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'access content'));
  15. variable_set('xmlsitemap_node_status_page', 1);
  16. }
  17. //function addNodes($count) {
  18. // for ($i = count($this->nodes); $i < ($count + 1); $i++) {
  19. // $this->nodes[$i] = $this->drupalCreateNode();
  20. // }
  21. //}
  22. protected function assertNodeSitemapLinkVisible(stdClass $node) {
  23. $link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
  24. return $this->assertSitemapLinkVisible($link);
  25. }
  26. protected function assertNodeSitemapLinkNotVisible(stdClass $node) {
  27. $link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
  28. return $this->assertSitemapLinkNotVisible($link);
  29. }
  30. protected function assertNodeSitemapLinkValues(stdClass $node, array $values) {
  31. $link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
  32. if (!$link) {
  33. $this->fail(t('Could not load sitemap link for node @nid.', array('@nid' => $node->nid)));
  34. }
  35. else {
  36. $this->assertSitemapLinkValues($link, $values);
  37. }
  38. }
  39. }
  40. //class XMLSitemapNodeUnitTest extends DrupalWebTestCase {
  41. // public static function getInfo() {
  42. // return array(
  43. // 'name' => 'XML Sitemap node unit tests',
  44. // 'description' => 'Unit tests for the XML Sitemap node module.',
  45. // 'group' => 'XML Sitemap',
  46. // );
  47. // }
  48. //
  49. // function setUp() {
  50. // parent::setUp('xmlsitemap', 'xmlsitemap_node');
  51. // }
  52. //}
  53. class XMLSitemapNodeFunctionalTest extends XMLSitemapNodeTestHelper {
  54. public static function getInfo() {
  55. return array(
  56. 'name' => 'XML sitemap node functional tests',
  57. 'description' => 'Interface tests for the XML sitemap node module.',
  58. 'group' => 'XML sitemap',
  59. );
  60. }
  61. function testNodeSettings() {
  62. $node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid));
  63. $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  64. $this->drupalLogin($this->normal_user);
  65. $this->drupalGet('node/' . $node->nid . '/edit');
  66. $this->assertNoField('xmlsitemap[status]');
  67. $this->assertNoField('xmlsitemap[priority]');
  68. $edit = array(
  69. 'title' => 'Test node title',
  70. 'body' => 'Test node body',
  71. );
  72. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  73. $this->assertText('Page Test node title has been updated.');
  74. $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  75. $this->drupalLogin($this->admin_user);
  76. $this->drupalGet('node/' . $node->nid . '/edit');
  77. $this->assertField('xmlsitemap[status]');
  78. $this->assertField('xmlsitemap[priority]');
  79. $edit = array(
  80. 'xmlsitemap[status]' => 0,
  81. 'xmlsitemap[priority]' => 0.9,
  82. 'status' => TRUE,
  83. );
  84. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  85. $this->assertText('Page Test node title has been updated.');
  86. $this->assertNodeSitemapLinkValues($node, array('access' => 1, 'status' => 0, 'priority' => 0.9, 'status_override' => 1, 'priority_override' => 1));
  87. $edit = array(
  88. 'xmlsitemap[status]' => 'default',
  89. 'xmlsitemap[priority]' => 'default',
  90. 'status' => FALSE,
  91. );
  92. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  93. $this->assertText('Page Test node title has been updated.');
  94. $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
  95. }
  96. /**
  97. * Test the content type settings.
  98. */
  99. function testTypeSettings() {
  100. $this->drupalLogin($this->admin_user);
  101. $node_old = $this->drupalCreateNode();
  102. $this->assertNodeSitemapLinkValues($node_old, array('status' => 1, 'priority' => 0.5));
  103. $edit = array(
  104. 'xmlsitemap_node_status' => 0,
  105. 'xmlsitemap_node_priority' => '0.0',
  106. );
  107. $this->drupalGet('admin/content/node-type/page');
  108. $this->drupalPost('admin/content/node-type/page', $edit, t('Save content type'));
  109. $this->assertText(t('The content type Page has been updated.'));
  110. $node = $this->drupalCreateNode();
  111. $this->assertNodeSitemapLinkValues($node, array('status' => 0, 'priority' => 0.0));
  112. $this->assertNodeSitemapLinkValues($node_old, array('status' => 0, 'priority' => 0.0));
  113. $edit = array(
  114. 'xmlsitemap_node_status' => 1,
  115. 'xmlsitemap_node_priority' => '0.5',
  116. );
  117. $this->drupalPost('admin/content/node-type/page', $edit, t('Save content type'));
  118. $this->assertText(t('The content type Page has been updated.'));
  119. $this->assertNodeSitemapLinkValues($node, array('status' => 1, 'priority' => 0.5));
  120. $this->assertNodeSitemapLinkValues($node_old, array('status' => 1, 'priority' => 0.5));
  121. //$this->drupalPost('node/' . $node->nid . '/edit', array(), t('Save'));
  122. //$this->assertText(t('Page @title has been updated.', array('@title' => $node->title)));
  123. }
  124. /**
  125. * Test the import of old nodes via cron.
  126. */
  127. function testCron() {
  128. $this->drupalLogin($this->admin_user);
  129. $limit = 5;
  130. $this->drupalPost('admin/config/search/xmlsitemap', array('xmlsitemap_batch_limit' => $limit), t('Save configuration'));
  131. $this->assertText(t('The configuration options have been saved.'));
  132. $nodes = array();
  133. for ($i = 1; $i <= ($limit + 1); $i++) {
  134. $node = $this->drupalCreateNode();
  135. array_push($nodes, $node);
  136. // Need to delay by one second so the nodes don't all have the same
  137. // timestamp.
  138. sleep(1);
  139. }
  140. // Clear all the node link data so we can emulate 'old' nodes.
  141. db_delete('xmlsitemap')
  142. ->condition('type', 'node')
  143. ->execute();
  144. // Run cron to import old nodes.
  145. xmlsitemap_node_cron();
  146. for ($i = 1; $i <= ($limit + 1); $i++) {
  147. $node = array_pop($nodes);
  148. if ($i <= $limit) {
  149. // The first $limit nodes should be inserted.
  150. $this->assertNodeSitemapLinkValues($node, array('access' => 1, 'status' => 1, 'lastmod' => $node->changed));
  151. }
  152. else {
  153. // Any beyond $limit should not be in the sitemap.
  154. $this->assertNoSitemapLink(array('type' => 'node', 'id' => $node->nid));
  155. }
  156. }
  157. }
  158. }