Skip to content
Snippets Groups Projects
Verified Commit e76a4fe1 authored by Damien's avatar Damien
Browse files

[REFACTORING] test right + res tu

parent a809b206
No related branches found
No related tags found
No related merge requests found
<?php
namespace MaarchTest;
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
......@@ -9,311 +7,9 @@ namespace MaarchTest;
*
*/
namespace MaarchTest;
use PHPUnit\Framework\TestCase;
class ResControllerTest extends TestCase
{
public function testPrepareStorage()
{
$action = new \Core\Controllers\ResController();
$data = [];
array_push(
$data,
array(
'column' => 'custom_t1',
'value' => 'TEST',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'custom_t10',
'value' => 'lgi@maarch.org',
'type' => 'string',
)
);
$aArgs = [
'data' => $data,
'docserverId' => 'FASTHD_MAN',
'status' => 'new',
'fileFormat' => 'pdf',
];
$response = $action->prepareStorage($aArgs);
$this->assertArrayHasKey('column', $response[0]);
}
public function testStoreResource()
{
$action = new \Core\Controllers\ResController();
$path = $_SESSION['config']['tmppath'] . '/test/';
if (!is_dir($path)) {
mkdir($path);
}
$fileSource = 'test_source.txt';
if (file_exists($path . $fileSource)) {
unlink($path . $fileSource);
}
$fp = fopen($path . $fileSource, 'a');
fwrite($fp, 'a unit test');
fclose($fp);
$fileContent = file_get_contents($path . $fileSource, FILE_BINARY);
$encodedFile = base64_encode($fileContent);
$data = [];
array_push(
$data,
array(
'column' => 'subject',
'value' => 'UNIT TEST',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'type_id',
'value' => 110,
'type' => 'integer',
)
);
array_push(
$data,
array(
'column' => 'custom_t1',
'value' => 'TEST',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'custom_t10',
'value' => 'lgi@maarch.org',
'type' => 'string',
)
);
$aArgs = [
'encodedFile' => $encodedFile,
'data' => $data,
'collId' => 'letterbox_coll',
'table' => 'res_letterbox',
'fileFormat' => 'txt',
'status' => 'new',
];
$response = $action->storeResource($aArgs);
$this->assertGreaterThanOrEqual(0, $response[0]);
}
public function testDelete()
{
$action = new \Core\Controllers\ResController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'DELETE',
]
);
$resId = \Core\Models\ResModel::getLastId(['select' => ['res_id']]);
$aArgs = [
'id'=> $resId[0]['res_id']
];
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
$response = $action->delete($request, $response, $aArgs);
$this->assertSame((string)$response->getBody(), '[true]');
}
public function testCreate()
{
$action = new \Core\Controllers\ResController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'POST',
]
);
$path = $_SESSION['config']['tmppath'] . '/test/';
if (!is_dir($path)) {
mkdir($path);
}
$fileSource = 'test_source.txt';
if (file_exists($path . $fileSource)) {
unlink($path . $fileSource);
}
$fp = fopen($path . $fileSource, 'a');
fwrite($fp, 'a unit test');
fclose($fp);
$fileContent = file_get_contents($path . $fileSource, FILE_BINARY);
$encodedFile = base64_encode($fileContent);
//echo $encodedFile . PHP_EOL;exit;
$data = [];
array_push(
$data,
array(
'column' => 'subject',
'value' => 'UNIT TEST from slim',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'type_id',
'value' => 110,
'type' => 'integer',
)
);
array_push(
$data,
array(
'column' => 'custom_t1',
'value' => 'TEST',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'custom_t10',
'value' => 'lgi@maarch.org',
'type' => 'string',
)
);
//print_r(json_encode($data));
//sample in json : [{"column":"subject","value":"UNIT T
//EST from slim","type":"string"},{"column":"type_id","value":110,"type":"integer"},
//{"column":"custom_t1","value":"TES
//T","type":"string"},{"column":"custom_t10","value":"lgi@maarch.org","type":"string"}]
$aArgs = [
'encodedFile' => $encodedFile,
'data' => $data,
'collId' => 'letterbox_coll',
'table' => 'res_letterbox',
'fileFormat' => 'txt',
'status' => 'new',
];
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
$response = $action->create($request, $response, $aArgs);
$this->assertGreaterThan(1, json_decode($response->getBody())->resId);
}
public function testDeleteRes()
{
$action = new \Core\Controllers\ResController();
$resId = \Core\Models\ResModel::getLastId(['select' => ['res_id']]);
$aArgs = [
'id'=> $resId[0]['res_id']
];
$response = $action->deleteRes($aArgs);
$this->assertTrue($response);
}
public function testUpdate()
{
$action = new \Core\Controllers\ResController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'POST',
]
);
$data = [];
array_push(
$data,
array(
'column' => 'status',
'value' => 'NEW',
'type' => 'string',
)
);
$resId = \Core\Models\ResModel::getLastId(['select' => ['res_id']]);
$aArgs = [
'table' => 'res_letterbox',
'res_id' => $resId[0]['res_id'],
'data' => $data,
];
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
$response = $action->update($request, $response, $aArgs);
//print_r(json_decode($response->getBody())[0]->res_id);exit;
$this->assertGreaterThan(1, json_decode($response->getBody())[0]->res_id);
}
public function testUpdateResource()
{
$action = new \Core\Controllers\ResController();
$data = [];
array_push(
$data,
array(
'column' => 'status',
'value' => 'NEW',
'type' => 'string',
)
);
$resId = \Core\Models\ResModel::getLastId(['select' => ['res_id']]);
$aArgs = [
'table' => 'res_letterbox',
'res_id' => $resId[0]['res_id'],
'data' => $data,
];
$response = $action->updateResource($aArgs);
//print_r($response);exit;
$this->assertGreaterThan(1, $response);
}
}
......@@ -171,7 +171,9 @@ class ResController
ValidatorModel::stringType($aArgs, ['userId']);
ValidatorModel::intVal($aArgs, ['resId']);
if ($aArgs['userId'] == 'superadmin') {
return true;
}
$groups = UserModel::getGroupsByUserId(['userId' => $aArgs['userId']]);
$groupsClause = '';
foreach ($groups as $key => $group) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment