General.php 11.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
<?php
namespace App\Test\TestCase\Controller;

use Cake\TestSuite\IntegrationTestCase;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;

//use App\Controller\MaterielsController;

/**
 * App\Controller\GeneralController Test Case
 */
class General extends IntegrationTestCase {


    //TODO: definine this in a superclass
    /*
    const PROFILE_USER = 1;
    const PROFILE_RESPONSABLE = 2;
    const PROFILE_ADMIN = 3;
    const PROFILE_ADMINPLUS = 4;
    const PROFILE_SUPERADMIN = 5;
    */
    protected $ROLES = [
        'USER',
        'RESP',
        'ADMIN',
        'ADMINP',
        'SUPER'
    ];
    protected $ROLES6 = [
        ['USER_from_ldap'],
        ['USER'],
        ['RESP'],
        ['ADMIN'],
        ['ADMINP'],
        ['SUPER']
    ];
    protected $ROLES5 = [
        //['USER_from_ldap'],
        ['USER'],
        ['RESP'],
        ['ADMIN'],
        ['ADMINP'],
        ['SUPER']
    ];
    
    const PROFILES = AppController::PROFILES;
    
    // Current role (profile) of the user
    private $CURRENT_ROLE = null;

    /*
    public $ControllerApp = null;
    public function setUp() {
        parent::setUp();
        
        /*
        $config = TableRegistry::exists('Materiels') ? [] : [
            'className' => 'App\Model\Table\MaterielsTable'
        ];
        $this->Materiels = TableRegistry::get('Materiels', $config);
        
        $config = TableRegistry::exists('Suivis') ? [] : [
            'className' => 'App\Model\Table\SuivisTable'
        ];
        $this->Suivis = TableRegistry::get('Suivis', $config);
        */ /*
        
        $this->ControllerApp = new AppController();
    }
    
    public function tearDown() {
        //unset($this->Materiels);
        //unset($this->Suivis);
        unset($this->ControllerApp);
        
        parent::tearDown();
    }
    */
    
    
    /* FONCTIONS UTILITAIRES UTILISÉES PAR (tous) LES TESTS */
 
    public static function getRoleLevel($role) { return AppController::getRoleLevel($role); }
    
    // Definition DIFFERENTE de celle de AppController
    public function getUserRole() {
        //return $this->ControllerMateriels->getUserRole();
        if (! $this->CURRENT_ROLE) {
            $user = TableRegistry::get('Users')->find()->where([
                //'username' => 'user1_SUPER'
                //'username' => $this->_session['Auth']['User']['cn'][0]
                'username' => $this->_session['Auth']['User']['uid'][0]
            ])->first();
            // Unpriviledged user
            $role = $user ? $user['role'] : "Utilisateur";
            $this->CURRENT_ROLE = $role;
        }
        return $this->CURRENT_ROLE;
    }
    // MEME Definition de celle de AppController (mais n'utilise pas la meme fonction getUserRole())
    public function userHasRole($expectedRole, $ORMORE=false) {
        $role = $this->getUserRole();
        //debug($role);
        if (! $ORMORE) return ($role == $expectedRole);
        return ($this->getRoleLevel($role) >= $this->getRoleLevel($expectedRole));
    }
    // MEME Definition de celle de AppController (mais n'utilise la meme fonction getUserRole())
    public function userHasRoleAtLeast($expectedRole) {
        return $this->userHasRole($expectedRole, true);
    }
    // MEME Definition de celle de AppController (mais n'utilise la meme fonction getUserRole())
    public function USER_IS_ADMIN_AT_LEAST() { return $this->userHasRoleAtLeast('Administration'); }
    public function USER_IS_RESP_AT_LEAST() { return $this->userHasRoleAtLeast('Responsable'); }
    public function USER_IS_SUPERADMIN() { return $this->userHasRole('Super Administrateur'); }
    public function USER_IS_ADMIN() { return $this->userHasRole('Administration'); }
    public function USER_IS_RESP() { return $this->userHasRole('Responsable'); }
    public function USER_IS_USER() { return $this->userHasRole('Utilisateur'); }
    /*
    public function USER_IS_ADMIN_AT_LEAST($role=null) {
        if (is_null($role)) { 
            $role = $this->getUserRole();
            $roles = ['Administration','Administration Plus','Super Administrateur'];
        }
        else {
            $roles = ['ADMIN','ADMINP','SUPER'];
        }
        return in_array($role, $roles);
    }
    */
    
    
    public function authAs($role) {
        switch ($role) {
            case 'USER_from_ldap':
                $this->authUtilisateurFromLdap();
                //$this->CURRENT_ROLE = 'Utilisateur';
                break;
            case 'USER': 
                $this->authUtilisateurFromTable();
                //$this->CURRENT_ROLE = 'Utilisateur';
                break;
            case 'RESP': 
                $this->authResponsable();
                //$this->CURRENT_ROLE = 'Responsable';
                break;
            case 'ADMIN': 
                $this->authAdmin();
                //$this->CURRENT_ROLE = 'Administration';
                break;
            case 'ADMINP': 
                $this->authAdminPlus();
                //$this->CURRENT_ROLE = 'Administration Plus';
                break;
            case 'SUPER': 
                $this->authSuperAdmin();
                //$this->CURRENT_ROLE = 'Super Administrateur';
                break;
        }
    }
    
