Skip to content

Commit

Permalink
more tests for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Dec 3, 2018
1 parent 2d6108f commit 2257f5e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 9 deletions.
2 changes: 1 addition & 1 deletion TODO_mongo_odm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
- ExcludeFieldsConfigPass
- ListFormFiltersConfigPass OK
- ShortFormTypeConfigPass: OK
- ShowViewConfigPass: OK
- ShowViewConfigPass: ?

# Tests !
20 changes: 12 additions & 8 deletions src/Configuration/ListFormFiltersConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private function processObjectListFormFilters(string $objectType, array &$object

$formFilters = array();

foreach ($objectConfig['list']['form_filters'] as $i => $formFilter) {
foreach ($objectConfig['list']['form_filters'] as $key => $formFilter) {
// Detects invalid config node
if (!\is_string($formFilter) && !is_array($formFilter)) {
throw new \RuntimeException(
Expand All @@ -65,13 +65,17 @@ private function processObjectListFormFilters(string $objectType, array &$object
$filterConfig = array('property' => $formFilter);
} else {
if (!\array_key_exists('property', $formFilter)) {
throw new \RuntimeException(
\sprintf(
'One of the values of the "form_filters" option for the "list" view of the "%s" object of type "%s" does not define the mandatory option "property".',
$objectConfig['class'],
$objectType
)
);
if (\is_string($key)) {
$formFilter['property'] = $key;
} else {
throw new \RuntimeException(
\sprintf(
'One of the values of the "form_filters" option for the "list" view of the "%s" object of type "%s" does not define the mandatory option "property".',
$objectConfig['class'],
$objectType
)
);
}
}

$filterConfig = $formFilter;
Expand Down
63 changes: 63 additions & 0 deletions tests/Configuration/ListFormFiltersConfigPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration;

use AlterPHP\EasyAdminExtensionBundle\Configuration\ListFormFiltersConfigPass;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class ListFormFiltersConfigPassTest extends \PHPUnit_Framework_TestCase
{
public function testDefinedListFormFilters()
{
$doctrineOrm = $this->createMock(ManagerRegistry::class);

$listFormFiltersConfigPass = new ListFormFiltersConfigPass($doctrineOrm);

$backendConfig = array(
'entities' => array(
'TestEntity' => array(
'class' => 'App\\Entity\\TestEntity',
'list' => array('form_filters' => array(
'filter1' => array('type' => 'foo'),
'filter2' => array('type' => 'bar'),
)),
),
),
'documents' => array(
'TestDocument' => array(
'class' => 'App\\Document\\TestDocument',
'list' => array('form_filters' => array(
'filter1' => array('type' => 'foo'),
'filter2' => array('type' => 'bar'),
)),
),
),
);

$backendConfig = $listFormFiltersConfigPass->process($backendConfig);

$expectedBackendConfig = array(
'entities' => array(
'TestEntity' => array(
'class' => 'App\\Entity\\TestEntity',
'list' => array('form_filters' => array(
'filter1' => array('type' => 'foo', 'property' => 'filter1'),
'filter2' => array('type' => 'bar', 'property' => 'filter2'),
)),
),
),
'documents' => array(
'TestDocument' => array(
'class' => 'App\\Document\\TestDocument',
'list' => array('form_filters' => array(
'filter1' => array('type' => 'foo', 'property' => 'filter1'),
'filter2' => array('type' => 'bar', 'property' => 'filter2'),
)),
),
),
);

$this->assertSame($backendConfig, $expectedBackendConfig);
}
}
87 changes: 87 additions & 0 deletions tests/Helper/MenuHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,93 @@ public function testAcessDeniedEntityEntriesArePruned()
$this->assertSame($expectedPrunedMenu, $prunedMenu);
}

public function testAcessDeniedDocumentEntriesArePruned()
{
$menuConfig = array(
0 => array('label' => 'Dashboard', 'type' => 'route', 'children' => array()),
1 => array('label' => 'Organizations', 'type' => 'document', 'document' => 'Organization'),
2 => array('label' => 'Members', 'type' => 'document', 'document' => 'Member'),
3 => array('label' => 'Events', 'type' => 'empty', 'children' => array(
0 => array('label' => 'Seminaries', 'type' => 'document', 'document' => 'Seminary'),
1 => array('label' => 'Meetings', 'type' => 'document', 'document' => 'Meeting'),
2 => array('label' => 'Plenary meetings', 'type' => 'document', 'document' => 'PlenaryMeeting'),
)),
4 => array('label' => 'System', 'type' => 'empty', 'children' => array(
0 => array('label' => 'Admin users', 'type' => 'document', 'document' => 'AdminUser'),
1 => array('label' => 'Admin groups', 'type' => 'document', 'document' => 'AdminGroup'),
)),
);

$documentsConfig = array(
'Organization' => array('role_prefix' => 'ROLE_ORGANIZATION'),
'Member' => array('role_prefix' => 'ROLE_MEMBER'),
'Seminary' => array('role_prefix' => 'ROLE_SEMINARY'),
'Meeting' => array('role_prefix' => 'ROLE_MEETING'),
'PlenaryMeeting' => array('role_prefix' => 'ROLE_PLENARYMEETING'),
'AdminUser' => array('role_prefix' => 'ROLE_ADMINUSER'),
'AdminGroup' => array('role_prefix' => 'ROLE_ADMINGROUP'),
);

$adminAuthorizationChecker = $this->createMock(AdminAuthorizationChecker::class);
$symfonyAuthorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);

$grantedRoleMap = array(
array($documentsConfig['Organization'], 'list', null, true),
array($documentsConfig['Member'], 'list', null, false),
array($documentsConfig['Seminary'], 'list', null, true),
array($documentsConfig['Meeting'], 'list', null, false),
array($documentsConfig['PlenaryMeeting'], 'list', null, true),
array($documentsConfig['AdminUser'], 'list', null, false),
array($documentsConfig['AdminGroup'], 'list', null, false),
);
$adminAuthorizationChecker->method('isEasyAdminGranted')->will($this->returnValueMap($grantedRoleMap));

$helper = new MenuHelper($adminAuthorizationChecker, $symfonyAuthorizationChecker);

$prunedMenu = $helper->pruneMenuItems($menuConfig, $documentsConfig);

$expectedPrunedMenu = array(
0 => array(
'label' => 'Dashboard',
'type' => 'route',
'children' => array(),
'menu_index' => 0,
'submenu_index' => -1,
),
1 => array(
'label' => 'Organizations',
'type' => 'document',
'document' => 'Organization',
'menu_index' => 1,
'submenu_index' => -1,
),
2 => array(
'label' => 'Events',
'type' => 'empty',
'children' => array(
0 => array(
'label' => 'Seminaries',
'type' => 'document',
'document' => 'Seminary',
'menu_index' => 2,
'submenu_index' => 0,
),
1 => array(
'label' => 'Plenary meetings',
'type' => 'document',
'document' => 'PlenaryMeeting',
'menu_index' => 2,
'submenu_index' => 1,
),
),
'menu_index' => 2,
'submenu_index' => -1,
),
);

$this->assertSame($expectedPrunedMenu, $prunedMenu);
}

public function testAcessDeniedStaticEntriesArePruned()
{
$menuConfig = array(
Expand Down

0 comments on commit 2257f5e

Please sign in to comment.