From e7a2e05cfd9712376e3945157833710158cf6cd4 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Wed, 10 Jun 2020 13:54:35 +0200 Subject: [PATCH] Tests génériques automatiques pour Materiels sont presque complets, --- .gitignore | 2 ++ src/Controller/MaterielsController.php | 12 +++++++++++- tests/TestCase/Controller/MaterielsControllerTest.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 43a360d..958588b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ webroot/test webroot/debug_kit webroot/dompdf webroot/bootstrap_u_i + +MODIFS_DE_VENDOR/* diff --git a/src/Controller/MaterielsController.php b/src/Controller/MaterielsController.php index d7b60d9..b2ab702 100755 --- a/src/Controller/MaterielsController.php +++ b/src/Controller/MaterielsController.php @@ -348,6 +348,16 @@ class MaterielsController extends AppController { } // TEST only + public function getNextStatusFrom($status) { + $status_num = self::statuses[$status]; + // si ARCHIVED => on reboucle à CREATED + if ($status_num==4) $status_num=0; + // Les indices commencent à 0, donc $status_num est déjà l'indice suivant... + $newstatus = array_keys(self::statuses)[$status_num]; + //debug("new status is $newstatus"); + return $newstatus; + } + public function isStatus($status) { return in_array($status, array_keys(self::statuses)); } @@ -1780,7 +1790,7 @@ class MaterielsController extends AppController { if ($IS_ADD) { // 1) on crée un materiel vide $materiel = $this->Materiels->newEntity(); - // - COPIE de materiel (on a cliqué sur "Copier ce materiel") => id passé en argument + // - add_by_copy : COPIE de materiel (on a cliqué sur "Copier ce materiel") => id passé en argument if (isset($this->request->getAttribute('params')['pass'][0])) { // On récupère le materiel à copier et on le copie dans $materiel $materiel_to_copy = $this->Materiels->get($this->request->getAttribute('params')['pass'][0]); diff --git a/tests/TestCase/Controller/MaterielsControllerTest.php b/tests/TestCase/Controller/MaterielsControllerTest.php index db750f5..3e7ff77 100755 --- a/tests/TestCase/Controller/MaterielsControllerTest.php +++ b/tests/TestCase/Controller/MaterielsControllerTest.php @@ -175,10 +175,11 @@ class MaterielsControllerTest extends General { $actions_and_roles = []; $actions = [ // CRUD - 'add','view','index','edit','delete' + 'add','add_by_copy', 'view','index','edit','delete', //'view', //'index', // Autres + 'statusCreated', ]; $roles4 = [ 'user', @@ -187,8 +188,9 @@ class MaterielsControllerTest extends General { 'super', ]; foreach ($actions as $action) - foreach ([0] as $i) { - //foreach ([0,1,2,3] as $i) { + //foreach ([0] as $i) { + //foreach ([0,1] as $i) { + foreach ([0,1,2,3] as $i) { $item = [ $action, $roles4[$i], $this->ROLES4[$i][0] ]; //debug($item); $actions_and_roles[] = $item; @@ -1840,7 +1842,7 @@ class MaterielsControllerTest extends General { * (OLD dataProvider dataProviderRoles4) * @dataProvider dataProviderRoles4AndStatuses4 */ - public function NON_testViewsViewAndIndexForRole($user_role, $matos_status) { + public function OFF_testViewsViewAndIndexForRole($user_role, $matos_status) { // Force le rechargement des entités materiel en BD (car on les modifie en cours de route) //AppController::forceReload(); @@ -1848,7 +1850,7 @@ class MaterielsControllerTest extends General { //$actions = ['view', ]; //$actions = ['index', ]; $actions = ['delete', ]; - foreach ($actions as $action) $this->_testViewForRole($action, $user_role, $matos_status); + foreach ($actions as $action) $this->OFF_testViewForRole($action, $user_role, $matos_status); //foreach (['view', ] as $view) $this->_testViewForRole($view, $user_role, $matos_status); //foreach (['index'] as $view) $this->_testViewForRole($view, $user_role, $matos_status); //foreach (['view', 'index'] as $view) $this->_testViewForRole($view, $user_role, $matos_status); @@ -1859,7 +1861,7 @@ class MaterielsControllerTest extends General { * @return void * */ - private function _testViewForRole($view, $user_role, $matos_status) { + private function OFF_testViewForRole($view, $user_role, $matos_status) { // Force le rechargement des entités materiel en BD (car on les modifie en cours de route) //AppController::forceReload(); @@ -2150,19 +2152,28 @@ class MaterielsControllerTest extends General { elseif (is_string($access_condition_on_status) && $access_condition_on_belonging===1) { debug("- is [*,1]"); //$m = $this->Materiels->get($id); - // (1) (T,T) => accès AUTORISÉ - $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); - $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, $this->USER_IS_RESP()); - $this->_testExecActionForId($action,$id,true); - // (2) (T,F) => accès REFUSÉ + // (1) (T,F) => accès REFUSÉ + debug("(TF) should be KO"); $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, false, false); + $m = $this->Materiels->get($id); + debug("$m->status, $m->nom_responsable, $m->nom_createur"); $this->_testExecActionForId($action,$id,false); - // (3) (F,T) => accès REFUSÉ + // (2) (F,T) => accès REFUSÉ + debug("(FT) should be KO"); $this->_updateMatosWithStatusAs($id, $access_condition_on_status, false); $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, true); + $m = $this->Materiels->get($id); + debug("$m->status, $m->nom_responsable, $m->nom_createur"); $this->_testExecActionForId($action,$id,false); - // (4) (F,F) => inutile de tester ce cas + // (3) (F,F) => accès REFUSÉ => inutile de tester ce cas + // (4) (T,T) => accès AUTORISÉ + debug("(TT) should be OK"); + $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); + $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, $this->USER_IS_RESP()); + $m = $this->Materiels->get($id); + debug("$m->status, $m->nom_responsable, $m->nom_createur"); + $this->_testExecActionForId($action,$id,true); } else throw new \ErrorException("La conditions d'accès $access_condition est incorrecte (1)"); } @@ -2188,8 +2199,14 @@ class MaterielsControllerTest extends General { */ // MEME statut (par défaut) ? $m->status = $status; + //debug("bef: $m->status"); // ou STATUT différent ? if ( ($NOT && $WITH_SAME_STATUS) || (!$NOT && !$WITH_SAME_STATUS) ) $m->status = $mc->getNextStatusFrom($status); + //debug("after: $m->status"); + + // On sauvegarde le matos modifié + if (! $this->Materiels->save($m)) + throw new \Exception("La sauvegarde du materiel modifié ne se fait pas !!!"); } else throw new \Exception("Le statut du matériel est incorrect !!!"); @@ -2221,20 +2238,19 @@ class MaterielsControllerTest extends General { //debug($this->_flashMessages); $entities = 'materiels'; - $getpost = in_array($action, ['view','index']) ? 'get' : 'post'; + $getpost = in_array($action, ['add','add_by_copy','edit','delete', 'statusCreated']) ? 'post' : 'get'; //$this->post("/materiels/$action/$matos_id"); // Si 'add' ou 'index' => pas d'id //debug("0"); debug($_SESSION); - if ( in_array($action, ['add', 'index']) ) { - $this->$getpost("/$entities/$action"); - $id=0; - } - // Sinon, on passe aussi l'id - else - $this->$getpost("/$entities/$action/$id"); + $data = ($action=='add') ? $this->newMaterielWithAllMandatoryFields : []; + $exec_action = $action; + if ($action=='add_by_copy') $exec_action='add'; + $full_action = "/$entities/$exec_action"; + if (! in_array($action, ['add', 'index'])) $full_action .= "/$id"; + $this->$getpost($full_action, $data); //debug("2"); debug($_SESSION);return; - //debug("Execution action : \$this->$action_type('/$entities/$action/$id');"); + debug("Execution action : \$this->$getpost($full_action, data); (avec data=)"); debug($data); //debug($_SESSION);return; /* if ($SUCCESS) $this->assertNoRedirect(); @@ -2248,19 +2264,34 @@ class MaterielsControllerTest extends General { //debug($flash_message); if ($SUCCESS) { + $flash_message = null; + $this->assertResponseSuccess(); if ($getpost == 'get') { + $this->assertResponseOk(); $this->assertResponseNotEmpty(); $this->assertNoRedirect(); + if (isset($_SESSION['Flash'])) { + $flash_message = $_SESSION['Flash']; + //$this->assertEquals([], $flash_message); + } } - $this->assertResponseSuccess(); + else { + $expected_flash_message = 'Le matériel a bien été '; + switch($action) { + case 'add': $expected_flash_message .= 'ajouté'; break; + case 'edit': $expected_flash_message .= 'modifié'; break; + case 'delete': $expected_flash_message .= 'supprimé'; break; + case 'statusCreated': $expected_flash_message .= 'rétrogradé au statut CREATED'; break; + } + $flash_message = isset($_SESSION['Flash']) ? $_SESSION['Flash']['flash'][0]['message']:[]; + //debug("flash is: $flash_message"); + //if ($flash_message==null) $flash_message = []; + } + //$this->assertEquals($expected_flash_message, $flash_message); + if ($flash_message) $this->assertTrue(strpos($flash_message, $expected_flash_message) === 0, "Le message flash est incorrect : $flash_message"); // 1) Assert no redirection // 2) Assert ok - $this->assertResponseOk(); // 3) Assert no Flash error message - if (isset($_SESSION['Flash'])) { - $flash_message = $_SESSION['Flash']; - $this->assertEquals([], $flash_message); - } // 4) Assert content ok //$this->assertResponseContains("Matos Test 2 (C)", "L'action $action n'est pas autorisée (pour le role {$this->getUserRole()}) !!!"); // BUG !!!! pourquoi ça marche avec l'action 'view' ??? @@ -2273,12 +2304,16 @@ class MaterielsControllerTest extends General { //$this->assertRedirect(['controller' => 'Pages', 'action' => 'home']); //$this->assertResponseSuccess(); $this->assertResponseEmpty(); - $this->assertRedirect('/'); + //if (!in_array($action,['add_by_copy'])) $this->assertResponseEmpty(); + //if ($getpost == 'get') $this->assertRedirect('/'); + if (!in_array($action,['edit','delete'])) $this->assertRedirect('/'); // 2) Assert Flash error message $flash_message = $_SESSION['Flash']['flash'][0]['message']; + //var_dump($flash_message); $this->assertSession("Désolé, vous n'êtes pas autorisé à accéder à cette zone.", 'Flash.flash.0.message'); $this->assertEquals("Désolé, vous n'êtes pas autorisé à accéder à cette zone.", $flash_message); - $this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") !== false); + //$this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") !== false); + $this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") === 0); // BUG !!! why ??? //$this->assertResponseContains("Désolé", "L'action $action est autorisée (pour le role {$this->getUserRole()}) alors qu'elle ne le devrait pas !"); } -- libgit2 0.21.2