    private function authUser($cn, $givenName='test1', $sn='test2') {
        $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => $sn
                    ],
                    'mail' => [
                        0 => 'testa@test.fr'
                    ],
                    'givenname' => [
                        0 => $givenName
                    ],
                    /*
                    'cn' => [
                        0 => $cn
                    ],
                    */
                    'uid' => [
                        0 => $cn
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);
        
        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    
    public function authSuperAdmin() {
        $this->authUser('user1_SUPER', 'user1', 'SUPER');
        //$this->authUser('user1_SUPER', 'test1', 'test2');
        //$this->authUser('testa', 'user1', 'SUPER');
    }
    /*
    public function authSuperAdmin() {
        $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => 'test2'
                    ],
                    'mail' => [
                        0 => 'testa@test.fr'
                    ],
                    'givenname' => [
                        0 => 'test1'
                    ],
                    'cn' => [
                        //0 => 'testa'
                        0 => 'user1_SUPER'
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);

        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    */

    public function authAdminPlus() {
        $this->authUser('user2_ADMINPLUS', 'test3', 'test4');
    }
    /*
     $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => 'test4'
                    ],
                    'mail' => [
                        0 => 'testz@test.fr'
                    ],
                    'givenname' => [
                        0 => 'test3'
                    ],
                    'cn' => [
                        //0 => 'testz'
                        0 => 'user2_ADMINPLUS'
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);

        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    */

    public function authAdmin() {
        $this->authUser('user3_ADMIN', 'test5', 'test6');
    }
    /*
        $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => 'test6'
                    ],
                    'mail' => [
                        0 => 'teste@test.fr'
                    ],
                    'givenname' => [
                        0 => 'test5'
                    ],
                    'cn' => [
                        //0 => 'teste'
                        0 => 'user3_ADMIN'
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);

        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    */

    public function authResponsable() {
        $this->authUser('user4_RESP', 'test7', 'test8');
    }
    /*
        $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => 'test8'
                    ],
                    'mail' => [
                        0 => 'testr@test.fr'
                    ],
                    'givenname' => [
                        0 => 'test7'
                    ],
                    'cn' => [
                        //0 => 'testr'
                        0 => 'user4_RESP'
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);

        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    */

    public function authUtilisateur() { $this->authUtilisateurFromTable(); }
    public function authUtilisateurFromTable() {
        $this->authUser('user5_USER', 'test9', 'test0');
    }
    public function authUtilisateurFromLdap() {
        //$this->authUser('_NouvelUtilisateur_username', 'NOUVEL', 'UTILISATEUR');
        $this->authUser('_fake_ldap_user_', 'FAKE_LDAP', 'UTILISATEUR');
    }
    /*
     $user = [
            'Auth' => [
                'User' => [
                    'sn' => [
                        0 => 'test0'
                    ],
                    'mail' => [
                        0 => 'testt@test.fr'
                    ],
                    'givenname' => [
                        0 => 'test9'
                    ],
                    'cn' => [
                        //0 => 'testt'
                        0 => 'user5_USER'
                    ],
                    'userpassword' => [
                        0 => 'test'
                    ]
                ]
            ]
        ];
        $this->session($user);

        $authType = [
            'authType' => 'cn'
        ];
        $this->session($authType);
    }
    */
    
    //protected function assertResponseContainsIf($role, $condition, $content, $messageIfNot=null) {
    //protected function assertResponseContainsIf(string $role, bool $condition, array $contents, $testOpposite=true) {
    protected function assertResponseContainsIf($role, $condition, array $contents, $testOpposite=true) {
        if ($condition) {
            $messageOnFail = 'devrait avoir accès';
            $assertMethod = 'assertResponseContains'; 
        }
        else {
            if (!$testOpposite) return;
            $messageOnFail = 'ne devrait PAS avoir accès';
            $assertMethod = 'assertResponseNotContains';
        }
        foreach ($contents as $content=>$endmsg)
            $this->$assertMethod($content, "Le profil (rôle) $role $messageOnFail $endmsg");
        /*
        $this->assertResponseContains($content, 'Le profil (rôle) '.$role.' devrait avoir accès à '. $messageIfNot);
        }
        else {
            $this->assertResponseNotContains($content, 'Le profil (rôle) '.$role.' ne devrait PAS avoir accès à '. $messageIfNot);
        }
        */
    }
    

}