HelloCSE/tests/Unit/ProfileTest.php
2024-04-10 13:56:22 +02:00

285 lignes
11 Kio
PHP

<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use App\Http\Controllers\Core\Users;
use App\Models\Profile;
use App\Http\Utility\debug;
use Illuminate\Testing\Fluent\AssertableJson;
class ProfilTest extends TestCase{
use WithFaker;
/* ---------------------- LECTURE ---------------------- */
// liste des profils
public function test_get_profiles_list(){
$response=$this->getJson('/profile/list');
$response->assertJsonStructure([
'status',
'profiles',
]);
$json=$response->decodeResponseJson();
$this->assertEquals($json['status'],1);
$this->assertIsArray($json['profiles']);
}
// simple profil
// pas besoin de tester la liste de profil pour les détails d'un profil : c'est la même méthode qui contruit
// le retour du profil (ProfileManager::buildProfileData)
public function test_get_single_profile_without_auth(){
$profile=Profile::select('id')->where('status', '1')->get()->first();
$response=$this->getJson("/profile/$profile->id");
$response->assertJsonStructure([
'status',
'profile',
]);
$json=$response->decodeResponseJson();
$this->assertEquals($json['status'],1);
$this->assertIsArray($json['profile']);
$this->assertArrayNotHasKey('status',$json['profile'][0]);
}
// simple profil authentifié
// pas besoin de tester la liste de profil pour les détails d'un profil : c'est la même méthode qui contruit
// le retour du profil (ProfileManager::buildProfileData)
public function test_get_single_profile_with_auth(){
$profile=Profile::select('id')->where('status', '1')->get()->first();
$adminList=(new Users())->getUserList('1');
$response=$this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->getJson("/profile/$profile->id");
$response->assertJsonStructure([
'status',
'profile',
]);
$json=$response->decodeResponseJson();
$this->assertEquals($json['status'],1);
$this->assertIsArray($json['profile']);
$this->assertArrayHasKey('status',$json['profile'][0]);
}
/* ---------------------- ECRITURE ---------------------- */
// Création d'un profil avec autentification
// séparé du reste pour pouvoir bénéficier de l'identifiant du profil créé pour le reste des tests
public function test_post_profile_with_auth(){
global $_GLOBAL;
$adminList=(new Users())->getUserList('1');
$data=[
'firstname' => $this->faker->firstName(),
'lastname' => $this->faker->lastName(),
'status' => $this->faker->numberBetween(0, 2),
'imgbase64' => 'iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAByUDbMAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIDSURBVDhPxZPPS1RRFMf9P8659+GPQcOFGIIihqRICwkX2cCIIqIItWgRtArDEK1IqoU6M9mA2g+1tm7duHPhvoVIi6AfzqShEmr07dz73ryZ6xsNQWjx5b137zmf8/OVMVXgovQfYVwFUjGwUYn7KIwr3e9zqHRmkgFTFEpXEuC+p+Drd0HeJT/D2OXw3oVd7Qf1PCp8F5cUH4WaP4J+C+g3f8DjG+B7K+C2gdDehd0YgfcB4KEUqLqxcF5VD57+ApXZAye/g5JZqLlDKDnjjuHQzoXVtUNNfbbR+fkmaHAG3NoHunYL/HJH9MM+VToL/foYfHvB97NtOQGjhk7w2DrU7C741Z6Fqsw+VGrbh5hzA01l4b2TgIkJ3zdohQtrjkv6vyyIUjlxElnIzxBEBpbO2TJtxsaXI7BgevGH0Mmv4mDKcmWBaSlVsuUXn0Ax6WXoXwzL71dti/RrC3rhN0gcVQBRswFUSrbTTOSnXlghdwBGZstvyhpMfvSzCDOTd5mkXpQ+3l8F6xqxd3fR7Zn2F5E8MTR7ldyWyeUkQ+mdlKal6WpkDVTTFPicCpOLxi6oO+/BM9/gLQPekr+gKnMAfrYJ7nksAasL9qGvr2iZJmr7IKj7Abj3Cdg8W3vB5bVFdlGQURR2loLlPE0lYBLV7I3pXf7f/Ackr/NldqYq8Bc1NV+dJ87sugAAAABJRU5ErkJggg==',
'imgtype' => 'png',
];
$response = $this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->post('/profile',$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'profileId',
]);
$this->assertEquals($json['status'],1);
$this->assertIsInt($json['profileId']);
$_GLOBAL['lastProfileId']=$json['profileId'];
}
/* ---------------------- SANS AUTHENTIFICATION ---------------------- */
// Création d'un profil sans autentification : doit renvoyer une erreur
public function test_post_profile_without_auth(){
$data=[
'firstname' => $this->faker->firstName(),
'lastname' => $this->faker->lastName(),
'status' => $this->faker->numberBetween(0, 2),
'imgbase64' => 'iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAByUDbMAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIDSURBVDhPxZPPS1RRFMf9P8659+GPQcOFGIIihqRICwkX2cCIIqIItWgRtArDEK1IqoU6M9mA2g+1tm7duHPhvoVIi6AfzqShEmr07dz73ryZ6xsNQWjx5b137zmf8/OVMVXgovQfYVwFUjGwUYn7KIwr3e9zqHRmkgFTFEpXEuC+p+Drd0HeJT/D2OXw3oVd7Qf1PCp8F5cUH4WaP4J+C+g3f8DjG+B7K+C2gdDehd0YgfcB4KEUqLqxcF5VD57+ApXZAye/g5JZqLlDKDnjjuHQzoXVtUNNfbbR+fkmaHAG3NoHunYL/HJH9MM+VToL/foYfHvB97NtOQGjhk7w2DrU7C741Z6Fqsw+VGrbh5hzA01l4b2TgIkJ3zdohQtrjkv6vyyIUjlxElnIzxBEBpbO2TJtxsaXI7BgevGH0Mmv4mDKcmWBaSlVsuUXn0Ax6WXoXwzL71dti/RrC3rhN0gcVQBRswFUSrbTTOSnXlghdwBGZstvyhpMfvSzCDOTd5mkXpQ+3l8F6xqxd3fR7Zn2F5E8MTR7ldyWyeUkQ+mdlKal6WpkDVTTFPicCpOLxi6oO+/BM9/gLQPekr+gKnMAfrYJ7nksAasL9qGvr2iZJmr7IKj7Abj3Cdg8W3vB5bVFdlGQURR2loLlPE0lYBLV7I3pXf7f/Ackr/NldqYq8Bc1NV+dJ87sugAAAABJRU5ErkJggg==',
'imgtype' => 'png',
];
$response = $this->post('/profile',$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'msg',
]);
$this->assertEquals($json['status'],0);
$this->assertNotEmpty($json['msg']);
}
// modification d'un profil sans autentification
public function test_patch_profile_without_auth(){
global $_GLOBAL;
$data=[
'firstname' => $this->faker->firstName(),
'lastname' => $this->faker->lastName(),
'status' => 2,
];
$route="/profile/$_GLOBAL[lastProfileId]";
$response = $this->patch($route,$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'msg',
]);
$this->assertEquals($json['status'],0);
$this->assertNotEmpty($json['msg']);
}
// Création d'un commentaire de profil sans autentification
public function test_post_profile_comment_without_auth(){
global $_GLOBAL;
$data=[
'content' => 'Un test de commentaire',
];
$route="/profile/$_GLOBAL[lastProfileId]/comment";
$response = $this->post($route,$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'msg',
]);
$this->assertEquals($json['status'],0);
$this->assertNotEmpty($json['msg']);
}
// Suppression d'un profil sans autentification
public function test_delete_profile_without_auth(){
global $_GLOBAL;
$route="/profile/$_GLOBAL[lastProfileId]";
$response = $this->delete($route);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'msg',
]);
$this->assertEquals($json['status'],0);
$this->assertNotEmpty($json['msg']);
}
/* ---------------------- AVEC AUTHENTIFICATION ---------------------- */
// modification d'un profil avec autentification
public function test_patch_profile_with_auth(){
global $_GLOBAL;
$adminList=(new Users())->getUserList('1');
$data=[
'firstname' => $this->faker->firstName(),
'lastname' => $this->faker->lastName(),
'status' => 2,
];
$route="/profile/$_GLOBAL[lastProfileId]";
$response = $this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->patch($route,$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'profileId',
]);
$this->assertEquals($json['status'],1);
$this->assertIsInt($json['profileId']);
}
// Création d'un commentaire de profil avec autentification
public function test_post_profile_comment_with_auth(){
global $_GLOBAL;
$adminList=(new Users())->getUserList('1');
$data=[
'content' => 'Un test de commentaire',
];
$route="/profile/$_GLOBAL[lastProfileId]/comment";
$response = $this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->post($route,$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'commentId',
]);
$this->assertEquals($json['status'],1);
$this->assertIsInt($json['commentId']);
}
// Création d'un second commentaire de profil avec autentification du même auteur : doit retourner une erreur
public function test_post_second_profile_comment_with_auth(){
global $_GLOBAL;
$adminList=(new Users())->getUserList('1');
$data=[
'content' => 'Un test de commentaire',
];
$route="/profile/$_GLOBAL[lastProfileId]/comment";
$response = $this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->post($route,$data);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
'msg',
]);
$this->assertEquals($json['status'],0);
$this->assertNotEmpty($json['msg']);
}
// Suppression d'un profil avec autentification
public function test_delete_profile_with_auth(){
global $_GLOBAL;
$adminList=(new Users())->getUserList('1');
$route="/profile/$_GLOBAL[lastProfileId]";
$response = $this->withHeaders([
'PHP_AUTH_USER' => $adminList[0]['login'],
'PHP_AUTH_PW' => $adminList[0]['password'],
])->delete($route);
$json=$response->decodeResponseJson();
$response->assertJsonStructure([
'status',
]);
$this->assertEquals($json['status'],1);
}
}