2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
2016-08-11 01:14:02 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Admin\Tests;
|
|
|
|
|
2016-08-11 01:14:02 +02:00
|
|
|
use SilverStripe\Admin\CMSMenu;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\Admin\CMSMenuItem;
|
2016-08-11 01:14:02 +02:00
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2016-11-13 08:35:43 +01:00
|
|
|
use SilverStripe\Admin\ModelAdmin;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\Admin\SecurityAdmin;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
2016-08-11 01:40:23 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
class CMSMenuTest extends SapphireTest implements TestOnly {
|
|
|
|
|
|
|
|
public function testBasicMenuHandling() {
|
|
|
|
// Clear existing menu
|
|
|
|
CMSMenu::clear_menu();
|
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
|
|
|
$this->assertTrue((empty($menuItems)), 'Menu can be cleared');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Add a controller to the menu and check it is as expected
|
2016-11-13 08:35:43 +01:00
|
|
|
CMSMenu::add_controller(CMSMenuTest\LeftAndMainController::class);
|
2011-03-23 10:51:00 +01:00
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
2016-11-13 08:35:43 +01:00
|
|
|
$menuItem = $menuItems['SilverStripe-Admin-Tests-CMSMenuTest-LeftAndMainController'];
|
|
|
|
$this->assertInstanceOf(CMSMenuItem::class, $menuItem, 'Controller menu item is of class CMSMenuItem');
|
|
|
|
$this->assertContains($menuItem->url, CMSMenuTest\LeftAndMainController::singleton()->Link(),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Controller menu item has the correct link');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertEquals($menuItem->controller, CMSMenuTest\LeftAndMainController::class,
|
2012-09-26 23:34:00 +02:00
|
|
|
'Controller menu item has the correct controller class');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertEquals($menuItem->priority, CMSMenuTest\LeftAndMainController::singleton()->stat('menu_priority'),
|
2014-08-15 08:53:05 +02:00
|
|
|
'Controller menu item has the correct priority');
|
2011-03-23 10:51:00 +01:00
|
|
|
CMSMenu::clear_menu();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-02 10:57:43 +02:00
|
|
|
// Add another controller
|
2016-11-13 08:35:43 +01:00
|
|
|
CMSMenu::add_controller(CMSMenuTest\CustomTitle::class);
|
2016-05-02 10:57:43 +02:00
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
2016-11-13 08:35:43 +01:00
|
|
|
$menuItem = $menuItems['SilverStripe-Admin-Tests-CMSMenuTest-CustomTitle'];
|
|
|
|
$this->assertInstanceOf(CMSMenuItem::class, $menuItem, 'Controller menu item is of class CMSMenuItem');
|
|
|
|
$this->assertEquals(CMSMenuTest\CustomTitle::class . ' (localised)', $menuItem->title);
|
2016-05-02 10:57:43 +02:00
|
|
|
CMSMenu::clear_menu();
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Add a link to the menu
|
|
|
|
CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com');
|
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
|
|
|
$menuItem = $menuItems['LinkCode'];
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertInstanceOf(CMSMenuItem::class, $menuItem, 'Link menu item is of class CMSMenuItem');
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals($menuItem->title, 'link title', 'Link menu item has the correct title');
|
|
|
|
$this->assertEquals($menuItem->url,'http://www.example.com', 'Link menu item has the correct link');
|
|
|
|
$this->assertNull($menuItem->controller, 'Link menu item has no controller class');
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals($menuItem->priority, -1, 'Link menu item has the correct priority');
|
2011-03-23 10:51:00 +01:00
|
|
|
CMSMenu::clear_menu();
|
2013-10-02 08:06:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 01:18:44 +02:00
|
|
|
public function testRemove() {
|
|
|
|
CMSMenu::clear_menu();
|
|
|
|
CMSMenu::add_menu_item('custom', 'Custom Title', 'custom');
|
2016-11-13 08:35:43 +01:00
|
|
|
CMSMenu::add_menu_item('other', 'Other Section', 'other', CMSMenuTest\LeftAndMainController::class);
|
2016-08-11 01:18:44 +02:00
|
|
|
$this->assertNotEmpty(CMSMenu::get_menu_items());
|
|
|
|
|
2016-11-13 08:35:43 +01:00
|
|
|
CMSMenu::remove_menu_class(CMSMenuTest\LeftAndMainController::class);
|
2016-08-11 01:18:44 +02:00
|
|
|
CMSMenu::remove_menu_item('custom');
|
|
|
|
|
|
|
|
$this->assertEmpty(CMSMenu::get_menu_items());
|
|
|
|
}
|
|
|
|
|
2013-10-02 08:06:54 +02:00
|
|
|
public function testLinkWithExternalAttributes() {
|
|
|
|
CMSMenu::clear_menu();
|
|
|
|
|
|
|
|
CMSMenu::add_link('LinkCode', 'link title', 'http://www.example.com', -2, array(
|
|
|
|
'target' => '_blank'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-02 08:06:54 +02:00
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
2016-08-11 01:40:23 +02:00
|
|
|
/** @var CMSMenuItem $menuItem */
|
2013-10-02 08:06:54 +02:00
|
|
|
$menuItem = $menuItems['LinkCode'];
|
|
|
|
|
|
|
|
$this->assertEquals('target="_blank"', $menuItem->getAttributesHTML());
|
|
|
|
|
|
|
|
CMSMenu::clear_menu();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCmsClassDetection() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Get CMS classes and check that:
|
2011-03-29 06:54:08 +02:00
|
|
|
// 1.) SecurityAdmin is included
|
2011-03-23 10:51:00 +01:00
|
|
|
// 2.) LeftAndMain & ModelAdmin are excluded
|
|
|
|
$cmsClasses = CMSMenu::get_cms_classes();
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertContains(SecurityAdmin::class, $cmsClasses, 'SecurityAdmin included in valid CMS Classes');
|
|
|
|
$this->assertNotContains(LeftAndMain::class, $cmsClasses, 'LeftAndMain not included in valid CMS Classes');
|
|
|
|
$this->assertNotContains(ModelAdmin::class, $cmsClasses, 'LeftAndMain not included in valid CMS Classes');
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdvancedMenuHandling() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
// Populate from CMS Classes, check for existance of SecurityAdmin
|
2011-03-23 10:51:00 +01:00
|
|
|
CMSMenu::clear_menu();
|
|
|
|
CMSMenu::populate_menu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItem = CMSMenu::get_menu_item('SilverStripe-Admin-SecurityAdmin');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertInstanceOf(CMSMenuItem::class, $menuItem, 'SecurityAdmin menu item exists');
|
2016-08-11 01:40:23 +02:00
|
|
|
$this->assertContains($menuItem->url, SecurityAdmin::singleton()->Link(), 'Menu item has the correct link');
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(
|
2016-11-13 08:35:43 +01:00
|
|
|
SecurityAdmin::class,
|
|
|
|
$menuItem->controller,
|
|
|
|
'Menu item has the correct controller class'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-08-11 01:40:23 +02:00
|
|
|
SecurityAdmin::singleton()->stat('menu_priority'),
|
2016-11-13 08:35:43 +01:00
|
|
|
$menuItem->priority,
|
2011-03-23 10:51:00 +01:00
|
|
|
'Menu item has the correct priority'
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Check that menu order is correct by priority
|
|
|
|
// Note this will break if populate_menu includes normal links (ie, as not controller)
|
|
|
|
$menuItems = CMSMenu::get_menu_items();
|
|
|
|
$priority = 9999; // ok, *could* be set larger, but shouldn't need to be!
|
|
|
|
foreach($menuItems as $menuItem) {
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$menuItem->priority,
|
|
|
|
singleton($menuItem->controller)->stat('menu_priority'),
|
2011-03-23 10:51:00 +01:00
|
|
|
"Menu item $menuItem->title has the correct priority"
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertLessThanOrEqual($priority, $menuItem->priority, 'Menu item is of lower or equal priority');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
}
|