Issues (3640)

Plugin/AclEntityRuleDataImportPluginTest.php (5 issues)

1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerTest\Zed\AclEntityDataImport\Communication\Plugin;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\Transfer\AclRoleCriteriaTransfer;
12
use Generated\Shared\Transfer\DataImporterConfigurationTransfer;
13
use Generated\Shared\Transfer\DataImporterReaderConfigurationTransfer;
14
use Generated\Shared\Transfer\DataImporterReportTransfer;
15
use Generated\Shared\Transfer\RoleTransfer;
16
use Spryker\Shared\AclEntity\AclEntityConstants;
17
use Spryker\Zed\AclEntityDataImport\AclEntityDataImportConfig;
18
use Spryker\Zed\AclEntityDataImport\Communication\Plugin\AclEntityRuleDataImportPlugin;
19
use Spryker\Zed\DataImport\Business\Exception\DataImportException;
20
21
/**
22
 * Auto-generated group annotations
23
 *
24
 * @group SprykerTest
25
 * @group Zed
26
 * @group AclEntityDataImport
27
 * @group Communication
28
 * @group Plugin
29
 * @group AclEntityRuleDataImportPluginTest
30
 * Add your own group annotations below this line
31
 */
32
class AclEntityRuleDataImportPluginTest extends Unit
33
{
34
    /**
35
     * @var int
36
     */
37
    protected const EXPECTED_IMPORT_COUNT = 5;
38
39
    /**
40
     * @var string
41
     */
42
    protected const ACL_ROLE_REFERENCE_1 = 'GK9rS4jIzVP3Jq71iqNY';
43
44
    /**
45
     * @var string
46
     */
47
    protected const ACL_ROLE_REFERENCE_2 = 'SIfdvYi3rZwxNKJRehEm';
48
49
    /**
50
     * @var string
51
     */
52
    protected const ACL_ROLE_REFERENCE_3 = 'sCrVK2mGyV77B7uifjPI';
53
54
    /**
55
     * @var string
56
     */
57
    protected const ACL_ROLE_REFERENCE_4 = 'rV3aEVTp66WBE46c1tPN';
58
59
    /**
60
     * @var string
61
     */
62
    protected const ACL_ROLE_REFERENCE_5 = 'e73vG9wnDKI6rzuIJIts';
63
64
    /**
65
     * @var string
66
     */
67
    protected const ACL_ROLE_NAME_1 = 'Role 1';
68
69
    /**
70
     * @var string
71
     */
72
    protected const ACL_ROLE_NAME_2 = 'Role 2';
73
74
    /**
75
     * @var string
76
     */
77
    protected const ACL_ROLE_NAME_3 = 'Role 3';
78
79
    /**
80
     * @var string
81
     */
82
    protected const ACL_ROLE_NAME_4 = 'Role 4';
83
84
    /**
85
     * @var string
86
     */
87
    protected const ACL_ROLE_NAME_5 = 'Role 5';
88
89
    /**
90
     * @var string
91
     */
92
    protected const ACL_ENTITY_RULE_ENTITY_1 = 'Orm\Zed\Company\Persistence\SpyCompany';
93
94
    /**
95
     * @var string
96
     */
97
    protected const ACL_ENTITY_RULE_ENTITY_2 = 'Orm\Zed\Product\Persistence\SpyProductAbstract';
98
99
    /**
100
     * @var string
101
     */
102
    protected const ACL_ENTITY_RULE_ENTITY_3 = 'Orm\Zed\Product\Persistence\SpyProduct';
103
104
    /**
105
     * @var string
106
     */
107
    protected const ACL_ENTITY_RULE_ENTITY_4 = 'Orm\Zed\CmsBlock\Persistence\SpyCmsBlock';
108
109
    /**
110
     * @var string
111
     */
112
    protected const ACL_ENTITY_RULE_ENTITY_5 = 'Orm\Zed\Customer\Persistence\SpyCustomer';
113
114
    /**
115
     * @var string
116
     */
117
    protected const ACL_ENTITY_SEGMENT_REFERENCE_1 = 'sH9qLMZtt6sxWqRJVYib';
118
119
    /**
120
     * @var string
121
     */
122
    protected const ACL_ENTITY_SEGMENT_REFERENCE_2 = '5nIYY1SETa50lSDiwxf8';
123
124
    /**
125
     * @var string
126
     */
127
    protected const ACL_ENTITY_SEGMENT_NAME_1 = 'Segment 1';
128
129
    /**
130
     * @var string
131
     */
132
    protected const ACL_ENTITY_SEGMENT_NAME_2 = 'Segment 2';
133
134
    /**
135
     * @var string
136
     */
137
    protected const ACL_ENTITY_SEGMENT_TARGET_ENTITY = 'Orm\Zed\Merchant\Persistence\SpyMerchant';
138
139
    /**
140
     * @var \SprykerTest\Zed\AclEntityDataImport\AclEntityDataImportCommunicationTester
141
     */
142
    protected $tester;
143
144
    /**
145
     * @return void
146
     */
147
    protected function setUp(): void
148
    {
149
        parent::setUp();
150
151
        $this->deleteAclRoles();
152
    }
153
154
    /**
155
     * @return void
156
     */
157
    public function testImportImportsAclEntityRule(): void
158
    {
159
        // Arrange
160
        $this->generateAclRoles();
161
162
        $configurationTransfer = new DataImporterReaderConfigurationTransfer();
163
        $configurationTransfer->setFileName(codecept_data_dir() . 'import/acl_entity_rule.csv');
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

163
        $configurationTransfer->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . 'import/acl_entity_rule.csv');
Loading history...
164
165
        $dataImporterConfigurationTransfer = (new DataImporterConfigurationTransfer())
166
            ->setReaderConfiguration($configurationTransfer);
167
168
        // Act
169
        $dataImportPlugin = new AclEntityRuleDataImportPlugin();
170
        $dataImporterReportTransfer = $dataImportPlugin->import($dataImporterConfigurationTransfer);
171
172
        // Assert
173
        $this->assertInstanceOf(DataImporterReportTransfer::class, $dataImporterReportTransfer);
174
        $this->assertTrue($dataImporterReportTransfer->getIsSuccess());
175
        $this->assertSame(static::EXPECTED_IMPORT_COUNT, $dataImporterReportTransfer->getImportedDataSetCount());
176
177
        $aclRoleEntity1 = $this->tester->getAclRole(static::ACL_ROLE_REFERENCE_1);
178
        $aclRoleEntity2 = $this->tester->getAclRole(static::ACL_ROLE_REFERENCE_2);
179
        $aclRoleEntity3 = $this->tester->getAclRole(static::ACL_ROLE_REFERENCE_3);
180
        $aclRoleEntity4 = $this->tester->getAclRole(static::ACL_ROLE_REFERENCE_4);
181
        $aclRoleEntity5 = $this->tester->getAclRole(static::ACL_ROLE_REFERENCE_5);
182
183
        $aclEntityRuleEntity1 = $this->tester->getAclEntityRule(
184
            $aclRoleEntity1->getIdAclRole(),
185
            static::ACL_ENTITY_RULE_ENTITY_1,
186
            AclEntityConstants::SCOPE_GLOBAL,
187
        );
188
        $aclEntityRuleEntity2 = $this->tester->getAclEntityRule(
189
            $aclRoleEntity2->getIdAclRole(),
190
            static::ACL_ENTITY_RULE_ENTITY_2,
191
            AclEntityConstants::SCOPE_GLOBAL,
192
        );
193
        $aclEntityRuleEntity3 = $this->tester->getAclEntityRule(
194
            $aclRoleEntity3->getIdAclRole(),
195
            static::ACL_ENTITY_RULE_ENTITY_3,
196
            AclEntityConstants::SCOPE_GLOBAL,
197
        );
198
        $aclEntityRuleEntity4 = $this->tester->getAclEntityRule(
199
            $aclRoleEntity4->getIdAclRole(),
200
            static::ACL_ENTITY_RULE_ENTITY_4,
201
            AclEntityConstants::SCOPE_GLOBAL,
202
        );
203
        $aclEntityRuleEntity5 = $this->tester->getAclEntityRule(
204
            $aclRoleEntity5->getIdAclRole(),
205
            static::ACL_ENTITY_RULE_ENTITY_5,
206
            AclEntityConstants::SCOPE_GLOBAL,
207
        );
208
209
        $this->assertSame(AclEntityConstants::OPERATION_MASK_CREATE, $aclEntityRuleEntity1->getPermissionMask());
210
        $this->assertSame(AclEntityConstants::OPERATION_MASK_READ, $aclEntityRuleEntity2->getPermissionMask());
211
        $this->assertSame(AclEntityConstants::OPERATION_MASK_UPDATE, $aclEntityRuleEntity3->getPermissionMask());
212
        $this->assertSame(AclEntityConstants::OPERATION_MASK_DELETE, $aclEntityRuleEntity4->getPermissionMask());
213
        $this->assertSame(
214
            AclEntityConstants::OPERATION_MASK_CREATE
215
                | AclEntityConstants::OPERATION_MASK_READ
216
                | AclEntityConstants::OPERATION_MASK_UPDATE
217
                | AclEntityConstants::OPERATION_MASK_DELETE,
218
            $aclEntityRuleEntity5->getPermissionMask(),
219
        );
220
    }
221
222
    /**
223
     * @return void
224
     */
225
    public function testImportImportsAclEntityRuleWithIncorrectRoleReference(): void
226
    {
227
        // Assert
228
        $this->expectException(DataImportException::class);
229
        $this->expectExceptionMessage('Could not find AclRole by reference: "incorrectReference"');
230
231
        // Arrange
232
        $this->generateAclRoles();
233
234
        $configurationTransfer = new DataImporterReaderConfigurationTransfer();
235
        $configurationTransfer->setFileName(codecept_data_dir() . 'import/acl_entity_rule_incorrect_role_reference.csv');
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

235
        $configurationTransfer->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . 'import/acl_entity_rule_incorrect_role_reference.csv');
Loading history...
236
237
        $dataImporterConfigurationTransfer = (new DataImporterConfigurationTransfer())
238
            ->setReaderConfiguration($configurationTransfer)
239
            ->setThrowException(true);
240
241
        // Act
242
        $dataImportPlugin = new AclEntityRuleDataImportPlugin();
243
        $dataImportPlugin->import($dataImporterConfigurationTransfer);
244
    }
245
246
    /**
247
     * @return void
248
     */
249
    public function testImportImportsAclEntityRuleWithIncorrectSegmentReference(): void
250
    {
251
        // Assert
252
        $this->expectException(DataImportException::class);
253
        $this->expectExceptionMessage('Could not find AclEntitySegment by reference: "incorrectReference"');
254
255
        // Arrange
256
        $this->generateAclRoles();
257
258
        $configurationTransfer = new DataImporterReaderConfigurationTransfer();
259
        $configurationTransfer->setFileName(codecept_data_dir() . 'import/acl_entity_rule_incorrect_segment_reference.csv');
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

259
        $configurationTransfer->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . 'import/acl_entity_rule_incorrect_segment_reference.csv');
Loading history...
260
261
        $dataImporterConfigurationTransfer = (new DataImporterConfigurationTransfer())
262
            ->setReaderConfiguration($configurationTransfer)
263
            ->setThrowException(true);
264
265
        // Act
266
        $dataImportPlugin = new AclEntityRuleDataImportPlugin();
267
        $dataImportPlugin->import($dataImporterConfigurationTransfer);
268
    }
269
270
    /**
271
     * @dataProvider importImportsAclEntityRuleWithEmptyRequiredFieldProvider
272
     *
273
     * @param string $importFile
274
     * @param string $expectedExceptionMessage
275
     *
276
     * @return void
277
     */
278
    public function testImportImportsAclEntityRuleWithEmptyRequiredField(string $importFile, string $expectedExceptionMessage): void
279
    {
280
        // Arrange
281
        $this->generateAclRoles();
282
        $configurationTransfer = new DataImporterReaderConfigurationTransfer();
283
        $configurationTransfer->setFileName(codecept_data_dir() . $importFile);
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

283
        $configurationTransfer->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . $importFile);
Loading history...
284
285
        $dataImporterConfigurationTransfer = (new DataImporterConfigurationTransfer())
286
            ->setReaderConfiguration($configurationTransfer)
287
            ->setThrowException(true);
