xmlsitemap.test 30 KB

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