From 5cf9e25f6d7bd8fa48cb84c468e99aecc1fb540f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9C=C3=A9?= Date: Sat, 27 Jan 2018 00:26:41 +0100 Subject: [PATCH] MenuHelper test --- tests/Helper/MenuHelperTest.php | 124 ++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/Helper/MenuHelperTest.php diff --git a/tests/Helper/MenuHelperTest.php b/tests/Helper/MenuHelperTest.php new file mode 100644 index 00000000..37aecabf --- /dev/null +++ b/tests/Helper/MenuHelperTest.php @@ -0,0 +1,124 @@ + array('label' => 'Dashboard', 'type' => 'route', 'children' => array()), + 1 => array('label' => 'Organizations', 'type' => 'entity', 'role' => 'ROLE_ORGANIZATION_LIST', 'children' => array()), + 2 => array('label' => 'Members', 'type' => 'entity', 'role' => 'ROLE_PLAYER_LIST', 'children' => array()), + 3 => array('label' => 'Events', 'type' => 'empty', 'children' => array( + 0 => array('label' => 'Seminaries', 'type' => 'entity', 'role' => 'ROLE_SEMINARY_LIST'), + 1 => array('label' => 'Meetings', 'type' => 'entity', 'role' => 'ROLE_MEETING_LIST'), + 2 => array('label' => 'Plenary meetings', 'type' => 'entity', 'role' => 'ROLE_PLENARYMEETING_LIST'), + )), + 4 => array('label' => 'System', 'type' => 'empty', 'children' => array( + 0 => array('label' => 'Admin users', 'type' => 'entity', 'role' => 'ROLE_ADMINUSER_LIST'), + 1 => array('label' => 'Admin groups', 'type' => 'entity', 'role' => 'ROLE_ADMINGROUP_LIST'), + )), + ); + + public function testAcessDeniedEntriesArePruned() + { + $tokenStorage = $this->createMock(TokenStorageInterface::class); + $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); + + // We just tell tokenStorage to return a not null value + $tokenStorage->method('getToken')->will($this->returnValue('foo')); + + $grantedRoleMap = array( + array('ROLE_ORGANIZATION_LIST', null, true), + array('ROLE_PLAYER_LIST', null, false), + array('ROLE_SEMINARY_LIST', null, true), + array('ROLE_MEETING_LIST', null, false), + array('ROLE_PLENARYMEETING_LIST', null, true), + array('ROLE_ADMINUSER_LIST', null, false), + array('ROLE_ADMINGROUP_LIST', null, false), + ); + $authorizationChecker->method('isGranted')->will($this->returnValueMap($grantedRoleMap)); + + $helper = new MenuHelper($tokenStorage, $authorizationChecker); + + $prunedMenu = $helper->pruneMenuItems($this->menuConfig); + + $expectedPrunedMenu = array( + 0 => array( + "label" => "Dashboard", + "type" => "route", + "children" => array(), + ), + 1 => array( + "label" => "Organizations", + "type" => "entity", + "role" => "ROLE_ORGANIZATION_LIST", + "children" => array(), + ), + 3 => array( + "label" => "Events", + "type" => "empty", + "children" => array( + 0 => array( + "label" => "Seminaries", + "type" => "entity", + "role" => "ROLE_SEMINARY_LIST", + ), + 2 => array( + "label" => "Plenary meetings", + "type" => "entity", + "role" => "ROLE_PLENARYMEETING_LIST", + ), + ), + ), + ); + + $this->assertSame($expectedPrunedMenu, $prunedMenu); + } + + public function testRestrictedEntriesArePrunedIfNoToken() + { + $tokenStorage = $this->createMock(TokenStorageInterface::class); + $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); + + // We just tell tokenStorage to return a not null value + $tokenStorage->method('getToken')->will($this->returnValue(null)); + + $grantedRoleMap = array( + array('ROLE_ORGANIZATION_LIST', null, true), + array('ROLE_PLAYER_LIST', null, false), + array('ROLE_SEMINARY_LIST', null, true), + array('ROLE_MEETING_LIST', null, false), + array('ROLE_PLENARYMEETING_LIST', null, true), + array('ROLE_ADMINUSER_LIST', null, false), + array('ROLE_ADMINGROUP_LIST', null, false), + ); + $authorizationChecker->method('isGranted')->will($this->returnValueMap($grantedRoleMap)); + + $helper = new MenuHelper($tokenStorage, $authorizationChecker); + + $prunedMenu = $helper->pruneMenuItems($this->menuConfig); + + $expectedPrunedMenu = array( + 0 => array( + "label" => "Dashboard", + "type" => "route", + "children" => array(), + ) + ); + + $this->assertSame($expectedPrunedMenu, $prunedMenu); + } +}