testCustomMaxAgeIsHonoured()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 1
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\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