288
289
        // Assert
290
        $this->expectException(DataImportException::class);
291
        $this->expectExceptionMessage($expectedExceptionMessage);
292
293
        // Act
294
        (new AclEntityRuleDataImportPlugin())->import($dataImporterConfigurationTransfer);
295
    }
296
297
    /**
298
     * @return array<array<\string>>
299
     */
300
    public function importImportsAclEntityRuleWithEmptyRequiredFieldProvider(): array
301
    {
302
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array('impo...ope" cannot be empty')) returns the type array<integer,array<integer,string>> which is incompatible with the documented return type array<mixed,string[]>.
Loading history...
303
            ['import/acl_entity_rule_empty_acl_role_reference.csv', 'Field "acl_role_reference" cannot be empty'],
304
            ['import/acl_entity_rule_empty_entity.csv', 'Field "entity" cannot be empty'],
305
            ['import/acl_entity_rule_empty_permission_mask.csv', 'Field "permission_mask" cannot be empty'],
306
            ['import/acl_entity_rule_empty_scope.csv', 'Field "scope" cannot be empty'],
307
        ];
308
    }
309
310
    /**
311
     * @return void
312
     */
313
    public function testGetImportTypeReturnsTypeOfImporter(): void
314
    {
315
        // Arrange
316
        $dataImportPlugin = new AclEntityRuleDataImportPlugin();
317
318
        // Act
319
        $importType = $dataImportPlugin->getImportType();
320
321
        // Assert
322
        $this->assertEquals(AclEntityDataImportConfig::IMPORT_TYPE_ACL_ENTITY_RULE, $importType);
323
    }
324
325
    /**
326
     * @return void
327
     */
328
    protected function generateAclRoles(): void
329
    {
330
        $this->tester->haveRole(
331
            [
332
                RoleTransfer::REFERENCE => static::ACL_ROLE_REFERENCE_1,
333
                RoleTransfer::NAME => static::ACL_ROLE_NAME_1,
334
            ],
335
        );
336
        $this->tester->haveRole(
337
            [
338
                RoleTransfer::REFERENCE => static::ACL_ROLE_REFERENCE_2,
339
                RoleTransfer::NAME => static::ACL_ROLE_NAME_2,
340
            ],
341
        );
342
        $this->tester->haveRole(
343
            [
344
                RoleTransfer::REFERENCE => static::ACL_ROLE_REFERENCE_3,
345
                RoleTransfer::NAME => static::ACL_ROLE_NAME_3,
346
            ],
347
        );
348
        $this->tester->haveRole(
349
            [
350
                RoleTransfer::REFERENCE => static::ACL_ROLE_REFERENCE_4,
351
                RoleTransfer::NAME => static::ACL_ROLE_NAME_4,
352
            ],
353
        );
354
        $this->tester->haveRole(
355
            [
356
                RoleTransfer::REFERENCE => static::ACL_ROLE_REFERENCE_5,
357
                RoleTransfer::NAME => static::ACL_ROLE_NAME_5,
358
            ],
359
        );
360
    }
361
362
    /**
363
     * @return void
364
     */
365
    protected function deleteAclRoles(): void
366
    {
367
        $this->tester->deleteRoles(
368
            (new AclRoleCriteriaTransfer())->setNames(
369
                [
370
                    static::ACL_ROLE_NAME_1,
371
                    static::ACL_ROLE_NAME_2,
372
                    static::ACL_ROLE_NAME_3,
373
                    static::ACL_ROLE_NAME_4,
374
                    static::ACL_ROLE_NAME_5,
375
                ],
376
            ),
377
        );
378
    }
379
380
    /**
381
     * @return void
382
     */
383
    protected function _after(): void
384
    {
385
        parent::_after();
386
387
        $this->deleteAclRoles();
388
        $this->tester->deleteAclEntitySegments();
389
    }
390
}
391