silverstripe-cms/tests/controller/SilverStripeNavigatorTest.php

75 lines
2.1 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Controllers\SilverStripeNavigator;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem;
/**
* @package cms
* @subpackage tests
*/
class SilverStripeNavigatorTest extends SapphireTest {
2016-03-08 21:50:55 +01:00
protected static $fixture_file = 'cms/tests/controller/CMSMainTest.yml';
2016-03-08 21:50:55 +01:00
public function testGetItems() {
$page = $this->objFromFixture('Page', 'page1');
$navigator = new SilverStripeNavigator($page);
2016-03-08 21:50:55 +01:00
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
2016-07-22 01:32:32 +02:00
$this->assertContains('SilverStripe\\CMS\\Controllers\\SilverStripeNavigatorItem_StageLink', $classes,
'Adds default classes'
);
2016-03-08 21:50:55 +01:00
$this->assertContains('SilverStripeNavigatorTest_TestItem', $classes,
'Autodiscovers new classes'
);
}
2016-03-08 21:50:55 +01:00
public function testCanView() {
$page = $this->objFromFixture('Page', 'page1');
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
$navigator = new SilverStripeNavigator($page);
2016-03-08 21:50:55 +01:00
// TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default
$this->logInWithPermission('CMS_ACCESS_CMSMain');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
$this->assertNotContains('SilverStripeNavigatorTest_ProtectedTestItem', $classes);
$this->logInWithPermission('ADMIN');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
$this->assertContains('SilverStripeNavigatorTest_ProtectedTestItem', $classes);
}
2016-03-08 21:50:55 +01:00
}
class SilverStripeNavigatorTest_TestItem extends SilverStripeNavigatorItem implements TestOnly {
public function getTitle() {
return __CLASS__;
}
public function getHTML() {
return null;
}
}
class SilverStripeNavigatorTest_ProtectedTestItem extends SilverStripeNavigatorItem implements TestOnly {
public function getTitle() {
return __CLASS__;
}
public function getHTML() {
return null;
}
public function canView($member = null) {
if(!$member) $member = Member::currentUser();
return Permission::checkMember($member, 'ADMIN');
}
}