xmlsitemap.test 25.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?php
// $Id$

/**
 * @file
 * Unit tests for the xmlsitemap module.
 *
 * @ingroup xmlsitemap
 */

/**
 * Helper test class with some added functions for testing.
 */
class XMLSitemapTestHelper extends DrupalWebTestCase {
  protected $admin_user;

  function setUp() {
    // Call parent::setUp() allowing test cases to pass further modules.
    $modules = func_get_args();
    $modules = array_merge(array('xmlsitemap'), $modules);
    call_user_func_array(array('parent', 'setUp'), $modules);

    // Ensure the files directory is created and writable during testing.
    // @todo This appears to be a bug with SimpleTest.
    $this->checkFilesDirectory();
  }

  function tearDown() {
    // Capture any (remaining) watchdog errors.
    $this->assertNoWatchdogErrors();
    // Reset the watchdog seen IDs for the next test run.
    $this->getWatchdogMessages(TRUE);

    parent::tearDown();
  }

  /**
   * Check the files directory is created (massive fails if not done).
   */
  protected function checkFilesDirectory() {
    $path = file_create_path(variable_get('xmlsitemap_path', 'xmlsitemap'));
    if (!file_check_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      $this->fail(t('Directory %dir was found and writable for testing.', array('%dir' => $path)));
    }
  }

  protected function getLanguageObj($langcode = '', $reset = FALSE) {
    static $languages;

    if (!isset($languages) || $reset) {
      $languages = language_list('language', TRUE);
      $languages[''] = NULL;
    }

    return $languages[$langcode];
  }

  /**
   * Assert that a link is found in the sitemap.
   *
   * @param $conditions
   *   An array of values/conditions to match keyed by field name.
   */
  protected function assertInSitemap($conditions) {
    $link = xmlsitemap_load_link($conditions);
    $this->assertTrue($link && $link['access'] && $link['status'], t('Link found in sitemap.'));
    return $link;
  }

  /**
   * Assert that a link is not found in the sitemap.
   *
   * @param $conditions
   *   An array of values/conditions to match keyed by field name.
   */
  protected function assertNotInSitemap($conditions) {
    $link = xmlsitemap_load_link($conditions);
    $this->assertTrue(!$link || !$link['access'] || !$link['status'], t('Link not found in sitemap.'));
    return $link;
  }

  protected function assertLinkInSitemap(&$link) {
    $link = xmlsitemap_load_link(array('id' => $link['id'], 'type' => $link['type']));
    $this->assertTrue($link && $link['access'] && $link['status'], t('Link found in sitemap.'));
    return $link;
  }

  protected function drupalGetSitemap($language = '', $regenerate = FALSE) {
    if ($regenerate) {
      $this->regenerateSitemap();
    }

    $this->drupalGet('sitemap.xml', array('language' => $this->getLanguageObj($language)));
    $this->assertResponse(200);
  }

  protected function assertLinkNotInSitemap(&$link) {
    $link = xmlsitemap_load_link(array('id' => $link['id'], 'type' => $link['type']));
    $this->assertTrue(!$link || !$link['access'] || !$link['status'], t('Link found in sitemap.'));
    return $link;
  }

  protected function assertLinkValues($link, array $values) {
    foreach ($values as $key => $value) {
      if ($value === NULL || $link[$key] === NULL) {
        // For nullable fields, always check for identical values (===).
        $this->assertIdentical($link[$key], $value, t('Identical values for link field @key.', array('@key' => $key)));
      }
      else {
        // Otherwise check simple equality (==).
        $this->assertEqual($link[$key], $value, t('Equal values for link field @key.', array('@key' => $key)));
      }
    }
  }

  protected function assertLinksInSitemap() {
    $links = func_get_args();
    foreach ($links as $link) {
      $path = url($link['loc'], array('language' => $this->getLanguageObj($link['language']), 'absolute' => TRUE));
      $this->assertRaw($link['loc'], t('Link %path found in the sitemap.', array('%path' => $path)));
    }
  }

  protected function assertLinksNotInSitemap() {
    $links = func_get_args();
    foreach ($links as $link) {
      $path = url($link['loc'], array('language' => $this->getLanguageObj($link['language']), 'absolute' => TRUE));
      $this->assertNoRaw($link['loc'], t('Link %path not found in the sitemap.', array('%path' => $path)));
    }
  }

