-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Allow to extend the hours in the contracts
- Loading branch information
1 parent
9b1afa7
commit 1895e83
Showing
6 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Controller\Contracts; | ||
|
||
use App\Controller\BaseController; | ||
use App\Entity\Contract; | ||
use App\Repository\ContractRepository; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class MaxHoursController extends BaseController | ||
{ | ||
#[Route('/contracts/{uid}/max-hours/edit', name: 'edit contract max hours', methods: ['GET', 'HEAD'])] | ||
public function edit( | ||
Contract $contract, | ||
Request $request, | ||
): Response { | ||
$organization = $contract->getOrganization(); | ||
|
||
$this->denyAccessUnlessGranted('orga:manage:contracts', $organization); | ||
|
||
return $this->render('contracts/max_hours/edit.html.twig', [ | ||
'organization' => $organization, | ||
'contract' => $contract, | ||
'additionalHours' => 0, | ||
]); | ||
} | ||
|
||
#[Route('/contracts/{uid}/max-hours/edit', name: 'update contract max hours', methods: ['POST'])] | ||
public function update( | ||
Contract $contract, | ||
Request $request, | ||
ContractRepository $contractRepository, | ||
TranslatorInterface $translator, | ||
): Response { | ||
$organization = $contract->getOrganization(); | ||
|
||
$this->denyAccessUnlessGranted('orga:manage:contracts', $organization); | ||
|
||
$additionalHours = $request->request->getInt('additionalHours'); | ||
$csrfToken = $request->request->getString('_csrf_token'); | ||
|
||
if ($additionalHours < 0) { | ||
$additionalHours = 0; | ||
} | ||
|
||
if (!$this->isCsrfTokenValid('update contract max hours', $csrfToken)) { | ||
return $this->renderBadRequest('contracts/max_hours/edit.html.twig', [ | ||
'organization' => $organization, | ||
'contract' => $contract, | ||
'additionalHours' => $additionalHours, | ||
'error' => $translator->trans('csrf.invalid', [], 'errors'), | ||
]); | ||
} | ||
|
||
$maxHours = $contract->getMaxHours() + $additionalHours; | ||
$contract->setMaxHours($maxHours); | ||
|
||
$contractRepository->save($contract, true); | ||
|
||
return $this->redirectToRoute('organization contract', [ | ||
'uid' => $organization->getUid(), | ||
'contract_uid' => $contract->getUid(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{# | ||
# This file is part of Bileto. | ||
# Copyright 2022-2023 Probesys | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
#} | ||
|
||
{% extends 'modal.html.twig' %} | ||
|
||
{% block title %}{{ 'contracts.max_hours.edit.title' | trans }}{% endblock %} | ||
|
||
{% block body %} | ||
<form | ||
action="{{ path('update contract max hours', {'uid': contract.uid}) }}" | ||
method="post" | ||
class="wrapper wrapper--small wrapper--center flow" | ||
> | ||
<input type="hidden" name="_csrf_token" value="{{ csrf_token('update contract max hours') }}"> | ||
|
||
{% if error %} | ||
{{ include('alerts/_error.html.twig', { message: error }, with_context = false) }} | ||
{% endif %} | ||
|
||
<div class="flow flow--small"> | ||
<label for="additional-hours"> | ||
{{ 'contracts.max_hours.edit.additional_hours' | trans }} | ||
</label> | ||
|
||
<input | ||
type="number" | ||
id="additional-hours" | ||
name="additionalHours" | ||
value="{{ additionalHours }}" | ||
min="0" | ||
step="1" | ||
required | ||
class="input--size3" | ||
/> | ||
</div> | ||
|
||
<div class="form__actions"> | ||
<button class="button--primary" type="submit"> | ||
{{ 'forms.save_changes' | trans }} | ||
</button> | ||
</div> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Tests\Controller\Tickets; | ||
|
||
use App\Tests\AuthorizationHelper; | ||
use App\Tests\Factory\ContractFactory; | ||
use App\Tests\Factory\UserFactory; | ||
use App\Tests\SessionHelper; | ||
use App\Utils; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
|
||
class MaxHoursControllerTest extends WebTestCase | ||
{ | ||
use AuthorizationHelper; | ||
use Factories; | ||
use ResetDatabase; | ||
use SessionHelper; | ||
|
||
public function testGetEditRendersCorrectly(): void | ||
{ | ||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$this->grantOrga($user->object(), ['orga:manage:contracts']); | ||
$contract = ContractFactory::createOne(); | ||
|
||
$client->request('GET', "/contracts/{$contract->getUid()}/max-hours/edit"); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertSelectorTextContains('h1', 'Extend the number of hours of the contract'); | ||
} | ||
|
||
public function testGetEditFailsIfAccessIsForbidden(): void | ||
{ | ||
$this->expectException(AccessDeniedException::class); | ||
|
||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$contract = ContractFactory::createOne(); | ||
|
||
$client->catchExceptions(false); | ||
$client->request('GET', "/contracts/{$contract->getUid()}/max-hours/edit"); | ||
} | ||
|
||
public function testPostUpdateSavesTheMaxHours(): void | ||
{ | ||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$this->grantOrga($user->object(), ['orga:manage:contracts']); | ||
$contract = ContractFactory::createOne([ | ||
'maxHours' => 10, | ||
]); | ||
|
||
$client->request('POST', "/contracts/{$contract->getUid()}/max-hours/edit", [ | ||
'_csrf_token' => $this->generateCsrfToken($client, 'update contract max hours'), | ||
'additionalHours' => 5, | ||
]); | ||
|
||
$organization = $contract->getOrganization(); | ||
$this->assertResponseRedirects("/organizations/{$organization->getUid()}/contracts/{$contract->getUid()}", 302); | ||
$contract->refresh(); | ||
$this->assertSame(15, $contract->getMaxHours()); | ||
} | ||
|
||
public function testPostUpdateDoesNotAcceptNegativeAdditionalHours(): void | ||
{ | ||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$this->grantOrga($user->object(), ['orga:manage:contracts']); | ||
$contract = ContractFactory::createOne([ | ||
'maxHours' => 10, | ||
]); | ||
|
||
$client->request('POST', "/contracts/{$contract->getUid()}/max-hours/edit", [ | ||
'_csrf_token' => $this->generateCsrfToken($client, 'update contract max hours'), | ||
'additionalHours' => -5, | ||
]); | ||
|
||
$organization = $contract->getOrganization(); | ||
$this->assertResponseRedirects("/organizations/{$organization->getUid()}/contracts/{$contract->getUid()}", 302); | ||
$contract->refresh(); | ||
$this->assertSame(10, $contract->getMaxHours()); | ||
} | ||
|
||
public function testPostUpdateFailsIfCsrfTokenIsInvalid(): void | ||
{ | ||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$this->grantOrga($user->object(), ['orga:manage:contracts']); | ||
$contract = ContractFactory::createOne([ | ||
'maxHours' => 10, | ||
]); | ||
|
||
$client->request('POST', "/contracts/{$contract->getUid()}/max-hours/edit", [ | ||
'_csrf_token' => 'not the token', | ||
'additionalHours' => 5, | ||
]); | ||
|
||
$organization = $contract->getOrganization(); | ||
$this->assertSelectorTextContains('[data-test="alert-error"]', 'The security token is invalid'); | ||
$contract->refresh(); | ||
$this->assertSame(10, $contract->getMaxHours()); | ||
} | ||
|
||
public function testPostUpdateFailsIfAccessIsForbidden(): void | ||
{ | ||
$this->expectException(AccessDeniedException::class); | ||
|
||
$client = static::createClient(); | ||
$user = UserFactory::createOne(); | ||
$client->loginUser($user->object()); | ||
$contract = ContractFactory::createOne([ | ||
'maxHours' => 10, | ||
]); | ||
|
||
$client->catchExceptions(false); | ||
$client->request('POST', "/contracts/{$contract->getUid()}/max-hours/edit", [ | ||
'_csrf_token' => $this->generateCsrfToken($client, 'update contract max hours'), | ||
'additionalHours' => 5, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters