InheritedAclQueryScopeTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
c 0
b 0
f 0
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Zed\AclEntity\Persistence\AclDirector;
11
12
use Codeception\Test\Unit;
13
use Generated\Shared\Transfer\AclEntityRuleTransfer;
14
use Generated\Shared\Transfer\AclEntitySegmentCriteriaTransfer;
15
use Generated\Shared\Transfer\AclEntitySegmentRequestTransfer;
16
use Generated\Shared\Transfer\MerchantProductTransfer;
17
use Generated\Shared\Transfer\ProductOfferTransfer;
18
use Generated\Shared\Transfer\RolesTransfer;
19
use Generated\Shared\Transfer\RoleTransfer;
20
use Orm\Zed\Merchant\Persistence\SpyMerchant;
21
use Orm\Zed\MerchantProduct\Persistence\Map\SpyMerchantProductAbstractTableMap;
22
use Orm\Zed\MerchantProduct\Persistence\SpyMerchantProductAbstract;
23
use Orm\Zed\MerchantProduct\Persistence\SpyMerchantProductAbstractQuery;
24
use Orm\Zed\Product\Persistence\Map\SpyProductAbstractTableMap;
25
use Orm\Zed\Product\Persistence\SpyProductAbstract;
26
use Orm\Zed\Product\Persistence\SpyProductAbstractQuery;
27
use Orm\Zed\ProductOffer\Persistence\SpyProductOffer;
28
use Orm\Zed\ProductOffer\Persistence\SpyProductOfferQuery;
29
use Propel\Runtime\ActiveQuery\ModelJoin;
30
use Pyz\Zed\Merchant\MerchantDependencyProvider;
31
use Pyz\Zed\ProductOffer\ProductOfferDependencyProvider;
32
use PyzTest\Zed\AclEntity\AclQueryDirectorTester;
33
use Spryker\Shared\AclEntity\AclEntityConstants;
34
use Spryker\Zed\AclEntity\AclEntityDependencyProvider;
35
use Spryker\Zed\AclMerchantPortal\Communication\Plugin\AclEntity\MerchantPortalConfigurationAclEntityMetadataConfigExpanderPlugin;
36
use Spryker\Zed\PropelOrm\Business\Runtime\ActiveQuery\Criteria;
37
38
/**
39
 * Auto-generated group annotations
40
 *
41
 * @group PyzTest
42
 * @group Zed
43
 * @group AclEntity
44
 * @group Persistence
45
 * @group AclDirector
46
 * @group InheritedAclQueryScopeTest
47
 * Add your own group annotations below this line
48
 */