  protected function addSitemapLink(array $link = array()) {
    static $last_id = 1;

    $link += array(
      'type' => 'testing',
      'id' => $last_id,
    );

    // Make the default path easier to read than a random string.
    $link += array('loc' => $link['type'] . '-' . $link['id']);

    $last_id = $link['id'] + 1;
    xmlsitemap_save_link($link);
    return $link;
  }

  /**
   * Regenerate the sitemap by setting the regenerate flag and running cron.
   */
  protected function regenerateSitemap() {
    variable_set('xmlsitemap_regenerate_needed', TRUE);
    variable_set('xmlsitemap_generated_last', 0);
    module_load_include('inc', 'xmlsitemap');
    xmlsitemap_regenerate();
    $this->assertTrue(variable_get('xmlsitemap_generated_last', 0), t('XML sitemaps regenerated.'));
  }

  protected function assertXMLSitemapProblems($problem_text = FALSE) {
160
    $this->drupalGet('admin/config/search/xmlsitemap');
161 162 163 164 165 166 167 168
    $this->assertText(t('One or more problems were detected with your sitemap configuration. Please check the status report for more information.'));
    if ($problem_text) {
      $this->clickLink(t('status report'));
      $this->assertText($problem_text);
    }
  }

  protected function assertNoXMLSitemapProblems() {
169
    $this->drupalGet('admin/config/search/xmlsitemap');
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    $this->assertNoText(t('One or more problems were detected with your sitemap configuration. Please check the status report for more information.'));
  }

  private function getWatchdogMessage(array $conditions) {
    static $watchdog_schema;

    if (!isset($watchdog_schema)) {
      $watchdog_schema = drupal_get_schema('watchdog');
    }

    $args = array();
    foreach ($conditions as $field => $value) {
      $conditions[$field] = $field . ' = ' . db_type_placeholder($watchdog_schema['fields'][$field]['type']);
      $args[] = ($field == 'variables' && is_array($value)) ? serialize($value) : $value;
    }

186
    return db_query_range("SELECT 1 FROM {watchdog} WHERE " . implode(' AND ', $conditions), $args, 0, 1)->fetchField();
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  }

  protected function assertWatchdogMessage(array $conditions, $message = 'Watchdog message found.') {
    $this->assertTrue($this->getWatchdogMessage($conditions), $message);
  }

  protected function assertNoWatchdogMessage(array $conditions, $message = 'Watchdog message not found.') {
    $this->assertFalse($this->getWatchdogMessage($conditions), $message);
  }

  protected function assertSitemapLink($conditions) {
    $link = xmlsitemap_load_link($conditions);
    $this->assertTrue($link);
    return $link;
  }

  protected function assertNotSitemapLink($conditions) {
    $link = xmlsitemap_load_link($conditions);
    $this->assertFalse($link);
    return $link;
  }

