pastebin

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