xmlsitemap.test 26.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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;
16
  protected $seen_ids = array();
17
18
19
20

  function setUp() {
    // Call parent::setUp() allowing test cases to pass further modules.
    $modules = func_get_args();
21
22
    $modules[] = 'xmlsitemap';
    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
    drupal_static_reset('xmlsitemap_get_directory');
27
28
29
30
31
32
33
    $this->checkFilesDirectory();
  }

  function tearDown() {
    // Capture any (remaining) watchdog errors.
    $this->assertNoWatchdogErrors();
    // Reset the watchdog seen IDs for the next test run.
34
    $this->seen_ids = array();
35
36
37
38
39
40

    parent::tearDown();
  }

  /**
   * 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
46
    if (!xmlsitemap_check_directory()) {
      $this->fail(t('Sitemap directory was found and writable for testing.'));
47
48
49
    }
  }

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  protected function drupalGetSitemap($language = LANGUAGE_NONE, $regenerate = FALSE) {
    if ($regenerate) {
      $this->regenerateSitemap();
    }

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

  /**
   * 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);
65
    $this->cronRun();
66
67
68
    $this->assertTrue(variable_get('xmlsitemap_generated_last', 0) && !variable_get('xmlsitemap_regenerate_needed', FALSE), t('XML sitemaps regenerated and flag cleared.'));
  }

69
70
71
72
73
74
75
76
  protected function assertSitemapLink($entity_type, $entity_id = NULL) {
    if (is_array($entity_type)) {
      $links = xmlsitemap_link_load_multiple($entity_type);
      $link = $links ? reset($links) : FALSE;
    }
    else {
      $link = xmlsitemap_link_load($entity_type, $entity_id);
    }
77
78
79
80
    $this->assertTrue(is_array($link), 'Link loaded.');
    return $link;
  }

81
82
83
84
85
86
87
88
  protected function assertNoSitemapLink($entity_type, $entity_id = NULL) {
    if (is_array($entity_type)) {
      $links = xmlsitemap_link_load_multiple($entity_type);
      $link = $links ? reset($links) : FALSE;
    }
    else {
      $link = xmlsitemap_link_load($entity_type, $entity_id);
    }
89
90
91
92
    $this->assertFalse($link, 'Link not loaded.');
    return $link;
  }

93
94
95
  protected function assertSitemapLinkVisible($entity_type, $entity_id) {
    $link = xmlsitemap_link_load($entity_type, $entity_id);
    $this->assertTrue($link && $link['access'] && $link['status'], t('Sitemap link @type @id is visible.', array('@type' => $entity_type, '@id' => $entity_id)));
96
97
  }

98
99
100
  protected function assertSitemapLinkNotVisible($entity_type, $entity_id) {
    $link = xmlsitemap_link_load($entity_type, $entity_id);
    $this->assertTrue($link && !($link['access'] && $link['status']), t('Sitemap link @type @id is not visible.', array('@type' => $entity_type, '@id' => $entity_id)));
101
102
  }

103
104
105
106
107
108
109
  protected function assertSitemapLinkValues($entity_type, $entity_id, array $conditions) {
    $link = xmlsitemap_link_load($entity_type, $entity_id);

    if (!$link) {
      return $this->fail(t('Could not load sitemap link for @type @id.', array('@type' => $entity_type, '@id' => $entity_id)));
    }

110
111
112
    foreach ($conditions as $key => $value) {
      if ($value === NULL || $link[$key] === NULL) {
        // For nullable fields, always check for identical values (===).
113
        $this->assertIdentical($link[$key], $value, t('Identical values for @type @id link field @key.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key)));
114
115
116
      }
      else {
        // Otherwise check simple equality (==).
117
        $this->assertEqual($link[$key], $value, t('Equal values for @type @id link field @key.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key)));
118
119
120
121
      }
    }
  }

122
123
124
125
126
127
128
  protected function assertNotSitemapLinkValues($entity_type, $entity_id, array $conditions) {
    $link = xmlsitemap_link_load($entity_type, $entity_id);

    if (!$link) {
      return $this->fail(t('Could not load sitemap link for @type @id.', array('@type' => $entity_type, '@id' => $entity_id)));
    }

129
130
131
    foreach ($conditions as $key => $value) {
      if ($value === NULL || $link[$key] === NULL) {
        // For nullable fields, always check for identical values (===).
132
        $this->assertNotIdentical($link[$key], $value, t('Not identical values for @type @id link field @key.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key)));
133
134
135
      }
      else {
        // Otherwise check simple equality (==).
136
        $this->assertNotEqual($link[$key], $value, t('Not equal values for link @type @id field @key.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key)));
137
138
139
140
141
      }
    }
  }

  protected function assertRawSitemapLinks() {
142
143
    $links = func_get_args();
    foreach ($links as $link) {
144
      $path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
145
146
147
148
      $this->assertRaw($link['loc'], t('Link %path found in the sitemap.', array('%path' => $path)));
    }
  }

149
  protected function assertNoRawSitemapLinks() {
150
151
    $links = func_get_args();
    foreach ($links as $link) {
152
      $path = url($link['loc'], array('language' => xmlsitemap_language_load($link['language']), 'absolute' => TRUE));
153
154
155
156
157
      $this->assertNoRaw($link['loc'], t('Link %path not found in the sitemap.', array('%path' => $path)));
    }
  }

  protected function addSitemapLink(array $link = array()) {
158
    $last_id = &drupal_static(__FUNCTION__, 1);
159
160
161
162

    $link += array(
      'type' => 'testing',
      'id' => $last_id,
163
164
      'access' => 1,
      'status' => 1,
165
166
167
168
169
    );

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

170
    $last_id = max($last_id, $link['id']) + 1;
171
    xmlsitemap_link_save($link);
172
173
174
    return $link;
  }

175
176
177
178
179
180
181
182
  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'));
183
184
185
  }

  protected function assertXMLSitemapProblems($problem_text = FALSE) {
186
    $this->drupalGet('admin/config/search/xmlsitemap');
187
    $this->assertText(t('One or more problems were detected with your XML sitemap configuration'));
188
189
190
191
192
193
    if ($problem_text) {
      $this->assertText($problem_text);
    }
  }

  protected function assertNoXMLSitemapProblems() {
194
    $this->drupalGet('admin/config/search/xmlsitemap');
195
    $this->assertNoText(t('One or more problems were detected with your XML sitemap configuration'));
196
197
198
199
  }

  /**
   * Fetch all seen watchdog messages.
200
201
   *
   * @todo Add unit tests for this function.
202
   */
