XmlSitemapFunctionalTest.php 3.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php

/**
 * @file
 * Contains \Drupal\xmlsitemap\Tests\XmlSitemapFunctionalTest.
 */

namespace Drupal\xmlsitemap\Tests;

/**
 * Tests the generation of sitemaps.
 */
13
class XmlSitemapFunctionalTest extends XmlSitemapTestBase {
14

15
  public static $modules = array('xmlsitemap', 'path', 'node', 'system', 'user', 'help', 'block');
16
17
  protected $state;
  protected $config;
18

19
20
21
22
23
24
25
26
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap interface tests',
      'description' => 'Functional tests for the XML sitemap module.',
      'group' => 'XML sitemap',
    );
  }

27
28
29
  public function setUp() {
    parent::setUp();

30
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer site configuration', 'administer xmlsitemap', 'access administration pages', 'access site reports', 'administer permissions', 'view the administration theme'));
31
32
    $this->state = \Drupal::state();
    $this->config = \Drupal::configFactory()->get('xmlsitemap.settings');
33
    $this->drupalPlaceBlock('system_help_block', array('region' => 'help'));
34
35
36
37
38
39
  }

  /**
   * Test the sitemap file caching.
   */
  public function testSitemapCaching() {
40
    $this->drupalLogin($this->admin_user);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    $this->regenerateSitemap();
    $this->drupalGetSitemap();
    $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->drupalGetSitemap(array(), array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
    $this->assertResponse(304);
  }

  /**
   * Test base URL functionality.
   */
  public function testBaseURL() {
57
    $this->drupalLogin($this->admin_user);
58
    $edit = array('xmlsitemap_base_url' => '');
59
    $this->drupalPostForm('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
60

61
    $edit = array('xmlsitemap_base_url' => 'invalid');
62
    $this->drupalPostForm('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
63
64
    $this->assertText(t('Invalid base URL.'));

65
    $edit = array('xmlsitemap_base_url' => 'http://example.com/ ');
66
    $this->drupalPostForm('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
67
68
    $this->assertText(t('Invalid base URL.'));

69
    $edit = array('xmlsitemap_base_url' => 'http://example.com/');
70
    $this->drupalPostForm('admin/config/search/xmlsitemap/settings', $edit, t('Save configuration'));
71
72
73
74
75
76
77
78
79
80
81
82
    $this->assertText(t('The configuration options have been saved.'));

    $this->regenerateSitemap();
    $this->drupalGetSitemap(array(), array('base_url' => NULL));
    $this->assertRaw('<loc>http://example.com/</loc>');
  }

  /**
   * Test that configuration problems are reported properly in the status report.
   */
  public function testStatusReport() {
    // Test the rebuild flag.
83
    $this->drupalLogin($this->admin_user);
84
85
    $this->state->set('xmlsitemap_generated_last', REQUEST_TIME);
    $this->state->set('xmlsitemap_rebuild_needed', TRUE);
86
87
88
    $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);
89
    $this->state->set('xmlsitemap_rebuild_needed', FALSE);
90
91
    $this->assertNoXMLSitemapProblems();
    //Test the regenerate flag (and cron hasn't run in a while).
92
    $this->state->set('xmlsitemap_regenerate_needed', TRUE);
93
    $this->state->set('xmlsitemap_generated_last', REQUEST_TIME - $this->config->get('cron_threshold_warning') - 100);
94
95
96
97
98
99
100
    $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();
  }

}