Issues (144)

tests/php/LeftAndMainSubsitesTest.php (2 issues)

1
<?php
2
3
namespace SilverStripe\Subsites\Tests;
4
5
use SilverStripe\Admin\LeftAndMain;
6
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
7
use SilverStripe\CMS\Controllers\CMSMain;
8
use SilverStripe\CMS\Controllers\CMSPageEditController;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\FunctionalTest;
11
use SilverStripe\Security\Member;
12
use SilverStripe\Subsites\Extensions\LeftAndMainSubsites;
13
use SilverStripe\Subsites\Model\Subsite;
14
use SilverStripe\Subsites\State\SubsiteState;
15
16
class LeftAndMainSubsitesTest extends FunctionalTest
17
{
18
    protected static $fixture_file = 'SubsiteTest.yml';
19
20
    /**
21
     * Avoid subsites filtering on fixture fetching.
22
     * @param string $className
23
     * @param string $identifier
24
     * @return \SilverStripe\ORM\DataObject
25
     */
26
    protected function objFromFixture($className, $identifier)
27
    {
28
        Subsite::disable_subsite_filter(true);
29
        $obj = parent::objFromFixture($className, $identifier);
30
        Subsite::disable_subsite_filter(false);
31
32
        return $obj;
33
    }
34
35
    public function testSectionSites()
36
    {
37
        $member = $this->objFromFixture(Member::class, 'subsite1member');
38
39
        $cmsmain = singleton(CMSMain::class);
40
        $subsites = $cmsmain->sectionSites(true, 'Main site', $member);
41
        $this->assertDOSEquals([
42
            ['Title' => 'Subsite1 Template']
43
        ], $subsites, 'Lists member-accessible sites for the accessible controller.');
0 ignored issues
show
The call to SilverStripe\Dev\SapphireTest::assertDOSEquals() has too many arguments starting with 'Lists member-accessible...accessible controller.'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               assertDOSEquals([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
45
        $assetadmin = singleton(AssetAdmin::class);
46
        $subsites = $assetadmin->sectionSites(true, 'Main site', $member);
47
        $this->assertDOSEquals([], $subsites, 'Does not list any sites for forbidden controller.');
48
49
        $member = $this->objFromFixture(Member::class, 'editor');
50
51
        $cmsmain = singleton(CMSMain::class);
52
        $subsites = $cmsmain->sectionSites(true, 'Main site', $member);
53
        $this->assertDOSContains([
54
            ['Title' => 'Main site']
55
        ], $subsites, 'Includes the main site for members who can access all sites.');
0 ignored issues
show
The call to SilverStripe\Dev\SapphireTest::assertDOSContains() has too many arguments starting with 'Includes the main site ... can access all sites.'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               assertDOSContains([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
56
    }
57
58
    public function testAccessChecksDontChangeCurrentSubsite()
59
    {
60
        $this->logInAs('admin');
61
        $ids = [];
62
63
        $subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1');
64
        $subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2');
65
        $subsite3 = $this->objFromFixture(Subsite::class, 'domaintest3');
66
        $ids[] = $subsite1->ID;
67
        $ids[] = $subsite2->ID;
68
        $ids[] = $subsite3->ID;
69
        $ids[] = 0;
70
71
        // Enable session-based subsite tracking.
72
        SubsiteState::singleton()->setUseSessions(true);
73
74
        foreach ($ids as $id) {
75
            Subsite::changeSubsite($id);
76
            $this->assertEquals($id, SubsiteState::singleton()->getSubsiteId());
77
78
            $left = new LeftAndMain();
79
            $this->assertTrue($left->canView(), "Admin user can view subsites LeftAndMain with id = '$id'");
80
            $this->assertEquals(
81
                $id,
82
                SubsiteState::singleton()->getSubsiteId(),
83
                'The current subsite has not been changed in the process of checking permissions for admin user.'
84
            );
85
        }
86
    }
87
88
    public function testShouldChangeSubsite()
89
    {
90
        $l = new LeftAndMain();
91
92
        Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', false);
93
        $this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 0, 5));
94
        $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 0));
95
        $this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5));
96
        $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1));
97
98
        Config::modify()->set(CMSPageEditController::class, 'treats_subsite_0_as_global', true);
99
        $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 5));
100
        $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 0, 0));
101
        $this->assertTrue($l->shouldChangeSubsite(CMSPageEditController::class, 1, 5));
102
        $this->assertFalse($l->shouldChangeSubsite(CMSPageEditController::class, 1, 1));
103
    }
104
105
    public function testCanAccessWithPassedMember()
106
    {
107
        $memberID = $this->logInWithPermission('ADMIN');
108
        $member = Member::get()->byID($memberID);
109
110
        /** @var LeftAndMain&LeftAndMainSubsites $leftAndMain */
111
        $leftAndMain = new LeftAndMain();
112
        $this->assertTrue($leftAndMain->canAccess($member));
113
    }
114
}
115