xmlsitemap.test 28 KB

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