1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerFeatureTest\Zed\SelfServicePortal\Business\Facade; |
9
|
|
|
|
10
|
|
|
use Codeception\Test\Unit; |
11
|
|
|
use DateTime; |
12
|
|
|
use Generated\Shared\Transfer\CompanyBusinessUnitTransfer; |
13
|
|
|
use Generated\Shared\Transfer\CriteriaRangeFilterTransfer; |
14
|
|
|
use Generated\Shared\Transfer\FileAttachmentConditionsTransfer; |
15
|
|
|
use Generated\Shared\Transfer\FileAttachmentCriteriaTransfer; |
16
|
|
|
use Generated\Shared\Transfer\FileAttachmentSearchConditionsTransfer; |
17
|
|
|
use Generated\Shared\Transfer\FileInfoTransfer; |
18
|
|
|
use Generated\Shared\Transfer\FileTransfer; |
19
|
|
|
use Generated\Shared\Transfer\PermissionCollectionTransfer; |
20
|
|
|
use Generated\Shared\Transfer\SspAssetBusinessUnitAssignmentTransfer; |
21
|
|
|
use Generated\Shared\Transfer\SspAssetTransfer; |
22
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
23
|
|
|
use Spryker\Service\Flysystem\Plugin\FileSystem\FileSystemWriterPlugin; |
24
|
|
|
use Spryker\Service\FlysystemLocalFileSystem\Plugin\Flysystem\LocalFilesystemBuilderPlugin; |
25
|
|
|
use Spryker\Shared\FileSystem\FileSystemConstants; |
26
|
|
|
use SprykerFeature\Shared\SelfServicePortal\Plugin\Permission\ViewBusinessUnitSspAssetPermissionPlugin; |
27
|
|
|
use SprykerFeature\Shared\SelfServicePortal\Plugin\Permission\ViewCompanyBusinessUnitFilesPermissionPlugin; |
28
|
|
|
use SprykerFeature\Shared\SelfServicePortal\Plugin\Permission\ViewCompanyFilesPermissionPlugin; |
29
|
|
|
use SprykerFeature\Shared\SelfServicePortal\Plugin\Permission\ViewCompanyUserFilesPermissionPlugin; |
30
|
|
|
use SprykerFeature\Zed\SelfServicePortal\Business\CompanyFile\Permission\FileAttachmentCriteriaPermissionExpander; |
31
|
|
|
use SprykerFeature\Zed\SelfServicePortal\Business\SelfServicePortalBusinessFactory; |
32
|
|
|
use SprykerFeature\Zed\SelfServicePortal\Business\SelfServicePortalFacadeInterface; |
33
|
|
|
use SprykerFeature\Zed\SelfServicePortal\Persistence\SelfServicePortalRepository; |
34
|
|
|
use SprykerFeatureTest\Zed\SelfServicePortal\SelfServicePortalBusinessTester; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Auto-generated group annotations |
38
|
|
|
* |
39
|
|
|
* @group SprykerFeatureTest |
40
|
|
|
* @group Zed |
41
|
|
|
* @group SelfServicePortal |
42
|
|
|
* @group Business |
43
|
|
|
* @group Facade |
44
|
|
|
* @group GetFileAttachmentCollectionFacadeTest |
45
|
|
|
* Add your own group annotations below this line |
46
|
|
|
*/ |
47
|
|
|
class GetFileAttachmentCollectionFacadeTest extends Unit |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected const DATE_TIME_FORMAT = 'Y-m-d H:i:s'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected const EXTENSION_PDF = 'pdf'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected const FILE_NAME_SEARCH = 'test_search_name.pdf'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected const FILE_REFERENCE_SEARCH = 'test_search_reference'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected const FILE_NAME_OTHER = 'other.pdf'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected const FILE_REFERENCE_OTHER = 'other_ref'; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
public const PLUGIN_COLLECTION_FILESYSTEM_BUILDER = 'filesystem builder plugin collection'; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected const PLUGIN_WRITER = 'PLUGIN_WRITER'; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var \SprykerFeatureTest\Zed\SelfServicePortal\SelfServicePortalBusinessTester |
91
|
|
|
*/ |
92
|
|
|
protected SelfServicePortalBusinessTester $tester; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var \SprykerFeature\Zed\SelfServicePortal\Business\SelfServicePortalFacadeInterface |
96
|
|
|
*/ |
97
|
|
|
protected SelfServicePortalFacadeInterface $facade; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var \SprykerFeature\Zed\SelfServicePortal\Business\CompanyFile\Permission\FileAttachmentCriteriaPermissionExpander|\PHPUnit\Framework\MockObject\MockObject |
101
|
|
|
*/ |
102
|
|
|
protected FileAttachmentCriteriaPermissionExpander|MockObject $fileAttachmentPermissionExpanderMock; |
103
|
|
|
|
104
|
|
|
protected function _before(): void |
105
|
|
|
{ |
106
|
|
|
$this->tester->setDependency(static::PLUGIN_WRITER, new FileSystemWriterPlugin()); |
107
|
|
|
$this->tester->setDependency(static::PLUGIN_COLLECTION_FILESYSTEM_BUILDER, [ |
108
|
|
|
new LocalFilesystemBuilderPlugin(), |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
$localFilesystemBuilderConfiguration = [ |
112
|
|
|
'sprykerAdapterClass' => LocalFilesystemBuilderPlugin::class, |
113
|
|
|
'root' => '/data/data/tmp/ssp-files', |
114
|
|
|
'path' => '/', |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$this->tester->setConfig(FileSystemConstants::FILESYSTEM_SERVICE, [ |
118
|
|
|
's3-import' => $localFilesystemBuilderConfiguration, |
119
|
|
|
'files' => $localFilesystemBuilderConfiguration, |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->tester->ensureFileAttachmentTablesAreEmpty(); |
123
|
|
|
|
124
|
|
|
$this->facade = $this->tester->getFacade(); |
125
|
|
|
|
126
|
|
|
$this->fileAttachmentPermissionExpanderMock = $this->createPartialMock( |
127
|
|
|
FileAttachmentCriteriaPermissionExpander::class, |
128
|
|
|
['can'], |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGetFileAttachmentsForSspAssetWithPermissions(): void |
133
|
|
|
{ |
134
|
|
|
// Arrange |
135
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
136
|
|
|
$fileTransfer = $this->tester->haveFile(); |
137
|
|
|
|
138
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
139
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
140
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
141
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
145
|
|
|
|
146
|
|
|
$sspAssetTransfer = $this->tester->haveAsset([ |
147
|
|
|
SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => |
148
|
|
|
[[SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer]], |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$this->tester->haveSspAssetFileAttachment([ |
152
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
153
|
|
|
'idSspAsset' => $sspAssetTransfer->getIdSspAssetOrFail(), |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
157
|
|
|
->setCompanyUser($companyUserTransfer) |
158
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
159
|
|
|
->setWithSspAssetRelation(true); |
160
|
|
|
|
161
|
|
|
// Act |
162
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
163
|
|
|
|
164
|
|
|
// Assert |
165
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
166
|
|
|
$this->assertSame( |
167
|
|
|
$fileTransfer->getIdFile(), |
168
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testGetFilesAttachmentsForCompanyWithoutPermissions(): void |
173
|
|
|
{ |
174
|
|
|
// Arrange |
175
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
176
|
|
|
$fileTransfer = $this->tester->haveFile(); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$emptyPermissionCollectionTransfer = new PermissionCollectionTransfer(); |
179
|
|
|
$companyUserTransfer = $this->tester->haveCompanyUserWithPermissions( |
180
|
|
|
$companyTransfer, |
181
|
|
|
$emptyPermissionCollectionTransfer, |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$companyUserTransfer->setCompany($companyTransfer); |
185
|
|
|
|
186
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
187
|
|
|
->setCompanyUser($companyUserTransfer) |
188
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
189
|
|
|
->setWithBusinessUnitRelation(false) |
190
|
|
|
->setWithCompanyUserRelation(false) |
191
|
|
|
->setWithSspAssetRelation(false); |
192
|
|
|
|
193
|
|
|
// Act |
194
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
195
|
|
|
|
196
|
|
|
$this->assertCount(0, $fileAttachmentCollectionTransfer->getFileAttachments()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testGetFilesAttachmentsForCompanyUserWithPermissions(): void |
200
|
|
|
{ |
201
|
|
|
// Arrange |
202
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
203
|
|
|
$fileTransfer = $this->tester->haveFile(); |
204
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
205
|
|
|
|
206
|
|
|
$this->mockPermissions([ |
207
|
|
|
ViewCompanyUserFilesPermissionPlugin::KEY => true, |
208
|
|
|
]); |
209
|
|
|
|
210
|
|
|
$this->tester->haveCompanyUserFileAttachment([ |
211
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
212
|
|
|
'idCompanyUser' => $companyUserTransfer->getIdCompanyUserOrFail(), |
213
|
|
|
]); |
214
|
|
|
|
215
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
216
|
|
|
->setCompanyUser($companyUserTransfer) |
217
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
218
|
|
|
->setWithCompanyUserRelation(true); |
219
|
|
|
|
220
|
|
|
// Act |
221
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
222
|
|
|
|
223
|
|
|
// Assert |
224
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
225
|
|
|
$this->assertSame( |
226
|
|
|
$fileTransfer->getIdFile(), |
227
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testGetFilesAttachmentsForBusinessUnitWithPermissions(): void |
232
|
|
|
{ |
233
|
|
|
// Arrange |
234
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
235
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
236
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
237
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
238
|
|
|
]); |
239
|
|
|
$fileTransfer = $this->tester->haveFile(); |
240
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
241
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
242
|
|
|
|
243
|
|
|
$this->mockPermissions([ |
244
|
|
|
ViewCompanyBusinessUnitFilesPermissionPlugin::KEY => true, |
245
|
|
|
]); |
246
|
|
|
|
247
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
248
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
249
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
250
|
|
|
]); |
251
|
|
|
|
252
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
253
|
|
|
->setCompanyUser($companyUserTransfer) |
254
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
255
|
|
|
->setWithBusinessUnitRelation(true); |
256
|
|
|
|
257
|
|
|
// Act |
258
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
259
|
|
|
|
260
|
|
|
// Assert |
261
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
262
|
|
|
$this->assertSame( |
263
|
|
|
$fileTransfer->getIdFile(), |
264
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testGetFilesAttachmentsWithMixedPermissionLevels(): void |
269
|
|
|
{ |
270
|
|
|
// Arrange |
271
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
272
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
273
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
274
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
275
|
|
|
]); |
276
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
277
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
278
|
|
|
|
279
|
|
|
$this->mockPermissions([ |
280
|
|
|
ViewCompanyFilesPermissionPlugin::KEY => true, |
281
|
|
|
ViewCompanyBusinessUnitFilesPermissionPlugin::KEY => true, |
282
|
|
|
ViewCompanyUserFilesPermissionPlugin::KEY => true, |
283
|
|
|
]); |
284
|
|
|
|
285
|
|
|
$companyFileTransfer = $this->tester->haveFile(); |
|
|
|
|
286
|
|
|
$businessUnitFileTransfer = $this->tester->haveFile(); |
287
|
|
|
$userFileTransfer = $this->tester->haveFile(); |
288
|
|
|
|
289
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
290
|
|
|
'idFile' => $businessUnitFileTransfer->getIdFileOrFail(), |
291
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
292
|
|
|
]); |
293
|
|
|
|
294
|
|
|
$this->tester->haveCompanyUserFileAttachment([ |
295
|
|
|
'idFile' => $userFileTransfer->getIdFileOrFail(), |
296
|
|
|
'idCompanyUser' => $companyUserTransfer->getIdCompanyUserOrFail(), |
297
|
|
|
]); |
298
|
|
|
|
299
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
300
|
|
|
->setCompanyUser($companyUserTransfer) |
301
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
302
|
|
|
->setWithBusinessUnitRelation(true) |
303
|
|
|
->setWithCompanyUserRelation(true); |
304
|
|
|
|
305
|
|
|
// Act |
306
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
307
|
|
|
|
308
|
|
|
$this->assertCount(2, $fileAttachmentCollectionTransfer->getFileAttachments()); |
309
|
|
|
|
310
|
|
|
$returnedFileIds = []; |
311
|
|
|
foreach ($fileAttachmentCollectionTransfer->getFileAttachments() as $fileAttachment) { |
312
|
|
|
$returnedFileIds[] = $fileAttachment->getFile()->getIdFile(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->assertContains($businessUnitFileTransfer->getIdFile(), $returnedFileIds); |
316
|
|
|
$this->assertContains($userFileTransfer->getIdFile(), $returnedFileIds); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testGetFilesAttachmentsWithMixedEntityTypes(): void |
320
|
|
|
{ |
321
|
|
|
// Arrange |
322
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
323
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
324
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
325
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
326
|
|
|
]); |
327
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
328
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
329
|
|
|
|
330
|
|
|
$this->mockPermissions([ |
331
|
|
|
ViewCompanyFilesPermissionPlugin::KEY => true, |
332
|
|
|
ViewBusinessUnitSspAssetPermissionPlugin::KEY => true, |
333
|
|
|
]); |
334
|
|
|
|
335
|
|
|
$companyFileTransfer = $this->tester->haveFile(); |
336
|
|
|
$sspAssetFileTransfer = $this->tester->haveFile(); |
337
|
|
|
|
338
|
|
|
$sspAssetTransfer = $this->tester->haveAsset([ |
339
|
|
|
SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [ |
340
|
|
|
[SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer], |
341
|
|
|
], |
342
|
|
|
]); |
343
|
|
|
|
344
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
345
|
|
|
'idFile' => $companyFileTransfer->getIdFileOrFail(), |
346
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
347
|
|
|
]); |
348
|
|
|
|
349
|
|
|
$this->tester->haveSspAssetFileAttachment([ |
350
|
|
|
'idFile' => $sspAssetFileTransfer->getIdFileOrFail(), |
351
|
|
|
'idSspAsset' => $sspAssetTransfer->getIdSspAssetOrFail(), |
352
|
|
|
]); |
353
|
|
|
|
354
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
355
|
|
|
->setCompanyUser($companyUserTransfer) |
356
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
357
|
|
|
->setWithSspAssetRelation(true) |
358
|
|
|
->setWithBusinessUnitRelation(true); |
359
|
|
|
|
360
|
|
|
// Act |
361
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
362
|
|
|
|
363
|
|
|
$this->assertCount(2, $fileAttachmentCollectionTransfer->getFileAttachments()); |
364
|
|
|
|
365
|
|
|
$returnedFileIds = []; |
366
|
|
|
foreach ($fileAttachmentCollectionTransfer->getFileAttachments() as $fileAttachment) { |
367
|
|
|
$returnedFileIds[] = $fileAttachment->getFile()->getIdFile(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$this->assertContains($companyFileTransfer->getIdFile(), $returnedFileIds); |
371
|
|
|
$this->assertContains($sspAssetFileTransfer->getIdFile(), $returnedFileIds); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function testGetFilesAttachmentsWithPartialPermissions(): void |
375
|
|
|
{ |
376
|
|
|
// Arrange |
377
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
378
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
379
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
380
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
381
|
|
|
]); |
382
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
383
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
384
|
|
|
|
385
|
|
|
$this->mockPermissions([ |
386
|
|
|
ViewCompanyBusinessUnitFilesPermissionPlugin::KEY => true, |
387
|
|
|
]); |
388
|
|
|
|
389
|
|
|
$companyFileTransfer = $this->tester->haveFile(); |
|
|
|
|
390
|
|
|
$businessUnitFileTransfer = $this->tester->haveFile(); |
391
|
|
|
$userFileTransfer = $this->tester->haveFile(); |
392
|
|
|
|
393
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
394
|
|
|
'idFile' => $businessUnitFileTransfer->getIdFileOrFail(), |
395
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
396
|
|
|
]); |
397
|
|
|
|
398
|
|
|
$this->tester->haveCompanyUserFileAttachment([ |
399
|
|
|
'idFile' => $userFileTransfer->getIdFileOrFail(), |
400
|
|
|
'idCompanyUser' => $companyUserTransfer->getIdCompanyUserOrFail(), |
401
|
|
|
]); |
402
|
|
|
|
403
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
404
|
|
|
->setCompanyUser($companyUserTransfer) |
405
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
406
|
|
|
->setWithBusinessUnitRelation(true) |
407
|
|
|
->setWithCompanyUserRelation(false) |
408
|
|
|
->setWithSspAssetRelation(false); |
409
|
|
|
|
410
|
|
|
// Act |
411
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
412
|
|
|
|
413
|
|
|
// Assert - Should return only business unit file |
414
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
415
|
|
|
$this->assertSame( |
416
|
|
|
$businessUnitFileTransfer->getIdFile(), |
417
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
418
|
|
|
); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param array<string, bool> $permissions |
423
|
|
|
* |
424
|
|
|
* @return void |
425
|
|
|
*/ |
426
|
|
|
protected function mockPermissions(array $permissions): void |
427
|
|
|
{ |
428
|
|
|
$this->fileAttachmentPermissionExpanderMock |
429
|
|
|
->method('can') |
|
|
|
|
430
|
|
|
->willReturnCallback(function (string $permissionKey, int $companyUserId) use ($permissions): bool { |
431
|
|
|
return $permissions[$permissionKey] ?? false; |
432
|
|
|
}); |
433
|
|
|
|
434
|
|
|
$factoryMock = $this->createPartialMock(SelfServicePortalBusinessFactory::class, ['createFileAttachmentPermissionExpander']); |
435
|
|
|
$factoryMock->setRepository((new SelfServicePortalRepository())); |
436
|
|
|
$factoryMock->method('createFileAttachmentPermissionExpander')->willReturn($this->fileAttachmentPermissionExpanderMock); |
437
|
|
|
$this->facade->setFactory($factoryMock); |
|
|
|
|
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
public function testGetFileAttachmentsReturnsEmptyCollectionWhenNoFilesExist(): void |
441
|
|
|
{ |
442
|
|
|
// Arrange |
443
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
444
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
445
|
|
|
|
446
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
447
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
448
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
449
|
|
|
]); |
450
|
|
|
|
451
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
452
|
|
|
|
453
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
454
|
|
|
->setCompanyUser($companyUserTransfer) |
455
|
|
|
->setFileAttachmentConditions(new FileAttachmentConditionsTransfer()) |
456
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
457
|
|
|
->setWithBusinessUnitRelation(true) |
458
|
|
|
->setWithCompanyUserRelation(true); |
459
|
|
|
|
460
|
|
|
// Act |
461
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
462
|
|
|
|
463
|
|
|
// Assert |
464
|
|
|
$this->assertCount(0, $fileAttachmentCollectionTransfer->getFileAttachments()); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
public function testGetFileAttachmentsFilteredByDateRange(): void |
468
|
|
|
{ |
469
|
|
|
// Arrange |
470
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
471
|
|
|
$currentTime = time(); |
472
|
|
|
$fileTransfer = $this->tester->haveFile([], [ |
473
|
|
|
FileInfoTransfer::CREATED_AT => (new DateTime())->createFromFormat(static::DATE_TIME_FORMAT, $currentTime), |
474
|
|
|
]); |
475
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
476
|
|
|
|
477
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
478
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
479
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
480
|
|
|
]); |
481
|
|
|
|
482
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
483
|
|
|
|
484
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
485
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
486
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
487
|
|
|
]); |
488
|
|
|
|
489
|
|
|
$currentTime = time(); |
490
|
|
|
|
491
|
|
|
$rangeTransfer = (new CriteriaRangeFilterTransfer()) |
492
|
|
|
->setFrom($currentTime - 3600) |
493
|
|
|
->setTo($currentTime + 3600); |
494
|
|
|
|
495
|
|
|
$fileAttachmentConditionsTransfer = (new FileAttachmentConditionsTransfer()) |
496
|
|
|
->setRangeCreatedAt($rangeTransfer); |
497
|
|
|
|
498
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
499
|
|
|
->setFileAttachmentConditions($fileAttachmentConditionsTransfer) |
500
|
|
|
->setCompanyUser($companyUserTransfer) |
501
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
502
|
|
|
->setWithBusinessUnitRelation(true) |
503
|
|
|
->setWithCompanyUserRelation(true); |
504
|
|
|
|
505
|
|
|
// Act |
506
|
|
|
$fileAttachmentCollectionTransfer = $this->facade |
507
|
|
|
->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
508
|
|
|
|
509
|
|
|
// Assert |
510
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
511
|
|
|
$this->assertSame( |
512
|
|
|
$fileTransfer->getIdFile(), |
513
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
514
|
|
|
); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
public function testGetFileAttachmentsFilteredByFileType(): void |
518
|
|
|
{ |
519
|
|
|
// Arrange |
520
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
521
|
|
|
$fileTransfer = $this->tester->haveFile([], [ |
522
|
|
|
FileInfoTransfer::EXTENSION => static::EXTENSION_PDF, |
523
|
|
|
]); |
524
|
|
|
|
525
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
526
|
|
|
|
527
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
528
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
529
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
530
|
|
|
]); |
531
|
|
|
|
532
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
533
|
|
|
|
534
|
|
|
$fileAttachmentConditionsTransfer = (new FileAttachmentConditionsTransfer()) |
535
|
|
|
->setFileTypes([static::EXTENSION_PDF]); |
536
|
|
|
|
537
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
538
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
539
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
540
|
|
|
]); |
541
|
|
|
|
542
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
543
|
|
|
->setFileAttachmentConditions($fileAttachmentConditionsTransfer) |
544
|
|
|
->setCompanyUser($companyUserTransfer) |
545
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
546
|
|
|
->setWithBusinessUnitRelation(true) |
547
|
|
|
->setWithCompanyUserRelation(true); |
548
|
|
|
|
549
|
|
|
// Act |
550
|
|
|
$fileAttachmentCollectionTransfer = $this->facade |
551
|
|
|
->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
552
|
|
|
|
553
|
|
|
// Assert |
554
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
555
|
|
|
$this->assertSame( |
556
|
|
|
$fileTransfer->getIdFile(), |
557
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
558
|
|
|
); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
public function testGetFileAttachmentsFilteredBySearchString(): void |
562
|
|
|
{ |
563
|
|
|
$fileByNameTransfer = $this->tester->haveFile([ |
564
|
|
|
FileTransfer::FILE_NAME => static::FILE_NAME_SEARCH, |
565
|
|
|
]); |
566
|
|
|
$fileByReferenceTransfer = $this->tester->haveFile([ |
567
|
|
|
FileTransfer::FILE_REFERENCE => static::FILE_REFERENCE_SEARCH, |
568
|
|
|
]); |
569
|
|
|
$fileNotMatchingTransfer = $this->tester->haveFile([ |
570
|
|
|
FileTransfer::FILE_NAME => static::FILE_NAME_OTHER, |
571
|
|
|
FileTransfer::FILE_REFERENCE => static::FILE_REFERENCE_OTHER, |
572
|
|
|
]); |
573
|
|
|
// Arrange |
574
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
575
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
576
|
|
|
|
577
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
578
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
579
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
580
|
|
|
]); |
581
|
|
|
|
582
|
|
|
$companyUserTransfer->setCompanyBusinessUnit($businessUnitTransfer); |
583
|
|
|
|
584
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
585
|
|
|
'idFile' => $fileByNameTransfer->getIdFileOrFail(), |
586
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
587
|
|
|
]); |
588
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
589
|
|
|
'idFile' => $fileByReferenceTransfer->getIdFileOrFail(), |
590
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
591
|
|
|
]); |
592
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
593
|
|
|
'idFile' => $fileNotMatchingTransfer->getIdFileOrFail(), |
594
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
595
|
|
|
]); |
596
|
|
|
|
597
|
|
|
$fileAttachmentSearchConditionsTransfer = (new FileAttachmentSearchConditionsTransfer()) |
598
|
|
|
->setSearchString('test_search'); |
599
|
|
|
|
600
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
601
|
|
|
->setCompanyUser($companyUserTransfer) |
602
|
|
|
->setFileAttachmentConditions(new FileAttachmentConditionsTransfer()) |
603
|
|
|
->setFileAttachmentSearchConditions($fileAttachmentSearchConditionsTransfer) |
604
|
|
|
->setWithBusinessUnitRelation(true) |
605
|
|
|
->setWithCompanyUserRelation(true); |
606
|
|
|
|
607
|
|
|
// Act |
608
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
609
|
|
|
|
610
|
|
|
// Assert |
611
|
|
|
$this->assertCount(2, $fileAttachmentCollectionTransfer->getFileAttachments()); |
612
|
|
|
|
613
|
|
|
$fileId1 = $fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(); |
614
|
|
|
$fileId2 = $fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(1)->getFile()->getIdFile(); |
615
|
|
|
$this->assertContains($fileByNameTransfer->getIdFile(), [$fileId1, $fileId2]); |
616
|
|
|
$this->assertContains($fileByReferenceTransfer->getIdFile(), [$fileId1, $fileId2]); |
617
|
|
|
$this->assertNotContains($fileNotMatchingTransfer->getIdFile(), [$fileId1, $fileId2]); |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
public function testGetFileAttachmentCollectionWithCompanyPermissionAndBusinessUnitFilterShouldReturnFiles(): void |
621
|
|
|
{ |
622
|
|
|
// Arrange |
623
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
624
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
625
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
626
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
627
|
|
|
]); |
628
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
629
|
|
|
|
630
|
|
|
$customerBusinessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
631
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
632
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
633
|
|
|
]); |
634
|
|
|
$companyUserTransfer |
635
|
|
|
->setCompany($companyTransfer) |
636
|
|
|
->setCompanyBusinessUnit($customerBusinessUnitTransfer); |
637
|
|
|
|
638
|
|
|
$fileTransfer = $this->tester->haveFile(); |
639
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
640
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
641
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
642
|
|
|
]); |
643
|
|
|
|
644
|
|
|
$this->tester->haveCompanyUserFileAttachment([ |
645
|
|
|
'idFile' => $this->tester->haveFile()->getIdFileOrFail(), |
646
|
|
|
'idCompanyUser' => $companyUserTransfer->getIdCompanyUserOrFail(), |
647
|
|
|
]); |
648
|
|
|
|
649
|
|
|
$this->mockPermissions([ |
650
|
|
|
ViewCompanyFilesPermissionPlugin::KEY => true, |
651
|
|
|
ViewCompanyBusinessUnitFilesPermissionPlugin::KEY => false, |
652
|
|
|
]); |
653
|
|
|
|
654
|
|
|
$fileAttachmentConditionsTransfer = (new FileAttachmentConditionsTransfer()) |
655
|
|
|
->addBusinessUnitUuid($businessUnitTransfer->getUuidOrFail()); |
656
|
|
|
|
657
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
658
|
|
|
->setCompanyUser($companyUserTransfer) |
659
|
|
|
->setFileAttachmentConditions($fileAttachmentConditionsTransfer) |
660
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
661
|
|
|
->setWithBusinessUnitRelation(true); |
662
|
|
|
|
663
|
|
|
// Act |
664
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
665
|
|
|
|
666
|
|
|
// Assert |
667
|
|
|
$this->assertCount(1, $fileAttachmentCollectionTransfer->getFileAttachments()); |
668
|
|
|
$this->assertSame( |
669
|
|
|
$fileTransfer->getIdFile(), |
670
|
|
|
$fileAttachmentCollectionTransfer->getFileAttachments()->offsetGet(0)->getFile()->getIdFile(), |
671
|
|
|
); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
public function testGetFileAttachmentCollectionWithBusinessUnitPermissionAndBusinessUnitFilterShouldNotReturnFiles(): void |
675
|
|
|
{ |
676
|
|
|
// Arrange |
677
|
|
|
$companyTransfer = $this->tester->haveCompany(); |
678
|
|
|
$businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
679
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
680
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
681
|
|
|
]); |
682
|
|
|
$companyUserTransfer = $this->tester->createCompanyUser($companyTransfer); |
683
|
|
|
|
684
|
|
|
$customerBusinessUnitTransfer = $this->tester->haveCompanyBusinessUnit([ |
685
|
|
|
CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(), |
686
|
|
|
CompanyBusinessUnitTransfer::COMPANY => $companyTransfer, |
687
|
|
|
]); |
688
|
|
|
$companyUserTransfer |
689
|
|
|
->setCompany($companyTransfer) |
690
|
|
|
->setCompanyBusinessUnit($customerBusinessUnitTransfer); |
691
|
|
|
|
692
|
|
|
$fileTransfer = $this->tester->haveFile(); |
693
|
|
|
$this->tester->haveCompanyBusinessUnitFileAttachment([ |
694
|
|
|
'idFile' => $fileTransfer->getIdFileOrFail(), |
695
|
|
|
'idCompanyBusinessUnit' => $businessUnitTransfer->getIdCompanyBusinessUnitOrFail(), |
696
|
|
|
]); |
697
|
|
|
|
698
|
|
|
$this->mockPermissions([ |
699
|
|
|
ViewCompanyFilesPermissionPlugin::KEY => false, |
700
|
|
|
ViewCompanyBusinessUnitFilesPermissionPlugin::KEY => true, |
701
|
|
|
]); |
702
|
|
|
|
703
|
|
|
$fileAttachmentConditionsTransfer = (new FileAttachmentConditionsTransfer()) |
704
|
|
|
->addBusinessUnitUuid($businessUnitTransfer->getUuidOrFail()); |
705
|
|
|
|
706
|
|
|
$fileAttachmentCriteriaTransfer = (new FileAttachmentCriteriaTransfer()) |
707
|
|
|
->setCompanyUser($companyUserTransfer) |
708
|
|
|
->setFileAttachmentConditions($fileAttachmentConditionsTransfer) |
709
|
|
|
->setFileAttachmentSearchConditions(new FileAttachmentSearchConditionsTransfer()) |
710
|
|
|
->setWithBusinessUnitRelation(true); |
711
|
|
|
|
712
|
|
|
// Act |
713
|
|
|
$fileAttachmentCollectionTransfer = $this->facade->getFileAttachmentCollection($fileAttachmentCriteriaTransfer); |
714
|
|
|
|
715
|
|
|
// Assert |
716
|
|
|
$this->assertCount(0, $fileAttachmentCollectionTransfer->getFileAttachments()); |
717
|
|
|
} |
718
|
|
|
} |
719
|
|
|
|