This commit is contained in:
Kevin Papst
2021-09-16 17:06:32 +02:00
committed by GitHub
parent 2536105ad0
commit f2eec4f315
9 changed files with 140 additions and 62 deletions

View File

@@ -91,6 +91,10 @@ class ActivityControllerTest extends APIControllerBaseTest
$this->assertUrlIsSecured('/api/activities');
}
/**
* @return array<Project|Activity>
* @throws \Exception
*/
protected function loadActivityTestData(): array
{
$em = $this->getEntityManager();
@@ -111,8 +115,8 @@ class ActivityControllerTest extends APIControllerBaseTest
$activity = (new Activity())->setName('second one')->setComment('2');
$em->persist($activity);
$activity = (new Activity())->setName('third one')->setComment('3')->setProject($project);
$em->persist($activity);
$activity1 = (new Activity())->setName('third one')->setComment('3')->setProject($project);
$em->persist($activity1);
$activity = (new Activity())->setName('fourth one')->setComment('4')->setProject($project2)->setVisible(false);
$em->persist($activity);
@@ -131,7 +135,7 @@ class ActivityControllerTest extends APIControllerBaseTest
$em->flush();
return [$project, $project2];
return [$project, $project2, $activity1];
}
/**
@@ -304,6 +308,29 @@ class ActivityControllerTest extends APIControllerBaseTest
$this->assertNotEmpty($result['id']);
}
public function testPatchActionWithNonGlobalActivity()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$imports = $this->loadActivityTestData();
$data = [
'name' => 'foo',
'comment' => '',
'visible' => true,
'project' => $imports[1]->getId(),
'budget' => '999',
'timeBudget' => '7200',
];
$this->request($client, '/api/activities/' . $imports[2]->getId(), 'PATCH', [], json_encode($data));
$this->assertTrue($client->getResponse()->isSuccessful());
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
self::assertApiResponseTypeStructure('ActivityEntity', $result);
$this->assertNotEmpty($result['id']);
$this->assertEquals($imports[1]->getId(), $result['project']);
}
public function testPatchActionWithInvalidUser()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);

View File

@@ -0,0 +1,84 @@
<?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\Form;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Form\ActivityEditForm;
use Symfony\Component\Form\Test\TypeTestCase;
/**
* @covers \App\Form\ActivityEditForm
*/
class ActivityEditFormTest extends TypeTestCase
{
public function testWithGlobalNewActivity()
{
$model = new Activity();
$form = $this->factory->createBuilder(ActivityEditForm::class, $model);
$attr = $form->getFormConfig()->getOption('attr');
self::assertArrayHasKey('data-form-event', $attr);
self::assertEquals('kimai.activityUpdate', $attr['data-form-event']);
self::assertTrue($form->has('name'));
self::assertTrue($form->has('comment'));
self::assertTrue($form->has('project'));
self::assertTrue($form->has('color'));
self::assertTrue($form->has('metaFields'));
self::assertTrue($form->has('visible'));
}
public function testWithGlobalNewActivityAndOptions()
{
$model = new Activity();
$form = $this->factory->createBuilder(ActivityEditForm::class, $model, [
'include_budget' => true
]);
self::assertTrue($form->has('budget'));
self::assertTrue($form->has('timeBudget'));
}
public function testWithGlobalExistingActivityAndOptions()
{
$model = $this->createMock(Activity::class);
$model->expects($this->once())->method('getId')->willReturn(1);
$form = $this->factory->createBuilder(ActivityEditForm::class, $model, [
'include_budget' => true
]);
self::assertFalse($form->has('project'));
self::assertTrue($form->has('budget'));
self::assertTrue($form->has('timeBudget'));
}
public function testWithNonGlobalExistingActivityAndOptions()
{
$project = new Project();
$customer = new Customer();
$project->setCustomer($customer);
$model = $this->createMock(Activity::class);
$model->expects($this->any())->method('getId')->willReturn(1);
$model->expects($this->any())->method('getProject')->willReturn($project);
$form = $this->factory->createBuilder(ActivityEditForm::class, $model, [
'include_budget' => true
]);
self::assertTrue($form->has('name'));
self::assertTrue($form->has('comment'));
self::assertTrue($form->has('project'));
self::assertTrue($form->has('color'));
self::assertTrue($form->has('metaFields'));
self::assertTrue($form->has('visible'));
self::assertTrue($form->has('project'));
self::assertTrue($form->has('budget'));
self::assertTrue($form->has('timeBudget'));
}
}