49
class InheritedAclQueryScopeTest extends Unit
50
{
51
    /**
52
     * @var string
53
     */
54
    protected const ACL_ENTITY_SEGMENT_MERCHANT_1_NAME = 'segment merchant 1';
55
56
    /**
57
     * @var string
58
     */
59
    protected const ACL_ENTITY_SEGMENT_MERCHANT_1_REFERENCE = 'segment_1_reference';
60
61
    /**
62
     * @var string
63
     */
64
    protected const ACL_ENTITY_SEGMENT_MERCHANT_2_NAME = 'segment merchant 2';
65
66
    /**
67
     * @var string
68
     */
69
    protected const ACL_ENTITY_SEGMENT_MERCHANT_2_REFERENCE = 'segment_2_reference';
70
71
    /**
72
     * @var string
73
     */
74
    protected const ACL_ENTITY_SEGMENT_MERCHANT_3_NAME = 'segment merchant 3';
75
76
    /**
77
     * @var string
78
     */
79
    protected const ACL_ENTITY_SEGMENT_MERCHANT_3_REFERENCE = 'segment_3_reference';
80
81
    /**
82
     * @var \PyzTest\Zed\AclEntity\AclQueryDirectorTester
83
     */
84
    protected $tester;
85
86
    /**
87
     * @return void
88
     */
89
    protected function setUp(): void
90
    {
91
        parent::setUp();
92
93
        $this->tester->setDependency(MerchantDependencyProvider::PLUGINS_MERCHANT_POST_CREATE, []);
94
        $this->tester->setDependency(ProductOfferDependencyProvider::PLUGINS_PRODUCT_OFFER_POST_CREATE, []);
95
        $this->tester->setDependency(
96
            AclEntityDependencyProvider::PLUGINS_ACL_ENTITY_METADATA_COLLECTION_EXPANDER,
97
            [new MerchantPortalConfigurationAclEntityMetadataConfigExpanderPlugin()],
98
        );
99
100
        $this->tester->deleteTestData();
101
        $this->tester->deleteAclEntitySegments(
102
            (new AclEntitySegmentCriteriaTransfer())
103
                ->setReferences(
104
                    [
105
                        static::ACL_ENTITY_SEGMENT_MERCHANT_1_REFERENCE,
106
                        static::ACL_ENTITY_SEGMENT_MERCHANT_2_REFERENCE,
107
                        static::ACL_ENTITY_SEGMENT_MERCHANT_3_REFERENCE,
108
                    ],
109
                ),
110
        );
111
    }
112
113
    /**
114
     * @group AclEntityCreate
115
     *
116
     * @return void
117
     */
118
    public function testInspectCreateWithCreatePermission(): void
119
    {
120
        // Arrange
121
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
122
123
        $this->tester->haveAclEntityRule(
124
            [
125
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
126
                AclEntityRuleTransfer::ENTITY => SpyProductAbstract::class,
127
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
128
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_CREATE,
129
            ],
130
        );
131
        $this->tester->haveAclEntityRule(
132
            [
133
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
134
                AclEntityRuleTransfer::ENTITY => SpyMerchantProductAbstract::class,
135
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
136
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ
137
                    | AclEntityConstants::OPERATION_MASK_CREATE,
138
            ],
139
        );
140
        $this->tester->haveAclEntityRule(
141
            [
142
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
143
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
144
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
145
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
146
            ],
147
        );
148
149
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
150
        $aclModelDirector = $this->tester->createAclModelDirector(
151
            $rolesTransfer,
152
            $this->tester->createProductAbstractMerchantMetadataHierarchy(),
153
        );
154
155
        // Act, Assert
156
        $aclModelDirector->inspectCreate(new SpyProductAbstract());
157
    }
158
159
    /**
160
     * @group AclEntityUpdate
161
     *
162
     * @return void
163
     */
164
    public function testInspectUpdateWithUpdatePermissionAndSegmentScopeRuleForRootEntity(): void
165
    {
166
        // Arrange
167
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
168
169
        $productAbstractTransfer = $this->tester->haveProductAbstract();
170
        $merchantTransfer = $this->tester->haveMerchant();
171
        $this->tester->haveMerchantProduct(
172
            [
173
                MerchantProductTransfer::ID_MERCHANT => $merchantTransfer->getIdMerchantOrFail(),
174
                MerchantProductTransfer::ID_PRODUCT_ABSTRACT => $productAbstractTransfer->getIdProductAbstractOrFail(),
175
            ],
176
        );
177
178
        $aclEntitySegmentTransfer = $this->tester->haveAclEntitySegment(
179
            [
180
                AclEntitySegmentRequestTransfer::NAME => static::ACL_ENTITY_SEGMENT_MERCHANT_1_NAME,
181
                AclEntitySegmentRequestTransfer::REFERENCE => static::ACL_ENTITY_SEGMENT_MERCHANT_1_REFERENCE,
182
                AclEntitySegmentRequestTransfer::ENTITY => SpyMerchant::class,
183
                AclEntitySegmentRequestTransfer::ENTITY_IDS => [$merchantTransfer->getIdMerchantOrFail()],
184
            ],
185
        );
186
187
        $this->tester->haveAclEntityRule(
188
            [
189
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
190
                AclEntityRuleTransfer::ENTITY => SpyProductAbstract::class,
191
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
192
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_UPDATE,
193
            ],
194
        );
195
        $this->tester->haveAclEntityRule(
196
            [
197
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
198
                AclEntityRuleTransfer::ENTITY => SpyMerchantProductAbstract::class,
199
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
200
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
201
            ],
202
        );
203
204
        $this->tester->haveAclEntityRule(
205
            [
206
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_SEGMENT,
207
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
208
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
209
                AclEntityRuleTransfer::ID_ACL_ENTITY_SEGMENT => $aclEntitySegmentTransfer->getIdAclEntitySegment(),
210
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
211
            ],
212
        );
213
214
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
215
        $aclModelDirector = $this->tester->createAclModelDirector(
216
            $rolesTransfer,
217
            $this->tester->createProductAbstractMerchantMetadataHierarchy(),
218
        );
219
220
        // Act, Assert
221
        $aclModelDirector->inspectUpdate(
222
            $this->tester->findProductAbstractByIdProductAbstract(
0 ignored issues
show
Bug introduced by
It seems like $this->tester->findProdu...roductAbstractOrFail()) can also be of type null; however, parameter $entity of Spryker\Zed\AclEntity\Pe...rector::inspectUpdate() does only seem to accept Propel\Runtime\ActiveRecord\ActiveRecordInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

222
            /** @scrutinizer ignore-type */ $this->tester->findProductAbstractByIdProductAbstract(
Loading history...
223
                $productAbstractTransfer->getIdProductAbstractOrFail(),
224
            ),
225
        );
226
    }
227
228
    /**
229
     * @group AclEntityDelete
230
     *
231
     * @return void
232
     */
233
    public function testInspectDeleteWithDeletePermission(): void
234
    {
235
        // Arrange
236
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
237
238
        $productAbstractTransfer = $this->tester->haveProductAbstract();
239
        $merchantTransfer = $this->tester->haveMerchant();
240
        $this->tester->haveMerchantProduct(
241
            [
242
                MerchantProductTransfer::ID_MERCHANT => $merchantTransfer->getIdMerchantOrFail(),
243
                MerchantProductTransfer::ID_PRODUCT_ABSTRACT => $productAbstractTransfer->getIdProductAbstractOrFail(),
244
            ],
245
        );
246
247
        $this->tester->haveAclEntityRule(
248
            [
249
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
250
                AclEntityRuleTransfer::ENTITY => SpyProductAbstract::class,
251
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
252
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_DELETE,
253
            ],
254
        );
255
        $this->tester->haveAclEntityRule(
256
            [
257
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
258
                AclEntityRuleTransfer::ENTITY => SpyMerchantProductAbstract::class,
259
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
260
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
261
            ],
262
        );
263
264
        $this->tester->haveAclEntityRule(
265
            [
266
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
267
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
268
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
269
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
270
            ],
271
        );
272
273
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
274
        $aclModelDirector = $this->tester->createAclModelDirector(
275
            $rolesTransfer,
276
            $this->tester->createProductAbstractMerchantMetadataHierarchy(),
277
        );
278
279
        // Act, Assert
280
        $aclModelDirector->inspectDelete(
281
            $this->tester->findProductAbstractByIdProductAbstract(
0 ignored issues
show
Bug introduced by
It seems like $this->tester->findProdu...roductAbstractOrFail()) can also be of type null; however, parameter $entity of Spryker\Zed\AclEntity\Pe...rector::inspectDelete() does only seem to accept Propel\Runtime\ActiveRecord\ActiveRecordInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
            /** @scrutinizer ignore-type */ $this->tester->findProductAbstractByIdProductAbstract(
Loading history...
282
                $productAbstractTransfer->getIdProductAbstractOrFail(),
283
            ),
284
        );
285
    }
286
287
    /**
288
     * @group AclEntityApplyAclRules
289
     *
290
     * @return void
291
     */
292
    public function testApplyAclRuleOnSelectQueryWithReadPermission(): void
293
    {
294
        // Arrange
295
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
296
297
        $productAbstractTransfer = $this->tester->haveProductAbstract();
298
        $merchantTransfer = $this->tester->haveMerchant();
299
        $this->tester->haveMerchantProduct(
300
            [
301
                MerchantProductTransfer::ID_MERCHANT => $merchantTransfer->getIdMerchantOrFail(),
302
                MerchantProductTransfer::ID_PRODUCT_ABSTRACT => $productAbstractTransfer->getIdProductAbstractOrFail(),
303
            ],
304
        );
305
306
        $this->tester->haveAclEntityRule(
307
            [
308
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
309
                AclEntityRuleTransfer::ENTITY => SpyProductAbstract::class,
310
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
311
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
312
            ],
313
        );
314
        $this->tester->haveAclEntityRule(
315
            [
316
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
317
                AclEntityRuleTransfer::ENTITY => SpyMerchantProductAbstract::class,
318
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
319
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
320
            ],
321
        );
322
323
        $this->tester->haveAclEntityRule(
324
            [
325
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
326
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
327
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
328
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
329
            ],
330
        );
331
332
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
333
        $aclQueryDirector = $this->tester->createAclQueryDirector(
334
            $rolesTransfer,
335
            $this->tester->createProductAbstractMerchantMetadataHierarchy(),
336
        );
337
        // Act
338
        $query = $aclQueryDirector->applyAclRuleOnSelectQuery(SpyProductAbstractQuery::create());
339
340
        // Assert
341
        $this->assertNotEmpty($query->count());
342
    }
343
344
    /**
345
     * @group AclEntityCreate
346
     *
347
     * @return void
348
     */
349
    public function testInspectCreateWithCreatePermissionAndReferenceBasedRelation(): void
350
    {
351
        // Arrange
352
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
353
354
        $this->tester->haveAclEntityRule(
355
            [
356
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
357
                AclEntityRuleTransfer::ENTITY => SpyProductOffer::class,
358
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
359
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_CREATE,
360
            ],
361
        );
362
363
        $this->tester->haveAclEntityRule(
364
            [
365
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
366
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
367
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
368
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
369
            ],
370
        );
371
372
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
373
        $aclModelDirector = $this->tester->createAclModelDirector(
374
            $rolesTransfer,
375
            $this->tester->createProductOfferMetadataHierarchy(),
376
        );
377
378
        // Act, Assert
379
        $aclModelDirector->inspectCreate(new SpyProductOffer());
380
    }
381
382
    /**
383
     * @group AclEntityUpdate
384
     *
385
     * @return void
386
     */
387
    public function testInspectUpdateWithUpdatePermissionAndReferenceBasedRelation(): void
388
    {
389
        // Arrange
390
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
391
392
        $merchantTransfer = $this->tester->haveMerchant();
393
        $productOfferTransfer = $this->tester->haveProductOffer(
394
            [ProductOfferTransfer::MERCHANT_REFERENCE => $merchantTransfer->getMerchantReference()],
395
        );
396
397
        $this->tester->haveAclEntityRule(
398
            [
399
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
400
                AclEntityRuleTransfer::ENTITY => SpyProductOffer::class,
401
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
402
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_UPDATE,
403
            ],
404
        );
405
406
        $this->tester->haveAclEntityRule(
407
            [
408
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
409
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
410
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
411
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
412
            ],
413
        );
414
415
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
416
        $aclModelDirector = $this->tester->createAclModelDirector(
417
            $rolesTransfer,
418
            $this->tester->createProductOfferMetadataHierarchy(),
419
        );
420
421
        // Act, Assert
422
        $aclModelDirector->inspectUpdate(
423
            $this->tester->findProductOfferByIdProductOffer($productOfferTransfer->getIdProductOfferOrFail()),
0 ignored issues
show
Bug introduced by
It seems like $this->tester->findProdu...IdProductOfferOrFail()) can also be of type null; however, parameter $entity of Spryker\Zed\AclEntity\Pe...rector::inspectUpdate() does only seem to accept Propel\Runtime\ActiveRecord\ActiveRecordInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

423
            /** @scrutinizer ignore-type */ $this->tester->findProductOfferByIdProductOffer($productOfferTransfer->getIdProductOfferOrFail()),
Loading history...
424
        );
425
    }
