NxTIC/test/phpunit/tests/ModuleRequest.php

207 lines
7.6 KiB
PHP
Raw Normal View History

2016-04-18 08:30:40 +00:00
<?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'] = [];
2016-04-18 08:30:40 +00:00
$req = new \api\core\ModuleRequest('module/method');
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructNoPath(){
$req = new \api\core\ModuleRequest();
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::MissingPath );
}
public function testConstructIncorrectPathType(){
$reqArray = new \api\core\ModuleRequest( array(1) );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $reqArray->error, \manager\ManagerError::WrongPathModule );
$reqNumber = new \api\core\ModuleRequest( 10 );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $reqNumber->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathSyntax(){
$req = new \api\core\ModuleRequest('wrong.syntax');
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathModule(){
$req = new \api\core\ModuleRequest( 'unknownModule/method' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::UnknownModule );
}
public function testConstructIncorrectPathMethod(){
$req = new \api\core\ModuleRequest( 'module/unknownMethod' );
2016-04-18 08:30:40 +00:00
$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'] = [];
2016-04-18 08:30:40 +00:00
$req = new \api\core\ModuleRequest( 'module/method' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMissing(){
$_SESSION['permission'] = [];
2016-04-18 08:30:40 +00:00
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructPermissionsMatch(){
$_SESSION['permission'] = array('a');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchOther(){
$_SESSION['permission'] = array('b');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchAll(){
$_SESSION['permission'] = array('a', 'b');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsDontMatch(){
$_SESSION['permission'] = array('c');
$req = new \api\core\ModuleRequest( 'module/phpunitPermissions' );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
// {2} Gestion des permissions quand un token est joint //
public function testConstructNoTokenAndNoConnection(){
// $_SESSION['permission'] = [];
2016-04-18 08:30:40 +00:00
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
2016-04-18 08:30:40 +00:00
}
2016-04-18 09:05:35 +00:00
public function testConstructInvalidTokenType(){
$_SERVER['PHP_AUTH_DIGEST'] = str_repeat('f', 10);
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructInvalidTokenValue(){
2016-04-18 08:30:40 +00:00
$_SERVER['PHP_AUTH_DIGEST'] = str_repeat('f', 40);
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
2016-04-18 08:30:40 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::TokenError );
}
2016-04-18 09:05:35 +00:00
public function testConstructValidTokenInBdd(){
$_SERVER['PHP_AUTH_DIGEST'] = '52945efbed43b50c12413f2f0e9519bfd9e98ce8';
$postdata = array( 'path' => 'module/method' );
$req = \api\core\ModuleRequest::fromPost( $postdata );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
/* (3) Gestion des paramètres */
public function testConstructNoParamRequired(){
$_SESSION['permission'] = [];
2016-04-18 09:05:35 +00:00
$req = new \api\core\ModuleRequest( 'module/method', array('useless') );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructMissingParam(){
$_SESSION['permission'] = [];
2016-04-18 09:05:35 +00:00
$req = new \api\core\ModuleRequest( 'module/phpunitParams', [] );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamType(){
$_SESSION['permission'] = [];
2016-04-18 09:05:35 +00:00
$params = array( 'p1' => 'sometext', 'p2' => 'sometext' );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamTypeOther(){
$_SESSION['permission'] = [];
2016-04-18 09:05:35 +00:00
$params = array( 'p1' => 10, 'p2' => 'sometext' );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
2016-04-18 09:05:35 +00:00
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructCorrectParams(){
$_SESSION['permission'] = [];
2016-04-18 09:05:35 +00:00
$params = array( 'p1' => 'sometext', 'p2' => 10 );
$req = new \api\core\ModuleRequest( 'module/phpunitParams', $params );
2016-04-18 09:05:35 +00:00
$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 );
}
2016-04-18 08:30:40 +00:00
}
?>