[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/xmlsitemap/ -> xmlsitemap.test (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7