added tags for timesheets (#604)
This commit is contained in:
89
tests/API/TagControllerTest.php
Normal file
89
tests/API/TagControllerTest.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?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\API;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Tests\DataFixtures\TagFixtures;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \App\API\TagController
|
||||
* @group integration
|
||||
*/
|
||||
class TagControllerTest extends APIControllerBaseTest
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
||||
|
||||
$tagList = ['Test', 'Administration', 'Support', '#2018-001', '#2018-002', '#2018-003', 'Development',
|
||||
'Marketing', 'First Level Support', 'Bug Fixing'];
|
||||
|
||||
$fixture = new TagFixtures();
|
||||
$fixture->setTagArray($tagList);
|
||||
$this->importFixture($em, $fixture);
|
||||
}
|
||||
|
||||
public function testGetCollection()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||
$this->assertAccessIsGranted($client, '/api/tags');
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(10, count($result));
|
||||
$this->assertEquals('Test', $result[9]);
|
||||
}
|
||||
|
||||
public function testEmptyCollection()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||
$query = ['name' => 'nothing'];
|
||||
$this->assertAccessIsGranted($client, '/api/tags', 'GET', $query);
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertEmpty($result);
|
||||
$this->assertEquals(0, count($result));
|
||||
}
|
||||
|
||||
public function testPartOfEntries()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||
$query = ['name' => 'in'];
|
||||
$this->assertAccessIsGranted($client, '/api/tags', 'GET', $query);
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(3, count($result));
|
||||
|
||||
$this->assertEquals('Administration', $result[0]);
|
||||
$this->assertEquals('Bug Fixing', $result[1]);
|
||||
$this->assertEquals('Marketing', $result[2]);
|
||||
}
|
||||
|
||||
public function testDeleteAction()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
|
||||
|
||||
$this->request($client, '/api/tags/1', 'DELETE');
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
|
||||
$this->assertEmpty($client->getResponse()->getContent());
|
||||
|
||||
$this->assertAccessIsGranted($client, '/api/tags');
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertEquals(9, count($result));
|
||||
}
|
||||
}
|
||||
@@ -586,10 +586,55 @@ class TimesheetControllerTest extends APIControllerBaseTest
|
||||
$this->assertApiResponseAccessDenied($client->getResponse(), 'You are not allowed to stop this timesheet');
|
||||
}
|
||||
|
||||
public function testGetCollectionWithTags()
|
||||
{
|
||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
||||
|
||||
$fixture = new TimesheetFixtures();
|
||||
$fixture
|
||||
->setFixedRate(true)
|
||||
->setHourlyRate(true)
|
||||
->setAmount(10)
|
||||
->setUser($this->getUserByRole($em, User::ROLE_USER))
|
||||
->setStartDate(new \DateTime('-10 days'))
|
||||
->setAllowEmptyDescriptions(false)
|
||||
->setUseTags(true)
|
||||
->setTags(['Test', 'Administration']);
|
||||
$this->importFixture($em, $fixture);
|
||||
|
||||
$query = ['tags' => 'Test'];
|
||||
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(5, count($result));
|
||||
$this->assertDefaultStructure($result[0], false);
|
||||
|
||||
$query = ['tags' => 'Test,Admin'];
|
||||
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(10, count($result));
|
||||
$this->assertDefaultStructure($result[0], false);
|
||||
|
||||
$query = ['tags' => 'Nothing'];
|
||||
$this->assertAccessIsGranted($client, '/api/timesheets', 'GET', $query);
|
||||
$result = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertEquals(20, count($result));
|
||||
$this->assertDefaultStructure($result[0], false);
|
||||
}
|
||||
|
||||
protected function assertDefaultStructure(array $result, $full = true)
|
||||
{
|
||||
$expectedKeys = [
|
||||
'id', 'begin', 'end', 'duration', 'description', 'rate', 'activity', 'project', 'user'
|
||||
'id', 'begin', 'end', 'duration', 'description', 'rate', 'activity', 'project', 'tags', 'user'
|
||||
];
|
||||
|
||||
if ($full) {
|
||||
@@ -622,5 +667,4 @@ class TimesheetControllerTest extends APIControllerBaseTest
|
||||
$this->assertArrayHasKey('name', $result['project']['customer']);
|
||||
$this->assertArrayHasKey('visible', $result['project']['customer']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user