Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SubsiteAdminFunctionalTest::getAndFollowAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
3
namespace SilverStripe\Subsites\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\CMSPageEditController;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\FunctionalTest;
9
use SilverStripe\Subsites\Model\Subsite;
10
11
class SubsiteAdminFunctionalTest extends FunctionalTest
12
{
13
    protected static $fixture_file = 'SubsiteTest.yml';
14
    protected static $use_draft_site = true;
15
16
    protected $autoFollowRedirection = false;
17
18
    /**
19
     * Helper: FunctionalTest is only able to follow redirection once, we want to go all the way.
20
     * @param string $url
21
     * @return \SilverStripe\Control\HTTPResponse
22
     */
23
    public function getAndFollowAll($url)
24
    {
25
        $response = $this->get($url);
26
        while ($location = $response->getHeader('Location')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $location is dead and can be removed.
Loading history...
27
            $response = $this->mainSession->followRedirection();
28
        }
29
        echo $response->getHeader('Location');
30
31
        return $response;
32
    }
33
34
    /**
35
     * Anonymous user cannot access anything.
36
     */
37
    public function testAnonymousIsForbiddenAdminAccess()
38
    {
39
        $this->logOut();
40
41
        $response = $this->getAndFollowAll('admin/pages/?SubsiteID=0');
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
42
        $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
43
44
        $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
45
        $response = $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
46
        $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'Admin is disallowed');
47
48
        $response = $this->getAndFollowAll('admin/subsite_xhr');
49
        $this->assertContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is disallowed');
50
    }
51
52
    /**
53
     * Admin should be able to access all subsites and the main site
54
     */
55 View Code Duplication
    public function testAdminCanAccessAllSubsites()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this->logInAs('admin');
58
59
        $this->getAndFollowAll('admin/pages/?SubsiteID=0');
60
        $this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
61
        $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
62
63
        $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
64
        $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
65
66
        // Check the session manually, since the state is unique to the request, not this test
67
        $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
68
        $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
69
70
        $response = $this->getAndFollowAll('admin/subsite_xhr');
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
71
        $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
72
    }
73
74
    public function testAdminIsRedirectedToObjectsSubsite()
75
    {
76
        $this->logInAs('admin');
77
78
        $mainSubsitePage = $this->objFromFixture(Page::class, 'mainSubsitePage');
79
        $subsite1Home = $this->objFromFixture(Page::class, 'subsite1_home');
80
81
        // Requesting a page from another subsite will redirect to that subsite
82
        Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false);
83
        $response = $this->get("admin/pages/edit/show/$subsite1Home->ID");
84
85
        $this->assertEquals(302, $response->getStatusCode());
86
        $this->assertContains(
87
            'admin/pages/edit/show/' . $subsite1Home->ID . '?SubsiteID=' . $subsite1Home->SubsiteID,
88
            $response->getHeader('Location')
89
        );
90
91
        // Loading a non-main-site object still switches the subsite if configured with treats_subsite_0_as_global
92
        Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', true);
93
94
        $response = $this->get("admin/pages/edit/show/$subsite1Home->ID");
95
        $this->assertEquals(302, $response->getStatusCode());
96
        $this->assertContains(
97
            'admin/pages/edit/show/' . $subsite1Home->ID . '?SubsiteID=' . $subsite1Home->SubsiteID,
98
            $response->getHeader('Location')
99
        );
100
101
        // Loading a main-site object does not change the subsite if configured with treats_subsite_0_as_global
102
        $response = $this->get("admin/pages/edit/show/$mainSubsitePage->ID");
103
        $this->assertEquals(200, $response->getStatusCode());
104
    }
105
106
    /**
107
     * User which has AccessAllSubsites set to 1 should be able to access all subsites and main site,
108
     * even though he does not have the ADMIN permission.
109
     */
110 View Code Duplication
    public function testEditorCanAccessAllSubsites()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $this->logInAs('editor');
113
114
        $this->get('admin/pages/?SubsiteID=0');
115
        $this->assertEquals(0, $this->session()->get('SubsiteID'), 'Can access main site.');
116
        $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
117
118
        $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
119
        $this->get("admin/pages/?SubsiteID={$subsite1->ID}");
120
        $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access other subsite.');
121
        $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Lands on the correct section');
122
123
        $response = $this->get('admin/subsite_xhr');
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
124
        $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
125
    }
126
127
    /**
128
     * Test a member who only has access to one subsite (subsite1) and only some sections (pages and security).
129
     */
130
    public function testSubsiteAdmin()
131
    {
132
        $this->markTestSkipped('wip');
133
        $this->logInAs('subsite1member');
134
135
        $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
136
137
        // Check allowed URL.
138
        $this->getAndFollowAll("admin/pages/?SubsiteID={$subsite1->ID}");
139
        $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Can access own subsite.');
140
        $this->assertContains('admin/pages', $this->mainSession->lastUrl(), 'Can access permitted section.');
141
142
        // Check forbidden section in allowed subsite.
143
        $this->getAndFollowAll("admin/assets/?SubsiteID={$subsite1->ID}");
144
        $this->assertEquals($subsite1->ID, $this->session()->get('SubsiteID'), 'Is redirected within subsite.');
145
        $this->assertNotContains(
146
            'admin/assets',
147
            $this->mainSession->lastUrl(),
148
            'Is redirected away from forbidden section'
149
        );
150
151
        // Check forbidden site, on a section that's allowed on another subsite
152
        $this->getAndFollowAll('admin/pages/?SubsiteID=0');
153
        $this->assertEquals(
154
            $this->session()->get('SubsiteID'),
155
            $subsite1->ID,
156
            'Is redirected to permitted subsite.'
157
        );
158
159
        // Check forbidden site, on a section that's not allowed on any other subsite
160
        $this->getAndFollowAll('admin/assets/?SubsiteID=0');
161
        $this->assertEquals(
162
            $this->session()->get('SubsiteID'),
163
            $subsite1->ID,
164
            'Is redirected to first permitted subsite.'
165
        );
166
        $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'Is not denied access');
167
168
        // Check the standalone XHR controller.
169
        $response = $this->getAndFollowAll('admin/subsite_xhr');
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
170
        $this->assertNotContains('Security/login', $this->mainSession->lastUrl(), 'SubsiteXHRController is reachable');
171
    }
172
}
173