SimplesitemapTest.php 1.75 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
<?php

/**
 * @file
 * Contains \Drupal\simplesitemap\Tests\SimplesitemapTest
 */

namespace Drupal\simplesitemap\Tests;

use Drupal\simplesitemap\Simplesitemap;
use Drupal\simpletest\WebTestBase;

/**
 * Tests Simple XML sitemap integration.
 *
 * @group Simplesitemap
 */
class SimplesitemapTest extends WebTestBase {

  protected $dumpHeaders = TRUE;
  protected $strictConfigSchema = FALSE;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['simplesitemap', 'node'];

  /**
   * Implements setup().
   */
  protected function setUp() {
    parent::setUp();

    $this->drupalCreateContentType(['type' => 'page']);
    $this->config('simplesitemap.settings')
      ->set('entity_types', ['node_type' => ['page' =>  ['index' => 1, 'priority' => '0.5']]])
      ->save();
  }

  /**
   * Test Simple sitemap integration.
   */
  public function testSimplesitemap() {
    $sitemap = new Simplesitemap;
    $sitemap->generate_sitemap();

    // Verify sitemap.xml can be cached.
    $this->drupalGet('sitemap.xml');
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
    $this->drupalGet('sitemap.xml');
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');

    /* @var $node \Drupal\Node\NodeInterface */
    $node = $this->createNode(['title' => 'A new page']);

    // Generate new sitemap.
    $sitemap->generate_sitemap();

    // Verify the cache was flushed and node is in the sitemap.
    $this->drupalGet('sitemap.xml');
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
    $this->assertText('node/' . $node->id());
    $this->drupalGet('sitemap.xml');
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
    $this->assertText('node/' . $node->id());
  }
}