426
427
    /**
428
     * @group AclEntityDelete
429
     *
430
     * @return void
431
     */
432
    public function testInspectDeleteWithDeletePermissionAndReferenceBasedRelation(): void
433
    {
434
        // Arrange
435
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
436
437
        $merchantTransfer = $this->tester->haveMerchant();
438
        $productOfferTransfer = $this->tester->haveProductOffer(
439
            [ProductOfferTransfer::MERCHANT_REFERENCE => $merchantTransfer->getMerchantReference()],
440
        );
441
442
        $this->tester->haveAclEntityRule(
443
            [
444
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
445
                AclEntityRuleTransfer::ENTITY => SpyProductOffer::class,
446
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
447
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_DELETE,
448
            ],
449
        );
450
451
        $this->tester->haveAclEntityRule(
452
            [
453
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
454
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
455
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
456
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
457
            ],
458
        );
459
460
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
461
        $aclModelDirector = $this->tester->createAclModelDirector(
462
            $rolesTransfer,
463
            $this->tester->createProductOfferMetadataHierarchy(),
464
        );
465
466
        // Act, Assert
467
        $aclModelDirector->inspectDelete(
468
            $this->tester->findProductOfferByIdProductOffer($productOfferTransfer->getIdProductOfferOrFail()),
0 ignored issues
show
Bug introduced by
It seems like $this->tester->findProdu...IdProductOfferOrFail()) can also be of type null; however, parameter $entity of Spryker\Zed\AclEntity\Pe...rector::inspectDelete() does only seem to accept Propel\Runtime\ActiveRecord\ActiveRecordInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

468
            /** @scrutinizer ignore-type */ $this->tester->findProductOfferByIdProductOffer($productOfferTransfer->getIdProductOfferOrFail()),
Loading history...
469
        );
470
    }
471
472
    /**
473
     * @group AclEntityApplyAclRules
474
     *
475
     * @return void
476
     */
477
    public function testApplyAclRuleOnSelectQueryWithReadPermissionAndReferenceBasedRelation(): void
478
    {
479
        // Arrange
480
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
481
482
        $merchantTransfer = $this->tester->haveMerchant();
483
        $this->tester->haveProductOffer(
484
            [ProductOfferTransfer::MERCHANT_REFERENCE => $merchantTransfer->getMerchantReference()],
485
        );
486
487
        $this->tester->haveAclEntityRule(
488
            [
489
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
490
                AclEntityRuleTransfer::ENTITY => SpyProductOffer::class,
491
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
492
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
493
            ],
494
        );
495
496
        $this->tester->haveAclEntityRule(
497
            [
498
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_GLOBAL,
499
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
500
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
501
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
502
            ],
503
        );
504
505
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
506
        $aclQueryDirector = $this->tester->createAclQueryDirector(
507
            $rolesTransfer,
508
            $this->tester->createProductOfferMetadataHierarchy(),
509
        );
510
511
        $query = SpyProductOfferQuery::create()->filterByCreatedAt(time(), Criteria::LESS_THAN);
512
513
        // Act
514
        $query = $aclQueryDirector->applyAclRuleOnSelectQuery($query);
515
516
        // Assert
517
        $this->assertNotEmpty($query->count());
518
    }
