NxTIC/subDir/test/phpunit/tests/ModuleRequest.php

207 lines
7.6 KiB
PHP

<?php namespace phpunit;
class ModuleRequest extends \PHPUnit_Framework_TestCase{
/* [1] Tests du constructeur
=========================================================*/
/* (1) Tests du chemin de délégation */
public function testConstructCorrectPath(){
$_SESSION['permission'] = [];
$req = new \api\core\ModuleRequest('module/method');
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructNoPath(){
$req = new \api\core\ModuleRequest();
$this->assertEquals( $req->error, \manager\ManagerError::MissingPath );
}
public function testConstructIncorrectPathType(){
$reqArray = new \api\core\ModuleRequest( array(1) );
$this->assertEquals( $reqArray->error, \manager\ManagerError::WrongPathModule );
$reqNumber = new \api\core\ModuleRequest( 10 );
$this->assertEquals( $reqNumber->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathSyntax(){
$req = new \api\core\ModuleRequest('wrong.syntax');
$this->assertEquals( $req->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathModule(){
$req = new \api\core\ModuleRequest( 'unknownModule/method' );
$this->assertEquals( $req->error, \manager\ManagerError::UnknownModule );
}
public function testConstructIncorrectPathMethod(){
$req = new \api\core\ModuleRequest( 'module/unknownMethod' );
$this->assertEquals( $req->error, \manager\ManagerError::UnknownMethod );
}
/* (2) Tests des permissions */
// {1} Gestion des permissions quand l'utilisateur est connecté //
public function testConstructNoPermissionsRequired(){
$_SESSION['permission'] = [];
$req = new \api\core\ModuleRequest( 'module/method' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMissing(){
$_SESSION['permission'] = [];
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructPermissionsMatch(){
$_SESSION['permission'] = array('a');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchOther(){
$_SESSION['permission'] = array('b');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchAll(){
$_SESSION['permission'] = array('a', 'b');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsDontMatch(){
$_SESSION['permission'] = array('c');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
// {2} Gestion des permissions quand un token est joint //
public function testConstructNoTokenAndNoConnection(){
// $_SESSION['permission'] = [];
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructInvalidTokenType(){
$_SERVER['PHP_AUTH_DIGEST'] = str_repeat('f', 10);
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructInvalidTokenValue(){
$_SERVER['PHP_AUTH_DIGEST'] = str_repeat('f', 40);
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
$this->assertEquals( $req->error, \manager\ManagerError::TokenError );
}
public function testConstructValidTokenInBdd(){
$_SERVER['PHP_AUTH_DIGEST'] = '52945efbed43b50c12413f2f0e9519bfd9e98ce8';
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
/* (3) Gestion des paramètres */
public function testConstructNoParamRequired(){
$_SESSION['permission'] = [];
$req = new \api\core\ModuleRequest( 'module/method', array('useless') );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructMissingParam(){
$_SESSION['permission'] = [];
$req = new \api\core\ModuleRequest( 'module/phpunitParams', [] );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamType(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 'sometext' );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamTypeOther(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 10, 'p2' => 'sometext' );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructCorrectParams(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 10 );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructParamOrdered(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 10 );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
$ans = $req->dispatch();
$this->assertEquals( $ans->get('p1'), $params['p1'] );
$this->assertEquals( $ans->get('p2'), $params['p2'] );
}
public function testConstructParamUnordered(){
$_SESSION['permission'] = [];
$params = array( 'p2' => 10, 'p1' => 'sometext' );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
$ans = $req->dispatch();
$this->assertEquals( $ans->get('p1'), $params['p1'] );
$this->assertEquals( $ans->get('p2'), $params['p2'] );
}
/* (4) Gestion des paramètres optionnels */
public function testConstructOptionalParamGivenIncorrectType(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo', 'p3' => -10 );
$req = new \api\core\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructOptionalParamGiven(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo', 'p3' => 10 );
$req = new \api\core\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructOptionalParamNotGiven(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo' );
$req = new \api\core\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructOptionalParamButRequiredMissing(){
$_SESSION['permission'] = [];
$params = array( 'p1' => 'sometext', 'p3' => 10 );
$req = new \api\core\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
}
?>