Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
use PHPUnit\Framework\TestCase;
class AttachmentControllerTest extends TestCase
{
private static $id = null;
public function testCreate()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
// CREATE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$fileContent = file_get_contents('test/unitTests/samples/test.txt');
$encodedFile = base64_encode($fileContent);
$aArgs = [
'title' => 'Nulle pierre ne peut être polie sans friction, nul homme ne peut parfaire son expérience sans épreuve.',
'type' => 'response_project',
'chrono' => 'MAARCH/2019D/24',
'resIdMaster' => 100,
'encodedFile' => $encodedFile,
'format' => 'txt',
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $attachmentController->create($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string)$response->getBody());
self::$id = $responseBody->id;
$this->assertIsInt(self::$id);
// CHECK ERROR EMPTY TYPE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgsFail = $aArgs;
unset($aArgsFail['type']);
$fullRequest = \httpRequestCustom::addContentInBody($aArgsFail, $request);
$response = $attachmentController->create($fullRequest, new \Slim\Http\Response());
$this->assertSame(400, $response->getStatusCode());
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('Body type is empty or not a string', $response['errors']);
// READ
$res = \Attachment\models\AttachmentModel::getById(['id' => self::$id, 'select' => ['*']]);
$this->assertIsArray($res);
$this->assertSame($aArgs['title'], $res['title']);
$this->assertSame($aArgs['type'], $res['attachment_type']);
$this->assertSame('txt', $res['format']);
$this->assertSame('A_TRA', $res['status']);
$this->assertSame('superadmin', $res['typist']);
$this->assertSame(1, $res['relation']);
$this->assertSame($aArgs['chrono'], $res['identifier']);
$this->assertNotNull($res['path']);
$this->assertNotNull($res['filename']);
$this->assertNotNull($res['docserver_id']);
$this->assertNotNull($res['fingerprint']);
$this->assertNotNull($res['filesize']);
$this->assertNull($res['origin_id']);
}
public function testUpdate()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
// UPDATE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'title' => 'La plus chétive cabane renferme plus de vertus que les palais des rois.',
'type' => 'response_project',
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $attachmentController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(204, $response->getStatusCode());
// CHECK ERROR EMPTY TYPE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgsFail = $aArgs;
unset($aArgsFail['type']);
$fullRequest = \httpRequestCustom::addContentInBody($aArgsFail, $request);
$response = $attachmentController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(400, $response->getStatusCode());
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('Body type is empty or not a string', $response['errors']);
// READ
$response = $attachmentController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]);
$res = json_decode((string)$response->getBody(), true);
$this->assertIsArray($res);
$this->assertSame($aArgs['title'], $res['title']);
$this->assertSame($aArgs['type'], $res['type']);
$this->assertSame('A_TRA', $res['status']);
$this->assertSame(1, $res['relation']);
}
public function testGetByResId()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->getByResId($request, new \Slim\Http\Response(), ['resId' => 100]);
$response = json_decode((string)$response->getBody(), true);
$this->assertNotNull($response['attachments']);
$this->assertIsArray($response['attachments']);
$this->assertIsBool($response['mailevaEnabled']);
foreach ($response['attachments'] as $value) {
if ($value['resId'] == self::$id) {
$userInfo = \User\models\UserModel::getByLogin(['login' => 'superadmin', 'select' => ['id']]);
$this->assertSame('La plus chétive cabane renferme plus de vertus que les palais des rois.', $value['title']);
$this->assertSame('response_project', $value['type']);
$this->assertSame('A_TRA', $value['status']);
$this->assertSame($userInfo['id'], $value['typist']);
$this->assertSame(1, $value['relation']);
$this->assertSame('MAARCH/2019D/24', $value['chrono']);
$this->assertNull($value['originId']);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
$this->assertNotNull($value['modificationDate']);
$this->assertNotNull($value['modifiedBy']);
$this->assertNotNull($value['typeLabel']);
$this->assertIsBool($value['canConvert']);
break;
}
}
// ERROR
$GLOBALS['userId'] = 'bblier';
$userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$GLOBALS['id'] = $userInfo['id'];
$response = $attachmentController->getByResId($request, new \Slim\Http\Response(), ['resId' => 123940595]);
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('Document out of perimeter', $response['errors']);
$GLOBALS['userId'] = 'superadmin';
$userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$GLOBALS['id'] = $userInfo['id'];
}
public function testSetInSignatureBook()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->setInSignatureBook($request, new \Slim\Http\Response(), ['id' => self::$id]);
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('success', $response['success']);
// ERROR
$response = $attachmentController->setInSignatureBook($request, new \Slim\Http\Response(), ['id' => 123940595]);
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('Attachment not found', $response['errors']);
}
public function testSetInSendAttachment()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->setInSendAttachment($request, new \Slim\Http\Response(), ['id' => self::$id]);
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('success', $response['success']);
// ERROR
$response = $attachmentController->setInSendAttachment($request, new \Slim\Http\Response(), ['id' => 123940595]);
$response = json_decode((string)$response->getBody(), true);
$this->assertSame('Attachment not found', $response['errors']);
}
public function testGetAttachmentTypes()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->getAttachmentsTypes($request, new \Slim\Http\Response());
$response = json_decode((string)$response->getBody(), true);
$this->assertNotNull($response['attachmentsTypes']);
$this->assertIsArray($response['attachmentsTypes']);
foreach ($response['attachmentsTypes'] as $value) {
$this->assertNotNull($value['label']);
$this->assertIsBool($value['sign']);
$this->assertIsBool($value['chrono']);
$this->assertIsBool($value['attachInMail']);
$this->assertIsBool($value['show']);
}
}
public function testDelete()
{
$attachmentController = new \Attachment\controllers\AttachmentController();
// DELETE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(204, $response->getStatusCode());
// DELETE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $attachmentController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
$res = json_decode((string)$response->getBody(), true);
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('Attachment does not exist', $res['errors']);
// READ
$response = $attachmentController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]);
$res = json_decode((string)$response->getBody(), true);
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('Attachment does not exist', $res['errors']);
}