203
  protected function getWatchdogMessages(array $conditions = array(), $reset = FALSE) {
204
    static $levels;
205

206
207
    if (!module_exists('dblog') || $reset) {
      $this->seen_ids = array();
208
209
      return;
    }
210
211
212
    if (!isset($levels)) {
      $levels = watchdog_severity_levels();
    }
213

Dave Reid's avatar
Dave Reid committed
214
215
    $query = db_select('watchdog');
    $query->fields('watchdog', array('wid', 'type', 'severity', 'message', 'variables', 'timestamp'));
216
217
218
219
220
221
    foreach ($conditions as $field => $value) {
      if ($field == 'variables' && is_array($value)) {
        $value = serialize($value);
      }
      $query->condition($field, $value);
    }
222
    if ($this->seen_ids) {
223
      $query->condition('wid', $this->seen_ids, 'NOT IN');
224
    }
225
    $query->orderBy('timestamp');
Dave Reid's avatar
Dave Reid committed
226
    $messages = $query->execute()->fetchAllAssoc('wid');
227

228
    foreach ($messages as &$message) {
229
230
231
232
      $message->variables = unserialize($message->variables);
      if (!is_array($message->variables)) {
        $message->variables = array();
      }
233
      $message->text = $message->timestamp . ' - ' . $levels[$message->severity] . ' - ' . $message->type . ' - ' . t($message->message, $message->variables);
234
235
    }

236
    $this->seen_ids = array_merge($this->seen_ids, array_keys($messages));
237
238
239
    return $messages;
  }

240
241
242
243
244
245
246
247
  protected function assertWatchdogMessage(array $conditions, $message = 'Watchdog message found.') {
    $this->assertTrue($this->getWatchdogMessages($conditions), $message);
  }

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

