Skip to content

Commit

Permalink
Use admin entry as service ID (sonata-project#3903)
Browse files Browse the repository at this point in the history
In df42796, the admin groups variable was
changed from containing only service IDs to containing an array of options
per item.

To get the service ID, the 'admin' entry should be used.
  • Loading branch information
wouterj authored and soullivaneuh committed Jun 1, 2016
1 parent f3a30f8 commit c7e103c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public function getAdminsByGroup($group)
return $admins;
}

foreach ($this->adminGroups[$group]['items'] as $id) {
$admins[] = $this->getInstance($id);
foreach ($this->adminGroups[$group]['items'] as $item) {
$admins[] = $this->getInstance($item['admin']);
}

return $admins;
Expand Down
57 changes: 43 additions & 14 deletions Tests/Admin/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetGroups()

$expectedOutput = array(
'adminGroup1' => array(
'sonata.user.admin.group1' => 'adminUserClass',
'sonata.user.admin.group1' => 'sonata_user_admin_group1_AdminClass',
),
);

Expand Down Expand Up @@ -74,13 +74,13 @@ public function testGetDashboardGroups()

$pool->setAdminGroups(array(
'adminGroup1' => array(
'items' => array('itemKey' => array('admin' => 'sonata.user.admin.group1', 'label' => '', 'route' => '', 'route_params' => array())),
'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group1')),
),
'adminGroup2' => array(
'items' => array('itemKey' => array('admin' => 'sonata.user.admin.group2', 'label' => '', 'route' => '', 'route_params' => array())),
'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group2')),
),
'adminGroup3' => array(
'items' => array('itemKey' => array('admin' => 'sonata.user.admin.group3', 'label' => '', 'route' => '', 'route_params' => array())),
'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group3')),
),
));

Expand Down Expand Up @@ -115,12 +115,22 @@ public function testGetAdminsByGroup()
{
$this->pool->setAdminServiceIds(array('sonata.admin1', 'sonata.admin2', 'sonata.admin3'));
$this->pool->setAdminGroups(array(
'adminGroup1' => array('items' => array('sonata.admin1', 'sonata.admin2')),
'adminGroup2' => array('items' => array('sonata.admin3')),
'adminGroup1' => array(
'items' => array(
$this->getItemArray('sonata.admin1'),
$this->getItemArray('sonata.admin2'),
),
),
'adminGroup2' => array(
'items' => array($this->getItemArray('sonata.admin3')),
),
));

$this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
$this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
$this->assertEquals(array(
'sonata_admin1_AdminClass',
'sonata_admin2_AdminClass',
), $this->pool->getAdminsByGroup('adminGroup1'));
$this->assertEquals(array('sonata_admin3_AdminClass'), $this->pool->getAdminsByGroup('adminGroup2'));
}

public function testGetAdminForClassWhenAdminClassIsNotSet()
Expand All @@ -146,17 +156,23 @@ public function testGetAdminForClassWithInvalidFormat()
*/
public function testGetAdminForClassWithTooManyRegisteredAdmin()
{
$this->pool->setAdminClasses(array('someclass' => array('sonata.user.admin.group1', 'sonata.user.admin.group2')));
$this->pool->setAdminClasses(array(
'someclass' => array('sonata.user.admin.group1', 'sonata.user.admin.group2'),
));

$this->assertTrue($this->pool->hasAdminByClass('someclass'));
$this->assertSame('adminUserClass', $this->pool->getAdminByClass('someclass'));
$this->pool->getAdminByClass('someclass');
}

public function testGetAdminForClassWhenAdminClassIsSet()
{
$this->pool->setAdminServiceIds(array('sonata.user.admin.group1'));
$this->pool->setAdminClasses(array('someclass' => array('sonata.user.admin.group1')));
$this->pool->setAdminClasses(array(
'someclass' => array('sonata.user.admin.group1'),
));

$this->assertTrue($this->pool->hasAdminByClass('someclass'));
$this->assertSame('adminUserClass', $this->pool->getAdminByClass('someclass'));
$this->assertSame('sonata_user_admin_group1_AdminClass', $this->pool->getAdminByClass('someclass'));
}

/**
Expand All @@ -171,7 +187,8 @@ public function testGetInstanceWithUndefinedServiceId()
public function testGetAdminByAdminCode()
{
$this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
$this->assertSame('adminUserClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));

$this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
}

public function testGetAdminByAdminCodeForChildClass()
Expand Down Expand Up @@ -261,8 +278,20 @@ private function getContainer()
$containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$containerMock->expects($this->any())
->method('get')
->will($this->returnValue('adminUserClass'));
->will($this->returnCallback(function ($serviceId) {
return str_replace('.', '_', $serviceId).'_AdminClass';
}));

return $containerMock;
}

private function getItemArray($serviceId)
{
return array(
'admin' => $serviceId,
'label' => '',
'route' => '',
'route_params' => array(),
);
}
}

0 comments on commit c7e103c

Please sign in to comment.