2013-10-29 12:06:26 +01:00
|
|
|
<?php
|
2017-12-11 00:10:56 +01:00
|
|
|
|
|
|
|
namespace SilverStripe\VersionFeed\Tests;
|
|
|
|
|
|
|
|
use Page;
|
2018-06-18 12:54:33 +02:00
|
|
|
use PageController;
|
2017-12-13 04:13:46 +01:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Director;
|
2018-06-18 12:54:33 +02:00
|
|
|
use SilverStripe\Core\Cache\CacheFactory;
|
2017-12-11 00:10:56 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-12-11 00:50:45 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-12-11 00:10:56 +01:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2017-12-13 04:13:46 +01:00
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\VersionFeed\Filters\CachedContentFilter;
|
|
|
|
use SilverStripe\VersionFeed\Filters\RateLimitFilter;
|
|
|
|
use SilverStripe\VersionFeed\VersionFeed;
|
|
|
|
use SilverStripe\VersionFeed\VersionFeedController;
|
2017-12-11 00:10:56 +01:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
class VersionFeedFunctionalTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
2018-06-18 12:54:33 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
protected $baseURI = 'http://www.fakesite.test';
|
2017-12-11 00:10:56 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
protected static $required_extensions = [
|
|
|
|
Page::class => [VersionFeed::class],
|
|
|
|
PageController::class => [VersionFeedController::class],
|
|
|
|
];
|
2013-10-29 12:06:26 +01:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
protected $userIP;
|
2017-05-12 00:20:27 +02:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
/**
|
|
|
|
* @var CacheInterface
|
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
2021-10-27 07:17:31 +02:00
|
|
|
protected function setUp(): void
|
2017-12-11 21:47:35 +01:00
|
|
|
{
|
|
|
|
Director::config()->set('alternate_base_url', $this->baseURI);
|
2018-06-18 12:54:33 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
parent::setUp();
|
2013-10-29 12:06:26 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache = Injector::inst()->get(
|
2017-12-11 21:47:35 +01:00
|
|
|
CacheInterface::class . '.VersionFeedController'
|
|
|
|
);
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache->clear();
|
2013-10-31 10:21:46 +01:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
$this->userIP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
|
2017-05-12 00:20:27 +02:00
|
|
|
|
|
|
|
// Enable history by default
|
2017-12-11 05:20:00 +01:00
|
|
|
Config::modify()->set(VersionFeed::class, 'changes_enabled', true);
|
|
|
|
Config::modify()->set(VersionFeed::class, 'allchanges_enabled', true);
|
2017-05-12 00:20:27 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
// Disable caching and locking by default
|
|
|
|
Config::modify()->set(CachedContentFilter::class, 'cache_enabled', false);
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_timeout', 0);
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_bypage', false);
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_byuserip', false);
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_cooldown', false);
|
2018-06-18 12:54:33 +02:00
|
|
|
|
|
|
|
// Ensure any version based caches read from the live cache
|
|
|
|
Versioned::set_reading_mode(Versioned::DEFAULT_MODE);
|
2017-12-11 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 07:17:31 +02:00
|
|
|
protected function tearDown(): void
|
2017-12-11 21:47:35 +01:00
|
|
|
{
|
|
|
|
Director::config()->set('alternate_base_url', null);
|
2018-06-18 12:54:33 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
$_SERVER['REMOTE_ADDR'] = $this->userIP;
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
public function testPublicHistoryPublicHistoryDisabled()
|
2017-12-11 21:47:35 +01:00
|
|
|
{
|
2018-06-18 12:54:33 +02:00
|
|
|
$page = $this->createPageWithChanges(['PublicHistory' => false]);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page->RelativeLink('changes'));
|
2017-12-18 04:38:16 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
404,
|
|
|
|
$response->getStatusCode(),
|
|
|
|
'With Page\'s "PublicHistory" disabled, `changes` action response code should be 404'
|
|
|
|
);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page->RelativeLink('allchanges'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-18 04:38:16 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$xml->channel->item,
|
|
|
|
'With Page\'s "PublicHistory" disabled, `allchanges` action should not have an item in the channel'
|
|
|
|
);
|
2018-06-18 12:54:33 +02:00
|
|
|
}
|
2017-12-11 21:47:35 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
public function testPublicHistoryPublicHistoryEnabled()
|
|
|
|
{
|
|
|
|
$page = $this->createPageWithChanges(['PublicHistory' => true]);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-18 04:38:16 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(bool)$xml->channel->item,
|
|
|
|
'With Page\'s "PublicHistory" enabled, `changes` action should have an item in the channel'
|
|
|
|
);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page->RelativeLink('allchanges'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-18 04:38:16 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(bool)$xml->channel->item,
|
|
|
|
'With "PublicHistory" enabled, `allchanges` action should have an item in the channel'
|
|
|
|
);
|
2017-12-11 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRateLimiting()
|
|
|
|
{
|
|
|
|
// Re-enable locking just for this test
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_timeout', 20);
|
|
|
|
Config::modify()->set(CachedContentFilter::class, 'cache_enabled', true);
|
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$page1 = $this->createPageWithChanges(['PublicHistory' => true, 'Title' => 'Page1']);
|
|
|
|
$page2 = $this->createPageWithChanges(['PublicHistory' => true, 'Title' => 'Page2']);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
// Artifically set cache lock
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache->set(RateLimitFilter::CACHE_PREFIX, time() + 10);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
// Test normal hit
|
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(429, $response->getStatusCode());
|
|
|
|
$this->assertGreaterThan(0, $response->getHeader('Retry-After'));
|
|
|
|
$response = $this->get($page2->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(429, $response->getStatusCode());
|
|
|
|
$this->assertGreaterThan(0, $response->getHeader('Retry-After'));
|
|
|
|
|
|
|
|
// Test page specific lock
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_bypage', true);
|
2018-06-18 12:54:33 +02:00
|
|
|
$key = implode('_', [
|
2017-12-11 21:47:35 +01:00
|
|
|
'changes',
|
|
|
|
$page1->ID,
|
|
|
|
Versioned::get_versionnumber_by_stage(SiteTree::class, 'Live', $page1->ID, false)
|
2018-06-18 12:54:33 +02:00
|
|
|
]);
|
2022-04-13 03:53:29 +02:00
|
|
|
$key = RateLimitFilter::CACHE_PREFIX . '_' . md5($key ?? '');
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache->set($key, time() + 10);
|
2017-12-11 21:47:35 +01:00
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(429, $response->getStatusCode());
|
|
|
|
$this->assertGreaterThan(0, $response->getHeader('Retry-After'));
|
|
|
|
$response = $this->get($page2->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_bypage', false);
|
|
|
|
|
|
|
|
// Test rate limit hit by IP
|
|
|
|
Config::modify()->set(RateLimitFilter::class, 'lock_byuserip', true);
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache->set(RateLimitFilter::CACHE_PREFIX . '_' . md5('127.0.0.1'), time() + 10);
|
2017-12-11 21:47:35 +01:00
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(429, $response->getStatusCode());
|
|
|
|
$this->assertGreaterThan(0, $response->getHeader('Retry-After'));
|
|
|
|
|
|
|
|
// Test rate limit doesn't hit other IP
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.20';
|
2018-06-18 12:54:33 +02:00
|
|
|
$this->cache->set(RateLimitFilter::CACHE_PREFIX . '_' . md5('127.0.0.1'), time() + 10);
|
2017-12-11 21:47:35 +01:00
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2017-12-18 04:38:16 +01:00
|
|
|
public function testChangesActionContainsChangesForCurrentPageOnly()
|
2017-12-11 21:47:35 +01:00
|
|
|
{
|
2018-06-18 12:54:33 +02:00
|
|
|
$page1 = $this->createPageWithChanges(['Title' => 'Page1']);
|
|
|
|
$page2 = $this->createPageWithChanges(['Title' => 'Page2']);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-11 21:47:35 +01:00
|
|
|
$titles = array_map(function ($item) {
|
|
|
|
return (string)$item->title;
|
2022-04-13 03:53:29 +02:00
|
|
|
}, $xml->xpath('//item') ?? []);
|
2023-10-10 03:46:19 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
$this->assertContains('Changed: Page1', $titles);
|
|
|
|
$this->assertNotContains('Changed: Page2', $titles);
|
|
|
|
|
|
|
|
$response = $this->get($page2->RelativeLink('changes'));
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-11 21:47:35 +01:00
|
|
|
$titles = array_map(function ($item) {
|
|
|
|
return (string)$item->title;
|
2022-04-13 03:53:29 +02:00
|
|
|
}, $xml->xpath('//item') ?? []);
|
2023-10-10 03:46:19 +02:00
|
|
|
|
2017-12-11 21:47:35 +01:00
|
|
|
$this->assertNotContains('Changed: Page1', $titles);
|
|
|
|
$this->assertContains('Changed: Page2', $titles);
|
|
|
|
}
|
|
|
|
|
2017-12-18 04:38:16 +01:00
|
|
|
public function testAllChangesActionContainsAllChangesForAllPages()
|
2017-12-11 21:47:35 +01:00
|
|
|
{
|
2018-06-18 12:54:33 +02:00
|
|
|
$page1 = $this->createPageWithChanges(['Title' => 'Page1']);
|
|
|
|
$page2 = $this->createPageWithChanges(['Title' => 'Page2']);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
$response = $this->get($page1->RelativeLink('allchanges'));
|
2022-04-13 03:53:29 +02:00
|
|
|
$xml = simplexml_load_string($response->getBody() ?? '');
|
2017-12-11 21:47:35 +01:00
|
|
|
$titles = array_map(function ($item) {
|
2017-12-13 04:13:46 +01:00
|
|
|
return str_replace('Changed: ', '', (string) $item->title);
|
2022-04-13 03:53:29 +02:00
|
|
|
}, $xml->xpath('//item') ?? []);
|
2017-12-11 21:47:35 +01:00
|
|
|
$this->assertContains('Page1', $titles);
|
|
|
|
$this->assertContains('Page2', $titles);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createPageWithChanges($seed = null)
|
|
|
|
{
|
|
|
|
$page = new Page();
|
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$seed = array_merge([
|
2017-12-11 21:47:35 +01:00
|
|
|
'Title' => 'My Title',
|
|
|
|
'Content' => 'My Content'
|
2018-06-18 12:54:33 +02:00
|
|
|
], $seed);
|
2017-12-11 21:47:35 +01:00
|
|
|
$page->update($seed);
|
|
|
|
$page->write();
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->publishSingle();
|
2017-12-11 21:47:35 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->update([
|
2017-12-11 21:47:35 +01:00
|
|
|
'Title' => 'Changed: ' . $seed['Title'],
|
|
|
|
'Content' => 'Changed: ' . $seed['Content'],
|
2018-06-18 12:54:33 +02:00
|
|
|
]);
|
2017-12-11 21:47:35 +01:00
|
|
|
$page->write();
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->publishSingle();
|
2017-12-11 21:47:35 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->update([
|
2017-12-11 21:47:35 +01:00
|
|
|
'Title' => 'Changed again: ' . $seed['Title'],
|
|
|
|
'Content' => 'Changed again: ' . $seed['Content'],
|
2018-06-18 12:54:33 +02:00
|
|
|
]);
|
2017-12-11 21:47:35 +01:00
|
|
|
$page->write();
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->publishSingle();
|
2017-12-11 21:47:35 +01:00
|
|
|
|
2018-06-18 12:54:33 +02:00
|
|
|
$page->update([
|
2017-12-11 21:47:35 +01:00
|
|
|
'Title' => 'Unpublished: ' . $seed['Title'],
|
|
|
|
'Content' => 'Unpublished: ' . $seed['Content'],
|
2018-06-18 12:54:33 +02:00
|
|
|
]);
|
2017-12-11 21:47:35 +01:00
|
|
|
$page->write();
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-13 04:13:46 +01:00
|
|
|
* Tests response code for globally disabled feeds
|
2017-12-11 21:47:35 +01:00
|
|
|
*/
|
|
|
|
public function testFeedViewability()
|
|
|
|
{
|
|
|
|
|
|
|
|
// Nested loop through each configuration
|
2018-06-18 12:54:33 +02:00
|
|
|
foreach ([true, false] as $publicHistory_Page) {
|
|
|
|
$page = $this->createPageWithChanges(['PublicHistory' => $publicHistory_Page, 'Title' => 'Page']);
|
2017-12-11 21:47:35 +01:00
|
|
|
|
|
|
|
// Test requests to 'changes' action
|
2018-06-18 12:54:33 +02:00
|
|
|
foreach ([true, false] as $publicHistory_Config) {
|
2017-12-11 21:47:35 +01:00
|
|
|
Config::modify()->set(VersionFeed::class, 'changes_enabled', $publicHistory_Config);
|
|
|
|
$expectedResponse = $publicHistory_Page && $publicHistory_Config ? 200 : 404;
|
|
|
|
$response = $this->get($page->RelativeLink('changes'));
|
|
|
|
$this->assertEquals($expectedResponse, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test requests to 'allchanges' action on each page
|
2018-06-18 12:54:33 +02:00
|
|
|
foreach ([true, false] as $allChanges_Config) {
|
|
|
|
foreach ([true, false] as $allChanges_SiteConfig) {
|
2017-12-11 21:47:35 +01:00
|
|
|
Config::modify()->set(VersionFeed::class, 'allchanges_enabled', $allChanges_Config);
|
|
|
|
$siteConfig = SiteConfig::current_site_config();
|
|
|
|
$siteConfig->AllChangesEnabled = $allChanges_SiteConfig;
|
|
|
|
$siteConfig->write();
|
|
|
|
|
|
|
|
$expectedResponse = $allChanges_Config && $allChanges_SiteConfig ? 200 : 404;
|
|
|
|
$response = $this->get($page->RelativeLink('allchanges'));
|
|
|
|
$this->assertEquals($expectedResponse, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 06:14:15 +02:00
|
|
|
}
|