  protected function assertFlag($variable, $assert_value = TRUE, $reset_if_true = TRUE) {
    $value = xmlsitemap_var($variable);

    if ($reset_if_true && $value) {
      variable_set('xmlsitemap_' . $variable, FALSE);
    }

    return $this->assertEqual($value, $assert_value, "xmlsitemap_$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
  }

  /**
   * Fetch all seen watchdog messages.
   */
  protected function getWatchdogMessages($reset = FALSE) {
    static $seen_ids = array();

    if (!module_exists('dblog') || $reset) {
      $seen_ids = array();
      return;
    }

    $sql = "SELECT wid, type, severity, message, variables, timestamp FROM {watchdog}";
    if ($seen_ids) {
      $sql .= " WHERE wid NOT IN (" . db_placeholders($seen_ids) . ")";
    }
    $sql .= " ORDER BY timestamp ASC";
    $query = db_query($sql, $seen_ids);

    $messages = array();
    while ($message = db_fetch_object($query)) {
      $message->text = $message->timestamp . ' ' . t($message->message, unserialize($message->variables));
      $messages[$message->wid] = $message;
    }

    $seen_ids = array_merge($seen_ids, array_keys($messages));
    return $messages;
  }

  /**
   * Check that there were no watchdog errors or worse.
   */
  protected function assertNoWatchdogErrors() {
    $messages = $this->getWatchdogMessages();
    $verbose = array();

    foreach ($messages as $message) {
      if ($message->severity < WATCHDOG_NOTICE) {
        $this->fail($message->text);
      }
      else {
        $verbose[] = $message->text;
      }
    }

    if ($verbose) {
      array_unshift($verbose, '<h2>Watchdog messages</h2>');
      $this->verbose(implode("<br />", $verbose));
    }
  }


}

class XMLSitemapUnitTest extends XMLSitemapTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap unit tests',
      'description' => 'Unit tests for the XML sitemap module.',
      'group' => 'XML sitemap',
    );
  }

  function setUp() {
    parent::setUp('xmlsitemap');
  }

  function testAssertFlag() {
    variable_set('xmlsitemap_rebuild_needed', TRUE);
    $this->assertTrue(xmlsitemap_var('rebuild_needed'));
    $this->assertTrue($this->assertFlag('rebuild_needed', TRUE, FALSE));
    $this->assertTrue(xmlsitemap_var('rebuild_needed'));
    $this->assertTrue($this->assertFlag('rebuild_needed', TRUE, TRUE));
    $this->assertFalse(xmlsitemap_var('rebuild_needed'));
    $this->assertTrue($this->assertFlag('rebuild_needed', FALSE, FALSE));
    $this->assertFalse(xmlsitemap_var('rebuild_needed'));
  }

  /**
   * Tests for xmlsitemap_get_changefreq().
   */
  function testGetChangefreq() {
    // The test values.
    $values = array(
      0,
      mt_rand(1, XMLSITEMAP_FREQUENCY_ALWAYS),
      mt_rand(XMLSITEMAP_FREQUENCY_ALWAYS + 1, XMLSITEMAP_FREQUENCY_HOURLY),
      mt_rand(XMLSITEMAP_FREQUENCY_HOURLY + 1, XMLSITEMAP_FREQUENCY_DAILY),
      mt_rand(XMLSITEMAP_FREQUENCY_DAILY + 1, XMLSITEMAP_FREQUENCY_WEEKLY),
      mt_rand(XMLSITEMAP_FREQUENCY_WEEKLY + 1, XMLSITEMAP_FREQUENCY_MONTHLY),
      mt_rand(XMLSITEMAP_FREQUENCY_MONTHLY + 1, XMLSITEMAP_FREQUENCY_YEARLY),
      mt_rand(XMLSITEMAP_FREQUENCY_YEARLY + 1, mt_getrandmax()),
    );

    // The expected values.
    $expected = array(
      FALSE,
      'always',
      'hourly',
      'daily',
      'weekly',
      'monthly',
      'yearly',
      'never',
    );

    foreach ($values as $i => $value) {
      $actual = xmlsitemap_get_changefreq($value);
      $this->assertIdentical($actual, $expected[$i]);
    }
  }

  /**
   * Tests for xmlsitemap_get_chunk_count().
   */
  function testGetChunkCount() {
    // Set a low chunk size for testing.
    variable_set('xmlsitemap_chunk_size', 4);

    // Make the total number of links just equal to the chunk size.
338
    $count = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();
339 340 341 342
    for ($i = $count; $i < 4; $i++) {
      $this->addSitemapLink();
      $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
    }
343
    $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 4);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    // Add a disabled link, should not change the chunk count.
    $this->addSitemapLink(array('status' => FALSE));
    $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);

    // Add a visible link, should finally bump up the chunk count.
    $this->addSitemapLink();
    $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 2);

    // Change all links to disabled. The chunk count should be 1 not 0.
    db_query("UPDATE {xmlsitemap} SET status = 0");
    $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
    $this->assertEqual(xmlsitemap_get_link_count(), 0);

    // Delete all links. The chunk count should be 1 not 0.
    db_query("DELETE FROM {xmlsitemap}");
