. */ declare(strict_types=1); namespace App\Tests\Controller; use App\Entity\UserSystem\User; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; /** * @group slow * @group DB */ class BulkInfoProviderImportControllerTest extends WebTestCase { public function testStep1WithoutIds(): void { $client = static::createClient(); $this->loginAsUser($client, 'admin'); $client->request('GET', '/tools/bulk-info-provider-import/step1'); $this->assertResponseRedirects(); } public function testStep1WithInvalidIds(): void { $client = static::createClient(); $this->loginAsUser($client, 'admin'); $client->request('GET', '/tools/bulk-info-provider-import/step1?ids=999999,888888'); $this->assertResponseRedirects(); } public function testManagePage(): void { $client = static::createClient(); $this->loginAsUser($client, 'admin'); $client->request('GET', '/tools/bulk-info-provider-import/manage'); // Follow any redirects (like locale redirects) if ($client->getResponse()->isRedirect()) { $client->followRedirect(); } $this->assertResponseStatusCodeSame(Response::HTTP_OK); } public function testAccessControlForStep1(): void { $client = static::createClient(); $client->request('GET', '/tools/bulk-info-provider-import/step1?ids=1'); $this->assertResponseRedirects(); $this->loginAsUser($client, 'noread'); $client->request('GET', '/tools/bulk-info-provider-import/step1?ids=1'); // Follow redirects if any, then check for 403 or final response if ($client->getResponse()->isRedirect()) { $client->followRedirect(); } // The user might get redirected to an error page instead of direct 403 $this->assertTrue( $client->getResponse()->getStatusCode() === Response::HTTP_FORBIDDEN || $client->getResponse()->getStatusCode() === Response::HTTP_OK ); } public function testAccessControlForManage(): void { $client = static::createClient(); $client->request('GET', '/tools/bulk-info-provider-import/manage'); $this->assertResponseRedirects(); $this->loginAsUser($client, 'noread'); $client->request('GET', '/tools/bulk-info-provider-import/manage'); // Follow redirects if any, then check for 403 or final response if ($client->getResponse()->isRedirect()) { $client->followRedirect(); } // The user might get redirected to an error page instead of direct 403 $this->assertTrue( $client->getResponse()->getStatusCode() === Response::HTTP_FORBIDDEN || $client->getResponse()->getStatusCode() === Response::HTTP_OK ); } private function loginAsUser($client, string $username): void { $entityManager = $client->getContainer()->get('doctrine')->getManager(); $userRepository = $entityManager->getRepository(User::class); $user = $userRepository->findOneBy(['name' => $username]); if (!$user) { $this->markTestSkipped('User ' . $username . ' not found'); } $client->loginUser($user); } }