Skip to content

Commit

Permalink
MenuHelper test
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Jan 26, 2018
1 parent 9ea3e63 commit 5cf9e25
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions tests/Helper/MenuHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Test for RestoreRolesTransformer.
*
* Copied from SonataUserBundle RestoreRolesTransformer.
*
* @see https://github.com/sonata-project/SonataUserBundle/blob/master/tests/Form/Transformer/RestoreRolesTransformerTest.php
*/

namespace AlterPHP\EasyAdminExtensionBundle\Tests\Helper;

use AlterPHP\EasyAdminExtensionBundle\Helper\MenuHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class MenuHelperTest extends TestCase
{
private $menuConfig = array(
0 => 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);
}
}

0 comments on commit 5cf9e25

Please sign in to comment.