360
    $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 0);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
  }

  //function testGetChunkFile() {
  //}
  //
  //function testGetChunkSize() {
  //}
  //
  //function testGetLinkCount() {
  //}

  /**
   * Tests for xmlsitemap_calculate_changereq().
   */
  function testCalculateChangefreq() {
    // The test values.
    $values = array(
      array(),
      array(REQUEST_TIME),
      array(REQUEST_TIME, REQUEST_TIME - 200),
      array(REQUEST_TIME - 200, REQUEST_TIME, REQUEST_TIME - 600),
    );

    // Expected values.
    $expected = array(0, 0, 200, 300);

    foreach ($values as $i => $value) {
      $actual = xmlsitemap_calculate_changefreq($value);
      $this->assertEqual($actual, $expected[$i]);

    }
  }

  /**
   * Test for xmlsitemap_recalculate_changefreq().
   */
  function testRecalculateChangefreq() {
    // The starting test value.
    $value = array('lastmod' => REQUEST_TIME - 1000, 'changefreq' => 0, 'changecount' => 0);

    // Expected values.
    $expecteds = array(
      array('lastmod' => REQUEST_TIME, 'changefreq' => 1000, 'changecount' => 1),
      array('lastmod' => REQUEST_TIME, 'changefreq' => 500, 'changecount' => 2),
      array('lastmod' => REQUEST_TIME, 'changefreq' => 333, 'changecount' => 3),
    );

    foreach ($expecteds as $expected) {
      xmlsitemap_recalculate_changefreq($value);
      $this->assertEqual($value, $expected);
    }
  }

  /**
   * Tests for xmlsitemap_switch_user and xmlsitemap_restore_user().
   */
  function testSwitchUser() {
    global $user;

    $original_user = $user;
    $new_user = $this->drupalCreateUser();

    // Switch to a new valid user.
    $this->assertEqual(xmlsitemap_switch_user($new_user), TRUE);
    $this->assertEqual($user->uid, $new_user->uid);

    // Switch again to the anonymous user.
    $this->assertEqual(xmlsitemap_switch_user(0), TRUE);
    $this->assertEqual($user->uid, 0);

    // Switch again to the new user.
    $this->assertEqual(xmlsitemap_switch_user($new_user->uid), TRUE);
    $this->assertEqual($user->uid, $new_user->uid);

    // Test that after two switches the original user was restored.
    $this->assertEqual(xmlsitemap_restore_user(), TRUE);
    $this->assertEqual($user->uid, $original_user->uid);

    // Attempt to switch to the same user.
    $this->assertEqual(xmlsitemap_switch_user($original_user->uid), FALSE);
    $this->assertEqual($user->uid, $original_user->uid);
    $this->assertEqual(xmlsitemap_restore_user(), FALSE);
    $this->assertEqual($user->uid, $original_user->uid);

    // Attempt to switch to an invalid user ID.
447
    $invalid_uid = db_query("SELECT MAX(uid) FROM {users}")->fetchField() + 100;
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    $this->assertEqual(xmlsitemap_switch_user($invalid_uid), FALSE);
    $this->assertEqual($user->uid, $original_user->uid);
    $this->assertEqual(xmlsitemap_restore_user(), FALSE);
    $this->assertEqual($user->uid, $original_user->uid);

    // Attempt user switching when the original user is anonymous.
    $user = drupal_anonymous_user();
    $this->assertEqual(xmlsitemap_switch_user(0), FALSE);
    $this->assertEqual($user->uid, 0);
    $this->assertEqual(xmlsitemap_restore_user(), FALSE);
    $this->assertEqual($user->uid, 0);
  }

  //function testWriteRecord() {
  //}
  //
  //function testLoadLink() {
  //}

  /**
   * Tests for xmlsitemap_save_link().
   */
  function testSaveLink() {
    $link = array('type' => 'testing', 'id' => 1, 'loc' => 'testing', 'status' => 1);
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['status'] = 0;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
    $link['loc'] = 'new_location';
    $link['status'] = 1;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.0;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.1;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 1.0;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 1;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', FALSE);

    $link['priority'] = 0;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
    $link['priority_override'] = 0;
    $link['status'] = 1;
    xmlsitemap_save_link($link);
    $this->assertFlag('regenerate_needed', FALSE);
  }

  /**
   * Tests for xmlsitemap_delete_link().
   */
  function testDeleteLink() {
    // Add our testing data.
    $link1 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 0));
    $link2 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 1));
    $link3 = $this->addSitemapLink(array('status' => 0));
    variable_set('xmlsitemap_regenerate_needed', FALSE);

    // Test delete multiple links.
    // Test that the regenerate flag is set when visible links are deleted.
    $deleted = xmlsitemap_delete_link(array('loc' => 'testing1'));
    $this->assertEqual($deleted, 2);
    $this->assertFalse(xmlsitemap_load_link(array('type' => $link1['type'], 'id' => $link1['id'])));
    $this->assertFalse(xmlsitemap_load_link(array('type' => $link2['type'], 'id' => $link2['id'])));
    $this->assertTrue(xmlsitemap_load_link(array('type' => $link3['type'], 'id' => $link3['id'])));
    $this->assertFlag('regenerate_needed', TRUE);

    $deleted = xmlsitemap_delete_link(array('type' => $link3['type'], 'id' => $link3['id']));
    $this->assertEqual($deleted, 1);
    $this->assertFalse(xmlsitemap_load_link(array('type' => $link3['type'], 'id' => $link3['id'])));
    $this->assertFlag('regenerate_needed', FALSE);
  }

  /**
   * Tests for xmlsitemap_update_links().
   */
  function testUpdateLinks() {
    // Add our testing data.
    $link1 = $this->addSitemapLink(array('subtype' => 'group1'));
    $link2 = $this->addSitemapLink(array('subtype' => 'group1'));
    $link3 = $this->addSitemapLink(array('subtype' => 'group2'));
    variable_set('xmlsitemap_regenerate_needed', FALSE);
    // id | type    | subtype | language | access | status | priority
    // 1  | testing | group1  | ''       | 1      | 1      | 0.5
    // 2  | testing | group1  | ''       | 1      | 1      | 0.5
    // 3  | testing | group2  | ''       | 1      | 1      | 0.5

    $updated = xmlsitemap_update_links(array('status' => 0), array('type' => 'testing', 'subtype' => 'group1', 'status_override' => 0));
    $this->assertEqual($updated, 2);
    //$this->assertLinkNotInSitemap($link1);
    //$this->assertLinkNotInSitemap($link2);
    //$this->assertLinkInSitemap($link3);
    $this->assertFlag('regenerate_needed', TRUE);
    // id | type    | subtype | language | status | priority
    // 1  | testing | group1  | ''       | 0      | 0.5
    // 2  | testing | group1  | ''       | 0      | 0.5
    // 3  | testing | group2  | ''       | 1      | 0.5

    $updated = xmlsitemap_update_links(array('priority' => 0.0), array('type' => 'testing', 'subtype' => 'group1', 'priority_override' => 0));
    $this->assertEqual($updated, 2);
    $this->assertFlag('regenerate_needed', FALSE);
    // id | type    | subtype | language | status | priority
    // 1  | testing | group1  | ''       | 0      | 0.0
    // 2  | testing | group1  | ''       | 0      | 0.0
    // 3  | testing | group2  | ''       | 1      | 0.5

    $updated = xmlsitemap_update_links(array('subtype' => 'group2'), array('type' => 'testing', 'subtype' => 'group1'));
    $this->assertEqual($updated, 2);
    $this->assertFlag('regenerate_needed', FALSE);
    // id | type    | subtype | language | status | priority
    // 1  | testing | group2  | ''       | 0      | 0.0
    // 2  | testing | group2  | ''       | 0      | 0.0
    // 3  | testing | group2  | ''       | 1      | 0.5

    $updated = xmlsitemap_update_links(array('status' => 1), array('type' => 'testing', 'subtype' => 'group2', 'status_override' => 0, 'status' => 0));
    $this->assertEqual($updated, 2);
    $this->assertFlag('regenerate_needed', TRUE);
    // id | type    | subtype | language | status | priority
    // 1  | testing | group2  | ''       | 1      | 0.0
    // 2  | testing | group2  | ''       | 1      | 0.0
    // 3  | testing | group2  | ''       | 1      | 0.5
  }
}

