Skip to content

Commit

Permalink
Merge 6962fb8 into 1df7ac8
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Jun 22, 2018
2 parents 1df7ac8 + 6962fb8 commit d30389c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
20 changes: 19 additions & 1 deletion src/Helper/MenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function pruneMenuItems(array $menuConfig, array $entitiesConfig)
{
$menuConfig = $this->pruneAccessDeniedEntries($menuConfig, $entitiesConfig);
$menuConfig = $this->pruneEmptyFolderEntries($menuConfig);
$menuConfig = $this->reindexMenuEntries($menuConfig);

return $menuConfig;
}
Expand All @@ -42,7 +43,7 @@ protected function pruneAccessDeniedEntries(array $menuConfig, array $entitiesCo
}
}

return $menuConfig;
return array_values($menuConfig);
}

protected function pruneEmptyFolderEntries(array $menuConfig)
Expand All @@ -59,6 +60,23 @@ protected function pruneEmptyFolderEntries(array $menuConfig)
}
}

return array_values($menuConfig);
}

protected function reindexMenuEntries($menuConfig)
{
foreach ($menuConfig as $key => $firstLevelItem) {
$menuConfig[$key]['menu_index'] = $key;
$menuConfig[$key]['submenu_index'] = -1;

if (isset($menuConfig[$key]['children']) && !empty($menuConfig[$key]['children'])) {
foreach ($menuConfig[$key]['children'] as $subkey => $secondLevelItem) {
$menuConfig[$key]['children'][$subkey]['menu_index'] = $key;
$menuConfig[$key]['children'][$subkey]['submenu_index'] = $subkey;
}
}
}

return $menuConfig;
}
}
52 changes: 45 additions & 7 deletions tests/Controller/UserRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,71 @@ public function testMenuIsWellPruned()

$this->assertSame(200, $this->client->getResponse()->getStatusCode());

$menuCrawler = $crawler->filter('body ul.sidebar-menu');

$this->assertSame(
1,
$crawler->filter('body ul.sidebar-menu li:contains("Catalog")')->count()
$menuCrawler->filter('li > a:contains("Catalog")')->count()
);
$this->assertSame(
1,
$crawler->filter('body ul.sidebar-menu li ul li:contains("Categories")')->count()
$menuCrawler->filter('li ul li > a:contains("Categories")')->count()
);
$this->assertSame(
0,
$crawler->filter('body ul.sidebar-menu li ul li:contains("Products")')->count()
$menuCrawler->filter('li ul li > a:contains("Products")')->count()
);
$this->assertSame(
0,
$crawler->filter('body ul.sidebar-menu li:contains("Images")')->count()
$menuCrawler->filter('li > a:contains("Images")')->count()
);
$this->assertSame(
0,
$crawler->filter('body ul.sidebar-menu li:contains("Sales")')->count()
$menuCrawler->filter('li > a:contains("Sales")')->count()
);
$this->assertSame(
0,
$crawler->filter('body ul.sidebar-menu li ul li:contains("Purchases")')->count()
$menuCrawler->filter('li ul li > a:contains("Purchases")')->count()
);
$this->assertSame(
0,
$crawler->filter('body ul.sidebar-menu li ul li:contains("Purchases items")')->count()
$menuCrawler->filter('li ul li > a:contains("Purchases items")')->count()
);
}

public function testMenuIsWellIndexedWhenPruned()
{
$this->logIn(['ROLE_ADMIN', 'ROLE_CATEGORY_LIST', 'ROLE_IMAGE_LIST', 'ROLE_ADMINGROUP_LIST']);

$this->client->followRedirects();

$crawler = $this->getBackendPage();

$this->assertSame(200, $this->client->getResponse()->getStatusCode());

/*
* Tests following menuIdex AND subMenuIndex !
*
* Catalog => (not to test because it is landing page)
* Categories => 0
* Images => 1
* System => 2
* Admin groups => 0
*/

$menuCrawler = $crawler->filter('body ul.sidebar-menu');

$this->assertRegexp(
'/menuIndex=0&submenuIndex=0/',
$menuCrawler->selectLink('Categories')->link()->getUri()
);
$this->assertRegexp(
'/menuIndex=1&submenuIndex=-1/',
$menuCrawler->selectLink('Images')->link()->getUri()
);
$this->assertRegexp(
'/menuIndex=2&submenuIndex=0/',
$menuCrawler->selectLink('Admin groups')->link()->getUri()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use AppTestBundle\Entity\FunctionalTests\Purchase;
use AppTestBundle\Entity\FunctionalTests\PurchaseItem;
use AppTestBundle\Entity\FunctionalTests\User;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
Expand Down
14 changes: 12 additions & 2 deletions tests/Helper/MenuHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,37 @@ public function testAcessDeniedEntriesArePruned()
'label' => 'Dashboard',
'type' => 'route',
'children' => array(),
'menu_index' => 0,
'submenu_index' => -1,
),
1 => array(
'label' => 'Organizations',
'type' => 'entity',
'entity' => 'Organization',
'menu_index' => 1,
'submenu_index' => -1,
),
3 => array(
2 => array(
'label' => 'Events',
'type' => 'empty',
'children' => array(
0 => array(
'label' => 'Seminaries',
'type' => 'entity',
'entity' => 'Seminary',
'menu_index' => 2,
'submenu_index' => 0,
),
2 => array(
1 => array(
'label' => 'Plenary meetings',
'type' => 'entity',
'entity' => 'PlenaryMeeting',
'menu_index' => 2,
'submenu_index' => 1,
),
),
'menu_index' => 2,
'submenu_index' => -1,
),
);

Expand Down

0 comments on commit d30389c

Please sign in to comment.