1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ControllerPolicy\Tests; |
4
|
|
|
|
5
|
|
|
use Page; |
|
|
|
|
6
|
|
|
use SilverStripe\CMS\Controllers\ModelAsController; |
|
|
|
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
10
|
|
|
use SilverStripe\ControllerPolicy\PageControlledPolicy; |
11
|
|
|
use SilverStripe\ControllerPolicy\Policies\CachingPolicy; |
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
use SilverStripe\Core\Injector\Injector; |
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
15
|
|
|
use SilverStripe\Forms\LiteralField; |
16
|
|
|
|
17
|
|
|
class PageControlledPolicyTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
protected static $fixture_file = 'PageControlledPolicyTest.yml'; |
20
|
|
|
|
21
|
|
|
protected static $required_extensions = [ |
22
|
|
|
Page::class => [ |
23
|
|
|
PageControlledPolicy::class, |
24
|
|
|
], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
|
31
|
|
|
// Load some default caching policy configuration |
32
|
|
|
Injector::inst()->load([ |
33
|
|
|
'GeneralCachingPolicy' => [ |
34
|
|
|
'class' => CachingPolicy::class, |
35
|
|
|
'properties' => [ |
36
|
|
|
'CacheAge' => 900, // 15 mins |
37
|
|
|
] |
38
|
|
|
] |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
Config::modify()->set(CachingPolicy::class, 'disable_cache_age_in_dev', false); |
42
|
|
|
|
43
|
|
|
// Set to disabled at null forcing level |
44
|
|
|
HTTPCacheControlMiddleware::config() |
45
|
|
|
->set('defaultState', HTTPCacheControlMiddleware::STATE_ENABLED) |
46
|
|
|
->set('defaultForcingLevel', 0); |
47
|
|
|
HTTPCacheControlMiddleware::reset(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testInfoMessageIsShownToAdminUsersOnly() |
51
|
|
|
{ |
52
|
|
|
/** @var Page $page */ |
53
|
|
|
$page = $this->objFromFixture(Page::class, 'some_page'); |
54
|
|
|
|
55
|
|
|
$this->logInWithPermission('ADMIN'); |
56
|
|
|
$fields = $page->getCMSFields(); |
57
|
|
|
$this->assertInstanceOf(LiteralField::class, $fields->fieldByName('Root.Caching.Instruction')); |
58
|
|
|
|
59
|
|
|
$this->logOut(); |
60
|
|
|
$fields = $page->getCMSFields(); |
61
|
|
|
$this->assertNull($fields->fieldByName('Root.Caching.Instruction')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCustomMaxAgeIsHonoured() |
65
|
|
|
{ |
66
|
|
|
/** @var Page $page */ |
67
|
|
|
$page = $this->objFromFixture(Page::class, 'some_page'); |
68
|
|
|
$controller = ModelAsController::controller_for($page); |
69
|
|
|
|
70
|
|
|
$cachingPolicy = new CachingPolicy(); |
71
|
|
|
$request = new HTTPRequest('GET', '/'); |
72
|
|
|
$response = new HTTPResponse('Hello world'); |
73
|
|
|
$cachingPolicy->applyToResponse($controller, $request, $response); |
74
|
|
|
|
75
|
|
|
$this->assertContains( |
76
|
|
|
'max-age=' . 27 * 60, // see PageControlledPolicy->getCacheAge() |
77
|
|
|
(string) $response->getHeader('Cache-Control'), |
78
|
|
|
'CMS defined max age value (27 minutes) is used instead of the default (15 mins)' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths