2013-10-29 12:06:26 +01:00
|
|
|
<?php
|
|
|
|
class VersionFeedFunctionalTest extends FunctionalTest {
|
|
|
|
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'Page' => array('VersionFeed'),
|
|
|
|
'Page_Controller' => array('VersionFeed_Controller'),
|
|
|
|
);
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
protected $userIP;
|
2013-10-29 12:06:26 +01:00
|
|
|
|
2013-10-31 10:21:46 +01:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$cache = SS_Cache::factory('VersionFeed_Controller');
|
|
|
|
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
$this->userIP = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : null;
|
|
|
|
|
|
|
|
Config::nest();
|
2014-05-21 00:44:59 +02:00
|
|
|
// Disable caching and locking by default
|
|
|
|
Config::inst()->update('VersionFeed\Filters\CachedContentFilter', 'cache_enabled', false);
|
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_timeout', 0);
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_bypage', false);
|
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_byuserip', false);
|
2014-04-30 05:40:51 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_cooldown', false);
|
2014-04-17 07:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
Config::unnest();
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = $this->userIP;
|
|
|
|
|
|
|
|
parent::tearDown();
|
2013-10-31 10:21:46 +01:00
|
|
|
}
|
|
|
|
|
2013-10-29 12:06:26 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
public function testRateLimiting() {
|
2014-05-21 00:44:59 +02:00
|
|
|
// Re-enable locking just for this test
|
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_timeout', 20);
|
|
|
|
Config::inst()->update('VersionFeed\Filters\CachedContentFilter', 'cache_enabled', true);
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
$page1 = $this->createPageWithChanges(array('PublicHistory' => true, 'Title' => 'Page1'));
|
|
|
|
$page2 = $this->createPageWithChanges(array('PublicHistory' => true, 'Title' => 'Page2'));
|
|
|
|
|
|
|
|
// Artifically set cache lock
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_byuserip', false);
|
2014-04-17 07:44:38 +02:00
|
|
|
$cache = SS_Cache::factory('VersionFeed_Controller');
|
|
|
|
$cache->setOption('automatic_serialization', true);
|
2014-04-30 05:40:51 +02:00
|
|
|
$cache->save(time() + 10, \VersionFeed\Filters\RateLimitFilter::CACHE_PREFIX);
|
2014-04-17 07:44:38 +02: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
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_bypage', true);
|
2014-04-17 07:44:38 +02:00
|
|
|
$key = implode('_', array(
|
|
|
|
'changes',
|
|
|
|
$page1->ID,
|
|
|
|
Versioned::get_versionnumber_by_stage('SiteTree', 'Live', $page1->ID, false)
|
|
|
|
));
|
2014-04-30 02:31:18 +02:00
|
|
|
$key = \VersionFeed\Filters\RateLimitFilter::CACHE_PREFIX . '_' . md5($key);
|
2014-04-30 05:40:51 +02:00
|
|
|
$cache->save(time() + 10, $key);
|
2014-04-17 07:44:38 +02: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());
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_bypage', false);
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
// Test rate limit hit by IP
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_byuserip', true);
|
2014-04-17 07:44:38 +02:00
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
|
2014-04-30 05:40:51 +02:00
|
|
|
$cache->save(time() + 10, \VersionFeed\Filters\RateLimitFilter::CACHE_PREFIX . '_' . md5('127.0.0.1'));
|
2014-04-17 07:44:38 +02: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['HTTP_CLIENT_IP'] = '127.0.0.20';
|
2014-04-30 05:40:51 +02:00
|
|
|
$cache->save(time() + 10, \VersionFeed\Filters\RateLimitFilter::CACHE_PREFIX . '_' . md5('127.0.0.1'));
|
2014-04-17 07:44:38 +02:00
|
|
|
$response = $this->get($page1->RelativeLink('changes'));
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
|
|
|
|
// Restore setting
|
2014-04-30 02:31:18 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_byuserip', false);
|
2014-05-21 00:44:59 +02:00
|
|
|
Config::inst()->update('VersionFeed\Filters\RateLimitFilter', 'lock_timeout', 0);
|
|
|
|
Config::inst()->update('VersionFeed\Filters\CachedContentFilter', 'cache_enabled', false);
|
2014-04-17 07:44:38 +02:00
|
|
|
}
|
2013-10-29 12:06:26 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-05-14 06:14:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests response code for globally disabled feedss
|
|
|
|
*/
|
|
|
|
public function testFeedViewability() {
|
|
|
|
|
|
|
|
// Nested loop through each configuration
|
|
|
|
foreach(array(true, false) as $publicHistory_Page) {
|
|
|
|
$page = $this->createPageWithChanges(array('PublicHistory' => $publicHistory_Page, 'Title' => 'Page'));
|
|
|
|
|
|
|
|
// Test requests to 'changes' action
|
|
|
|
foreach(array(true, false) as $publicHistory_Config) {
|
|
|
|
Config::inst()->update('VersionFeed', '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
|
|
|
|
foreach(array(true, false) as $allChanges_Config) {
|
|
|
|
foreach(array(true, false) as $allChanges_SiteConfig) {
|
|
|
|
Config::inst()->update('VersionFeed', 'allchanges_enabled', $allChanges_Config);
|
2014-05-23 08:29:07 +02:00
|
|
|
$siteConfig = SiteConfig::current_site_config();
|
2014-05-14 06:14:15 +02:00
|
|
|
$siteConfig->AllChangesEnabled = $allChanges_SiteConfig;
|
|
|
|
$siteConfig->write();
|
|
|
|
|
|
|
|
$expectedResponse = $allChanges_Config && $allChanges_SiteConfig ? 200 : 404;
|
|
|
|
$response = $this->get($page->RelativeLink('allchanges'));
|
|
|
|
$this->assertEquals($expectedResponse, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 12:06:26 +01:00
|
|
|
|
2014-05-14 06:14:15 +02:00
|
|
|
}
|
2015-03-30 03:16:17 +02:00
|
|
|
|