2011-03-23 10:51:00 +01:00
< ? php
/**
* @ package cms
* @ subpackage tests
*/
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' );
// Add a controller to the menu and check it is as expected
2011-03-29 06:54:08 +02:00
CMSMenu :: add_controller ( 'CMSMenuTest_LeftAndMainController' );
2011-03-23 10:51:00 +01:00
$menuItems = CMSMenu :: get_menu_items ();
2011-03-29 06:54:08 +02:00
$menuItem = $menuItems [ 'CMSMenuTest_LeftAndMainController' ];
2011-03-23 10:51:00 +01:00
$this -> assertType ( 'CMSMenuItem' , $menuItem , 'Controller menu item is of class CMSMenuItem' );
2011-03-29 06:54:08 +02:00
$this -> assertEquals ( $menuItem -> url , singleton ( 'CMSMenuTest_LeftAndMainController' ) -> Link (), 'Controller menu item has the correct link' );
$this -> assertEquals ( $menuItem -> controller , 'CMSMenuTest_LeftAndMainController' , 'Controller menu item has the correct controller class' );
$this -> assertEquals ( $menuItem -> priority , singleton ( 'CMSMenuTest_LeftAndMainController' ) -> stat ( 'menu_priority' ), 'Controller menu item has the correct priority' );
2011-03-23 10:51:00 +01:00
CMSMenu :: clear_menu ();
// Add a link to the menu
CMSMenu :: add_link ( 'LinkCode' , 'link title' , 'http://www.example.com' );
$menuItems = CMSMenu :: get_menu_items ();
$menuItem = $menuItems [ 'LinkCode' ];
$this -> assertType ( 'CMSMenuItem' , $menuItem , 'Link menu item is of class CMSMenuItem' );
$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' );
$this -> assertEquals ( $menuItem -> priority , - 1 , 'Link menu item has the correct priority' );
CMSMenu :: clear_menu ();
}
public function testCmsClassDetection () {
// 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 ();
2011-03-29 06:54:08 +02:00
$this -> assertContains ( 'SecurityAdmin' , $cmsClasses , 'SecurityAdmin included in valid CMS Classes' );
2011-03-23 10:51:00 +01:00
$this -> assertNotContains ( 'LeftAndMain' , $cmsClasses , 'LeftAndMain not included in valid CMS Classes' );
$this -> assertNotContains ( 'ModelAdmin' , $cmsClasses , 'LeftAndMain not included in valid CMS Classes' );
}
public function testAdvancedMenuHandling () {
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 ();
2011-03-29 06:54:08 +02:00
$menuItem = CMSMenu :: get_menu_item ( 'SecurityAdmin' );
$this -> assertType ( 'CMSMenuItem' , $menuItem , 'SecurityAdmin menu item exists' );
$this -> assertEquals ( $menuItem -> url , singleton ( 'SecurityAdmin' ) -> Link (), 'Menu item has the correct link' );
$this -> assertEquals ( $menuItem -> controller , 'SecurityAdmin' , 'Menu item has the correct controller class' );
2011-03-23 10:51:00 +01:00
$this -> assertEquals (
$menuItem -> priority ,
2011-03-29 06:54:08 +02:00
singleton ( 'SecurityAdmin' ) -> stat ( 'menu_priority' ),
2011-03-23 10:51:00 +01:00
'Menu item has the correct priority'
);
// 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 (
$menuItem -> priority ,
singleton ( $menuItem -> controller ) -> stat ( 'menu_priority' ),
" Menu item $menuItem->title has the correct priority "
);
$this -> assertLessThanOrEqual ( $priority , $menuItem -> priority , 'Menu item is of lower or equal priority' );
}
}
2011-03-29 06:54:08 +02:00
}
class CMSMenuTest_LeftAndMainController extends LeftAndMain implements TestOnly {
static $url_segment = 'CMSMenuTest_LeftAndMainController' ;
static $menu_title = 'CMSMenuTest_LeftAndMainController' ;
static $menu_priority = 50 ;
2011-03-23 10:51:00 +01:00
}