allow to customize dashboard permissions for latest row (#2806)
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\EventSubscriber;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Event\DashboardEvent;
|
||||
use App\EventSubscriber\DashboardSubscriber;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Repository\CustomerRepository;
|
||||
use App\Repository\ProjectRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
/**
|
||||
* @covers \App\EventSubscriber\DashboardSubscriber
|
||||
*/
|
||||
class DashboardSubscriberTest extends TestCase
|
||||
{
|
||||
public function testGetSubscribedEvents()
|
||||
{
|
||||
$events = DashboardSubscriber::getSubscribedEvents();
|
||||
$this->assertArrayHasKey(DashboardEvent::class, $events);
|
||||
$methodName = $events[DashboardEvent::class][0];
|
||||
$this->assertTrue(method_exists(DashboardSubscriber::class, $methodName));
|
||||
}
|
||||
|
||||
public function testWithNonAdminUser()
|
||||
{
|
||||
$sut = $this->getSubscriber(false, 13, 28, 37, 5);
|
||||
$event = new DashboardEvent(new User());
|
||||
|
||||
$this->assertEquals(0, \count($event->getSections()));
|
||||
$sut->onDashboardEvent($event);
|
||||
$this->assertEquals(0, \count($event->getSections()));
|
||||
}
|
||||
|
||||
public function testWithAdminUser()
|
||||
{
|
||||
$sut = $this->getSubscriber(true, 13, 28, 37, 5);
|
||||
$event = new DashboardEvent(new User());
|
||||
|
||||
$this->assertEquals(0, \count($event->getSections()));
|
||||
$sut->onDashboardEvent($event);
|
||||
|
||||
$sections = $event->getSections();
|
||||
$widgets = $sections[0]->getWidgets();
|
||||
|
||||
$this->assertEquals(1, \count($sections));
|
||||
$this->assertEquals(4, \count($widgets));
|
||||
|
||||
$this->assertEquals('stats.userTotal', $widgets[0]->getTitle());
|
||||
$this->assertEquals(13, $widgets[0]->getData());
|
||||
|
||||
$this->assertEquals('stats.customerTotal', $widgets[1]->getTitle());
|
||||
$this->assertEquals(5, $widgets[1]->getData());
|
||||
|
||||
$this->assertEquals('stats.projectTotal', $widgets[2]->getTitle());
|
||||
$this->assertEquals(37, $widgets[2]->getData());
|
||||
|
||||
$this->assertEquals('stats.activityTotal', $widgets[3]->getTitle());
|
||||
$this->assertEquals(28, $widgets[3]->getData());
|
||||
}
|
||||
|
||||
protected function getSubscriber(bool $isAdmin, int $userCount, int $activityCount, int $projectCount, int $customerCount)
|
||||
{
|
||||
$authMock = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
|
||||
$authMock->method('isGranted')->willReturn($isAdmin);
|
||||
|
||||
$userMock = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$userMock->method('countUsersForQuery')->willReturn($userCount);
|
||||
|
||||
$projectMock = $this->getMockBuilder(ProjectRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$projectMock->method('countProjectsForQuery')->willReturn($projectCount);
|
||||
|
||||
$activityMock = $this->getMockBuilder(ActivityRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$activityMock->method('countActivitiesForQuery')->willReturn($activityCount);
|
||||
|
||||
$customerMock = $this->getMockBuilder(CustomerRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$customerMock->method('countCustomersForQuery')->willReturn($customerCount);
|
||||
|
||||
return new DashboardSubscriber($authMock, $userMock, $activityMock, $projectMock, $customerMock);
|
||||
}
|
||||
}
|
||||
@@ -10,20 +10,12 @@
|
||||
namespace App\Tests\EventSubscriber;
|
||||
|
||||
use App\Configuration\MailConfiguration;
|
||||
use App\Entity\User;
|
||||
use App\Event\DashboardEvent;
|
||||
use App\Event\EmailEvent;
|
||||
use App\EventSubscriber\DashboardSubscriber;
|
||||
use App\EventSubscriber\EmailSubscriber;
|
||||
use App\Mail\KimaiMailer;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Repository\CustomerRepository;
|
||||
use App\Repository\ProjectRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
/**
|
||||
* @covers \App\EventSubscriber\EmailSubscriber
|
||||
@@ -54,51 +46,4 @@ class EmailSubscriberTest extends TestCase
|
||||
|
||||
$sut->onMailEvent($event);
|
||||
}
|
||||
|
||||
public function testWithAdminUser()
|
||||
{
|
||||
$sut = $this->getSubscriber(true, 13, 28, 37, 5);
|
||||
$event = new DashboardEvent(new User());
|
||||
|
||||
$this->assertEquals(0, \count($event->getSections()));
|
||||
$sut->onDashboardEvent($event);
|
||||
|
||||
$sections = $event->getSections();
|
||||
$widgets = $sections[0]->getWidgets();
|
||||
|
||||
$this->assertEquals(1, \count($sections));
|
||||
$this->assertEquals(4, \count($widgets));
|
||||
|
||||
$this->assertEquals('stats.userTotal', $widgets[0]->getTitle());
|
||||
$this->assertEquals(13, $widgets[0]->getData());
|
||||
|
||||
$this->assertEquals('stats.customerTotal', $widgets[1]->getTitle());
|
||||
$this->assertEquals(5, $widgets[1]->getData());
|
||||
|
||||
$this->assertEquals('stats.projectTotal', $widgets[2]->getTitle());
|
||||
$this->assertEquals(37, $widgets[2]->getData());
|
||||
|
||||
$this->assertEquals('stats.activityTotal', $widgets[3]->getTitle());
|
||||
$this->assertEquals(28, $widgets[3]->getData());
|
||||
}
|
||||
|
||||
protected function getSubscriber(bool $isAdmin, int $userCount, int $activityCount, int $projectCount, int $customerCount)
|
||||
{
|
||||
$authMock = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
|
||||
$authMock->method('isGranted')->willReturn($isAdmin);
|
||||
|
||||
$userMock = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$userMock->method('countUsersForQuery')->willReturn($userCount);
|
||||
|
||||
$projectMock = $this->getMockBuilder(ProjectRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$projectMock->method('countProjectsForQuery')->willReturn($projectCount);
|
||||
|
||||
$activityMock = $this->getMockBuilder(ActivityRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$activityMock->method('countActivitiesForQuery')->willReturn($activityCount);
|
||||
|
||||
$customerMock = $this->getMockBuilder(CustomerRepository::class)->disableOriginalConstructor()->getMock();
|
||||
$customerMock->method('countCustomersForQuery')->willReturn($customerCount);
|
||||
|
||||
return new DashboardSubscriber($authMock, $userMock, $activityMock, $projectMock, $customerMock);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,17 @@ abstract class AbstractWidgetTypeTest extends TestCase
|
||||
|
||||
abstract public function getDefaultOptions(): array;
|
||||
|
||||
public function testDefaultValues()
|
||||
protected function assertDefaultData(AbstractWidgetType $sut)
|
||||
{
|
||||
self::assertNull($sut->getData());
|
||||
}
|
||||
|
||||
public function testDefaultData()
|
||||
{
|
||||
$sut = $this->createSut();
|
||||
self::assertInstanceOf(AbstractWidgetType::class, $sut);
|
||||
self::assertEquals($this->getDefaultOptions(), $sut->getOptions());
|
||||
self::assertNull($sut->getData());
|
||||
$this->assertDefaultData($sut);
|
||||
self::assertEquals('bar', $sut->getOption('foo', 'bar'));
|
||||
}
|
||||
|
||||
|
||||
79
tests/Widget/Type/TotalsActivityTest.php
Normal file
79
tests/Widget/Type/TotalsActivityTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Widget\Type\AbstractWidgetType;
|
||||
use App\Widget\Type\TotalsActivity;
|
||||
|
||||
/**
|
||||
* @covers \App\Widget\Type\TotalsActivity
|
||||
* @covers \App\Widget\Type\SimpleWidget
|
||||
*/
|
||||
class TotalsActivityTest extends AbstractWidgetTypeTest
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function createSut(): AbstractWidgetType
|
||||
{
|
||||
return $this->createWidget();
|
||||
}
|
||||
|
||||
private function createWidget(int $results = 1): TotalsActivity
|
||||
{
|
||||
$repository = $this->createMock(ActivityRepository::class);
|
||||
$repository->expects($this->any())->method('countActivitiesForQuery')->willReturn($results);
|
||||
|
||||
$widget = new TotalsActivity($repository);
|
||||
$widget->setUser($this->user);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
public function getDefaultOptions(): array
|
||||
{
|
||||
return [
|
||||
'route' => 'admin_activity',
|
||||
'icon' => 'activity',
|
||||
'color' => 'primary',
|
||||
'dataType' => 'int',
|
||||
'user' => $this->user,
|
||||
];
|
||||
}
|
||||
|
||||
protected function assertDefaultData(AbstractWidgetType $sut)
|
||||
{
|
||||
self::assertEquals(1, $sut->getData());
|
||||
}
|
||||
|
||||
public function testData()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$sut = $this->createWidget(99);
|
||||
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
|
||||
$sut->setUser($user);
|
||||
|
||||
self::assertEquals(['view_activity', 'view_teamlead_activity', 'view_team_activity'], $sut->getPermissions());
|
||||
self::assertEquals(99, $sut->getData([]));
|
||||
}
|
||||
}
|
||||
79
tests/Widget/Type/TotalsCustomerTest.php
Normal file
79
tests/Widget/Type/TotalsCustomerTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\CustomerRepository;
|
||||
use App\Widget\Type\AbstractWidgetType;
|
||||
use App\Widget\Type\TotalsCustomer;
|
||||
|
||||
/**
|
||||
* @covers \App\Widget\Type\TotalsCustomer
|
||||
* @covers \App\Widget\Type\SimpleWidget
|
||||
*/
|
||||
class TotalsCustomerTest extends AbstractWidgetTypeTest
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function createSut(): AbstractWidgetType
|
||||
{
|
||||
return $this->createWidget();
|
||||
}
|
||||
|
||||
private function createWidget(int $results = 1): TotalsCustomer
|
||||
{
|
||||
$repository = $this->createMock(CustomerRepository::class);
|
||||
$repository->expects($this->any())->method('countCustomersForQuery')->willReturn($results);
|
||||
|
||||
$widget = new TotalsCustomer($repository);
|
||||
$widget->setUser($this->user);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
public function getDefaultOptions(): array
|
||||
{
|
||||
return [
|
||||
'route' => 'admin_customer',
|
||||
'icon' => 'customer',
|
||||
'color' => 'primary',
|
||||
'dataType' => 'int',
|
||||
'user' => $this->user,
|
||||
];
|
||||
}
|
||||
|
||||
protected function assertDefaultData(AbstractWidgetType $sut)
|
||||
{
|
||||
self::assertEquals(1, $sut->getData());
|
||||
}
|
||||
|
||||
public function testData()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$sut = $this->createWidget(99);
|
||||
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
|
||||
$sut->setUser($user);
|
||||
|
||||
self::assertEquals(['view_customer', 'view_teamlead_customer', 'view_team_customer'], $sut->getPermissions());
|
||||
self::assertEquals(99, $sut->getData([]));
|
||||
}
|
||||
}
|
||||
79
tests/Widget/Type/TotalsProjectTest.php
Normal file
79
tests/Widget/Type/TotalsProjectTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\ProjectRepository;
|
||||
use App\Widget\Type\AbstractWidgetType;
|
||||
use App\Widget\Type\TotalsProject;
|
||||
|
||||
/**
|
||||
* @covers \App\Widget\Type\TotalsProject
|
||||
* @covers \App\Widget\Type\SimpleWidget
|
||||
*/
|
||||
class TotalsProjectTest extends AbstractWidgetTypeTest
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function createSut(): AbstractWidgetType
|
||||
{
|
||||
return $this->createWidget();
|
||||
}
|
||||
|
||||
private function createWidget(int $results = 1): TotalsProject
|
||||
{
|
||||
$repository = $this->createMock(ProjectRepository::class);
|
||||
$repository->expects($this->any())->method('countProjectsForQuery')->willReturn($results);
|
||||
|
||||
$widget = new TotalsProject($repository);
|
||||
$widget->setUser($this->user);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
public function getDefaultOptions(): array
|
||||
{
|
||||
return [
|
||||
'route' => 'admin_project',
|
||||
'icon' => 'project',
|
||||
'color' => 'primary',
|
||||
'dataType' => 'int',
|
||||
'user' => $this->user,
|
||||
];
|
||||
}
|
||||
|
||||
protected function assertDefaultData(AbstractWidgetType $sut)
|
||||
{
|
||||
self::assertEquals(1, $sut->getData());
|
||||
}
|
||||
|
||||
public function testData()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$sut = $this->createWidget(99);
|
||||
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
|
||||
$sut->setUser($user);
|
||||
|
||||
self::assertEquals(['view_project', 'view_teamlead_project', 'view_team_project'], $sut->getPermissions());
|
||||
self::assertEquals(99, $sut->getData([]));
|
||||
}
|
||||
}
|
||||
79
tests/Widget/Type/TotalsUserTest.php
Normal file
79
tests/Widget/Type/TotalsUserTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\UserRepository;
|
||||
use App\Widget\Type\AbstractWidgetType;
|
||||
use App\Widget\Type\TotalsUser;
|
||||
|
||||
/**
|
||||
* @covers \App\Widget\Type\TotalsUser
|
||||
* @covers \App\Widget\Type\SimpleWidget
|
||||
*/
|
||||
class TotalsUserTest extends AbstractWidgetTypeTest
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function createSut(): AbstractWidgetType
|
||||
{
|
||||
return $this->createWidget();
|
||||
}
|
||||
|
||||
private function createWidget(int $results = 1): TotalsUser
|
||||
{
|
||||
$repository = $this->createMock(UserRepository::class);
|
||||
$repository->expects($this->any())->method('countUsersForQuery')->willReturn($results);
|
||||
|
||||
$widget = new TotalsUser($repository);
|
||||
$widget->setUser($this->user);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
public function getDefaultOptions(): array
|
||||
{
|
||||
return [
|
||||
'route' => 'admin_user',
|
||||
'icon' => 'user',
|
||||
'color' => 'primary',
|
||||
'dataType' => 'int',
|
||||
'user' => $this->user,
|
||||
];
|
||||
}
|
||||
|
||||
protected function assertDefaultData(AbstractWidgetType $sut)
|
||||
{
|
||||
self::assertEquals(1, $sut->getData());
|
||||
}
|
||||
|
||||
public function testData()
|
||||
{
|
||||
$user = new User();
|
||||
$user->setAlias('foo');
|
||||
|
||||
$sut = $this->createWidget(99);
|
||||
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
|
||||
$sut->setUser($user);
|
||||
|
||||
self::assertEquals(['view_user'], $sut->getPermissions());
|
||||
self::assertEquals(99, $sut->getData([]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user