519
520
    /**
521
     * @group AclEntityApplyAclRules
522
     *
523
     * @return void
524
     */
525
    public function testApplyAclRuleRuleWithReadPermissionAndMultipleRoles(): void
526
    {
527
        // Arrange
528
        $merchant1RoleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
529
        $merchant2RoleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_2_NAME]);
530
        $merchant3RoleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_3_NAME]);
531
532
        $merchant1Transfer = $this->tester->haveMerchant();
533
        $merchant2Transfer = $this->tester->haveMerchant();
534
        $merchant3Transfer = $this->tester->haveMerchant();
535
536
        $this->tester->haveProductOffer(
537
            [
538
                ProductOfferTransfer::MERCHANT_REFERENCE => $merchant1Transfer->getMerchantReference(),
539
            ],
540
        );
541
        $this->tester->haveProductOffer(
542
            [ProductOfferTransfer::MERCHANT_REFERENCE => $merchant2Transfer->getMerchantReference()],
543
        );
544
        $this->tester->haveProductOffer(
545
            [ProductOfferTransfer::MERCHANT_REFERENCE => $merchant3Transfer->getMerchantReference()],
546
        );
547
548
        $segmentMerchant1Transfer = $this->tester->haveAclEntitySegment(
549
            [
550
                AclEntitySegmentRequestTransfer::NAME => static::ACL_ENTITY_SEGMENT_MERCHANT_1_NAME,
551
                AclEntitySegmentRequestTransfer::REFERENCE => static::ACL_ENTITY_SEGMENT_MERCHANT_1_REFERENCE,
552
                AclEntitySegmentRequestTransfer::ENTITY => SpyMerchant::class,
553
                AclEntitySegmentRequestTransfer::ENTITY_IDS => [$merchant1Transfer->getIdMerchantOrFail()],
554
            ],
555
        );
556
        $segmentMerchant2Transfer = $this->tester->haveAclEntitySegment(
557
            [
558
                AclEntitySegmentRequestTransfer::NAME => static::ACL_ENTITY_SEGMENT_MERCHANT_2_NAME,
559
                AclEntitySegmentRequestTransfer::REFERENCE => static::ACL_ENTITY_SEGMENT_MERCHANT_2_REFERENCE,
560
                AclEntitySegmentRequestTransfer::ENTITY => SpyMerchant::class,
561
                AclEntitySegmentRequestTransfer::ENTITY_IDS => [$merchant2Transfer->getIdMerchantOrFail()],
562
            ],
563
        );
564
        $segmentMerchant3Transfer = $this->tester->haveAclEntitySegment(
565
            [
566
                AclEntitySegmentRequestTransfer::NAME => static::ACL_ENTITY_SEGMENT_MERCHANT_3_NAME,
567
                AclEntitySegmentRequestTransfer::REFERENCE => static::ACL_ENTITY_SEGMENT_MERCHANT_3_REFERENCE,
568
                AclEntitySegmentRequestTransfer::ENTITY => SpyMerchant::class,
569
                AclEntitySegmentRequestTransfer::ENTITY_IDS => [$merchant3Transfer->getIdMerchantOrFail()],
570
            ],
571
        );
572
573
        $this->tester->haveAclEntityRule(
574
            [
575
                AclEntityRuleTransfer::ID_ACL_ROLE => $merchant1RoleTransfer->getIdAclRole(),
576
                AclEntityRuleTransfer::ENTITY => SpyProductOffer::class,
577
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
578
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_CRUD,
579
            ],
580
        );
581
        $this->tester->haveAclEntityRule(
582
            [
583
                AclEntityRuleTransfer::ID_ACL_ROLE => $merchant1RoleTransfer->getIdAclRole(),
584
                AclEntityRuleTransfer::ID_ACL_ENTITY_SEGMENT => $segmentMerchant1Transfer->getIdAclEntitySegment(),
585
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_SEGMENT,
586
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
587
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
588
            ],
589
        );
590
        $this->tester->haveAclEntityRule(
591
            [
592
                AclEntityRuleTransfer::ID_ACL_ROLE => $merchant1RoleTransfer->getIdAclRole(),
593
                AclEntityRuleTransfer::ID_ACL_ENTITY_SEGMENT => $segmentMerchant2Transfer->getIdAclEntitySegment(),
594
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_SEGMENT,
595
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
596
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
597
            ],
598
        );
599
        $this->tester->haveAclEntityRule(
600
            [
601
                AclEntityRuleTransfer::ID_ACL_ROLE => $merchant1RoleTransfer->getIdAclRole(),
602
                AclEntityRuleTransfer::ID_ACL_ENTITY_SEGMENT => $segmentMerchant3Transfer->getIdAclEntitySegment(),
603
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_SEGMENT,
604
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
605
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
606
            ],
607
        );
608
609
        $rolesTransfer = (new RolesTransfer())
610
            ->addRole($merchant1RoleTransfer)
611
            ->addRole($merchant2RoleTransfer)
612
            ->addRole($merchant3RoleTransfer);
613
        $aclQueryDirector = $this->tester->createAclQueryDirector(
614
            $rolesTransfer,
615
            $this->tester->createProductOfferMetadataHierarchy(),
616
        );
