Commit e7a2e05cfd9712376e3945157833710158cf6cd4
1 parent
0eab1e3b
Exists in
master
and in
1 other branch
Tests génériques automatiques pour Materiels sont presque complets,
(c'est très prometteur) (reste à ajouter les autres entités telles que Suivis, Emprunts, ...) et à tester le contenu des vues (view et index)
Showing
3 changed files
with
78 additions
and
31 deletions
Show diff stats
.gitignore
src/Controller/MaterielsController.php
... | ... | @@ -348,6 +348,16 @@ class MaterielsController extends AppController { |
348 | 348 | } // TEST only |
349 | 349 | |
350 | 350 | |
351 | + public function getNextStatusFrom($status) { | |
352 | + $status_num = self::statuses[$status]; | |
353 | + // si ARCHIVED => on reboucle à CREATED | |
354 | + if ($status_num==4) $status_num=0; | |
355 | + // Les indices commencent à 0, donc $status_num est déjà l'indice suivant... | |
356 | + $newstatus = array_keys(self::statuses)[$status_num]; | |
357 | + //debug("new status is $newstatus"); | |
358 | + return $newstatus; | |
359 | + } | |
360 | + | |
351 | 361 | public function isStatus($status) { |
352 | 362 | return in_array($status, array_keys(self::statuses)); |
353 | 363 | } |
... | ... | @@ -1780,7 +1790,7 @@ class MaterielsController extends AppController { |
1780 | 1790 | if ($IS_ADD) { |
1781 | 1791 | // 1) on crée un materiel vide |
1782 | 1792 | $materiel = $this->Materiels->newEntity(); |
1783 | - // - COPIE de materiel (on a cliqué sur "Copier ce materiel") => id passé en argument | |
1793 | + // - add_by_copy : COPIE de materiel (on a cliqué sur "Copier ce materiel") => id passé en argument | |
1784 | 1794 | if (isset($this->request->getAttribute('params')['pass'][0])) { |
1785 | 1795 | // On récupère le materiel à copier et on le copie dans $materiel |
1786 | 1796 | $materiel_to_copy = $this->Materiels->get($this->request->getAttribute('params')['pass'][0]); | ... | ... |
tests/TestCase/Controller/MaterielsControllerTest.php
... | ... | @@ -175,10 +175,11 @@ class MaterielsControllerTest extends General { |
175 | 175 | $actions_and_roles = []; |
176 | 176 | $actions = [ |
177 | 177 | // CRUD |
178 | - 'add','view','index','edit','delete' | |
178 | + 'add','add_by_copy', 'view','index','edit','delete', | |
179 | 179 | //'view', |
180 | 180 | //'index', |
181 | 181 | // Autres |
182 | + 'statusCreated', | |
182 | 183 | ]; |
183 | 184 | $roles4 = [ |
184 | 185 | 'user', |
... | ... | @@ -187,8 +188,9 @@ class MaterielsControllerTest extends General { |
187 | 188 | 'super', |
188 | 189 | ]; |
189 | 190 | foreach ($actions as $action) |
190 | - foreach ([0] as $i) { | |
191 | - //foreach ([0,1,2,3] as $i) { | |
191 | + //foreach ([0] as $i) { | |
192 | + //foreach ([0,1] as $i) { | |
193 | + foreach ([0,1,2,3] as $i) { | |
192 | 194 | $item = [ $action, $roles4[$i], $this->ROLES4[$i][0] ]; |
193 | 195 | //debug($item); |
194 | 196 | $actions_and_roles[] = $item; |
... | ... | @@ -1840,7 +1842,7 @@ class MaterielsControllerTest extends General { |
1840 | 1842 | * (OLD dataProvider dataProviderRoles4) |
1841 | 1843 | * @dataProvider dataProviderRoles4AndStatuses4 |
1842 | 1844 | */ |
1843 | - public function NON_testViewsViewAndIndexForRole($user_role, $matos_status) { | |
1845 | + public function OFF_testViewsViewAndIndexForRole($user_role, $matos_status) { | |
1844 | 1846 | // Force le rechargement des entités materiel en BD (car on les modifie en cours de route) |
1845 | 1847 | //AppController::forceReload(); |
1846 | 1848 | |
... | ... | @@ -1848,7 +1850,7 @@ class MaterielsControllerTest extends General { |
1848 | 1850 | //$actions = ['view', ]; |
1849 | 1851 | //$actions = ['index', ]; |
1850 | 1852 | $actions = ['delete', ]; |
1851 | - foreach ($actions as $action) $this->_testViewForRole($action, $user_role, $matos_status); | |
1853 | + foreach ($actions as $action) $this->OFF_testViewForRole($action, $user_role, $matos_status); | |
1852 | 1854 | //foreach (['view', ] as $view) $this->_testViewForRole($view, $user_role, $matos_status); |
1853 | 1855 | //foreach (['index'] as $view) $this->_testViewForRole($view, $user_role, $matos_status); |
1854 | 1856 | //foreach (['view', 'index'] as $view) $this->_testViewForRole($view, $user_role, $matos_status); |
... | ... | @@ -1859,7 +1861,7 @@ class MaterielsControllerTest extends General { |
1859 | 1861 | * @return void |
1860 | 1862 | * |
1861 | 1863 | */ |
1862 | - private function _testViewForRole($view, $user_role, $matos_status) { | |
1864 | + private function OFF_testViewForRole($view, $user_role, $matos_status) { | |
1863 | 1865 | |
1864 | 1866 | // Force le rechargement des entités materiel en BD (car on les modifie en cours de route) |
1865 | 1867 | //AppController::forceReload(); |
... | ... | @@ -2150,19 +2152,28 @@ class MaterielsControllerTest extends General { |
2150 | 2152 | elseif (is_string($access_condition_on_status) && $access_condition_on_belonging===1) { |
2151 | 2153 | debug("- is [*,1]"); |
2152 | 2154 | //$m = $this->Materiels->get($id); |
2153 | - // (1) (T,T) => accès AUTORISÉ | |
2154 | - $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); | |
2155 | - $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, $this->USER_IS_RESP()); | |
2156 | - $this->_testExecActionForId($action,$id,true); | |
2157 | - // (2) (T,F) => accès REFUSÉ | |
2155 | + // (1) (T,F) => accès REFUSÉ | |
2156 | + debug("(TF) should be KO"); | |
2158 | 2157 | $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); |
2159 | 2158 | $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, false, false); |
2159 | + $m = $this->Materiels->get($id); | |
2160 | + debug("$m->status, $m->nom_responsable, $m->nom_createur"); | |
2160 | 2161 | $this->_testExecActionForId($action,$id,false); |
2161 | - // (3) (F,T) => accès REFUSÉ | |
2162 | + // (2) (F,T) => accès REFUSÉ | |
2163 | + debug("(FT) should be KO"); | |
2162 | 2164 | $this->_updateMatosWithStatusAs($id, $access_condition_on_status, false); |
2163 | 2165 | $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, true); |
2166 | + $m = $this->Materiels->get($id); | |
2167 | + debug("$m->status, $m->nom_responsable, $m->nom_createur"); | |
2164 | 2168 | $this->_testExecActionForId($action,$id,false); |
2165 | - // (4) (F,F) => inutile de tester ce cas | |
2169 | + // (3) (F,F) => accès REFUSÉ => inutile de tester ce cas | |
2170 | + // (4) (T,T) => accès AUTORISÉ | |
2171 | + debug("(TT) should be OK"); | |
2172 | + $this->_updateMatosWithStatusAs($id, $access_condition_on_status, true); | |
2173 | + $this->_updateMatosBelongingToCurrentUserAndInSameGroup($id, true, $this->USER_IS_RESP()); | |
2174 | + $m = $this->Materiels->get($id); | |
2175 | + debug("$m->status, $m->nom_responsable, $m->nom_createur"); | |
2176 | + $this->_testExecActionForId($action,$id,true); | |
2166 | 2177 | } |
2167 | 2178 | else throw new \ErrorException("La conditions d'accès $access_condition est incorrecte (1)"); |
2168 | 2179 | } |
... | ... | @@ -2188,8 +2199,14 @@ class MaterielsControllerTest extends General { |
2188 | 2199 | */ |
2189 | 2200 | // MEME statut (par défaut) ? |
2190 | 2201 | $m->status = $status; |
2202 | + //debug("bef: $m->status"); | |
2191 | 2203 | // ou STATUT différent ? |
2192 | 2204 | if ( ($NOT && $WITH_SAME_STATUS) || (!$NOT && !$WITH_SAME_STATUS) ) $m->status = $mc->getNextStatusFrom($status); |
2205 | + //debug("after: $m->status"); | |
2206 | + | |
2207 | + // On sauvegarde le matos modifié | |
2208 | + if (! $this->Materiels->save($m)) | |
2209 | + throw new \Exception("La sauvegarde du materiel modifié ne se fait pas !!!"); | |
2193 | 2210 | } |
2194 | 2211 | else throw new \Exception("Le statut du matériel est incorrect !!!"); |
2195 | 2212 | |
... | ... | @@ -2221,20 +2238,19 @@ class MaterielsControllerTest extends General { |
2221 | 2238 | //debug($this->_flashMessages); |
2222 | 2239 | |
2223 | 2240 | $entities = 'materiels'; |
2224 | - $getpost = in_array($action, ['view','index']) ? 'get' : 'post'; | |
2241 | + $getpost = in_array($action, ['add','add_by_copy','edit','delete', 'statusCreated']) ? 'post' : 'get'; | |
2225 | 2242 | //$this->post("/materiels/$action/$matos_id"); |
2226 | 2243 | // Si 'add' ou 'index' => pas d'id |
2227 | 2244 | //debug("0"); debug($_SESSION); |
2228 | 2245 | |
2229 | - if ( in_array($action, ['add', 'index']) ) { | |
2230 | - $this->$getpost("/$entities/$action"); | |
2231 | - $id=0; | |
2232 | - } | |
2233 | - // Sinon, on passe aussi l'id | |
2234 | - else | |
2235 | - $this->$getpost("/$entities/$action/$id"); | |
2246 | + $data = ($action=='add') ? $this->newMaterielWithAllMandatoryFields : []; | |
2247 | + $exec_action = $action; | |
2248 | + if ($action=='add_by_copy') $exec_action='add'; | |
2249 | + $full_action = "/$entities/$exec_action"; | |
2250 | + if (! in_array($action, ['add', 'index'])) $full_action .= "/$id"; | |
2251 | + $this->$getpost($full_action, $data); | |
2236 | 2252 | //debug("2"); debug($_SESSION);return; |
2237 | - //debug("Execution action : \$this->$action_type('/$entities/$action/$id');"); | |
2253 | + debug("Execution action : \$this->$getpost($full_action, data); (avec data=)"); debug($data); | |
2238 | 2254 | //debug($_SESSION);return; |
2239 | 2255 | /* |
2240 | 2256 | if ($SUCCESS) $this->assertNoRedirect(); |
... | ... | @@ -2248,19 +2264,34 @@ class MaterielsControllerTest extends General { |
2248 | 2264 | //debug($flash_message); |
2249 | 2265 | |
2250 | 2266 | if ($SUCCESS) { |
2267 | + $flash_message = null; | |
2268 | + $this->assertResponseSuccess(); | |
2251 | 2269 | if ($getpost == 'get') { |
2270 | + $this->assertResponseOk(); | |
2252 | 2271 | $this->assertResponseNotEmpty(); |
2253 | 2272 | $this->assertNoRedirect(); |
2273 | + if (isset($_SESSION['Flash'])) { | |
2274 | + $flash_message = $_SESSION['Flash']; | |
2275 | + //$this->assertEquals([], $flash_message); | |
2276 | + } | |
2254 | 2277 | } |
2255 | - $this->assertResponseSuccess(); | |
2278 | + else { | |
2279 | + $expected_flash_message = 'Le matériel a bien été '; | |
2280 | + switch($action) { | |
2281 | + case 'add': $expected_flash_message .= 'ajouté'; break; | |
2282 | + case 'edit': $expected_flash_message .= 'modifié'; break; | |
2283 | + case 'delete': $expected_flash_message .= 'supprimé'; break; | |
2284 | + case 'statusCreated': $expected_flash_message .= 'rétrogradé au statut CREATED'; break; | |
2285 | + } | |
2286 | + $flash_message = isset($_SESSION['Flash']) ? $_SESSION['Flash']['flash'][0]['message']:[]; | |
2287 | + //debug("flash is: $flash_message"); | |
2288 | + //if ($flash_message==null) $flash_message = []; | |
2289 | + } | |
2290 | + //$this->assertEquals($expected_flash_message, $flash_message); | |
2291 | + if ($flash_message) $this->assertTrue(strpos($flash_message, $expected_flash_message) === 0, "Le message flash est incorrect : $flash_message"); | |
2256 | 2292 | // 1) Assert no redirection |
2257 | 2293 | // 2) Assert ok |
2258 | - $this->assertResponseOk(); | |
2259 | 2294 | // 3) Assert no Flash error message |
2260 | - if (isset($_SESSION['Flash'])) { | |
2261 | - $flash_message = $_SESSION['Flash']; | |
2262 | - $this->assertEquals([], $flash_message); | |
2263 | - } | |
2264 | 2295 | // 4) Assert content ok |
2265 | 2296 | //$this->assertResponseContains("Matos Test 2 (C)", "L'action $action n'est pas autorisée (pour le role {$this->getUserRole()}) !!!"); |
2266 | 2297 | // BUG !!!! pourquoi ça marche avec l'action 'view' ??? |
... | ... | @@ -2273,12 +2304,16 @@ class MaterielsControllerTest extends General { |
2273 | 2304 | //$this->assertRedirect(['controller' => 'Pages', 'action' => 'home']); |
2274 | 2305 | //$this->assertResponseSuccess(); |
2275 | 2306 | $this->assertResponseEmpty(); |
2276 | - $this->assertRedirect('/'); | |
2307 | + //if (!in_array($action,['add_by_copy'])) $this->assertResponseEmpty(); | |
2308 | + //if ($getpost == 'get') $this->assertRedirect('/'); | |
2309 | + if (!in_array($action,['edit','delete'])) $this->assertRedirect('/'); | |
2277 | 2310 | // 2) Assert Flash error message |
2278 | 2311 | $flash_message = $_SESSION['Flash']['flash'][0]['message']; |
2312 | + //var_dump($flash_message); | |
2279 | 2313 | $this->assertSession("Désolé, vous n'êtes pas autorisé à accéder à cette zone.", 'Flash.flash.0.message'); |
2280 | 2314 | $this->assertEquals("Désolé, vous n'êtes pas autorisé à accéder à cette zone.", $flash_message); |
2281 | - $this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") !== false); | |
2315 | + //$this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") !== false); | |
2316 | + $this->assertTrue(strpos($flash_message, "Désolé, vous n'êtes pas") === 0); | |
2282 | 2317 | // BUG !!! why ??? |
2283 | 2318 | //$this->assertResponseContains("Désolé", "L'action $action est autorisée (pour le role {$this->getUserRole()}) alors qu'elle ne le devrait pas !"); |
2284 | 2319 | } | ... | ... |