General.php 8.54 KB
<?php
namespace App\Test\TestCase\Controller;

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

//use App\Controller\MaterielsController;
//use Cake\ORM\TableRegistry;

/**
 * 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;
    
    public static function getRoleLevel($role) { return AppController::getRoleLevel($role); }
    

    /*
    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 function USER_IS_ADMIN_AT_LEAST($role) {
        return in_array($role, ['ADMIN','ADMINP','SUPER']);
    }
    
    public function authAs($role) {
        switch ($role) {
            case 'USER': $this->authUtilisateur();
            break;
            case 'RESP': $this->authResponsable();
            break;
            case 'ADMIN': $this->authAdmin();
            break;
            case 'ADMINP': $this->authAdminPlus();
            break;
            case 'SUPER': $this->authSuperAdmin();
            break;
        }
    }
    
    public 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->authUser('user5_USER', 'test9', 'test0');
    }
    /*
     $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);
        }
        */
    }
    

}