Page specific cache key, unit and functional tests

Added templates from CWP template to make the module
useful standalone outside of CWP.
This commit is contained in:
Ingo Schommer 2013-10-29 12:06:26 +01:00
parent 778ae7d42c
commit 2d035a6e94
5 changed files with 189 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class VersionFeed_Controller extends Extension {
// Cache the diffs to remove DOS possibility.
$cache = SS_Cache::factory('VersionFeed_Controller');
$cache->setOption('automatic_serialization', true);
$key = 'changes' . $this->owner->Version;
$key = implode('_', array('changes', $this->owner->ID, $this->owner->Version));
$entries = $cache->load($key);
if(!$entries || isset($_GET['flush'])) {
$entries = $this->owner->getDiffedChanges();
@ -85,6 +85,8 @@ class VersionFeed_Controller extends Extension {
$cache->save($changeList, $key);
}
} else {
$changeList = new ArrayList();
}
// Produce output

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>$Title</title>
<link>$Link</link>
<atom:link href="$Link" rel="self" type="application/rss+xml"></atom:link>
<% loop Entries %>
<item>
<title>$Title.XML</title>
<link>$GeneratedLink</link>
<description>
<% if DiffTitle %>
$DiffTitle.XML
<% end_if %>
<% if DiffContent %>
$DiffContent.AbsoluteLinks.XML
<% end_if %>
</description>
<pubDate>$LastEdited.Rfc822</pubDate>
<guid>$GeneratedLink</guid>
</item>
<% end_loop %>
</channel>
</rss>

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>$Title</title>
<link>$Link</link>
<atom:link href="$Link" rel="self" type="application/rss+xml"></atom:link>
<% loop Entries %>
<item>
<title>$Title.XML</title>
<link>$GeneratedLink</link>
<description>
<% if DiffTitle %>
$DiffTitle.XML
<% end_if %>
<% if DiffContent %>
$DiffContent.AbsoluteLinks.XML
<% end_if %>
</description>
<pubDate>$LastEdited.Rfc822</pubDate>
<guid>$GeneratedLink</guid>
</item>
<% end_loop %>
</channel>
</rss>

View File

@ -0,0 +1,97 @@
<?php
class VersionFeedFunctionalTest extends FunctionalTest {
protected $requiredExtensions = array(
'Page' => array('VersionFeed'),
'Page_Controller' => array('VersionFeed_Controller'),
);
public function testPublicHistory() {
$page = $this->createPageWithChanges(array('PublicHistory' => false));
$response = $this->get($page->RelativeLink('changes'));
$this->assertEquals(404, $response->getStatusCode());
$response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$this->assertFalse((bool)$xml->channel->item);
$page = $this->createPageWithChanges(array('PublicHistory' => true));
$response = $this->get($page->RelativeLink('changes'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$this->assertTrue((bool)$xml->channel->item);
$response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$this->assertTrue((bool)$xml->channel->item);
}
public function testContainsChangesForPageOnly() {
$page1 = $this->createPageWithChanges(array('Title' => 'Page1'));
$page2 = $this->createPageWithChanges(array('Title' => 'Page2'));
$response = $this->get($page1->RelativeLink('changes'));
$xml = simplexml_load_string($response->getBody());
$titles = array_map(function($item) {return (string)$item->title;}, $xml->xpath('//item'));
// TODO Unclear if this should contain the original version
$this->assertContains('Changed: Page1', $titles);
$this->assertNotContains('Changed: Page2', $titles);
$response = $this->get($page2->RelativeLink('changes'));
$xml = simplexml_load_string($response->getBody());
$titles = array_map(function($item) {return (string)$item->title;}, $xml->xpath('//item'));
// TODO Unclear if this should contain the original version
$this->assertNotContains('Changed: Page1', $titles);
$this->assertContains('Changed: Page2', $titles);
}
public function testContainsAllChangesForAllPages() {
$page1 = $this->createPageWithChanges(array('Title' => 'Page1'));
$page2 = $this->createPageWithChanges(array('Title' => 'Page2'));
$response = $this->get($page1->RelativeLink('allchanges'));
$xml = simplexml_load_string($response->getBody());
$titles = array_map(function($item) {return (string)$item->title;}, $xml->xpath('//item'));
$this->assertContains('Page1', $titles);
$this->assertContains('Page2', $titles);
}
protected function createPageWithChanges($seed = null) {
$page = new Page();
$seed = array_merge(array(
'Title' => 'My Title',
'Content' => 'My Content'
), $seed);
$page->update($seed);
$page->write();
$page->publish('Stage', 'Live');
$page->update(array(
'Title' => 'Changed: ' . $seed['Title'],
'Content' => 'Changed: ' . $seed['Content'],
));
$page->write();
$page->publish('Stage', 'Live');
$page->update(array(
'Title' => 'Changed again: ' . $seed['Title'],
'Content' => 'Changed again: ' . $seed['Content'],
));
$page->write();
$page->publish('Stage', 'Live');
$page->update(array(
'Title' => 'Unpublished: ' . $seed['Title'],
'Content' => 'Unpublished: ' . $seed['Content'],
));
$page->write();
return $page;
}
}

39
tests/VersionFeedTest.php Normal file
View File

@ -0,0 +1,39 @@
<?php
class VersionFeedTest extends SapphireTest {
public function testDiffedChangesExcludesRestrictedItems() {
$this->markTestIncomplete();
}
public function testDiffedChangesIncludesFullHistory() {
$this->markTestIncomplete();
}
public function testDiffedChangesTitle() {
$page = new Page(array('Title' => 'My Title'));
$page->write();
$page->publish('Stage', 'Live');
$feed = new VersionFeed();
$feed->setOwner($page);
$page->Title = 'My Changed Title';
$page->write();
$page->publish('Stage', 'Live');
$page->Title = 'My Unpublished Changed Title';
$page->write();
$this->assertContains(
_t('RSSHistory.TITLECHANGED', 'Title has changed:') . 'My Changed Title',
array_map('strip_tags', $feed->getDiffedChanges()->column('DiffTitle')),
'Detects published title changes'
);
$this->assertNotContains(
_t('RSSHistory.TITLECHANGED', 'Title has changed:') . 'My Unpublished Changed Title',
array_map('strip_tags', $feed->getDiffedChanges()->column('DiffTitle')),
'Ignores unpublished title changes'
);
}
}