Files

115 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller\Admin\ApplicationFormSubmission;
use App\Entity\ApplicationFormSubmission;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* The return url of a detail page's own actions.
*
* Every action on this page has to hand on the "r" it was given, not the url of the page it sits
* on: return_url() encodes the current request, so a send button would send the reviewer back to
* the detail page they are already looking at - and because the send controller redirects with
* that same "r", "zurück" then points at the detail page itself and appears to do nothing, one
* nesting level deeper per click. forwarded_return_url() is what keeps pointing at the list.
*/
class DetailControllerTest extends WebTestCase
{
private const LIST_URL = '/admin/application-form-submission';
private KernelBrowser $client;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
$this->client = static::createClient(server: ['HTTPS' => true]);
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->entityManager->createQuery(sprintf('DELETE FROM %s', ApplicationFormSubmission::class))->execute();
$this->entityManager->createQuery(sprintf('DELETE FROM %s', User::class))->execute();
$this->client->loginUser($this->reviewer());
}
public function testTheActionsCarryTheReturnUrlTheyWereGivenOn(): void
{
$submission = $this->submission();
$crawler = $this->client->request(
'GET',
sprintf('/admin/application-form-submission/detail/%s', $submission->getUuid()),
['r' => rawurlencode(self::LIST_URL)]
);
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
// "zurück" goes where the reviewer came from.
$this->assertSame(self::LIST_URL, $crawler->filter('a.btn')->first()->attr('href'));
foreach (['send-receipt', 'send-follow-up'] as $action) {
$button = $crawler->filter(sprintf('button[hx-get*="%s"]', $action));
$this->assertCount(1, $button, sprintf('The %s button is missing', $action));
$url = $button->attr('hx-get');
$this->assertStringContainsString('r='.rawurlencode(self::LIST_URL), $url);
// The bug: the detail url itself as the return url, which "zurück" then loops on.
$this->assertStringNotContainsString('detail', urldecode(substr($url, (int) strpos($url, 'r='))));
}
}
public function testWithoutAReturnUrlTheActionsCarryNone(): void
{
$submission = $this->submission();
$crawler = $this->client->request(
'GET',
sprintf('/admin/application-form-submission/detail/%s', $submission->getUuid())
);
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
// Dropped rather than emitted empty, so the controllers fall back to the list route.
$this->assertStringNotContainsString('r=', $crawler->filter('button[hx-get*="send-receipt"]')->attr('hx-get'));
$this->assertSame(self::LIST_URL, $crawler->filter('a.btn')->first()->attr('href'));
}
private function submission(): ApplicationFormSubmission
{
$submission = (new ApplicationFormSubmission(
'applicationform',
'Bewerbungsformular',
new \DateTimeImmutable('2026-09-22T12:30:00+02:00'),
'F'
))->setEmail('[email protected]');
$this->entityManager->persist($submission);
$this->entityManager->flush();
return $submission;
}
private function reviewer(): User
{
$user = (new User())
->setFirstName('Rita')
->setLastName('Reviewer')
->setEmail('[email protected]')
->setRoles(['ROLE_TEAM_ADMIN'])
->setBusProAddressId(1)
->setBusProPersonId(1)
;
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
}