Passed
Pull Request — master (#326)
by Robbie
04:32
created

testMainsiteAdminCanAccessAllSubsites()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 20
rs 9.4285
1
<?php
2
3
namespace SilverStripe\Subsites\Tests;
4
5
use SilverStripe\CMS\Controllers\CMSMain;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\Session;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Subsites\Model\Subsite;
10
11
class SubsiteAdminTest extends BaseSubsiteTest
12
{
13
    protected static $fixture_file = 'SubsiteTest.yml';
14
15
    protected function adminLoggedInSession()
16
    {
17
        return new Session([
18
            'loggedInAs' => $this->idFromFixture(Member::class, 'admin')
19
        ]);
20
    }
21
22
    /**
23
     * Test generation of the view
24
     */
25
    public function testBasicView()
26
    {
27
        Subsite::$write_hostmap = false;
28
        $subsite1ID = $this->objFromFixture(Subsite::class, 'domaintest1')->ID;
29
30
        // Open the admin area logged in as admin
31
        $response1 = Director::test('admin/subsites/', null, $this->adminLoggedInSession());
0 ignored issues
show
Unused Code introduced by
The assignment to $response1 is dead and can be removed.
Loading history...
32
33
        // Confirm that this URL gets you the entire page, with the edit form loaded
34
        $response2 = Director::test(
35
            "admin/subsites/SilverStripe-Subsites-Model-Subsite/EditForm/field/SilverStripe-Subsites-Model-Subsite/item/$subsite1ID/edit",
36
            null,
37
            $this->adminLoggedInSession()
38
        );
39
        $this->assertTrue(
40
            strpos($response2->getBody(), 'id="Form_ItemEditForm_ID"') !== false,
41
            'Testing Form_ItemEditForm_ID exists'
42
        );
43
        $this->assertTrue(strpos($response2->getBody(), '<head') !== false, 'Testing <head> exists');
44
    }
45
46
47
    /**
48
     * Test that the main-site user with ADMIN permissions can access all subsites, regardless
49
     * of whether he is in a subsite-specific group or not.
50
     */
51
    public function testMainsiteAdminCanAccessAllSubsites()
52
    {
53
        $this->logInAs('admin');
54
55
        $cmsMain = new CMSMain();
56
        foreach ($cmsMain->Subsites() as $subsite) {
57
            $ids[$subsite->ID] = true;
58
        }
59
60
        $this->assertArrayHasKey(0, $ids, 'Main site accessible');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ids seems to be defined by a foreach iteration on line 56. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
61
        $this->assertArrayHasKey($this->idFromFixture(Subsite::class, 'main'), $ids, 'Site with no groups inaccesible');
62
        $this->assertArrayHasKey(
63
            $this->idFromFixture(Subsite::class, 'subsite1'),
64
            $ids,
65
            'Subsite1 Template inaccessible'
66
        );
67
        $this->assertArrayHasKey(
68
            $this->idFromFixture(Subsite::class, 'subsite2'),
69
            $ids,
70
            'Subsite2 Template inaccessible'
71
        );
72
    }
73
}
74