Add test for Icinga\Web\MenuItem

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-24 16:14:27 +02:00
parent 290fe9eeb5
commit c5c375e72d
3 changed files with 112 additions and 53 deletions

View File

@ -29,9 +29,9 @@
namespace Icinga\Web; namespace Icinga\Web;
use Icinga\Logger\Logger;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Logger\Logger;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
class Menu extends MenuItem class Menu extends MenuItem
@ -39,34 +39,49 @@ class Menu extends MenuItem
/** /**
* Create menu from the application's menu config file plus the config files from all enabled modules * Create menu from the application's menu config file plus the config files from all enabled modules
* *
* @return Menu * @return self
*/ */
public static function fromConfig() public static function fromConfig()
{ {
$menu = new static('menu'); $menu = new static('menu');
$manager = Icinga::app()->getModuleManager(); $manager = Icinga::app()->getModuleManager();
try { try {
$menuConfigs = array(Config::app('menu')); $menuConfigs = array(Config::app('menu'));
} catch (NotReadableError $e) { } catch (NotReadableError $e) {
Logger::error($e); Logger::error($e);
$menuConfigs = array(); $menuConfigs = array();
} }
try {
foreach ($manager->listEnabledModules() as $moduleName) { try {
$modules = $manager->listEnabledModules();
} catch (NotReadableError $e) {
Logger::error($e);
$modules = array();
}
foreach ($modules as $moduleName) {
try {
$moduleMenuConfig = Config::module($moduleName, 'menu'); $moduleMenuConfig = Config::module($moduleName, 'menu');
if ($moduleMenuConfig) { } catch (NotReadableError $e) {
Logger::error($e);
$moduleMenuConfig = array();
}
if (!empty($moduleMenuConfig)) {
$menuConfigs[] = $moduleMenuConfig; $menuConfigs[] = $moduleMenuConfig;
} }
} }
} catch (NotReadableError $e) {
Logger::error($e);
}
return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs)); return $menu->loadMenuItems($menu->flattenConfigs($menuConfigs));
} }
/** /**
* Flatten configs into a key-value array * Flatten configs
*
* @param array $configs An two dimensional array of menu configurations
*
* @return array The flattened config, as key-value array
*/ */
public function flattenConfigs(array $configs) public function flattenConfigs(array $configs)
{ {
@ -79,18 +94,23 @@ class Menu extends MenuItem
$flattened[$section] = $itemConfig; $flattened[$section] = $itemConfig;
} }
} }
ksort($flattened);
return $flattened; return $flattened;
} }
/** /**
* Load menu items from a key-value array * Load menu items
*
* @param array $items The items to load, as key-value array
*
* @return self
*/ */
public function loadMenuItems(array $flattened) public function loadMenuItems(array $items)
{ {
foreach ($flattened as $id => $itemConfig) { foreach ($items as $id => $itemConfig) {
$this->addChild($id, $itemConfig); $this->addChild($id, $itemConfig);
} }
return $this; return $this;
} }
} }

View File

@ -13,7 +13,7 @@ class MenuItem
* *
* @type string * @type string
*/ */
private $id; protected $id;
/** /**
* Item title * Item title
@ -22,7 +22,7 @@ class MenuItem
* *
* @type string * @type string
*/ */
private $title; protected $title;
/** /**
* Item priority * Item priority
@ -31,38 +31,42 @@ class MenuItem
* *
* @type int * @type int
*/ */
private $priority = 100; protected $priority = 100;
/** /**
* Item url * Item url
* *
* @type string * @type string
*/ */
private $url; protected $url;
/** /**
* Item icon path * Item icon path
* *
* @type string * @type string
*/ */
private $icon; protected $icon;
/** /**
* Item icon class * Item icon class
* *
* @type string * @type string
*/ */
private $iconClass; protected $iconClass;
/** /**
* Item's children * Item's children
* *
* @type array * @type array
*/ */
private $children = array(); protected $children = array();
private $attribs = array();
/**
* HTML anchor tag attributes
*
* @var array
*/
protected $attribs = array();
/** /**
* Create a new MenuItem * Create a new MenuItem
@ -114,7 +118,6 @@ class MenuItem
return $this; return $this;
} }
/** /**
* Getter for title * Getter for title
* *
@ -220,7 +223,7 @@ class MenuItem
/** /**
* Set the configuration for the item * Set the configuration for the item
* *
* @param object $config * @param Zend_Config $config
*/ */
public function setConfig($config) public function setConfig($config)
{ {
@ -249,18 +252,21 @@ class MenuItem
} else { } else {
// Submenu item // Submenu item
list($parentId, $id) = explode('.', $id, 2); list($parentId, $id) = explode('.', $id, 2);
if ($this->hasChild($parentId)) { if ($this->hasChild($parentId)) {
$parent = $this->getChild($parentId); $parent = $this->getChild($parentId);
} else { } else {
$parent = $this->addChild($parentId); $parent = $this->addChild($parentId);
} }
$menuItem = $parent->addChild($id, $itemConfig); $menuItem = $parent->addChild($id, $itemConfig);
} }
return $menuItem; return $menuItem;
} }
/** /**
* Check whether the item has children * Check whether the item has any children
* *
* @return bool * @return bool
*/ */
@ -270,10 +276,11 @@ class MenuItem
} }
/** /**
* Get children sorted * Get sorted children
* *
* @return array * @return array
* @see cmpChildren() *
* @see MenuItem::cmpChildren()
*/ */
public function getChildren() public function getChildren()
{ {
@ -282,11 +289,11 @@ class MenuItem
} }
/** /**
* Whether a given child id exists * Return whether a given child id exists
* *
* @param string $id * @param string $id
* *
* @return self|$default * @return bool
*/ */
public function hasChild($id) public function hasChild($id)
{ {
@ -300,18 +307,20 @@ class MenuItem
* @param mixed $default * @param mixed $default
* *
* @return MenuItem * @return MenuItem
*
* @throws ProgrammingError * @throws ProgrammingError
*/ */
public function getChild($id) public function getChild($id)
{ {
if ($this->hasChild($id)) { if (!$this->hasChild($id)) {
return $this->children[$id];
}
throw new ProgrammingError(sprintf('Trying to get invalid Menu child "%s"', $id)); throw new ProgrammingError(sprintf('Trying to get invalid Menu child "%s"', $id));
} }
return $this->children[$id];
}
/** /**
* Set HTML a tag attributes * Set HTML anchor tag attributes
* *
* @param array $attribs * @param array $attribs
* *
@ -324,7 +333,7 @@ class MenuItem
} }
/** /**
* Get HTML a tag attributes * Get HTML anchor tag attributes
* *
* @return array * @return array
*/ */
@ -346,6 +355,7 @@ class MenuItem
if ($a->priority === $b->priority) { if ($a->priority === $b->priority) {
return ($a->getTitle() > $b->getTitle()) ? 1 : -1; return ($a->getTitle() > $b->getTitle()) ? 1 : -1;
} }
return ($a->priority > $b->priority) ? 1 : -1; return ($a->priority > $b->priority) ? 1 : -1;
} }
} }

View File

@ -0,0 +1,29 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Web;
use Zend_Config;
use Icinga\Web\MenuItem;
use Icinga\Test\BaseTestCase;
class MenuItemTest extends BaseTestCase
{
public function testWhetherMenuItemsAreNaturallySorted()
{
$item = new MenuItem('test');
$item->addChild(5, new Zend_Config(array('title' => 'ccc5')));
$item->addChild(0, new Zend_Config(array('title' => 'aaa')));
$item->addChild(3, new Zend_Config(array('title' => 'ccc')));
$item->addChild(2, new Zend_Config(array('title' => 'bbb')));
$item->addChild(4, new Zend_Config(array('title' => 'ccc2')));
$item->addChild(1, new Zend_Config(array('title' => 'bb')));
$this->assertEquals(
array('aaa', 'bb', 'bbb', 'ccc', 'ccc2', 'ccc5'),
array_map(function ($it) { return $it->getTitle(); }, $item->getChildren()),
'MenuItem::getChildren does not return its elements in natural order'
);
}
}