PagesControllerTest.php 5.45 KB
<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         1.2.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace App\Test\TestCase\Controller;

use Cake\Core\Configure;
use Cake\TestSuite\IntegrationTestCase;

/**
 * PagesControllerTest class
 */
//class PagesControllerTest extends IntegrationTestCase
class PagesControllerTest extends General
{
	/**
	 * Fixtures
	 *
	 * @var array
	 */
	public $fixtures = [
	    'app.users',
	    'app.configurations',
	];

	 /*
     public function authUser() {
     	     $user = [
     	     		'Auth' => [
     	     				'User' => [
     	     						'sn' => [0 => 'test2'],
									'mail' => [0 => 'test@test.fr'],
									'givenname' => [0 => 'test1'],
									'cn' => [0 => 'testa'],
									'userpassword' => [0 => 'test'],
     	     				]
     	     		]
     	     ];
     	   $this->session($user);
     	   
     	   $authType = ['authType' => 'cn'];
     	   $this->session($authType);
     }
     */
	
	
	
    /**
     * testDisplay method
     *
     * @return void
     */

	//public function testDisplay()
	/*
	public function testPage10AccessHome() {
	    foreach ($this->ROLES as $role) $this->subtestPageAccessHomeAs($role);
	}
	*/
	public function testPage10AccessHomeAsUser() { $this->_testPageAccessHomeAs('USER'); }
	public function testPage10AccessHomeAsResp() { $this->_testPageAccessHomeAs('RESP'); }
	public function testPage10AccessHomeAsAdmin() { $this->_testPageAccessHomeAs('ADMIN'); }
	public function testPage10AccessHomeAsAdminPlus() { $this->_testPageAccessHomeAs('ADMINP'); }
	public function testPage10AccessHomeAsSuperAdmin() { $this->_testPageAccessHomeAs('SUPER'); }
	private function _testPageAccessHomeAs($role) {
	    //$this->authUser();
	    $this->authAs($role);
	    
	    $this->get('/pages/home');
	    $this->assertResponseOk();
	    $this->assertResponseContains('Accueil');
	    $this->assertResponseContains('Voir mes matériels');

        switch ($role) {
            case 'USER':
                $this->assertResponseContains('Utilisateur');
                //$configuration->procedure_sur_accueil = TRUE;
                $this->assertResponseContains('Procédure à suivre');
            break;
            case 'RESP':
            break;
            case 'ADMIN':
                $this->assertResponseContains('Administration');
                $this->assertResponseContains('Voir les matériels à valider');
            break;
            case 'ADMINP':
            break;
            case 'SUPER':
            	    $this->assertResponseContains('Super Administrateur');
            break;
        }
	        
	}

	//public function testDisplay()
	/*
	public function testPage20AccessTools() {
	    foreach ($this->ROLES as $role) $this->subtestPageAccessToolsAs($role);
	}
	*/
	public function testPage20AccessToolsAsUser() { $this->_testPageAccessToolsAs('USER'); }
	public function testPage20AccessToolsAsResp() { $this->_testPageAccessToolsAs('RESP'); }
	public function testPage20AccessToolsAsAdmin() { $this->_testPageAccessToolsAs('ADMIN'); }
	public function testPage20AccessToolsAsAdminPlus() { $this->_testPageAccessToolsAs('ADMINP'); }
	public function testPage20AccessToolsAsSuperAdmin() { $this->_testPageAccessToolsAs('SUPER'); }
	private function _testPageAccessToolsAs($role)
	{
	    $this->authAs($role);
	    $this->get('/pages/tools');
	    
	    if ( $this->USER_IS_ADMIN_AT_LEAST() ) {
	    //if ( in_array($role, ['ADMIN','ADMINP','SUPER']) ) {
	    //if ( $this->userHasRoleAtLeast('Administration') ) {
	    //if ( $this->ControllerApp->userHasRoleAtLeast('Administration') ) {
	       $this->assertResponseOk();
	       $this->assertResponseContains('Outils', $role.' should be allowed to access the Tools page');
	       $this->assertResponseContains('Gérer le contenu variable de ', $role);
	       if ($this->USER_IS_SUPERADMIN())
	       //if ($role == 'SUPER') 
        	       $this->assertResponseContains('Passer en mode DEBUG');
	       else
	           $this->assertResponseNotContains('Passer en mode DEBUG');
	    }
	    else {
	        $this->assertResponseNotContains('Outils', $role.' should not be allowed to access the Tools page');
	    }
	}
	
    
    /**
     * Test that missing template renders 404 page in production
     *
     * @return void
     */
    public function testMissingTemplate()
    {
        //$this->authUser();
        $this->authSuperAdmin();
        
        Configure::write('debug', false);
        $this->get('/pages/not_existing');

        $this->assertResponseContains('Cette action est impossible');
        
    }

    /**
     * Test that missing template in debug mode renders missing_template error page
     *
     * @return void
     */
    public function testMissingTemplateInDebug()
    {
        //$this->authUser();
        $this->authSuperAdmin();
        
        Configure::write('debug', true);
        $this->get('/pages/not_existing');

        $this->assertResponseFailure();
        $this->assertResponseContains('Missing Template');
        $this->assertResponseContains('Stacktrace');
    
    }
}