Easy_solution/tests/Controller/UserController.php

606 lines
22 KiB
PHP

<?php
namespace App\Tests\Controller;
use App\Service\AwsService;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\User;
use App\Entity\Apps;
use App\Entity\Roles;
use App\Entity\Organizations;
use App\Entity\UsersOrganizations;
use App\Entity\UserOrganizatonApp;
use App\Tests\Functional\AbstractFunctional;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
//This test will generate warning, ignore it
class UserController extends AbstractFunctional
{
//region Index Tests
#[Test]
public function test_index_super_admin_success(): void
{
$admin = $this->createUser('admin@admin.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/');
self::assertResponseIsSuccessful();
self::assertSelectorTextNotContains('body', 'Aucun utilisateur trouvé');
self::assertSelectorExists('#tabulator-userList');
}
#[Test]
public function test_index_regular_user_forbidden(): void
{
// 1. Arrange
$user = $this->createUser('user@mail.com');
$this->client->loginUser($user);
// 2. Act
$this->client->request('GET', '/user/');
// 3. Assert
self::assertResponseStatusCodeSame(403);
}
//Can't test for no users as page is designed to always have at least one user (the logged in one)
//endregion
//region Show Tests
#[Test]
public function test_view_super_admin(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$role = $this->createRole('ADMIN');
$role2 = $this->createRole('EMPTY');
$app = $this->createApp('Test App');
$organization = $this->createOrganization('Test Org');
$uo = $this->createUOLink($admin, $organization);
$uoa = $this->createUOALink($uo, $app, $role);
$this->client->request('GET', '/user/view/' . $admin->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', $admin->getEmail());
self::assertSelectorTextContains('body', $admin->getName());
self::assertSelectorTextContains('body', $app->getName());
self::assertSelectorTextContains('body', ucfirst(strtolower($role->getName())));
self::assertCheckboxChecked("roles[]", ucfirst(strtolower($role->getName())));
}
#[Test]
public function test_view_regular_user_forbidden(): void
{
// 1. Arrange
$user = $this->createUser('user@email.com');
$user2 = $this->createUser('user2@email.com');
$this->client->loginUser($user);
// 2. Act
$this->client->request('GET', '/user/view/' . $user2->getId());
// 3. Assert
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_view_admin(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_ADMIN']);
$user = $this->createUser('user@admin');
$this->client->loginUser($admin);
$role = $this->createRole('ADMIN');
$role2 = $this->createRole('USER');
$app = $this->createApp('Test App');
$organization = $this->createOrganization('Test Org');
$uo = $this->createUOLink($admin, $organization);
$uo2 = $this->createUOLink($user, $organization);
$uoa = $this->createUOALink($uo, $app, $role);
$uoa2 = $this->createUOALink($uo2, $app, $role2);
$this->client->request('GET', '/user/view/' . $user->getId() . '?organizationId=' . $organization->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', $user->getEmail());
self::assertSelectorTextContains('body', $user->getName());
self::assertSelectorTextContains('body', $app->getName());
self::assertSelectorTextContains('body', ucfirst(strtolower($role->getName())));
}
#[Test]
public function test_view_admin_different_organization_forbidden(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_ADMIN']);
$user = $this->createUser('user@admin');
$this->client->loginUser($admin);
$role = $this->createRole('ADMIN');
$role2 = $this->createRole('USER');
$app = $this->createApp('Test App');
$organization = $this->createOrganization('Test Org');
$organization2 = $this->createOrganization('Test Org2');
$uo = $this->createUOLink($admin, $organization);
$uo2 = $this->createUOLink($user, $organization2);
$uoa = $this->createUOALink($uo, $app, $role);
$uoa2 = $this->createUOALink($uo2, $app, $role2);
$this->client->request('GET', '/user/view/' . $user->getId() . '?organizationId=' . $organization->getId());
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_view_user_self_success(): void
{
$user = $this->createUser('user@email.com');
$this->client->loginUser($user);
$this->client->request('GET', '/user/view/' . $user->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', $user->getEmail());
}
#[Test]
public function test_view_user_self_with_organization_success(): void
{
$user = $this->createUser('user@email.com');
$organization = $this->createOrganization('Test Org');
$uo = $this->createUOLink($user, $organization);
$this->client->loginUser($user);
$this->client->request('GET', '/user/view/' . $user->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', $user->getEmail());
}
#[Test]
public function test_view_user_not_found(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/view/999999');
self::assertResponseStatusCodeSame(404);
}
//endregion
//region Edit Tests
#[Test]
public function test_edit_super_admin_success(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/edit/' . $admin->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', 'Modifier l\'utilisateur');
}
#[Test]
public function test_edit_regular_user_forbidden(): void
{
// 1. Arrange
$user = $this->createUser('user@mail.com');
$this->client->loginUser($user);
// 2. Act
$this->client->request('GET', '/user/edit/' . $user->getId());
// 3. Assert
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', 'Modifier l\'utilisateur');
}
#[Test]
public function test_edit_other_user_forbidden(): void
{
// 1. Arrange
$user = $this->createUser('user@email.com');
$user2 = $this->createUser('user2@email.com');
$this->client->loginUser($user);
// 2. Act
$this->client->request('GET', '/user/edit/' . $user2->getId());
// 3. Assert
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_edit_user_not_found(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/edit/999999');
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function test_edit_super_admin_edit_other_user_success(): void
{
// 1. Arrange: Disable reboot to keep our AWS mock alive
$this->client->disableReboot();
$admin = $this->createUser('admin@user.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
// 2. MOCK AWS Service (Crucial!)
// Your code calls $awsService->PutDocObj, so we must intercept that.
// 2. MOCK AWS Service
$awsMock = $this->createMock(AwsService::class);
$awsMock->expects($this->any())
->method('PutDocObj')
->willReturn(1); // <--- FIXED: Return an integer, not a boolean
// Inject the mock into the test container
static::getContainer()->set(AwsService::class, $awsMock);
// 3. Create a Dummy Image File
$tempFile = tempnam(sys_get_temp_dir(), 'test_logo');
file_put_contents($tempFile, 'fake image content'); // Create a dummy file
$logo = new UploadedFile(
$tempFile,
'logo.png',
'image/png',
null,
true // 'test' mode = true
);
// 4. Act: Submit the Edit Form
$this->client->request('GET', '/user/edit/' . $admin->getId());
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'new@mail.com',
'user_form[name]' => 'New Name',
'user_form[pictureUrl]' => $logo,
]);
// 5. Assert
self::assertResponseRedirects('/user/view/' . $admin->getId());
$this->client->followRedirect();
self::assertSelectorTextContains('body', 'new@mail.com');
// Clean up the temporary file}
unlink($tempFile);
}
#[Test]
public function test_edit_admin_user_not_found(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/edit/999999');
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function test_edit_admin_edit_other_user_success(): void
{
// 1. Arrange: Disable reboot to keep our AWS mock alive
$this->client->disableReboot();
$admin = $this->createUser('admin@user.com', ['ROLE_ADMIN']);
$user = $this->createUser('user@user.com');
$this->client->loginUser($admin);
$org = $this->createOrganization('Test Org');
$uoAdmin = $this->createUOLink($admin, $org);
$uoUser = $this->createUOLink($user, $org);
$app = $this->createApp('Test App');
$roleAdmin = $this->createRole('ADMIN');
$roleUser = $this->createRole('USER');
$this->createUOALink($uoAdmin, $app, $roleAdmin);
$this->createUOALink($uoUser, $app, $roleUser);
// 2. MOCK AWS Service (Crucial!)
// Your code calls $awsService->PutDocObj, so we must intercept that.
// 2. MOCK AWS Service
$awsMock = $this->createMock(AwsService::class);
$awsMock->expects($this->any())
->method('PutDocObj')
->willReturn(1); // <--- FIXED: Return an integer, not a boolean
// Inject the mock into the test container
static::getContainer()->set(AwsService::class, $awsMock);
// 3. Create a Dummy Image File
$tempFile = tempnam(sys_get_temp_dir(), 'test_logo');
file_put_contents($tempFile, 'fake image content'); // Create a dummy file
$logo = new UploadedFile(
$tempFile,
'logo.png',
'image/png',
null,
true // 'test' mode = true
);
// 4. Act: Submit the Edit Form
$this->client->request('GET', '/user/edit/' . $user->getId() . '?organizationId=' . $org->getId());
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'new@mail.com',
'user_form[name]' => 'New Name',
'user_form[pictureUrl]' => $logo,
]);
// 5. Assert
self::assertResponseRedirects('/user/view/' . $user->getId() . '?organizationId=' . $org->getId());
$this->client->followRedirect();
self::assertSelectorTextContains('body', 'new@mail.com');
// Clean up the temporary file}
unlink($tempFile);
}
#[Test]
public function test_edit_admin_edit_other_user_different_organization_forbidden(): void
{
// 1. Arrange: Disable reboot to keep our AWS mock alive
$this->client->disableReboot();
$admin = $this->createUser('admin@user.com', ['ROLE_ADMIN']);
$user = $this->createUser('user@user.com');
$this->client->loginUser($admin);
$org = $this->createOrganization('Test Org');
$org2 = $this->createOrganization('Test Org2');
$uoAdmin = $this->createUOLink($admin, $org);
$uoUser = $this->createUOLink($user, $org2);
$app = $this->createApp('Test App');
$roleAdmin = $this->createRole('ADMIN');
$roleUser = $this->createRole('USER');
$this->createUOALink($uoAdmin, $app, $roleAdmin);
$this->createUOALink($uoUser, $app, $roleUser);
// 2. MOCK AWS Service (Crucial!)
// Your code calls $awsService->PutDocObj, so we must intercept that.
// 2. MOCK AWS Service
$awsMock = $this->createMock(AwsService::class);
$awsMock->expects($this->any())
->method('PutDocObj')
->willReturn(1); // <--- FIXED: Return an integer, not a boolean
// Inject the mock into the test container
static::getContainer()->set(AwsService::class, $awsMock);
// 3. Create a Dummy Image File
$tempFile = tempnam(sys_get_temp_dir(), 'test_logo');
file_put_contents($tempFile, 'fake image content'); // Create a dummy file
$logo = new UploadedFile(
$tempFile,
'logo.png',
'image/png',
null,
true // 'test' mode = true
);
// 4. Act: Submit the Edit Form
$this->client->request('GET', '/user/edit/' . $user->getId() . '?organizationId=' . $org2->getId());
// 5. Assert
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_edit_user_not_found_admin(): void
{
$admin = $this->createUser('admin@admin', ['ROLE_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/edit/999999');
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function test_edit_user_self_success(): void
{
$user = $this->createUser('user@email.com');
$this->client->loginUser($user);
$this->client->request('GET', '/user/edit/' . $user->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', 'Modifier l\'utilisateur');
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'new@email.com',
'user_form[name]' => 'New Name',
]);
self::assertResponseRedirects('/user/view/' . $user->getId());
$this->client->followRedirect();
self::assertSelectorTextContains('body', 'new@email.com');
}
#[Test]
public function test_edit_user_self_with_organization_success(): void
{
$user = $this->createUser('user@email.com');
$this->client->loginUser($user);
$org = $this->createOrganization('Test Org');
$this->createUOLink($user, $org);
$this->client->request('GET', '/user/edit/' . $user->getId() . '?organizationId=' . $org->getId());
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', 'Modifier l\'utilisateur');
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'new@email.com',
'user_form[name]' => 'New Name',
]);
self::assertResponseRedirects('/user/view/' . $user->getId() . '?organizationId=' . $org->getId());
$this->client->followRedirect();
self::assertSelectorTextContains('body', 'new@email.com');
}
//endregion
//region Create Tests
#[Test]
public function test_create_super_admin_forbidden(): void
{
$admin = $this->createUser('admin@admin.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/new');
$this->client->followRedirect();
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('body', 'Accès non autorisé.');
}
#[Test]
public function test_create_regular_user_forbidden(): void
{
// 1. Arrange
$user = $this->createUser('user@email.com');
$this->client->loginUser($user);
// 2. Act
$this->client->request('GET', '/user/new');
// 3. Assert
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_create_admin_forbidden(): void
{
// 1. Arrange
$admin = $this->createUser('admin@email.com', ['ROLE_ADMIN']);
$this->client->loginUser($admin);
// 2. Act
$this->client->request('GET', '/user/new');
// 3. Assert
self::assertResponseRedirects('/user/');
$this->client->followRedirect();
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_create_super_admin_valid(): void
{
$admin = $this->createUser('admin@admin.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$org = $this->createOrganization('Test Org');
$uo = $this->createUOLink($admin, $org);
$app = $this->createApp('Test App');
$role = $this->createRole('ADMIN');
$this->createUOALink($uo, $app, $role);
$this->client->request('GET', '/user/new?organizationId=' . $org->getId());
self::assertResponseIsSuccessful();
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'email@email.com',
'user_form[name]' => 'name',
'user_form[surname]' => 'surname'
]);
self::assertResponseRedirects('/organization/view/' . $org->getId());
$this->client->followRedirect();
self::assertCount(2, $this->entityManager->getRepository(User::class)->findAll());
self::assertCount(2, $this->entityManager->getRepository(UsersOrganizations::class)->findAll());
}
#[Test]
public function test_create_admin_valid(): void
{
$admin = $this->createUser('admin@admin.com', ['ROLE_ADMIN']);
$this->client->loginUser($admin);
$org = $this->createOrganization('Test Org');
$uo = $this->createUOLink($admin, $org);
$app = $this->createApp('Test App');
$role = $this->createRole('ADMIN');
$this->createUOALink($uo, $app, $role);
$this->client->request('GET', '/user/new?organizationId=' . $org->getId());
self::assertResponseIsSuccessful();
$this->client->submitForm('Enregistrer', [
'user_form[email]' => 'email@email.com',
'user_form[name]' => 'name',
'user_form[surname]' => 'surname'
]);
self::assertResponseRedirects('/organization/view/' . $org->getId());
$this->client->followRedirect();
self::assertCount(2, $this->entityManager->getRepository(User::class)->findAll());
self::assertCount(2, $this->entityManager->getRepository(UsersOrganizations::class)->findAll());
}
#[Test]
public function test_create_admin_no_organization_forbidden(): void
{
$admin = $this->createUser('user@email.com', ['ROLE_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('GET', '/user/new');
self::assertResponseRedirects('/user/');
$this->client->followRedirect();
self::assertResponseStatusCodeSame(403);
}
//endregion
//region Delete Tests
#[Test]
public function test_delete_super_admin_success(): void
{
$admin = $this->createUser('admin@admin.com', ['ROLE_SUPER_ADMIN']);
$user = $this->createUser('user@emai.com');
$this->client->loginUser($admin);
$org = $this->createOrganization('Test Org');
$app = $this->createApp('Test App');
$role = $this->createRole('USER');
$uoUser = $this->createUOLink($user, $org);
$this->createUOALink($uoUser, $app, $role);
$this->client->request('POST', '/user/delete/' . $user->getId());
self::assertResponseRedirects('/user/');
$this->client->followRedirect();
self::assertCount(2, $this->entityManager->getRepository(User::class)->findAll());
self::assertCount(1, $this->entityManager->getRepository(UsersOrganizations::class)->findAll());
self::assertCount(1, $this->entityManager->getRepository(UserOrganizatonApp::class)->findAll());
}
#[Test]
public function test_delete_admin_forbidden(): void
{
$admin = $this->createUser('admin@email.com', ['ROLE_ADMIN']);
$user = $this->createUser('user@email.com');
$this->client->loginUser($admin);
$this->client->request('POST', '/user/delete/' . $user->getId());
self::assertResponseStatusCodeSame(403);
}
#[Test]
public function test_delete_not_found(): void
{
$admin = $this->createUser('admin@eamil.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->client->request('POST', '/user/delete/999999');
self::assertResponseStatusCodeSame(404);
}
//endregion
// même erreur que pour la sécurité. Problème lié au SSO.
//region activate/deactivate tests
// #[Test]
// public function test_deactivate_super_admin_success(): void
// {
// $admin = $this->createUser('admin@email.com', ['ROLE_SUPER_ADMIN']);
// $user = $this->createUser('user@email.com');
// $this->client->loginUser($admin);
// $org = $this->createOrganization('Test Org');
// $app = $this->createApp('Test App');
// $role = $this->createRole('USER');
// $uoUser = $this->createUOLink($user, $org);
// $this->createUOALink($uoUser, $app, $role);
// $this->client->request('POST', '/user/activeStatus/' . $user->getId(), ['status' => 'deactivate']);
// self::assertResponseRedirects('/user/');
// $this->client->followRedirect();
//
// }
//endregion
// même erreur que pour la sécurité. Problème lié au SSO.
//region tabulator tests
// #[Test]
// public function test_tabulator_super_admin_success(): void{
// $admin = $this->createUser('admin@email.com', ['ROLE_SUPER_ADMIN']);
// $this->client->loginUser($admin);
// $this->client->request('GET', '/user/data');
// self::assertResponseIsSuccessful();
// self::assertResponseHeaderSame('Content-Type', 'application/json');
//
// $response = $this->client->getResponse();
// $data = json_decode($response->getContent(), true);
// self::assertArrayHasKey('data', $data);
// }
//endregion
}