Completed
Push — master ( b2a2f3...f9d431 )
by
unknown
8s
created

PageControlledPolicyTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ControllerPolicy\Tests;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CMS\Controllers\ModelAsController;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Controllers\ModelAsController was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\ControllerPolicy\PageControlledPolicy;
10
use SilverStripe\ControllerPolicy\Policies\CachingPolicy;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Forms\LiteralField;
15
16
class PageControlledPolicyTest extends SapphireTest
17
{
18
    protected static $fixture_file = 'PageControlledPolicyTest.yml';
19
20
    protected static $required_extensions = [
21
        Page::class => [
22
            PageControlledPolicy::class,
23
        ],
24
    ];
25
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
30
        // Load some default caching policy configuration
31
        Injector::inst()->load([
32
            'GeneralCachingPolicy' => [
33
                'class' => CachingPolicy::class,
34
                'properties' => [
35
                    'CacheAge' => 900, // 15 mins
36
                ]
37
            ]
38
        ]);
39
40
        Config::modify()->set(CachingPolicy::class, 'disable_cache_age_in_dev', false);
41
    }
42
43
    public function testInfoMessageIsShownToAdminUsersOnly()
44
    {
45
        /** @var Page $page */
46
        $page = $this->objFromFixture(Page::class, 'some_page');
47
48
        $this->logInWithPermission('ADMIN');
49
        $fields = $page->getCMSFields();
50
        $this->assertInstanceOf(LiteralField::class, $fields->fieldByName('Root.Caching.Instruction'));
51
52
        $this->logOut();
53
        $fields = $page->getCMSFields();
54
        $this->assertNull($fields->fieldByName('Root.Caching.Instruction'));
55
    }
56
57
    public function testCustomMaxAgeIsHonoured()
58
    {
59
        /** @var Page $page */
60
        $page = $this->objFromFixture(Page::class, 'some_page');
61
        $controller = ModelAsController::controller_for($page);
62
63
        $cachingPolicy = new CachingPolicy();
64
        $request = new HTTPRequest('GET', '/');
65
        $response = new HTTPResponse('Hello world');
66
        $cachingPolicy->applyToResponse($controller, $request, $response);
67
68
        $this->assertContains(
69
            'max-age=1620',
70
            (string) $response->getHeader('Cache-Control'),
71
            'CMS defined max age value (27 minutes) is used instead of the default (15 mins)'
72
        );
73
    }
74
}
75