617
618
        $query = SpyProductOfferQuery::create()->orderByIdProductOffer();
619
620
        // Act
621
        $query = $aclQueryDirector->applyAclRuleOnSelectQuery($query);
622
623
        // Assert
624
        $this->assertSame(3, $query->count());
625
    }
626
627
    /**
628
     * @group AclEntityApplyAclRules
629
     *
630
     * @return void
631
     */
632
    public function testAclAddsProperAliasForAclJoin(): void
633
    {
634
        // Arrange
635
        $roleTransfer = $this->tester->haveRole([RoleTransfer::NAME => AclQueryDirectorTester::ACL_ROLE_1_NAME]);
636
        $aclEntitySegmentTransfer = $this->tester->haveAclEntitySegment(
637
            [
638
                AclEntitySegmentRequestTransfer::NAME => static::ACL_ENTITY_SEGMENT_MERCHANT_1_NAME,
639
                AclEntitySegmentRequestTransfer::REFERENCE => static::ACL_ENTITY_SEGMENT_MERCHANT_1_REFERENCE,
640
                AclEntitySegmentRequestTransfer::ENTITY => SpyMerchant::class,
641
                AclEntitySegmentRequestTransfer::ENTITY_IDS => [],
642
            ],
643
        );
644
645
        $this->tester->haveAclEntityRule(
646
            [
647
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
648
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
649
                AclEntityRuleTransfer::ENTITY => SpyProductAbstract::class,
650
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
651
            ],
652
        );
653
        $this->tester->haveAclEntityRule(
654
            [
655
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
656
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_INHERITED,
657
                AclEntityRuleTransfer::ENTITY => SpyMerchantProductAbstract::class,
658
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
659
            ],
660
        );
661
        $this->tester->haveAclEntityRule(
662
            [
663
                AclEntityRuleTransfer::ID_ACL_ROLE => $roleTransfer->getIdAclRole(),
664
                AclEntityRuleTransfer::SCOPE => AclEntityConstants::SCOPE_SEGMENT,
665
                AclEntityRuleTransfer::ENTITY => SpyMerchant::class,
666
                AclEntityRuleTransfer::PERMISSION_MASK => AclEntityConstants::OPERATION_MASK_READ,
667
                AclEntityRuleTransfer::ID_ACL_ENTITY_SEGMENT => $aclEntitySegmentTransfer->getIdAclEntitySegmentOrFail(),
668
            ],
669
        );
670
671
        $rolesTransfer = (new RolesTransfer())->addRole($roleTransfer);
672
        $aclQueryDirector = $this->tester->createAclQueryDirector(
673
            $rolesTransfer,
674
            $this->tester->createProductAbstractMerchantMetadataHierarchy(),
675
        );
676
        $query = SpyProductAbstractQuery::create();
677
        $join = new ModelJoin();
678
        $join->setLeftTableName(SpyProductAbstractTableMap::TABLE_NAME);
679
        $join->setRightTableName(SpyMerchantProductAbstractTableMap::TABLE_NAME);
680
        $join->setTableMap(SpyMerchantProductAbstractQuery::create()->getTableMap());
681
        $join->setJoinCondition(
682
            (new Criteria())->getNewCriterion(
683
                SpyMerchantProductAbstractTableMap::COL_ID_MERCHANT_PRODUCT_ABSTRACT,
684
                0,
685
                Criteria::GREATER_THAN,
686
            ),
687
        );
688
        $query->addJoinObject($join, 'SpyMerchantProductAbstract');
689
690
        // Act
691
        $query = $aclQueryDirector->applyAclRuleOnSelectQuery($query);
692
693
        // Assert
694
        $this->assertArrayHasKey('spy_merchant_product_abstract_acl', $query->getAliases());
695
        $this->assertArrayHasKey('SpyMerchantProductAbstractAcl', $query->getJoins());
696
        $this->assertSame(
697
            'spy_merchant_product_abstract_acl',
698
            $query->getJoin('SpyMerchantProductAbstractAcl')->getRightTableAlias(),
699
        );
700
    }
701
}
702