class XMLSitemapFunctionalTest extends XMLSitemapTestHelper {
  protected $admin_user;

  public static function getInfo() {
    return array(
      'name' => 'XML sitemap interface tests',
      'description' => 'Functional tests for the XML sitemap module.',
      'group' => 'XML sitemap',
    );
  }

  function setUp() {
    parent::setUp('xmlsitemap', 'path');
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer site configuration', 'administer xmlsitemap'));
    $this->drupalLogin($this->admin_user);
    $this->checkFilesDirectory();
    $this->regenerateSitemap();
  }

  /**
   * Test the sitemap file caching.
   */
  function testSitemapCaching() {
    $this->drupalGet('sitemap.xml');
    $this->assertResponse(200);
    $etag = $this->drupalGetHeader('etag');
    $last_modified = $this->drupalGetHeader('last-modified');
    $this->assertTrue($etag, t('Etag header found.'));
    $this->assertTrue($last_modified, t('Last-modified header found.'));

    $this->drupalGet('sitemap.xml', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
    $this->assertResponse(304);
  }

  /**
   * Test that the sitemap will not be genereated before the lifetime expires.
   */
  function testMinimumLifetime() {
    $edit = array('xmlsitemap_minimum_lifetime' => 300);
631
    $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    $this->assertText(t('The configuration options have been saved.'));

    xmlsitemap_save_link(array('type' => 'testing', 'id' => 1, 'loc' => 'lifetime-test'));
    drupal_cron_run();
    $this->drupalGet('sitemap.xml');
    $this->assertNoRaw('lifetime-test');

    variable_set('xmlsitemap_generated_last', REQUEST_TIME - 300);
    drupal_cron_run();
    $this->drupalGet('sitemap.xml');
    $this->assertRaw('lifetime-test');

    xmlsitemap_delete_link(array('type' => 'testing', 'id' => 1));
    drupal_cron_run();
    $this->drupalGet('sitemap.xml');
    $this->assertRaw('lifetime-test');

    $this->regenerateSitemap();
    $this->drupalGet('sitemap.xml');
    $this->assertNoRaw('lifetime-test');
  }

  /**
   * Test base URL functionality.
   */
  function testBaseURL() {
    $edit = array('xmlsitemap_base_url' => '');
659
    $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
660 661 662
    $this->assertText(t('Base URL field is required.'));

    $edit = array('xmlsitemap_base_url' => 'invalid');
663
    $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
664 665 666
    $this->assertText(t('Invalid base URL.'));

    $edit = array('xmlsitemap_base_url' => 'http://example.com/ ');
667
    $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
668 669 670
    $this->assertText(t('Invalid base URL.'));

    $edit = array('xmlsitemap_base_url' => 'http://example.com/');
671
    $this->drupalPost('admin/config/search/xmlsitemap', $edit, t('Save configuration'));
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
    $this->assertText(t('The configuration options have been saved.'));

    $this->regenerateSitemap();
    $this->drupalGet('sitemap.xml');
    $this->assertRaw('<loc>http://example.com/</loc>');
  }

  /**
   * Test that configuration problems are reported properly in the status report.
   */
  function testStatusReport() {
    // Test the rebuild flag.
    variable_set('xmlsitemap_rebuild_needed', TRUE);
    $this->assertXMLSitemapProblems(t('The XML sitemap data is out of sync and needs to be completely rebuilt.'));
    $this->clickLink(t('completely rebuilt'));
    $this->assertResponse(200);
    variable_set('xmlsitemap_rebuild_needed', FALSE);
    $this->assertNoXMLSitemapProblems();

    // Test the regenerate flag (and cron hasn't run in a while).
    variable_set('xmlsitemap_regenerate_needed', TRUE);
    variable_set('xmlsitemap_generated_last', REQUEST_TIME - variable_get('cron_threshold_warning', 172800) - 10);
    $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.'));
    $this->clickLink(t('run cron manually'));
    $this->assertResponse(200);
    $this->assertNoXMLSitemapProblems();

    // Test anonymous users access to sitemap.xml.
700
    $anon_permissions = db_query("SELECT perm FROM {permission} WHERE rid = %d", DRUPAL_ANONYMOUS_RID)->fetchField();
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    db_query("UPDATE {permission} SET perm = '' WHERE rid = %d", DRUPAL_ANONYMOUS_RID);
    $this->assertXMLSitemapProblems(t('In order to allow search engines to view the XML sitemap, the anonymous user role must have the access content permission.'));
    db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $anon_permissions, DRUPAL_ANONYMOUS_RID);
    $this->assertNoXMLSitemapProblems();

    // Test chunk count > 1000.
    // Test directory not writable.
  }

  /**
   * Test that duplicate paths are skipped during generation.
   */
  function testDuplicatePaths() {
    $link1 = $this->addSitemapLink(array('loc' => 'duplicate'));
    $link2 = $this->addSitemapLink(array('loc' => 'duplicate'));
    $this->drupalGetSitemap('', TRUE);
    $this->assertUniqueText('duplicate');
  }
}

class XMLSitemapRobotsTxtIntegrationTest extends XMLSitemapTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap robots.txt',
      'description' => 'Integration tests for the XML sitemap and robots.txt module.',
      'group' => 'XML sitemap',
      'dependencies' => array('robotstxt'),
    );
  }

  function setUp() {
    parent::setUp('xmlsitemap', 'robotstxt');
  }

  function testRobotsTxt() {
    // Request the un-clean robots.txt path so this will work in case there is
    // still the robots.txt file in the root directory.
    $this->drupalGet('', array('query' => 'q=robots.txt'));
    $this->assertRaw('Sitemap: ' . url('sitemap.xml', array('absolute' => TRUE)));
  }
}