248
249
250
251
  /**
   * Check that there were no watchdog errors or worse.
   */
  protected function assertNoWatchdogErrors() {
252
    $messages = $this->getWatchdogMessages();
253
254
255
    $verbose = array();

    foreach ($messages as $message) {
256
      if (in_array($message->severity, array(WATCHDOG_EMERGENCY, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING))) {
257
258
        $this->fail($message->text);
      }
259
      $verbose[] = $message->text;
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
    }

    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.
335
    $count = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();
336
337
338
339
    for ($i = $count; $i < 4; $i++) {
      $this->addSitemapLink();
      $this->assertEqual(xmlsitemap_get_chunk_count(TRUE), 1);
    }
340
    $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 4);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

    // 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}");
357
    $this->assertEqual(db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField(), 0);
358
359
360
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
    $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.
444
    $invalid_uid = db_query("SELECT MAX(uid) FROM {users}")->fetchField() + 100;
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    $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 testLoadLink() {
  //}

  /**
462
   * Tests for xmlsitemap_link_save().
463
464
465
   */
  function testSaveLink() {
    $link = array('type' => 'testing', 'id' => 1, 'loc' => 'testing', 'status' => 1);
466
    xmlsitemap_link_save($link);
467
468
469
    $this->assertFlag('regenerate_needed', TRUE);

    $link['status'] = 0;
470
    xmlsitemap_link_save($link);
471
472
473
474
475
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
    $link['loc'] = 'new_location';
    $link['status'] = 1;
476
    xmlsitemap_link_save($link);
477
478
479
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.0;
480
    xmlsitemap_link_save($link);
481
482
483
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.1;
484
    xmlsitemap_link_save($link);
485
486
487
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 1.0;
488
    xmlsitemap_link_save($link);
489
490
491
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 1;
492
    xmlsitemap_link_save($link);
493
494
495
    $this->assertFlag('regenerate_needed', FALSE);

    $link['priority'] = 0;
496
    xmlsitemap_link_save($link);
497
498
499
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
500
    xmlsitemap_link_save($link);
501
502
503
504
505
    $this->assertFlag('regenerate_needed', TRUE);

    $link['priority'] = 0.5;
    $link['priority_override'] = 0;
    $link['status'] = 1;
506
    xmlsitemap_link_save($link);
507
508
509
510
    $this->assertFlag('regenerate_needed', FALSE);
  }

  /**
511
   * Tests for xmlsitemap_link_delete().
512
   */
513
  function testLinkDelete() {
514
515
516
517
518
519
520
521
    // 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.
522
    $deleted = xmlsitemap_link_delete_multiple(array('loc' => 'testing1'));
523
    $this->assertEqual($deleted, 2);
524
525
526
    $this->assertFalse(xmlsitemap_link_load($link1['type'], $link1['id']));
    $this->assertFalse(xmlsitemap_link_load($link2['type'], $link2['id']));
    $this->assertTrue(xmlsitemap_link_load($link3['type'], $link3['id']));
527
528
    $this->assertFlag('regenerate_needed', TRUE);

529
    $deleted = xmlsitemap_link_delete($link3['type'], $link3['id']);
530
    $this->assertEqual($deleted, 1);
531
    $this->assertFalse(xmlsitemap_link_load($link3['type'], $link3['id']));
532
533
534
535
    $this->assertFlag('regenerate_needed', FALSE);
  }

  /**
536
   * Tests for xmlsitemap_link_update_multiple().
537
538
539
   */
  function testUpdateLinks() {
    // Add our testing data.
540
541
542
543
    $links = array();
    $links[1] = $this->addSitemapLink(array('subtype' => 'group1'));
    $links[2] = $this->addSitemapLink(array('subtype' => 'group1'));
    $links[3] = $this->addSitemapLink(array('subtype' => 'group2'));
544
545
546
547
548
549
    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

550
    $updated = xmlsitemap_link_update_multiple(array('status' => 0), array('type' => 'testing', 'subtype' => 'group1', 'status_override' => 0));
551
552
553
554
555
556
557
    $this->assertEqual($updated, 2);
    $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

558
    $updated = xmlsitemap_link_update_multiple(array('priority' => 0.0), array('type' => 'testing', 'subtype' => 'group1', 'priority_override' => 0));
559
560
561
562
563
564
565
    $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

566
    $updated = xmlsitemap_link_update_multiple(array('subtype' => 'group2'), array('type' => 'testing', 'subtype' => 'group1'));
567
568
569
570
571
572
573
    $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

574
    $updated = xmlsitemap_link_update_multiple(array('status' => 1), array('type' => 'testing', 'subtype' => 'group2', 'status_override' => 0, 'status' => 0));
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    $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 {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap interface tests',
      'description' => 'Functional tests for the XML sitemap module.',
      'group' => 'XML sitemap',
    );
  }

  function setUp() {
Dave Reid's avatar
Dave Reid committed
594
    parent::setUp('path');
595
596
597
598
599
600
601
602
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer site configuration', 'administer xmlsitemap'));
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Test the sitemap file caching.
   */
  function testSitemapCaching() {
603
    $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
604
605
606
607
608
609
610
611
612
613
614
615
616
617
    $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() {
Dave Reid's avatar
Dave Reid committed
618
619
    $this->regenerateSitemap();

620
    $edit = array('xmlsitemap_minimum_lifetime' => 300);
Dave Reid's avatar
Dave Reid committed
621
    $this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
622
623
    $this->assertText(t('The configuration options have been saved.'));

624
    $link = $this->addSitemapLink(array('loc' => 'lifetime-test'));
625
    drupal_cron_run();
Dave Reid's avatar
Dave Reid committed
626
    $this->drupalGetSitemap();
627
628
629
    $this->assertNoRaw('lifetime-test');

    variable_set('xmlsitemap_generated_last', REQUEST_TIME - 300);
Dave Reid's avatar
Dave Reid committed
630
    $this->cronRun();
Dave Reid's avatar
Dave Reid committed
631
    $this->drupalGetSitemap();
632
633
    $this->assertRaw('lifetime-test');

634
    xmlsitemap_link_delete($link['type'], $link['id']);
Dave Reid's avatar
Dave Reid committed
635
    $this->cronRun();
Dave Reid's avatar
Dave Reid committed
636
    $this->drupalGetSitemap();
637
638
    $this->assertRaw('lifetime-test');

Dave Reid's avatar
Dave Reid committed
639
    $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
640
641
642
643
644
645
646
647
    $this->assertNoRaw('lifetime-test');
  }

  /**
   * Test base URL functionality.
   */
  function testBaseURL() {
    $edit = array('xmlsitemap_base_url' => '');
Dave Reid's avatar
Dave Reid committed
648
    $this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
649
    $this->assertText(t('Default base URL field is required.'));
650
651

    $edit = array('xmlsitemap_base_url' => 'invalid');
Dave Reid's avatar
Dave Reid committed
652
    $this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
653
654
655
    $this->assertText(t('Invalid base URL.'));

    $edit = array('xmlsitemap_base_url' => 'http://example.com/ ');
Dave Reid's avatar
Dave Reid committed
656
    $this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
657
658
659
    $this->assertText(t('Invalid base URL.'));

    $edit = array('xmlsitemap_base_url' => 'http://example.com/');
Dave Reid's avatar
Dave Reid committed
660
    $this->drupalPost('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
661
662
    $this->assertText(t('The configuration options have been saved.'));

663
    $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
664
665
666
667
668
669
670
671
    $this->assertRaw('<loc>http://example.com/</loc>');
  }

  /**
   * Test that configuration problems are reported properly in the status report.
   */
  function testStatusReport() {
    // Test the rebuild flag.
672
    variable_set('xmlsitemap_generated_last', REQUEST_TIME);
673
674
675
676
677
678
679
680
681
    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);
682
    variable_set('xmlsitemap_generated_last', REQUEST_TIME - variable_get('cron_threshold_warning', 172800) - 100);
683
684
685
686
687
688
    $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.
Dave Reid's avatar
Dave Reid committed
689
    user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
690
    $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.'));
Dave Reid's avatar
Dave Reid committed
691
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
692
693
694
695
696
697
698
699
700
701
702
703
    $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'));
Dave Reid's avatar
Dave Reid committed
704
    $this->drupalGetSitemap(LANGUAGE_NONE, TRUE);
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    $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() {
720
    parent::setUp('robotstxt');
721
722
723
724
725
  }

  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.
726
    $this->drupalGet('', array('query' => array('q' => 'robots.txt')));
727
728
729
    $this->assertRaw('Sitemap: ' . url('sitemap.xml', array('absolute' => TRUE)));
  }
}