NxTIC/phpunit/tests/ModuleRequest.php

207 lines
7.6 KiB
PHP
Raw Permalink 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'] = array();
$req = new \manager\ModuleRequest('module/method');
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructNoPath(){
$req = new \manager\ModuleRequest();
$this->assertEquals( $req->error, \manager\ManagerError::MissingPath );
}
public function testConstructIncorrectPathType(){
$reqArray = new \manager\ModuleRequest( array(1) );
$this->assertEquals( $reqArray->error, \manager\ManagerError::WrongPathModule );
$reqNumber = new \manager\ModuleRequest( 10 );
$this->assertEquals( $reqNumber->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathSyntax(){
$req = new \manager\ModuleRequest('wrong.syntax');
$this->assertEquals( $req->error, \manager\ManagerError::WrongPathModule );
}
public function testConstructIncorrectPathModule(){
$req = new \manager\ModuleRequest( 'unknownModule/method' );
$this->assertEquals( $req->error, \manager\ManagerError::UnknownModule );
}
public function testConstructIncorrectPathMethod(){
$req = new \manager\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'] = array();
$req = new \manager\ModuleRequest( 'module/method' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMissing(){
$_SESSION['permission'] = array();
$req = new \manager\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
public function testConstructPermissionsMatch(){
$_SESSION['permission'] = array('a');
$req = new \manager\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchOther(){
$_SESSION['permission'] = array('b');
$req = new \manager\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsMatchAll(){
$_SESSION['permission'] = array('a', 'b');
$req = new \manager\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructPermissionsDontMatch(){
$_SESSION['permission'] = array('c');
$req = new \manager\ModuleRequest( 'module/phpunitPermissions' );
$this->assertEquals( $req->error, \manager\ManagerError::PermissionError );
}
// {2} Gestion des permissions quand un token est joint //
public function testConstructNoTokenAndNoConnection(){
// $_SESSION['permission'] = array();
$postdata = array( 'path' => 'module/method' );
$req = \manager\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 = \manager\ModuleRequest::fromPost( $postdata );
$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 = \manager\ModuleRequest::fromPost( $postdata );
$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 = \manager\ModuleRequest::fromPost( $postdata );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
/* (3) Gestion des paramètres */
public function testConstructNoParamRequired(){
$_SESSION['permission'] = array();
$req = new \manager\ModuleRequest( 'module/method', array('useless') );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructMissingParam(){
$_SESSION['permission'] = array();
$req = new \manager\ModuleRequest( 'module/phpunitParams', array() );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamType(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 'sometext' );
$req = new \manager\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructWrongParamTypeOther(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 10, 'p2' => 'sometext' );
$req = new \manager\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructCorrectParams(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 10 );
$req = new \manager\ModuleRequest( 'module/phpunitParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructParamOrdered(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 10 );
$req = new \manager\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'] = array();
$params = array( 'p2' => 10, 'p1' => 'sometext' );
$req = new \manager\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'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo', 'p3' => -10 );
$req = new \manager\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
public function testConstructOptionalParamGiven(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo', 'p3' => 10 );
$req = new \manager\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructOptionalParamNotGiven(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p2' => 'sometexttoo' );
$req = new \manager\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::Success );
}
public function testConstructOptionalParamButRequiredMissing(){
$_SESSION['permission'] = array();
$params = array( 'p1' => 'sometext', 'p3' => 10 );
$req = new \manager\ModuleRequest( 'module/phpunitOptionalParams', $params );
$this->assertEquals( $req->error, \manager\ManagerError::ParamError );
}
2016-04-18 08:30:40 +00:00
}
?>