301 lines
9.9 KiB
PHP
301 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Controller;
|
|
|
|
use App\Entity\Apps;
|
|
use App\Entity\Organizations;
|
|
use App\Service\ActionService;
|
|
use App\Service\LoggerService;
|
|
use App\Tests\Functional\AbstractFunctional;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
|
|
class ApplicationController extends AbstractFunctional
|
|
{
|
|
|
|
//region Index Tests
|
|
#[Test]
|
|
public function index_redirects_unauthenticated_user(): void
|
|
{
|
|
$this->client->request('GET', '/application/');
|
|
self::assertResponseRedirects('/login'); // Assuming your login route is /login
|
|
}
|
|
|
|
#[Test]
|
|
public function index_lists_applications_for_authenticated_user(): void
|
|
{
|
|
// 1. Arrange: Create User and Data
|
|
$user = $this->createUser('user@test.com');
|
|
$this->createApp('App One');
|
|
$this->createApp('App Two');
|
|
|
|
// 2. Act: Login and Request
|
|
$this->client->loginUser($user);
|
|
$this->client->request('GET', '/application/');
|
|
|
|
// 3. Assert
|
|
self::assertResponseIsSuccessful();
|
|
self::assertSelectorTextContains('body', 'App One');
|
|
self::assertSelectorTextContains('body', 'App Two');
|
|
}
|
|
|
|
|
|
#[Test]
|
|
public function index_no_application_found(): void
|
|
{
|
|
$user = $this->createUser('user@test.com');
|
|
$this->client->loginUser($user);
|
|
$this->client->request('GET', '/application/');
|
|
|
|
self::assertResponseIsSuccessful();
|
|
self::assertSelectorTextContains('body', 'Aucune application disponible');
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Edit Tests
|
|
|
|
#[Test]
|
|
public function edit_page_denies_access_to_regular_users(): void
|
|
{
|
|
$user = $this->createUser('regular@test.com');
|
|
$app = $this->createApp('Target App');
|
|
|
|
$this->client->loginUser($user);
|
|
$this->client->request('GET', '/application/edit/' . $app->getId());
|
|
|
|
self::assertResponseStatusCodeSame(403);
|
|
}
|
|
#[Test]
|
|
public function edit_page_denies_access_to_admin_users(): void
|
|
{
|
|
$user = $this->createUser('admin@test.com', ['ROLE_ADMIN']);
|
|
$app = $this->createApp('Target App');
|
|
|
|
$this->client->loginUser($user);
|
|
$this->client->request('GET', '/application/edit/' . $app->getId());
|
|
|
|
self::assertResponseStatusCodeSame(403);
|
|
}
|
|
|
|
#[Test]
|
|
public function edit_page_loads_for_super_admin(): void
|
|
{
|
|
$admin = $this->createUser('admin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('Editable App');
|
|
|
|
$this->client->loginUser($admin);
|
|
$crawler = $this->client->request('GET', '/application/edit/' . $app->getId());
|
|
|
|
self::assertResponseIsSuccessful();
|
|
$this->assertCount(1, $crawler->filter('input[name="name"]'));
|
|
}
|
|
|
|
#[Test]
|
|
public function edit_submits_changes_successfully(): void
|
|
{
|
|
$admin = $this->createUser('admin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('Old Name');
|
|
|
|
$this->client->loginUser($admin);
|
|
|
|
// Simulate POST request directly (mimicking form submission)
|
|
$this->client->request('POST', '/application/edit/' . $app->getId(), [
|
|
'name' => 'New Name',
|
|
'description' => 'Updated Description',
|
|
'descriptionSmall' => 'Updated Small',
|
|
]);
|
|
|
|
// Assert Redirection
|
|
self::assertResponseRedirects('/application/');
|
|
$this->client->followRedirect();
|
|
|
|
// Assert Database Update
|
|
$this->entityManager->clear(); // Clear identity map to force fresh fetch
|
|
$updatedApp = $this->entityManager->getRepository(Apps::class)->find($app->getId());
|
|
$this->assertEquals('New Name', $updatedApp->getName());
|
|
$this->assertEquals('Updated Description', $updatedApp->getDescription());
|
|
}
|
|
|
|
#[Test]
|
|
public function edit_handles_non_existent_id_get(): void
|
|
{
|
|
$admin = $this->createUser('admin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$this->client->loginUser($admin);
|
|
|
|
$this->client->request('GET', '/application/edit/999999');
|
|
|
|
self::assertResponseRedirects('/application/');
|
|
$this->client->followRedirect();
|
|
self::assertSelectorExists('.alert-danger');
|
|
|
|
self::assertSelectorTextContains('.alert-danger', "n'existe pas");
|
|
}
|
|
|
|
#[Test]
|
|
public function edit_handles_non_existent_id_post(): void
|
|
{
|
|
// Arrange
|
|
$admin = $this->createUser('superAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('App With Issue');
|
|
$this->client->loginUser($admin);
|
|
|
|
$this->client->request('POST', '/application/edit/' . 99999, [
|
|
'name' => 'New Name',
|
|
'description' => 'Updated Description',
|
|
'descriptionSmall' => 'Updated Small',
|
|
]);
|
|
|
|
self::assertResponseRedirects('/application/');
|
|
$this->client->followRedirect();
|
|
self::assertSelectorExists('.alert-danger');
|
|
self::assertSelectorTextContains('.alert-danger', "n'existe pas");
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region Authorize Tests
|
|
#[Test]
|
|
public function authorize_adds_organization_successfully(): void
|
|
{
|
|
$admin = $this->createUser('admin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('Auth App');
|
|
$org = $this->createOrganization('Test Org');
|
|
|
|
$this->client->loginUser($admin);
|
|
|
|
$this->client->request('POST', '/application/authorize/' . $app->getId(), [
|
|
'organizationId' => $org->getId()
|
|
]);
|
|
|
|
self::assertResponseStatusCodeSame(200);
|
|
|
|
// Clear Doctrine memory to force fetching fresh data from DB
|
|
$this->entityManager->clear();
|
|
|
|
$updatedApp = $this->entityManager->getRepository(Apps::class)->find($app->getId());
|
|
|
|
$exists = $updatedApp->getOrganization()->exists(function($key, $element) use ($org) {
|
|
return $element->getId() === $org->getId();
|
|
});
|
|
|
|
$this->assertTrue($exists, 'The application is not linked to the organization.');
|
|
}
|
|
|
|
#[Test]
|
|
public function authorize_fails_on_invalid_organization(): void
|
|
{
|
|
$admin = $this->createUser('sAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('App For Org Test');
|
|
|
|
$this->client->loginUser($admin);
|
|
$this->client->request('POST', '/applica tion/authorize/' . $app->getId(), [
|
|
'organizationId' => 99999
|
|
]);
|
|
|
|
self::assertResponseStatusCodeSame(404);
|
|
|
|
}
|
|
|
|
#[Test]
|
|
public function authorize_fails_on_invalid_application(): void
|
|
{
|
|
$admin = $this->createUser('sAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$this->client->loginUser($admin);
|
|
$this->client->request('POST', '/application/authorize/99999', [
|
|
'organizationId' => 1
|
|
]);
|
|
self::assertResponseStatusCodeSame(404);
|
|
}
|
|
//endregion
|
|
|
|
//region revoke Tests
|
|
|
|
#[Test]
|
|
public function revoke_denies_access_to_admins(): void
|
|
{
|
|
$user = $this->createUser('Admin@test.com', ['ROLE_ADMIN']);
|
|
$app = $this->createApp('App To Revoke');
|
|
$org = $this->createOrganization('Org To Revoke');
|
|
$this->client->loginUser($user);
|
|
$this->client->request('POST', '/application/revoke/'. $app->getId(), [
|
|
'organizationId' => $org->getId()
|
|
]);
|
|
self::assertResponseStatusCodeSame(403);
|
|
|
|
}
|
|
|
|
#[Test]
|
|
public function revoke_denies_access_to_user(): void
|
|
{
|
|
$user = $this->createUser('user@test.com');
|
|
$app = $this->createApp('App To Revoke');
|
|
$org = $this->createOrganization('Org To Revoke');
|
|
$this->client->loginUser($user);
|
|
$this->client->request('POST', '/application/revoke/'. $app->getId(), [
|
|
'organizationId' => $org->getId()
|
|
]);
|
|
self::assertResponseStatusCodeSame(403);
|
|
|
|
}
|
|
|
|
#[Test]
|
|
public function revoke_removes_organization_successfully(): void
|
|
{
|
|
$admin = $this->createUser('sAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('App To Revoke Org');
|
|
$org = $this->createOrganization('Org To Be Revoked');
|
|
// First, authorize the organization
|
|
$app->addOrganization($org);
|
|
$this->entityManager->persist($app);
|
|
$this->entityManager->flush();
|
|
$this->client->loginUser($admin);
|
|
$this->client->request('POST', '/application/revoke/'. $app->getId(), [
|
|
'organizationId' => $org->getId()
|
|
]);
|
|
self::assertResponseStatusCodeSame(200);
|
|
|
|
// Clear Doctrine memory to force fetching fresh data from DB
|
|
$this->entityManager->clear();
|
|
$updatedApp = $this->entityManager->getRepository(Apps::class)->find($app->getId());
|
|
$exists = $updatedApp->getOrganization()->exists(function($key, $element) use ($org) {
|
|
return $element === $org;
|
|
});
|
|
self::assertFalse($exists, 'The organization was removed from the application.');
|
|
}
|
|
|
|
#[Test]
|
|
public function revoke_fails_on_invalid_organization(): void
|
|
{
|
|
$admin = $this->createUser('sAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$app = $this->createApp('App To Revoke Org');
|
|
$org = $this->createOrganization('Org To Be Revoked');
|
|
// First, authorize the organization
|
|
$app->addOrganization($org);
|
|
$this->entityManager->persist($app);
|
|
$this->entityManager->flush();
|
|
$this->client->loginUser($admin);
|
|
$this->client->request('POST', '/application/revoke/' . $app->
|
|
getId(), [
|
|
'organizationId' => 99999
|
|
]);
|
|
self::assertResponseStatusCodeSame(404);
|
|
}
|
|
|
|
#[Test]
|
|
public function revoke_fails_on_invalid_application(): void
|
|
{
|
|
$admin = $this->createUser('sAdmin@test.com', ['ROLE_SUPER_ADMIN']);
|
|
$org = $this->createOrganization('Org To Be Revoked');
|
|
// First, authorize the organization
|
|
$this->client->loginUser($admin);
|
|
$this->client->request('POST', '/application/revoke/' . 9999, [
|
|
'organizationId' => 99999
|
|
]);
|
|
self::assertResponseStatusCodeSame(404, "L'application n'existe pas.");
|
|
}
|
|
//endregion
|
|
|
|
|
|
|
|
|
|
} |