Merge pull request #49 from creative-commoners/pulls/2.0/update-tests

FIX Use cached SiteTree object for history instead of creating a temporary object
This commit is contained in:
Robbie Averill 2017-12-18 18:02:42 +13:00 committed by GitHub
commit de70e59d80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 84 additions and 71 deletions

View File

@ -22,7 +22,7 @@ Install with composer by running `composer require silverstripe/versionfeed` in
For usage instructions see [user manual](docs/en/userguide/index.md). For usage instructions see [user manual](docs/en/userguide/index.md).
### Translations ## Translations
Translations of the natural language strings are managed through a third party translation interface, transifex.com. Newly added strings will be periodically uploaded there for translation, and any new translations will be merged back to the project source code. Translations of the natural language strings are managed through a third party translation interface, transifex.com. Newly added strings will be periodically uploaded there for translation, and any new translations will be merged back to the project source code.

View File

@ -23,8 +23,10 @@ for the current page. You can override this behaviour by defining the `getDefaul
and returning the URL of your desired RSS feed: and returning the URL of your desired RSS feed:
```php ```php
class MyPage extends Page { class MyPage extends Page
function getDefaultRSSLink() { {
public function getDefaultRSSLink()
{
return $this->Link('myrssfeed'); return $this->Link('myrssfeed');
} }
} }
@ -34,7 +36,7 @@ This can be used in templates as `$DefaultRSSLink`.
### Rate limiting and caching ### Rate limiting and caching
By default all content is filtered based on the rules specified in `versionfeed/_config/versionfeed.yml`. By default all content is filtered based on the rules specified in `vendor/silverstripe/versionfeed/_config/versionfeed.yml`.
Two filters are applied on top of one another: Two filters are applied on top of one another:
* `SilverStripe\VersionFeed\Filters\CachedContentFilter` provides caching of versions based on an identifier built up of the record ID and the * `SilverStripe\VersionFeed\Filters\CachedContentFilter` provides caching of versions based on an identifier built up of the record ID and the

View File

@ -9,4 +9,3 @@ en:
SilverStripe\VersionFeed\VersionFeedSiteConfig: SilverStripe\VersionFeed\VersionFeedSiteConfig:
ALLCHANGES: 'All page changes' ALLCHANGES: 'All page changes'
ALLCHANGESLABEL: 'Make global changes feed public' ALLCHANGESLABEL: 'Make global changes feed public'
Warning: 'Publicising the history will also disclose the changes that have at the time been protected from the public view.'

View File

@ -2,16 +2,16 @@
namespace SilverStripe\VersionFeed; namespace SilverStripe\VersionFeed;
use SilverStripe\ORM\FieldType\DBField; use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\View\Parsers\Diff;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Forms\FieldList;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\CMS\Model\SiteTreeExtension; use SilverStripe\View\Parsers\Diff;
class VersionFeed extends SiteTreeExtension class VersionFeed extends SiteTreeExtension
{ {
@ -146,7 +146,7 @@ class VersionFeed extends SiteTreeExtension
* Return a single diff representing this version. * Return a single diff representing this version.
* Returns the initial version if there is nothing to compare to. * Returns the initial version if there is nothing to compare to.
* *
* @returns DataObject Object with relevant fields diffed. * @return DataObject|null Object with relevant fields diffed.
*/ */
public function getDiff() public function getDiff()
{ {
@ -179,32 +179,34 @@ class VersionFeed extends SiteTreeExtension
public function updateSettingsFields(FieldList $fields) public function updateSettingsFields(FieldList $fields)
{ {
if (!Config::inst()->get(get_class(), 'changes_enabled')) { if (!$this->owner->config()->get('changes_enabled')) {
return; return;
} }
// Add public history field. // Add public history field.
$fields->addFieldToTab('Root.Settings', $publicHistory = new FieldGroup( $fields->addFieldToTab(
new CheckboxField('PublicHistory', $this->owner->fieldLabel('PublicHistory')) 'Root.Settings',
)); $publicHistory = FieldGroup::create(
CheckboxField::create('PublicHistory', $this->owner->fieldLabel('PublicHistory'))
$warning = _t( )
->setDescription(_t(
__CLASS__ . '.Warning', __CLASS__ . '.Warning',
"Publicising the history will also disclose the changes that have at the time been protected " . "Publicising the history will also disclose the changes that have at the "
"from the public view." . "time been protected from the public view."
))
); );
$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning', $warning), 'PublicHistory');
if ($this->owner->CanViewType != 'Anyone') { if ($this->owner->CanViewType != 'Anyone') {
$warning = _t( $canViewType = $fields->fieldByName('Root.Settings.CanViewType');
if ($canViewType) {
$canViewType->setDescription(_t(
__CLASS__ . '.Warning2', __CLASS__ . '.Warning2',
"Changing access settings in such a way that this page or pages under it become publicly<br>" . "Changing access settings in such a way that this page or pages under it become publicly<br>"
"accessible may result in publicising all historical changes on these pages too. Please review<br>" . . "accessible may result in publicising all historical changes on these pages too. Please review"
"this section's \"Public history\" settings to ascertain only intended information is disclosed." . "<br> this section's \"Public history\" settings to ascertain only intended information is "
); . "disclosed."
));
$fields->addFieldToTab('Root.Settings', new LiteralField('PublicHistoryWarning2', $warning), 'CanViewType'); }
} }
} }

View File

@ -4,6 +4,8 @@ namespace SilverStripe\VersionFeed;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Control\RSS\RSSFeed; use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Security;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
@ -118,7 +120,7 @@ class VersionFeedController extends Extension
// Cache the diffs to remove DOS possibility. // Cache the diffs to remove DOS possibility.
$key = 'allchanges' $key = 'allchanges'
. preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited']) . preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited'])
. (Member::currentUserID() ?: 'public'); . (Security::getCurrentUser() ? Security::getCurrentUser()->ID : 'public');
$changeList = $this->filterContent($key, function () use ($latestChanges) { $changeList = $this->filterContent($key, function () use ($latestChanges) {
$changeList = new ArrayList(); $changeList = new ArrayList();
$canView = array(); $canView = array();
@ -127,14 +129,15 @@ class VersionFeedController extends Extension
// WARNING: although we are providing historical details, we check the current configuration. // WARNING: although we are providing historical details, we check the current configuration.
$id = $record['RecordID']; $id = $record['RecordID'];
if (!isset($canView[$id])) { if (!isset($canView[$id])) {
$page = SiteTree::get()->byID($id); $page = DataObject::get_by_id(SiteTree::class, $id);
$canView[$id] = $page && $page->canView(new Member()); $canView[$id] = $page && $page->canView(Security::getCurrentUser());
} }
if (!$canView[$id]) { if (!$canView[$id]) {
continue; continue;
} }
// Get the diff to the previous version. // Get the diff to the previous version.
$record['ID'] = $record['RecordID'];
$version = SiteTree::create($record); $version = SiteTree::create($record);
if ($diff = $version->getDiff()) { if ($diff = $version->getDiff()) {
$changeList->push($diff); $changeList->push($diff);

View File

@ -2,11 +2,10 @@
namespace SilverStripe\VersionFeed; namespace SilverStripe\VersionFeed;
use SilverStripe\Forms\FieldList;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\VersionFeed\VersionFeed;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataExtension;
/** /**
@ -39,7 +38,7 @@ class VersionFeedSiteConfig extends DataExtension
FieldGroup::create(new CheckboxField('AllChangesEnabled', $this->owner->fieldLabel('AllChangesEnabled'))) FieldGroup::create(new CheckboxField('AllChangesEnabled', $this->owner->fieldLabel('AllChangesEnabled')))
->setTitle(_t(__CLASS__ . '.ALLCHANGES', 'All page changes')) ->setTitle(_t(__CLASS__ . '.ALLCHANGES', 'All page changes'))
->setDescription(_t( ->setDescription(_t(
__CLASS__ . '.Warning', 'SilverStripe\\VersionFeed\\VersionFeed.Warning',
"Publicising the history will also disclose the changes that have at the time been protected " . "Publicising the history will also disclose the changes that have at the time been protected " .
"from the public view." "from the public view."
)) ))

View File

@ -3,19 +3,18 @@
namespace SilverStripe\VersionFeed\Tests; namespace SilverStripe\VersionFeed\Tests;
use Page; use Page;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\VersionFeed\VersionFeed; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\VersionFeed\Filters\CachedContentFilter; use SilverStripe\Control\Director;
use SilverStripe\VersionFeed\Filters\RateLimitFilter;
use SilverStripe\VersionFeed\VersionFeedController;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Versioned\Versioned;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Control\Director; use SilverStripe\SiteConfig\SiteConfig;
use Psr\SimpleCache\CacheInterface; use SilverStripe\Versioned\Versioned;
use SilverStripe\VersionFeed\Filters\CachedContentFilter;
use SilverStripe\VersionFeed\Filters\RateLimitFilter;
use SilverStripe\VersionFeed\VersionFeed;
use SilverStripe\VersionFeed\VersionFeedController;
class VersionFeedFunctionalTest extends FunctionalTest class VersionFeedFunctionalTest extends FunctionalTest
{ {
@ -55,7 +54,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
Config::modify()->set(RateLimitFilter::class, 'lock_cooldown', false); Config::modify()->set(RateLimitFilter::class, 'lock_cooldown', false);
} }
public function tearDown() protected function tearDown()
{ {
Director::config()->set('alternate_base_url', null); Director::config()->set('alternate_base_url', null);
@ -69,24 +68,37 @@ class VersionFeedFunctionalTest extends FunctionalTest
$page = $this->createPageWithChanges(array('PublicHistory' => false)); $page = $this->createPageWithChanges(array('PublicHistory' => false));
$response = $this->get($page->RelativeLink('changes')); $response = $this->get($page->RelativeLink('changes'));
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(
404,
$response->getStatusCode(),
'With Page\'s "PublicHistory" disabled, `changes` action response code should be 404'
);
$response = $this->get($page->RelativeLink('allchanges')); $response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody()); $xml = simplexml_load_string($response->getBody());
$this->assertFalse((bool)$xml->channel->item); $this->assertFalse(
(bool)$xml->channel->item,
'With Page\'s "PublicHistory" disabled, `allchanges` action should not have an item in the channel'
);
$page = $this->createPageWithChanges(array('PublicHistory' => true)); $page = $this->createPageWithChanges(array('PublicHistory' => true));
$response = $this->get($page->RelativeLink('changes')); $response = $this->get($page->RelativeLink('changes'));
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody()); $xml = simplexml_load_string($response->getBody());
$this->assertTrue((bool)$xml->channel->item); $this->assertTrue(
(bool)$xml->channel->item,
'With Page\'s "PublicHistory" enabled, `changes` action should have an item in the channel'
);
$response = $this->get($page->RelativeLink('allchanges')); $response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody()); $xml = simplexml_load_string($response->getBody());
$this->assertTrue((bool)$xml->channel->item); $this->assertTrue(
(bool)$xml->channel->item,
'With "PublicHistory" enabled, `allchanges` action should have an item in the channel'
);
} }
public function testRateLimiting() public function testRateLimiting()
@ -149,7 +161,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
Config::modify()->set(CachedContentFilter::class, 'cache_enabled', false); Config::modify()->set(CachedContentFilter::class, 'cache_enabled', false);
} }
public function testContainsChangesForPageOnly() public function testChangesActionContainsChangesForCurrentPageOnly()
{ {
$page1 = $this->createPageWithChanges(array('Title' => 'Page1')); $page1 = $this->createPageWithChanges(array('Title' => 'Page1'));
$page2 = $this->createPageWithChanges(array('Title' => 'Page2')); $page2 = $this->createPageWithChanges(array('Title' => 'Page2'));
@ -173,7 +185,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$this->assertContains('Changed: Page2', $titles); $this->assertContains('Changed: Page2', $titles);
} }
public function testContainsAllChangesForAllPages() public function testAllChangesActionContainsAllChangesForAllPages()
{ {
$page1 = $this->createPageWithChanges(array('Title' => 'Page1')); $page1 = $this->createPageWithChanges(array('Title' => 'Page1'));
$page2 = $this->createPageWithChanges(array('Title' => 'Page2')); $page2 = $this->createPageWithChanges(array('Title' => 'Page2'));
@ -181,7 +193,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$response = $this->get($page1->RelativeLink('allchanges')); $response = $this->get($page1->RelativeLink('allchanges'));
$xml = simplexml_load_string($response->getBody()); $xml = simplexml_load_string($response->getBody());
$titles = array_map(function ($item) { $titles = array_map(function ($item) {
return (string)$item->title; return str_replace('Changed: ', '', (string) $item->title);
}, $xml->xpath('//item')); }, $xml->xpath('//item'));
$this->assertContains('Page1', $titles); $this->assertContains('Page1', $titles);
$this->assertContains('Page2', $titles); $this->assertContains('Page2', $titles);
@ -197,21 +209,21 @@ class VersionFeedFunctionalTest extends FunctionalTest
), $seed); ), $seed);
$page->update($seed); $page->update($seed);
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page->update(array( $page->update(array(
'Title' => 'Changed: ' . $seed['Title'], 'Title' => 'Changed: ' . $seed['Title'],
'Content' => 'Changed: ' . $seed['Content'], 'Content' => 'Changed: ' . $seed['Content'],
)); ));
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page->update(array( $page->update(array(
'Title' => 'Changed again: ' . $seed['Title'], 'Title' => 'Changed again: ' . $seed['Title'],
'Content' => 'Changed again: ' . $seed['Content'], 'Content' => 'Changed again: ' . $seed['Content'],
)); ));
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page->update(array( $page->update(array(
'Title' => 'Unpublished: ' . $seed['Title'], 'Title' => 'Unpublished: ' . $seed['Title'],
@ -223,7 +235,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
} }
/** /**
* Tests response code for globally disabled feedss * Tests response code for globally disabled feeds
*/ */
public function testFeedViewability() public function testFeedViewability()
{ {

View File

@ -3,15 +3,15 @@
namespace SilverStripe\VersionFeed\Tests; namespace SilverStripe\VersionFeed\Tests;
use Page; use Page;
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Versioned\Versioned;
use SilverStripe\VersionFeed\VersionFeed; use SilverStripe\VersionFeed\VersionFeed;
use SilverStripe\VersionFeed\VersionFeedController; use SilverStripe\VersionFeed\VersionFeedController;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Controllers\ContentController;
class VersionFeedTest extends SapphireTest class VersionFeedTest extends SapphireTest
{ {
protected $usesDatabase = true; protected $usesDatabase = true;
protected static $required_extensions = [ protected static $required_extensions = [
@ -19,10 +19,6 @@ class VersionFeedTest extends SapphireTest
ContentController::class => [VersionFeedController::class], ContentController::class => [VersionFeedController::class],
]; ];
protected $illegalExtensions = [
'SiteTree' => ['Translatable']
];
public function testDiffedChangesExcludesRestrictedItems() public function testDiffedChangesExcludesRestrictedItems()
{ {
$this->markTestIncomplete(); $this->markTestIncomplete();
@ -37,11 +33,11 @@ class VersionFeedTest extends SapphireTest
{ {
$page = new Page(['Title' => 'My Title']); $page = new Page(['Title' => 'My Title']);
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page->Title = 'My Changed Title'; $page->Title = 'My Changed Title';
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page->Title = 'My Unpublished Changed Title'; $page->Title = 'My Unpublished Changed Title';
$page->write(); $page->write();