xmlsitemap.test 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. // $Id: xmlsitemap.test,v 1.19 2010/01/24 07:12:25 davereid Exp $
  3. /**
  4. * @file
  5. * Unit tests for the xmlsitemap module.
  6. *
  7. * @ingroup xmlsitemap
  8. */
  9. /**
  10. * Helper test class with some added functions for testing.
  11. */
  12. class XMLSitemapTestHelper extends DrupalWebTestCase {
  13. protected $admin_user;
  14. function setUp() {
  15. // Call parent::setUp() allowing test cases to pass further modules.
  16. $modules = func_get_args();
  17. $modules = array_merge(array('xmlsitemap'), $modules);
  18. call_user_func_array(array('parent', 'setUp'), $modules);
  19. // Ensure the files directory is created and writable during testing.
  20. // @todo This can be removed when http://drupal.org/node/654752 is fixed.
  21. drupal_static_reset('xmlsitemap_get_directory');
  22. $this->checkFilesDirectory();
  23. }
  24. function tearDown() {
  25. // Capture any (remaining) watchdog errors.
  26. $this->assertNoWatchdogErrors();
  27. // Reset the watchdog seen IDs for the next test run.
  28. $this->getWatchdogMessages(array(), TRUE);
  29. parent::tearDown();
  30. }
  31. /**
  32. * Check the files directory is created (massive fails if not done).
  33. *
  34. * @todo This can be removed when http://drupal.org/node/654752 is fixed.
  35. */
  36. protected function checkFilesDirectory() {
  37. if (!xmlsitemap_check_directory()) {
  38. $this->fail(t('Sitemap directory was found and writable for testing.'));
  39. }
  40. }
  41. protected function drupalGetSitemap($language = LANGUAGE_NONE, $regenerate = FALSE) {
  42. if ($regenerate) {
  43. $this->regenerateSitemap();
  44. }
  45. $this->drupalGet('sitemap.xml', array('language' => xmlsitemap_language_load($language)));
  46. $this->assertResponse(200);
  47. }
  48. /**
  49. * Regenerate the sitemap by setting the regenerate flag and running cron.
  50. */
  51. protected function regenerateSitemap() {
  52. variable_set('xmlsitemap_regenerate_needed', TRUE);
  53. variable_set('xmlsitemap_generated_last', 0);
  54. module_load_include('inc', 'xmlsitemap');
  55. xmlsitemap_regenerate();
  56. $this->assertTrue(variable_get('xmlsitemap_generated_last', 0) && !variable_get('xmlsitemap_regenerate_needed', FALSE), t('XML sitemaps regenerated and flag cleared.'));
  57. }
  58. protected function assertSitemapLink($conditions) {
  59. $link = xmlsitemap_load_link($conditions);
  60. $this->assertTrue(is_array($link), 'Link loaded.');
  61. return $link;
  62. }
  63. protected function assertNoSitemapLink($conditions) {
  64. $link = xmlsitemap_load_link($conditions);
  65. $this->assertFalse($link, 'Link not loaded.');
  66. return $link;
  67. }
  68. protected function assertSitemapLinkVisible() {
  69. $links = func_get_args();
  70. foreach ($links as $link) {
  71. $this->assertTrue($link && $link['access'] && $link['status'], 'Sitemap link is visible.');
  72. }
  73. }
  74. protected function assertSitemapLinkNotVisible() {
  75. $links = func_get_args();
  76. foreach ($links as $link) {
  77. $this->assertTrue($link && !($link['access'] && $link['status']), 'Sitemap link is not visible.');
  78. }
  79. }
  80. protected function assertSitemapLinkValues(array $link, array $conditions) {
  81. foreach ($conditions as $key => $value) {
  82. if ($value === NULL || $link[$key] === NULL) {
  83. // For nullable fields, always check for identical values (===).
  84. $this->assertIdentical($link[$key], $value, t('Identical values for link field @key.', array('@key' => $key)));
  85. }
  86. else {
  87. // Otherwise check simple equality (==).
  88. $this->assertEqual($link[$key], $value, t('Equal values for link field @key.', array('@key' => $key)));
  89. }
  90. }
  91. }
  92. protected function assertNotSitemapLinkValues(array $link, array $conditions) {
  93. foreach ($conditions as $key => $value) {
  94. if ($value === NULL || $link[$key] === NULL) {
  95. // For nullable fields, always check for identical values (===).
  96. $this->assertNotIdentical($link[$key], $value, t('Not identical values for link field @key.', array('@key' => $key)));
  97. }
  98. else {
  99. // Otherwise check simple equality (==).
  100. $this->assertNotEqual($link[$key], $value, t('Not equal values for link field @key.', array('@key' => $key)));
  101. }
  102. }
  103. }
  104. protected function assertRawSitemapLinks() {
  105. $links = func_get_args();
  106. foreach ($links as $link) {
  107. $path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
  108. $this->assertRaw($link['loc'], t('Link %path found in the sitemap.', array('%path' => $path)));
  109. }
  110. }
  111. protected function assertNoRawSitemapLinks() {
  112. $links = func_get_args();
  113. foreach ($links as $link) {
  114. $path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
  115. $this->assertNoRaw($link['loc'], t('Link %path not found in the sitemap.', array('%path' => $path)));
  116. }
  117. }
  118. protected function addSitemapLink(array $link = array()) {
  119. $last_id = &drupal_static(__FUNCTION__, 1);
  120. $link += array(
  121. 'type' => 'testing',
  122. 'id' => $last_id,
  123. );
  124. // Make the default path easier to read than a random string.
  125. $link += array('loc' => $link['type'] . '-' . $link['id']);
  126. $last_id = $link['id'] + 1;
  127. xmlsitemap_save_link($link);
  128. return $link;
  129. }
  130. protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
  131. $value = xmlsitemap_var($variable);
  132. if ($reset_if_true && $value) {
  133. variable_set('xmlsitemap_' . $variable, FALSE);
  134. }
  135. return $this->assertEqual($value, $assert_value, "xmlsitemap_$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
  136. }
  137. protected function assertXMLSitemapProblems($problem_text = FALSE) {
  138. $this->drupalGet('admin/config/search/xmlsitemap');
  139. $this->assertText(t('One or more problems were detected with your XML sitemap configuration'));
  140. if ($problem_text) {
  141. $this->assertText($problem_text);
  142. }
  143. }
  144. protected function assertNoXMLSitemapProblems() {
  145. $this->drupalGet('admin/config/search/xmlsitemap');
  146. $this->assertNoText(t('One or more problems were detected with your XML sitemap configuration'));
  147. }
  148. /**
  149. * Fetch all seen watchdog messages.
  150. *
  151. * @todo Add unit tests for this function.
  152. */
  153. protected function getWatchdogMessages(array $conditions = array(), $reset = FALSE) {
  154. static $seen_ids = array();
  155. static $levels;
  156. if (!module_exists('dblog') || $reset) {
  157. $seen_ids = array();
  158. return;
  159. }
  160. if (!isset($levels)) {
  161. $levels = watchdog_severity_levels();
  162. }
  163. $query = db_select('watchdog');
  164. $query->fields('watchdog', array('wid', 'type', 'severity', 'message', 'variables', 'timestamp'));
  165. foreach ($conditions as $field => $value) {
  166. if ($field == 'variables' && is_array($value)) {
  167. $value = serialize($value);
  168. }
  169. $query->condition($field, $value);
  170. }
  171. if ($seen_ids) {
  172. $query->condition('wid', $seen_ids, 'NOT IN');
  173. }
  174. $query->orderBy('timestamp', 'ASC');
  175. $messages = $query->execute()->fetchAllAssoc('wid');
  176. foreach ($messages as &$message) {
  177. $message->variables = unserialize($message->variables);
  178. if (!is_array($message->variables)) {
  179. $message->variables = array();
  180. }
  181. $message->text = $message->timestamp . ' - ' . $levels[$message->severity] . ' - ' . t($message->message, $message->variables);
  182. }
  183. $seen_ids = array_merge($seen_ids, array_keys($messages));
  184. return $messages;
  185. }
  186. protected function assertWatchdogMessage(array $conditions, $message = 'Watchdog message found.') {
  187. $this->assertTrue($this->getWatchdogMessages($conditions), $message);
  188. }
  189. protected function assertNoWatchdogMessage(array $conditions, $message = 'Watchdog message not found.') {
  190. $this->assertFalse($this->getWatchdogMessages($conditions), $message);
  191. }
  192. /**
  193. * Check that there were no watchdog errors or worse.
  194. */
  195. protected function assertNoWatchdogErrors() {
  196. $messages = $this->getWatchdogMessages();
  197. $verbose = array();
  198. foreach ($messages as $message) {
  199. if (in_array($message->severity, array(WATCHDOG_EMERG, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING))) {
  200. $this->fail($message->text);
  201. }
  202. $verbose[] = $message->text;
  203. }
  204. if ($verbose) {
  205. array_unshift($verbose, '<h2>Watchdog messages</h2>');
  206. $this->verbose(implode("<br />", $verbose));
  207. }
  208. }
  209. }
  210. class XMLSitemapUnitTest extends XMLSitemapTestHelper {
  211. public static function getInfo() {
  212. return array(
  213. 'name' => 'XML sitemap unit tests',
  214. 'description' => 'Unit tests for the XML sitemap module.',
  215. 'group' => 'XML sitemap',
  216. );
  217. }
  218. function setUp() {
  219. parent::setUp('xmlsitemap');
  220. }
  221. function testAssertFlag() {
  222. variable_set('xmlsitemap_rebuild_needed', TRUE);
  223. $this->assertTrue(xmlsitemap_var('rebuild_needed'));
  224. $this->assertTrue($this->assertFlag('rebuild_needed', TRUE, FALSE));
  225. $this->assertTrue(xmlsitemap_var('rebuild_needed'));
  226. $this->assertTrue($this->assertFlag('rebuild_needed', TRUE, TRUE));
  227. $this->assertFalse(xmlsitemap_var('rebuild_needed'));
  228. $this->assertTrue($this->assertFlag('rebuild_needed', FALSE, FALSE));
  229. $this->assertFalse(xmlsitemap_var('rebuild_needed'));
  230. }
  231. /**
  232. * Tests for xmlsitemap_get_changefreq().
  233. */
  234. function testGetChangefreq() {
  235. // The test values.
  236. $values = array(
  237. 0,
  238. mt_rand(1, XMLSITEMAP_FREQUENCY_ALWAYS),
  239. mt_rand(XMLSITEMAP_FREQUENCY_ALWAYS + 1, XMLSITEMAP_FREQUENCY_HOURLY),
  240. mt_rand(XMLSITEMAP_FREQUENCY_HOURLY + 1, XMLSITEMAP_FREQUENCY_DAILY),
  241. mt_rand(XMLSITEMAP_FREQUENCY_DAILY + 1, XMLSITEMAP_FREQUENCY_WEEKLY),
  242. mt_rand(XMLSITEMAP_FREQUENCY_WEEKLY + 1, XMLSITEMAP_FREQUENCY_MONTHLY),
  243. mt_rand(XMLSITEMAP_FREQUENCY_MONTHLY + 1, XMLSITEMAP_FREQUENCY_YEARLY),
  244. mt_rand(XMLSITEMAP_FREQUENCY_YEARLY + 1, mt_getrandmax()),
  245. );
  246. // The expected values.
  247. $expected = array(
  248. FALSE,
  249. 'always',
  250. 'hourly',
  251. 'daily',
  252. 'weekly',
  253. 'monthly',
  254. 'yearly',
  255. 'never',
  256. );
  257. foreach ($values as $i => $value) {
  258. $actual = xmlsitemap_get_changefreq($value);
  259. $this->assertIdentical($actual, $expected[$i]);
  260. }
  261. }
  262. /**
  263. * Tests for xmlsitemap_get_chunk_count().
  264. */
  265. function testGetChunkCount() {
  266. // Set a low chunk size for testing.
  267. variable_set('xmlsitemap_chunk_size', 4);
  268. // Make the total number of links just equal to the chunk size.
  269. $count = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();
  270. for ($i = $count; $i < 4; $i++) {
  271. $this->addSitemapLink();
  272. $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
  273. }
  274. $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 4);
  275. // Add a disabled link, should not change the chunk count.
  276. $this->addSitemapLink(array('status' => FALSE));
  277. $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
  278. // Add a visible link, should finally bump up the chunk count.
  279. $this->addSitemapLink();
  280. $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 2);
  281. // Change all links to disabled. The chunk count should be 1 not 0.
  282. db_query("UPDATE {xmlsitemap} SET status = 0");
  283. $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
  284. $this->assertEqual(xmlsitemap_get_link_count(), 0);
  285. // Delete all links. The chunk count should be 1 not 0.
  286. db_query("DELETE FROM {xmlsitemap}");
  287. $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 0);
  288. $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
  289. }
  290. //function testGetChunkFile() {
  291. //}
  292. //
  293. //function testGetChunkSize() {
  294. //}
  295. //
  296. //function testGetLinkCount() {
  297. //}
  298. /**
  299. * Tests for xmlsitemap_calculate_changereq().
  300. */
  301. function testCalculateChangefreq() {
  302. // The test values.
  303. $values = array(
  304. array(),
  305. array(REQUEST_TIME),
  306. array(REQUEST_TIME, REQUEST_TIME - 200),
  307. array(REQUEST_TIME - 200, REQUEST_TIME, REQUEST_TIME - 600),
  308. );
  309. // Expected values.
  310. $expected = array(0, 0, 200, 300);
  311. foreach ($values as $i => $value) {
  312. $actual = xmlsitemap_calculate_changefreq($value);
  313. $this->assertEqual($actual, $expected[$i]);
  314. }
  315. }
  316. /**
  317. * Test for xmlsitemap_recalculate_changefreq().
  318. */
  319. function testRecalculateChangefreq() {
  320. // The starting test value.
  321. $value = array('lastmod' => REQUEST_TIME - 1000, 'changefreq' => 0, 'changecount' => 0);
  322. // Expected values.
  323. $expecteds = array(
  324. array('lastmod' => REQUEST_TIME, 'changefreq' => 1000, 'changecount' => 1),
  325. array('lastmod' => REQUEST_TIME, 'changefreq' => 500, 'changecount' => 2),
  326. array('lastmod' => REQUEST_TIME, 'changefreq' => 333, 'changecount' => 3),
  327. );
  328. foreach ($expecteds as $expected) {
  329. xmlsitemap_recalculate_changefreq($value);
  330. $this->assertEqual($value, $expected);
  331. }
  332. }
  333. /**
  334. * Tests for xmlsitemap_switch_user and xmlsitemap_restore_user().
  335. */
  336. function testSwitchUser() {
  337. global $user;
  338. $original_user = $user;
  339. $new_user = $this->drupalCreateUser();
  340. // Switch to a new valid user.
  341. $this->assertEqual(xmlsitemap_switch_user($new_user), TRUE);
  342. $this->assertEqual($user->uid, $new_user->uid);
  343. // Switch again to the anonymous user.
  344. $this->assertEqual(xmlsitemap_switch_user(0), TRUE);
  345. $this->assertEqual($user->uid, 0);
  346. // Switch again to the new user.
  347. $this->assertEqual(xmlsitemap_switch_user($new_user->uid), TRUE);
  348. $this->assertEqual($user->uid, $new_user->uid);
  349. // Test that after two switches the original user was restored.
  350. $this->assertEqual(xmlsitemap_restore_user(), TRUE);
  351. $this->assertEqual($user->uid, $original_user->uid);
  352. // Attempt to switch to the same user.
  353. $this->assertEqual(xmlsitemap_switch_user($original_user->uid), FALSE);
  354. $this->assertEqual($user->uid, $original_user->uid);
  355. $this->assertEqual(xmlsitemap_restore_user(), FALSE);
  356. $this->assertEqual($user->uid, $original_user->uid);
  357. // Attempt to switch to an invalid user ID.
  358. $invalid_uid = db_query("SELECT MAX(uid) FROM {users}")->fetchField() + 100;
  359. $this->assertEqual(xmlsitemap_switch_user($invalid_uid), FALSE);
  360. $this->assertEqual($user->uid, $original_user->uid);
  361. $this->assertEqual(xmlsitemap_restore_user(), FALSE);
  362. $this->assertEqual($user->uid, $original_user->uid);
  363. // Attempt user switching when the original user is anonymous.
  364. $user = drupal_anonymous_user();
  365. $this->assertEqual(xmlsitemap_switch_user(0), FALSE);
  366. $this->assertEqual($user->uid, 0);
  367. $this->assertEqual(xmlsitemap_restore_user(), FALSE);
  368. $this->assertEqual($user->uid, 0);
  369. }
  370. //function testLoadLink() {
  371. //}
  372. /**
  373. * Tests for xmlsitemap_save_link().
  374. */
  375. function testSaveLink() {
  376. $link = array('type' => 'testing', 'id' => 1, 'loc' => 'testing', 'status' => 1);
  377. xmlsitemap_save_link($link);
  378. $this->assertFlag('regenerate_needed', TRUE);
  379. $link['status'] = 0;
  380. xmlsitemap_save_link($link);
  381. $this->assertFlag('regenerate_needed', TRUE);
  382. $link['priority'] = 0.5;
  383. $link['loc'] = 'new_location';
  384. $link['status'] = 1;
  385. xmlsitemap_save_link($link);
  386. $this->assertFlag('regenerate_needed', TRUE);
  387. $link['priority'] = 0.0;
  388. xmlsitemap_save_link($link);
  389. $this->assertFlag('regenerate_needed', TRUE);
  390. $link['priority'] = 0.1;
  391. xmlsitemap_save_link($link);
  392. $this->assertFlag('regenerate_needed', TRUE);
  393. $link['priority'] = 1.0;
  394. xmlsitemap_save_link($link);
  395. $this->assertFlag('regenerate_needed', TRUE);
  396. $link['priority'] = 1;
  397. xmlsitemap_save_link($link);
  398. $this->assertFlag('regenerate_needed', FALSE);
  399. $link['priority'] = 0;
  400. xmlsitemap_save_link($link);
  401. $this->assertFlag('regenerate_needed', TRUE);
  402. $link['priority'] = 0.5;
  403. xmlsitemap_save_link($link);
  404. $this->assertFlag('regenerate_needed', TRUE);
  405. $link['priority'] = 0.5;
  406. $link['priority_override'] = 0;
  407. $link['status'] = 1;
  408. xmlsitemap_save_link($link);
  409. $this->assertFlag('regenerate_needed', FALSE);
  410. }
  411. /**
  412. * Tests for xmlsitemap_delete_link().
  413. */
  414. function testDeleteLink() {
  415. // Add our testing data.
  416. $link1 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 0));
  417. $link2 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 1));
  418. $link3 = $this->addSitemapLink(array('status' => 0));
  419. variable_set('xmlsitemap_regenerate_needed', FALSE);
  420. // Test delete multiple links.
  421. // Test that the regenerate flag is set when visible links are deleted.
  422. $deleted = xmlsitemap_delete_link(array('loc' => 'testing1'));
  423. $this->assertEqual($deleted, 2);
  424. $this->assertFalse(xmlsitemap_load_link(array('type' => $link1['type'], 'id' => $link1['id'])));
  425. $this->assertFalse(xmlsitemap_load_link(array('type' => $link2['type'], 'id' => $link2['id'])));
  426. $this->assertTrue(xmlsitemap_load_link(array('type' => $link3['type'], 'id' => $link3['id'])));
  427. $this->assertFlag('regenerate_needed', TRUE);
  428. $deleted = xmlsitemap_delete_link(array('type' => $link3['type'], 'id' => $link3['id']));
  429. $this->assertEqual($deleted, 1);
  430. $this->assertFalse(xmlsitemap_load_link(array('type' => $link3['type'], 'id' => $link3['id'])));
  431. $this->assertFlag('regenerate_needed', FALSE);
  432. }
  433. /**
  434. * Tests for xmlsitemap_update_links().
  435. */
  436. function testUpdateLinks() {
  437. // Add our testing data.
  438. $link1 = $this->addSitemapLink(array('subtype' => 'group1'));
  439. $link2 = $this->addSitemapLink(array('subtype' => 'group1'));
  440. $link3 = $this->addSitemapLink(array('subtype' => 'group2'));
  441. variable_set('xmlsitemap_regenerate_needed', FALSE);
  442. // id | type | subtype | language | access | status | priority
  443. // 1 | testing | group1 | '' | 1 | 1 | 0.5
  444. // 2 | testing | group1 | '' | 1 | 1 | 0.5
  445. // 3 | testing | group2 | '' | 1 | 1 | 0.5
  446. $updated = xmlsitemap_update_links(array('status' => 0), array('type' => 'testing', 'subtype' => 'group1', 'status_override' => 0));
  447. $this->assertEqual($updated, 2);
  448. $this->assertFlag('regenerate_needed', TRUE);
  449. // id | type | subtype | language | status | priority
  450. // 1 | testing | group1 | '' | 0 | 0.5
  451. // 2 | testing | group1 | '' | 0 | 0.5
  452. // 3 | testing | group2 | '' | 1 | 0.5
  453. $updated = xmlsitemap_update_links(array('priority' => 0.0), array('type' => 'testing', 'subtype' => 'group1', 'priority_override' => 0));
  454. $this->assertEqual($updated, 2);
  455. $this->assertFlag('regenerate_needed', FALSE);
  456. // id | type | subtype | language | status | priority
  457. // 1 | testing | group1 | '' | 0 | 0.0
  458. // 2 | testing | group1 | '' | 0 | 0.0
  459. // 3 | testing | group2 | '' | 1 | 0.5
  460. $updated = xmlsitemap_update_links(array('subtype' => 'group2'), array('type' => 'testing', 'subtype' => 'group1'));
  461. $this->assertEqual($updated, 2);
  462. $this->assertFlag('regenerate_needed', FALSE);
  463. // id | type | subtype | language | status | priority
  464. // 1 | testing | group2 | '' | 0 | 0.0
  465. // 2 | testing | group2 | '' | 0 | 0.0
  466. // 3 | testing | group2 | '' | 1 | 0.5
  467. $updated = xmlsitemap_update_links(array('status' => 1), array('type' => 'testing', 'subtype' => 'group2', 'status_override' => 0, 'status' => 0));
  468. $this->assertEqual($updated, 2);
  469. $this->assertFlag('regenerate_needed', TRUE);
  470. // id | type | subtype | language | status | priority
  471. // 1 | testing | group2 | '' | 1 | 0.0
  472. // 2 | testing | group2 | '' | 1 | 0.0
  473. // 3 | testing | group2 | '' | 1 | 0.5
  474. }
  475. }
  476. class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
  477. public static function getInfo() {
  478. return array(
  479. 'name' => 'XML sitemap interface tests',
  480. 'description' => 'Functional tests for the XML sitemap module.',
  481. 'group' => 'XML sitemap',
  482. );
  483. }
  484. function setUp() {
  485. parent::setUp('path');
  486. $this->admin_user = $this->drupalCreateUser(array('access content', 'administer site configuration', 'administer xmlsitemap'));
  487. $this->drupalLogin($this->admin_user);
  488. }
  489. /**
  490. * Test the sitemap file caching.
  491. */
  492. function testSitemapCaching() {
  493. $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
  494. $this->assertResponse(200);
  495. $etag = $this->drupalGetHeader('etag');
  496. $last_modified = $this->drupalGetHeader('last-modified');
  497. $this->assertTrue($etag, t('Etag header found.'));
  498. $this->assertTrue($last_modified, t('Last-modified header found.'));
  499. $this->drupalGet('sitemap.xml', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  500. $this->assertResponse(304);
  501. }
  502. /**
  503. * Test that the sitemap will not be genereated before the lifetime expires.
  504. */
  505. function testMinimumLifetime() {
  506. $this->regenerateSitemap();
  507. $edit = array('xmlsitemap_minimum_lifetime' => 300);
  508. $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
  509. $this->assertText(t('The configuration options have been saved.'));
  510. $link = $this->addSitemapLink(array('loc' => 'lifetime-test'));
  511. drupal_cron_run();
  512. $this->drupalGetSitemap();
  513. $this->assertNoRaw('lifetime-test');
  514. variable_set('xmlsitemap_generated_last', REQUEST_TIME - 300);
  515. drupal_cron_run();
  516. $this->drupalGetSitemap();
  517. $this->assertRaw('lifetime-test');
  518. xmlsitemap_delete_link(array('type' => $link['type'], 'id' => $link['id']));
  519. drupal_cron_run();
  520. $this->drupalGetSitemap();
  521. $this->assertRaw('lifetime-test');
  522. $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
  523. $this->assertNoRaw('lifetime-test');
  524. }
  525. /**
  526. * Test base URL functionality.
  527. */
  528. function testBaseURL() {
  529. $edit = array('xmlsitemap_base_url' => '');
  530. $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
  531. $this->assertText(t('Base URL field is required.'));
  532. $edit = array('xmlsitemap_base_url' => 'invalid');
  533. $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
  534. $this->assertText(t('Invalid base URL.'));
  535. $edit = array('xmlsitemap_base_url' => 'http://example.com/ ');
  536. $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
  537. $this->assertText(t('Invalid base URL.'));
  538. $edit = array('xmlsitemap_base_url' => 'http://example.com/');
  539. $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
  540. $this->assertText(t('The configuration options have been saved.'));
  541. $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
  542. $this->assertRaw('<loc>http://example.com/</loc>');
  543. }
  544. /**
  545. * Test that configuration problems are reported properly in the status report.
  546. */
  547. function testStatusReport() {
  548. // Test the rebuild flag.
  549. variable_set('xmlsitemap_generated_last', REQUEST_TIME);
  550. variable_set('xmlsitemap_rebuild_needed', TRUE);
  551. $this->assertXMLSitemapProblems(t('The XML sitemap data is out of sync and needs to be completely rebuilt.'));
  552. $this->clickLink(t('completely rebuilt'));
  553. $this->assertResponse(200);
  554. variable_set('xmlsitemap_rebuild_needed', FALSE);
  555. $this->assertNoXMLSitemapProblems();
  556. // Test the regenerate flag (and cron hasn't run in a while).
  557. variable_set('xmlsitemap_regenerate_needed', TRUE);
  558. variable_set('xmlsitemap_generated_last', REQUEST_TIME - variable_get('cron_threshold_warning', 172800) - 100);
  559. $this->assertXMLSitemapProblems(t('The XML cached files are out of date and need to be regenerated. You can run cron manually to regenerate the sitemap files.'));
  560. $this->clickLink(t('run cron manually'));
  561. $this->assertResponse(200);
  562. $this->assertNoXMLSitemapProblems();
  563. // Test anonymous users access to sitemap.xml.
  564. user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
  565. $this->assertXMLSitemapProblems(t('In order to allow search engines to view the XML sitemap and content on your site, the anonymous user role must have the access content permission.'));
  566. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
  567. $this->assertNoXMLSitemapProblems();
  568. // Test chunk count > 1000.
  569. // Test directory not writable.
  570. }
  571. /**
  572. * Test that duplicate paths are skipped during generation.
  573. */
  574. function testDuplicatePaths() {
  575. $link1 = $this->addSitemapLink(array('loc' => 'duplicate'));
  576. $link2 = $this->addSitemapLink(array('loc' => 'duplicate'));
  577. $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
  578. $this->assertUniqueText('duplicate');
  579. }
  580. }
  581. class XMLSitemapRobotsTxtIntegrationTest extends XMLSitemapTestHelper {
  582. public static function getInfo() {
  583. return array(
  584. 'name' => 'XML sitemap robots.txt',
  585. 'description' => 'Integration tests for the XML sitemap and robots.txt module.',
  586. 'group' => 'XML sitemap',
  587. 'dependencies' => array('robotstxt'),
  588. );
  589. }
  590. function setUp() {
  591. parent::setUp('xmlsitemap', 'robotstxt');
  592. }
  593. function testRobotsTxt() {
  594. // Request the un-clean robots.txt path so this will work in case there is
  595. // still the robots.txt file in the root directory.
  596. $this->drupalGet('', array('query' => array('q' => 'robots.txt')));
  597. $this->assertRaw('Sitemap: ' . url('sitemap.xml', array('absolute' => TRUE)));
  598. }
  599. }