General.php 11.4 KB
<?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'
    ];
    
    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() {
        if (! $this->CURRENT_ROLE) {
            $user = TableRegistry::get('Users')->find()->where([
                //'username' => 'user1_SUPER'
                'username' => $this->_session['Auth']['User']['cn'][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 la meme fonction getUserRole())
    public function userHasRole($expectedRole, $ORMORE=false) {
        $role = $this->getUserRole();
        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
                    ],
                    '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);
        }
        */
    }
    

}