Commit f98a82b6 authored by gbyte.co's avatar gbyte.co

Porting caching functionality from 1.x.

parent 5eeb92c8
......@@ -6,7 +6,7 @@
namespace Drupal\simple_sitemap\Controller;
//use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpFoundation\Response;
use Drupal\simple_sitemap\Simplesitemap;
......@@ -31,7 +31,9 @@ class SimplesitemapController {
$output = !$output ? '' : $output;
// Display sitemap with correct xml header.
// return new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
return new Response($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
$response = new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
$meta_data = $response->getCacheableMetadata();
$meta_data->addCacheTags(['simple_sitemap']);
return $response;
}
}
......@@ -6,6 +6,8 @@
namespace Drupal\simple_sitemap;
use Drupal\Core\Cache\Cache;
/**
* Simplesitemap class.
*
......@@ -169,6 +171,7 @@ class Simplesitemap {
* Generates the sitemap for all languages and saves it to the db.
*/
public function generate_sitemap($from = 'form') {
Cache::invalidateTags(array('simple_sitemap'));
db_truncate('simple_sitemap')->execute();
$generator = new SitemapGenerator($from);
$generator->set_custom_links($this->get_config('custom'));
......
<?php
/**
* @file
* Contains \Drupal\simple_sitemap\Tests\SimplesitemapTest
*/
namespace Drupal\simple_sitemap\Tests;
use Drupal\simple_sitemap\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 = ['simple_sitemap', 'node'];
/**
* Implements setup().
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'page']);
$this->config('simple_sitemap.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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment