xmlsitemap_node.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for the xmlsitemap_node module.
  5. */
  6. /**
  7. * Node Functional Test.
  8. */
  9. class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
  10. /**
  11. * Normal User.
  12. *
  13. * @var string
  14. *
  15. * @codingStandardsIgnoreStart
  16. */
  17. protected $normal_user;
  18. /**
  19. * Nodes.
  20. *
  21. * @var array
  22. */
  23. protected $nodes = array();
  24. /**
  25. * Get Info.
  26. */
  27. public static function getInfo() {
  28. return array(
  29. 'name' => 'XML sitemap node',
  30. 'description' => 'Functional tests for the XML sitemap node module.',
  31. 'group' => 'XML sitemap',
  32. );
  33. }
  34. /**
  35. * Setup.
  36. */
  37. public function setUp($modules = array()) {
  38. $modules[] = 'xmlsitemap_node';
  39. $modules[] = 'comment';
  40. parent::setUp($modules);
  41. $this->admin_user = $this->drupalCreateUser(array(
  42. 'administer nodes',
  43. 'bypass node access',
  44. 'administer content types',
  45. 'administer xmlsitemap',
  46. ));
  47. $this->normal_user = $this->drupalCreateUser(array(
  48. 'create page content',
  49. 'edit any page content',
  50. 'access content',
  51. 'view own unpublished content',
  52. ));
  53. xmlsitemap_link_bundle_settings_save('node', 'page', array('status' => 1, 'priority' => 0.5));
  54. }
  55. /**
  56. * Node Settings.
  57. */
  58. public function testNodeSettings() {
  59. $body_field = 'body[' . LANGUAGE_NONE . '][0][value]';
  60. $node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid));
  61. $this->cronRun();
  62. $this->assertSitemapLinkValues('node', $node->nid, array(
  63. 'access' => 0,
  64. 'status' => 1,
  65. 'priority' => 0.5,
  66. 'status_override' => 0,
  67. 'priority_override' => 0,
  68. ));
  69. $this->drupalLogin($this->normal_user);
  70. $this->drupalGet('node/' . $node->nid . '/edit');
  71. $this->assertNoField('xmlsitemap[status]');
  72. $this->assertNoField('xmlsitemap[priority]');
  73. $edit = array(
  74. 'title' => 'Test node title',
  75. $body_field => 'Test node body',
  76. );
  77. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  78. $this->assertText('Basic page Test node title has been updated.');
  79. $this->cronRun();
  80. $this->assertSitemapLinkValues('node', $node->nid, array(
  81. 'access' => 0,
  82. 'status' => 1,
  83. 'priority' => 0.5,
  84. 'status_override' => 0,
  85. 'priority_override' => 0,
  86. ));
  87. $this->drupalLogin($this->admin_user);
  88. $this->drupalGet('node/' . $node->nid . '/edit');
  89. $this->assertField('xmlsitemap[status]');
  90. $this->assertField('xmlsitemap[priority]');
  91. $edit = array(
  92. 'xmlsitemap[status]' => 0,
  93. 'xmlsitemap[priority]' => 0.9,
  94. 'status' => TRUE,
  95. );
  96. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  97. $this->assertText('Basic page Test node title has been updated.');
  98. $this->cronRun();
  99. $this->assertSitemapLinkValues('node', $node->nid, array(
  100. 'access' => 1,
  101. 'status' => 0,
  102. 'priority' => 0.9,
  103. 'status_override' => 1,
  104. 'priority_override' => 1,
  105. ));
  106. $edit = array(
  107. 'xmlsitemap[status]' => 'default',
  108. 'xmlsitemap[priority]' => 'default',
  109. 'status' => FALSE,
  110. );
  111. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  112. $this->assertText('Basic page Test node title has been updated.');
  113. $this->cronRun();
  114. $this->assertSitemapLinkValues('node', $node->nid, array(
  115. 'access' => 0,
  116. 'status' => 1,
  117. 'priority' => 0.5,
  118. 'status_override' => 0,
  119. 'priority_override' => 0,
  120. ));
  121. }
  122. /**
  123. * Test the content type settings.
  124. */
  125. public function testTypeSettings() {
  126. $this->drupalLogin($this->admin_user);
  127. $node_old = $this->drupalCreateNode();
  128. $this->cronRun();
  129. $this->assertSitemapLinkValues('node', $node_old->nid, array('status' => 1, 'priority' => 0.5));
  130. $edit = array(
  131. 'xmlsitemap[status]' => 0,
  132. 'xmlsitemap[priority]' => '0.0',
  133. );
  134. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  135. $this->assertText('The content type Basic page has been updated.');
  136. $node = $this->drupalCreateNode();
  137. $this->cronRun();
  138. $this->assertSitemapLinkValues('node', $node->nid, array('status' => 0, 'priority' => 0.0));
  139. $this->assertSitemapLinkValues('node', $node_old->nid, array('status' => 0, 'priority' => 0.0));
  140. $edit = array(
  141. 'type' => 'page2',
  142. 'xmlsitemap[status]' => 1,
  143. 'xmlsitemap[priority]' => '0.5',
  144. );
  145. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  146. $this->assertText('Changed the content type of 2 posts from page to page2.');
  147. $this->assertText('The content type Basic page has been updated.');
  148. $this->cronRun();
  149. $this->assertSitemapLinkValues('node', $node->nid, array(
  150. 'subtype' => 'page2',
  151. 'status' => 1,
  152. 'priority' => 0.5,
  153. ));
  154. $this->assertSitemapLinkValues('node', $node_old->nid, array(
  155. 'subtype' => 'page2',
  156. 'status' => 1,
  157. 'priority' => 0.5,
  158. ));
  159. $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page'))), 0);
  160. $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2'))), 2);
  161. $this->drupalPost('admin/structure/types/manage/page2/delete', array(), t('Delete'));
  162. $this->assertText('The content type Basic page has been deleted.');
  163. $this->assertFalse(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2')), 'Nodes with deleted node type removed from {xmlsitemap}.');
  164. }
  165. /**
  166. * Test the import of old nodes via cron.
  167. */
  168. public function testCron() {
  169. $limit = 5;
  170. variable_set('xmlsitemap_batch_limit', $limit);
  171. $nodes = array();
  172. for ($i = 1; $i <= ($limit + 1); $i++) {
  173. $node = $this->drupalCreateNode();
  174. array_push($nodes, $node);
  175. // Need to delay by one second so the nodes don't all have the same
  176. // timestamp.
  177. sleep(1);
  178. }
  179. // Clear all the node link data so we can emulate 'old' nodes.
  180. db_delete('xmlsitemap')
  181. ->condition('type', 'node')
  182. ->execute();
  183. // Run cron to import old nodes.
  184. xmlsitemap_node_cron();
  185. for ($i = 1; $i <= ($limit + 1); $i++) {
  186. $node = array_shift($nodes);
  187. if ($i <= $limit) {
  188. // The first $limit nodes should be inserted.
  189. $this->assertSitemapLinkValues('node', $node->nid, array(
  190. 'access' => 1,
  191. 'status' => 1,
  192. 'lastmod' => $node->changed,
  193. ));
  194. }
  195. else {
  196. // Any beyond $limit should not be in the sitemap.
  197. $this->assertNoSitemapLink(array('type' => 'node', 'id' => $node->nid));
  198. }
  199. }
  200. }
  201. }