Passed
Push — master ( c59f14...22b8ef )
by Daniel
04:29 queued 17s
created

SilverStripeNavigatorTest::testCanView()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Controllers;
4
5
use SilverStripe\CMS\Controllers\SilverStripeNavigator;
6
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_ArchiveLink;
7
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_LiveLink;
8
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_StageLink;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Security\Member;
11
12
class SilverStripeNavigatorTest extends SapphireTest
13
{
14
    protected static $fixture_file = 'CMSMainTest.yml';
15
16
    protected static $extra_dataobjects = [
17
        SilverStripeNavigatorTest\UnstagedRecord::class,
18
    ];
19
20
    public function testGetItems()
21
    {
22
        $page = $this->objFromFixture('Page', 'page1');
23
        $navigator = new SilverStripeNavigator($page);
0 ignored issues
show
Bug introduced by
$page of type SilverStripe\ORM\DataObject is incompatible with the type SilverStripe\ORM\CMSPreviewable expected by parameter $record of SilverStripe\CMS\Control...avigator::__construct(). ( Ignorable by Annotation )

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

23
        $navigator = new SilverStripeNavigator(/** @scrutinizer ignore-type */ $page);
Loading history...
24
25
        $items = $navigator->getItems();
26
        $classes = array_map('get_class', $items->toArray());
27
        $this->assertContains(
28
            SilverStripeNavigatorItem_StageLink::class,
29
            $classes,
30
            'Adds default classes'
31
        );
32
33
        $this->assertContains(
34
            SilverStripeNavigatorTest_TestItem::class,
35
            $classes,
36
            'Autodiscovers new classes'
37
        );
38
39
        // Non-versioned items don't have stage / live
40
    }
41
42
    public function testCanView()
43
    {
44
        $page = $this->objFromFixture('Page', 'page1');
45
        $admin = $this->objFromFixture(Member::class, 'admin');
0 ignored issues
show
Unused Code introduced by
The assignment to $admin is dead and can be removed.
Loading history...
46
        $navigator = new SilverStripeNavigator($page);
0 ignored issues
show
Bug introduced by
$page of type SilverStripe\ORM\DataObject is incompatible with the type SilverStripe\ORM\CMSPreviewable expected by parameter $record of SilverStripe\CMS\Control...avigator::__construct(). ( Ignorable by Annotation )

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

46
        $navigator = new SilverStripeNavigator(/** @scrutinizer ignore-type */ $page);
Loading history...
47
48
        // TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default
49
        $this->logInWithPermission('CMS_ACCESS_CMSMain');
50
        $items = $navigator->getItems();
51
        $classes = array_map('get_class', $items->toArray());
52
        $this->assertNotContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes);
53
54
        $this->logInWithPermission('ADMIN');
55
        $items = $navigator->getItems();
56
        $classes = array_map('get_class', $items->toArray());
57
        $this->assertContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes);
58
59
        // Unversioned record shouldn't be viewable in stage / live specific views
60
        $unversioned = new SilverStripeNavigatorTest\UnstagedRecord();
61
        $navigator2 = new SilverStripeNavigator($unversioned);
62
        $classes = array_map('get_class', $navigator2->getItems()->toArray());
63
        $this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
64
        $this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes);
65
        $this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
66
    }
67
}
68