Completed
Push — symfony5 ( 3e4e26...1059d0 )
by
unknown
12:24
created

EzPublishCoreExtensionTest   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 800
Duplicated Lines 16.25 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 130
loc 800
rs 9.08
c 0
b 0
f 0
wmc 39
lcom 2
cbo 9

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainerExtensions() 0 4 1
A getMinimalConfiguration() 0 4 1
A setUp() 0 30 1
B testSiteAccessConfiguration() 9 32 6
A testSiteAccessNoConfiguration() 0 9 1
A testImageMagickConfigurationBasic() 3 18 3
A testImageMagickConfigurationFilters() 3 26 3
A testImagePlaceholderConfiguration() 0 31 1
A testRoutingConfiguration() 0 11 1
A testCacheConfiguration() 0 6 1
A cacheConfigurationProvider() 0 30 1
A testCacheConfigurationCustomPurgeService() 0 12 1
A testLocaleConfiguration() 0 7 1
A testRepositoriesConfiguration() 0 47 2
A testRepositoriesConfigurationFieldGroups() 0 14 2
B repositoriesConfigurationFieldGroupsProvider() 0 119 1
A testRepositoriesConfigurationEmpty() 0 34 1
A testRepositoriesConfigurationStorageEmpty() 39 39 1
A testRepositoriesConfigurationSearchEmpty() 39 39 1
B testRepositoriesConfigurationCompatibility() 0 68 1
A testRepositoriesConfigurationCompatibility2() 37 37 1
A testRegisteredPolicies() 0 51 1
A testUrlAliasConfiguration() 0 33 1
A testQueryTypeAutomaticConfiguration() 0 15 1
A compileCoreContainer() 0 9 1
A disableCheckExceptionOnInvalidReferenceBehaviorPass() 0 12 1
A getCoreExtension() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the EzPublishCoreExtensionTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\QueryTypePass;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\Common;
13
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\Content;
14
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension;
15
use eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Stub\QueryTypeBundle\QueryType\TestQueryType;
16
use eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Stub\StubPolicyProvider;
17
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
18
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\Yaml\Yaml;
22
use ReflectionObject;
23
24
class EzPublishCoreExtensionTest extends AbstractExtensionTestCase
25
{
26
    private $minimalConfig = [];
27
28
    private $siteaccessConfig = [];
29
30
    /** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension */
31
    private $extension;
32
33
    protected function setUp(): void
34
    {
35
        parent::setUp();
36
37
        $this->siteaccessConfig = [
38
            'siteaccess' => [
39
                'default_siteaccess' => 'ezdemo_site',
40
                'list' => ['ezdemo_site', 'eng', 'fre', 'ezdemo_site_admin'],
41
                'groups' => [
42
                    'ezdemo_group' => ['ezdemo_site', 'eng', 'fre', 'ezdemo_site_admin'],
43
                    'ezdemo_frontend_group' => ['ezdemo_site', 'eng', 'fre'],
44
                    'empty_group' => [],
45
                ],
46
                'match' => [
47
                    'URILElement' => 1,
48
                    'Map\URI' => ['the_front' => 'ezdemo_site', 'the_back' => 'ezdemo_site_admin'],
49
                ],
50
            ],
51
            'system' => [
52
                'ezdemo_site' => [],
53
                'eng' => [],
54
                'fre' => [],
55
                'ezdemo_site_admin' => [],
56
                'empty_group' => ['var_dir' => 'foo'],
57
            ],
58
        ];
59
60
        // Parameters needed by EzPublishCoreExtension
61
        $this->container->setParameter('kernel.environment', 'test');
62
    }
63
64
    protected function getContainerExtensions(): array
65
    {
66
        return [EzPublishCoreExtension::class => $this->getCoreExtension()];
67
    }
68
69
    protected function getMinimalConfiguration(): array
70
    {
71
        return $this->minimalConfig = Yaml::parse(file_get_contents(__DIR__ . '/Fixtures/ezpublish_minimal_no_siteaccess.yml'));
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\...al_no_siteaccess.yml')) of type * is incompatible with the declared type array of property $minimalConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
    }
73
74
    public function testSiteAccessConfiguration()
75
    {
76
        $this->load($this->siteaccessConfig);
77
        $this->assertContainerBuilderHasParameter(
78
            'ezpublish.siteaccess.list',
79
            $this->siteaccessConfig['siteaccess']['list']
80
        );
81
        $this->assertContainerBuilderHasParameter(
82
            'ezpublish.siteaccess.default',
83
            $this->siteaccessConfig['siteaccess']['default_siteaccess']
84
        );
85
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups', $this->siteaccessConfig['siteaccess']['groups']);
86
87
        $expectedMatchingConfig = [];
88
        foreach ($this->siteaccessConfig['siteaccess']['match'] as $key => $val) {
89
            // Value is expected to always be an array (transformed by semantic configuration parser).
90
            $expectedMatchingConfig[$key] = is_array($val) ? $val : ['value' => $val];
91
        }
92
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.match_config', $expectedMatchingConfig);
93
        $this->assertContainerBuilderHasParameter('ezsettings.empty_group.var_dir', 'foo');
94
95
        $groupsBySiteaccess = [];
96 View Code Duplication
        foreach ($this->siteaccessConfig['siteaccess']['groups'] as $groupName => $groupMembers) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            foreach ($groupMembers as $member) {
98
                if (!isset($groupsBySiteaccess[$member])) {
99
                    $groupsBySiteaccess[$member] = [];
100
                }
101
102
                $groupsBySiteaccess[$member][] = $groupName;
103
            }
104
        }
105
    }
106
107
    public function testSiteAccessNoConfiguration()
108
    {
109
        $this->load();
110
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.list', ['setup']);
111
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.default', 'setup');
112
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups', []);
113
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups_by_siteaccess', []);
114
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.match_config', null);
115
    }
116
117
    public function testImageMagickConfigurationBasic()
118
    {
119 View Code Duplication
        if (!isset($_ENV['imagemagickConvertPath']) || !is_executable($_ENV['imagemagickConvertPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            $this->markTestSkipped('Missing or mis-configured Imagemagick convert path.');
121
        }
122
123
        $this->load(
124
            [
125
                'imagemagick' => [
126
                    'enabled' => true,
127
                    'path' => $_ENV['imagemagickConvertPath'],
128
                ],
129
            ]
130
        );
131
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.enabled', true);
132
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.executable_path', dirname($_ENV['imagemagickConvertPath']));
133
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.executable', basename($_ENV['imagemagickConvertPath']));
134
    }
135
136
    public function testImageMagickConfigurationFilters()
137
    {
138 View Code Duplication
        if (!isset($_ENV['imagemagickConvertPath']) || !is_executable($_ENV['imagemagickConvertPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            $this->markTestSkipped('Missing or mis-configured Imagemagick convert path.');
140
        }
141
142
        $customFilters = [
143
            'foobar' => '-foobar',
144
            'wow' => '-amazing',
145
        ];
146
        $this->load(
147
            [
148
                'imagemagick' => [
149
                    'enabled' => true,
150
                    'path' => $_ENV['imagemagickConvertPath'],
151
                    'filters' => $customFilters,
152
                ],
153
            ]
154
        );
155
        $this->assertTrue($this->container->hasParameter('ezpublish.image.imagemagick.filters'));
156
        $filters = $this->container->getParameter('ezpublish.image.imagemagick.filters');
157
        $this->assertArrayHasKey('foobar', $filters);
158
        $this->assertSame($customFilters['foobar'], $filters['foobar']);
159
        $this->assertArrayHasKey('wow', $filters);
160
        $this->assertSame($customFilters['wow'], $filters['wow']);
161
    }
162
163
    public function testImagePlaceholderConfiguration()
164
    {
165
        $this->load([
166
            'image_placeholder' => [
167
                'default' => [
168
                    'provider' => 'generic',
169
                    'options' => [
170
                        'foo' => 'Foo',
171
                        'bar' => 'Bar',
172
                    ],
173
                ],
174
                'fancy' => [
175
                    'provider' => 'remote',
176
                ],
177
            ],
178
        ]);
179
180
        $this->assertEquals([
181
            'default' => [
182
                'provider' => 'generic',
183
                'options' => [
184
                    'foo' => 'Foo',
185
                    'bar' => 'Bar',
186
                ],
187
            ],
188
            'fancy' => [
189
                'provider' => 'remote',
190
                'options' => [],
191
            ],
192
        ], $this->container->getParameter('image_alias.placeholder_providers'));
193
    }
194
195
    public function testRoutingConfiguration()
196
    {
197
        $this->load();
198
        $this->assertContainerBuilderHasAlias('router', 'ezpublish.chain_router');
199
200
        $this->assertTrue($this->container->hasParameter('ezpublish.default_router.non_siteaccess_aware_routes'));
201
        $nonSiteaccessAwareRoutes = $this->container->getParameter('ezpublish.default_router.non_siteaccess_aware_routes');
202
        // See ezpublish_minimal_no_siteaccess.yml fixture
203
        $this->assertContains('foo_route', $nonSiteaccessAwareRoutes);
204
        $this->assertContains('my_prefix_', $nonSiteaccessAwareRoutes);
205
    }
206
207
    /**
208
     * @dataProvider cacheConfigurationProvider
209
     *
210
     * @param array $customCacheConfig
211
     * @param string $expectedPurgeType
212
     */
213
    public function testCacheConfiguration(array $customCacheConfig, $expectedPurgeType)
214
    {
215
        $this->load($customCacheConfig);
216
217
        $this->assertContainerBuilderHasParameter('ezpublish.http_cache.purge_type', $expectedPurgeType);
218
    }
219
220
    public function cacheConfigurationProvider()
221
    {
222
        return [
223
            [[], 'local'],
224
            [
225
                [
226
                    'http_cache' => ['purge_type' => 'local'],
227
                ],
228
                'local',
229
            ],
230
            [
231
                [
232
                    'http_cache' => ['purge_type' => 'multiple_http'],
233
                ],
234
                'http',
235
            ],
236
            [
237
                [
238
                    'http_cache' => ['purge_type' => 'single_http'],
239
                ],
240
                'http',
241
            ],
242
            [
243
                [
244
                    'http_cache' => ['purge_type' => 'http'],
245
                ],
246
                'http',
247
            ],
248
        ];
249
    }
250
251
    public function testCacheConfigurationCustomPurgeService()
252
    {
253
        $serviceId = 'foobar';
254
        $this->setDefinition($serviceId, new Definition());
255
        $this->load(
256
            [
257
                'http_cache' => ['purge_type' => 'foobar', 'timeout' => 12],
258
            ]
259
        );
260
261
        $this->assertContainerBuilderHasParameter('ezpublish.http_cache.purge_type', 'foobar');
262
    }
263
264
    public function testLocaleConfiguration()
265
    {
266
        $this->load(['locale_conversion' => ['foo' => 'bar']]);
267
        $conversionMap = $this->container->getParameter('ezpublish.locale.conversion_map');
268
        $this->assertArrayHasKey('foo', $conversionMap);
269
        $this->assertSame('bar', $conversionMap['foo']);
270
    }
271
272
    public function testRepositoriesConfiguration()
273
    {
274
        $repositories = [
275
            'main' => [
276
                'storage' => [
277
                    'engine' => 'legacy',
278
                    'connection' => 'default',
279
                ],
280
                'search' => [
281
                    'engine' => 'legacy',
282
                    'connection' => 'blabla',
283
                ],
284
                'fields_groups' => [
285
                    'list' => ['content', 'metadata'],
286
                    'default' => '%ezsettings.default.content.field_groups.default%',
287
                ],
288
                'options' => [
289
                    'default_version_archive_limit' => 5,
290
                ],
291
            ],
292
            'foo' => [
293
                'storage' => [
294
                    'engine' => 'sqlng',
295
                    'connection' => 'default',
296
                ],
297
                'search' => [
298
                    'engine' => 'solr',
299
                    'connection' => 'lalala',
300
                ],
301
                'fields_groups' => [
302
                    'list' => ['content', 'metadata'],
303
                    'default' => '%ezsettings.default.content.field_groups.default%',
304
                ],
305
                'options' => [
306
                    'default_version_archive_limit' => 5,
307
                ],
308
            ],
309
        ];
310
        $this->load(['repositories' => $repositories]);
311
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
312
313
        foreach ($repositories as &$repositoryConfig) {
314
            $repositoryConfig['storage']['config'] = [];
315
            $repositoryConfig['search']['config'] = [];
316
        }
317
        $this->assertSame($repositories, $this->container->getParameter('ezpublish.repositories'));
318
    }
319
320
    /**
321
     * @dataProvider repositoriesConfigurationFieldGroupsProvider
322
     */
323
    public function testRepositoriesConfigurationFieldGroups($repositories, $expectedRepositories)
324
    {
325
        $this->load(['repositories' => $repositories]);
326
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
327
328
        $repositoriesPar = $this->container->getParameter('ezpublish.repositories');
329
        $this->assertEquals(count($repositories), count($repositoriesPar));
330
331
        foreach ($repositoriesPar as $key => $repo) {
332
            $this->assertArrayHasKey($key, $expectedRepositories);
333
            $this->assertArrayHasKey('fields_groups', $repo);
334
            $this->assertEqualsCanonicalizing($expectedRepositories[$key]['fields_groups'], $repo['fields_groups'], 'Invalid fields groups element');
335
        }
336
    }
337
338
    public function repositoriesConfigurationFieldGroupsProvider()
339
    {
340
        return [
341
            //empty config
342
            [
343
                ['main' => null],
344
                ['main' => [
345
                        'fields_groups' => [
346
                            'list' => ['content', 'metadata'],
347
                            'default' => '%ezsettings.default.content.field_groups.default%',
348
                        ],
349
                    ],
350
                ],
351
            ],
352
            //single item with custom fields
353
            [
354
                ['foo' => [
355
                        'fields_groups' => [
356
                            'list' => ['bar', 'baz', 'john'],
357
                            'default' => 'bar',
358
                        ],
359
                    ],
360
                ],
361
                ['foo' => [
362
                        'fields_groups' => [
363
                            'list' => ['bar', 'baz', 'john'],
364
                            'default' => 'bar',
365
                        ],
366
                    ],
367
                ],
368
            ],
369
            //mixed item with custom config and empty item
370
            [
371
                [
372
                    'foo' => [
373
                        'fields_groups' => [
374
                            'list' => ['bar', 'baz', 'john', 'doe'],
375
                            'default' => 'bar',
376
                        ],
377
                    ],
378
                    'anotherone' => null,
379
                ],
380
                [
381
                    'foo' => [
382
                        'fields_groups' => [
383
                            'list' => ['bar', 'baz', 'john', 'doe'],
384
                            'default' => 'bar',
385
                        ],
386
                    ],
387
                    'anotherone' => [
388
                        'fields_groups' => [
389
                            'list' => ['content', 'metadata'],
390
                            'default' => '%ezsettings.default.content.field_groups.default%',
391
                        ],
392
                    ],
393
                ],
394
            ],
395
            //items with only one field configured
396
            [
397
                [
398
                    'foo' => [
399
                        'fields_groups' => [
400
                            'list' => ['bar', 'baz', 'john'],
401
                        ],
402
                    ],
403
                    'bar' => [
404
                        'fields_groups' => [
405
                            'default' => 'metadata',
406
                        ],
407
                    ],
408
                ],
409
                [
410
                    'foo' => [
411
                        'fields_groups' => [
412
                            'list' => ['bar', 'baz', 'john'],
413
                            'default' => '%ezsettings.default.content.field_groups.default%',
414
                        ],
415
                    ],
416
                    'bar' => [
417
                        'fields_groups' => [
418
                            'list' => ['content', 'metadata'],
419
                            'default' => 'metadata',
420
                        ],
421
                    ],
422
                ],
423
            ],
424
            //two different repositories
425
            [
426
                [
427
                    'foo' => [
428
                        'fields_groups' => [
429
                            'list' => ['bar', 'baz', 'john', 'doe'],
430
                            'default' => 'bar',
431
                        ],
432
                    ],
433
                    'bar' => [
434
                        'fields_groups' => [
435
                            'list' => ['lorem', 'ipsum'],
436
                            'default' => 'lorem',
437
                        ],
438
                    ],
439
                ],
440
                [
441
                    'foo' => [
442
                        'fields_groups' => [
443
                            'list' => ['bar', 'baz', 'john', 'doe'],
444
                            'default' => 'bar',
445
                        ],
446
                    ],
447
                    'bar' => [
448
                        'fields_groups' => [
449
                            'list' => ['lorem', 'ipsum'],
450
                            'default' => 'lorem',
451
                        ],
452
                    ],
453
                ],
454
            ],
455
        ];
456
    }
457
458
    public function testRepositoriesConfigurationEmpty()
459
    {
460
        $repositories = [
461
            'main' => null,
462
        ];
463
        $expectedRepositories = [
464
            'main' => [
465
                'storage' => [
466
                    'engine' => '%ezpublish.api.storage_engine.default%',
467
                    'connection' => null,
468
                    'config' => [],
469
                ],
470
                'search' => [
471
                    'engine' => '%ezpublish.api.search_engine.default%',
472
                    'connection' => null,
473
                    'config' => [],
474
                ],
475
                'fields_groups' => [
476
                    'list' => ['content', 'metadata'],
477
                    'default' => '%ezsettings.default.content.field_groups.default%',
478
                ],
479
                'options' => [
480
                    'default_version_archive_limit' => 5,
481
                ],
482
            ],
483
        ];
484
        $this->load(['repositories' => $repositories]);
485
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
486
487
        $this->assertSame(
488
            $expectedRepositories,
489
            $this->container->getParameter('ezpublish.repositories')
490
        );
491
    }
492
493 View Code Duplication
    public function testRepositoriesConfigurationStorageEmpty()
494
    {
495
        $repositories = [
496
            'main' => [
497
                'search' => [
498
                    'engine' => 'fantasticfind',
499
                    'connection' => 'french',
500
                ],
501
            ],
502
        ];
503
        $expectedRepositories = [
504
            'main' => [
505
                'search' => [
506
                    'engine' => 'fantasticfind',
507
                    'connection' => 'french',
508
                    'config' => [],
509
                ],
510
                'storage' => [
511
                    'engine' => '%ezpublish.api.storage_engine.default%',
512
                    'connection' => null,
513
                    'config' => [],
514
                ],
515
                'fields_groups' => [
516
                    'list' => ['content', 'metadata'],
517
                    'default' => '%ezsettings.default.content.field_groups.default%',
518
                ],
519
                'options' => [
520
                    'default_version_archive_limit' => 5,
521
                ],
522
            ],
523
        ];
524
        $this->load(['repositories' => $repositories]);
525
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
526
527
        $this->assertSame(
528
            $expectedRepositories,
529
            $this->container->getParameter('ezpublish.repositories')
530
        );
531
    }
532
533 View Code Duplication
    public function testRepositoriesConfigurationSearchEmpty()
534
    {
535
        $repositories = [
536
            'main' => [
537
                'storage' => [
538
                    'engine' => 'persistentprudence',
539
                    'connection' => 'yes',
540
                ],
541
            ],
542
        ];
543
        $expectedRepositories = [
544
            'main' => [
545
                'storage' => [
546
                    'engine' => 'persistentprudence',
547
                    'connection' => 'yes',
548
                    'config' => [],
549
                ],
550
                'search' => [
551
                    'engine' => '%ezpublish.api.search_engine.default%',
552
                    'connection' => null,
553
                    'config' => [],
554
                ],
555
                'fields_groups' => [
556
                    'list' => ['content', 'metadata'],
557
                    'default' => '%ezsettings.default.content.field_groups.default%',
558
                ],
559
                'options' => [
560
                    'default_version_archive_limit' => 5,
561
                ],
562
            ],
563
        ];
564
        $this->load(['repositories' => $repositories]);
565
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
566
567
        $this->assertSame(
568
            $expectedRepositories,
569
            $this->container->getParameter('ezpublish.repositories')
570
        );
571
    }
572
573
    public function testRepositoriesConfigurationCompatibility()
574
    {
575
        $repositories = [
576
            'main' => [
577
                'engine' => 'legacy',
578
                'connection' => 'default',
579
                'search' => [
580
                    'engine' => 'legacy',
581
                    'connection' => 'blabla',
582
                ],
583
            ],
584
            'foo' => [
585
                'engine' => 'sqlng',
586
                'connection' => 'default',
587
                'search' => [
588
                    'engine' => 'solr',
589
                    'connection' => 'lalala',
590
                ],
591
            ],
592
        ];
593
        $expectedRepositories = [
594
            'main' => [
595
                'search' => [
596
                    'engine' => 'legacy',
597
                    'connection' => 'blabla',
598
                    'config' => [],
599
                ],
600
                'storage' => [
601
                    'engine' => 'legacy',
602
                    'connection' => 'default',
603
                    'config' => [],
604
                ],
605
                'fields_groups' => [
606
                    'list' => ['content', 'metadata'],
607
                    'default' => '%ezsettings.default.content.field_groups.default%',
608
                ],
609
                'options' => [
610
                    'default_version_archive_limit' => 5,
611
                ],
612
            ],
613
            'foo' => [
614
                'search' => [
615
                    'engine' => 'solr',
616
                    'connection' => 'lalala',
617
                    'config' => [],
618
                ],
619
                'storage' => [
620
                    'engine' => 'sqlng',
621
                    'connection' => 'default',
622
                    'config' => [],
623
                ],
624
                'fields_groups' => [
625
                    'list' => ['content', 'metadata'],
626
                    'default' => '%ezsettings.default.content.field_groups.default%',
627
                ],
628
                'options' => [
629
                    'default_version_archive_limit' => 5,
630
                ],
631
            ],
632
        ];
633
        $this->load(['repositories' => $repositories]);
634
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
635
636
        $this->assertSame(
637
            $expectedRepositories,
638
            $this->container->getParameter('ezpublish.repositories')
639
        );
640
    }
641
642 View Code Duplication
    public function testRepositoriesConfigurationCompatibility2()
643
    {
644
        $repositories = [
645
            'main' => [
646
                'engine' => 'legacy',
647
                'connection' => 'default',
648
            ],
649
        ];
650
        $expectedRepositories = [
651
            'main' => [
652
                'storage' => [
653
                    'engine' => 'legacy',
654
                    'connection' => 'default',
655
                    'config' => [],
656
                ],
657
                'search' => [
658
                    'engine' => '%ezpublish.api.search_engine.default%',
659
                    'connection' => null,
660
                    'config' => [],
661
                ],
662
                'fields_groups' => [
663
                    'list' => ['content', 'metadata'],
664
                    'default' => '%ezsettings.default.content.field_groups.default%',
665
                ],
666
                'options' => [
667
                    'default_version_archive_limit' => 5,
668
                ],
669
            ],
670
        ];
671
        $this->load(['repositories' => $repositories]);
672
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
673
674
        $this->assertSame(
675
            $expectedRepositories,
676
            $this->container->getParameter('ezpublish.repositories')
677
        );
678
    }
679
680
    public function testRegisteredPolicies()
681
    {
682
        $this->load();
683
        self::assertContainerBuilderHasParameter('ezpublish.api.role.policy_map');
684
        $previousPolicyMap = $this->container->getParameter('ezpublish.api.role.policy_map');
685
686
        $policies1 = [
687
            'custom_module' => [
688
                'custom_function_1' => null,
689
                'custom_function_2' => ['CustomLimitation'],
690
            ],
691
            'helloworld' => [
692
                'foo' => ['bar'],
693
                'baz' => null,
694
            ],
695
        ];
696
        $this->extension->addPolicyProvider(new StubPolicyProvider($policies1));
697
698
        $policies2 = [
699
            'custom_module2' => [
700
                'custom_function_3' => null,
701
                'custom_function_4' => ['CustomLimitation2', 'CustomLimitation3'],
702
            ],
703
            'helloworld' => [
704
                'foo' => ['additional_limitation'],
705
                'some' => ['thingy', 'thing', 'but', 'wait'],
706
            ],
707
        ];
708
        $this->extension->addPolicyProvider(new StubPolicyProvider($policies2));
709
710
        $expectedPolicies = [
711
            'custom_module' => [
712
                'custom_function_1' => [],
713
                'custom_function_2' => ['CustomLimitation' => true],
714
            ],
715
            'helloworld' => [
716
                'foo' => ['bar' => true, 'additional_limitation' => true],
717
                'baz' => [],
718
                'some' => ['thingy' => true, 'thing' => true, 'but' => true, 'wait' => true],
719
            ],
720
            'custom_module2' => [
721
                'custom_function_3' => [],
722
                'custom_function_4' => ['CustomLimitation2' => true, 'CustomLimitation3' => true],
723
            ],
724
        ];
725
726
        $this->load();
727
        self::assertContainerBuilderHasParameter('ezpublish.api.role.policy_map');
728
        $expectedPolicies = array_merge_recursive($expectedPolicies, $previousPolicyMap);
729
        self::assertEquals($expectedPolicies, $this->container->getParameter('ezpublish.api.role.policy_map'));
730
    }
731
732
    public function testUrlAliasConfiguration()
733
    {
734
        $configuration = [
735
            'transformation' => 'urlalias_lowercase',
736
            'separator' => 'dash',
737
            'transformation_groups' => [
738
                'urlalias' => [
739
                    'commands' => [
740
                        'ascii_lowercase',
741
                        'cyrillic_lowercase',
742
                    ],
743
                    'cleanup_method' => 'url_cleanup',
744
                ],
745
                'urlalias_compact' => [
746
                    'commands' => [
747
                        'greek_normalize',
748
                        'exta_lowercase',
749
                    ],
750
                    'cleanup_method' => 'compact_cleanup',
751
                ],
752
            ],
753
        ];
754
        $this->load([
755
            'url_alias' => [
756
                'slug_converter' => $configuration,
757
            ],
758
        ]);
759
        $parsedConfig = $this->container->getParameter('ezpublish.url_alias.slug_converter');
760
        $this->assertSame(
761
            $configuration,
762
            $parsedConfig
763
        );
764
    }
765
766
    /**
767
     * Test automatic configuration of services implementing QueryType interface.
768
     *
769
     * @see \eZ\Publish\Core\QueryType\QueryType
770
     */
771
    public function testQueryTypeAutomaticConfiguration(): void
772
    {
773
        $definition = new Definition(TestQueryType::class);
774
        $definition->setAutoconfigured(true);
775
        $this->setDefinition(TestQueryType::class, $definition);
776
777
        $this->load();
778
779
        $this->compileCoreContainer();
780
781
        $this->assertContainerBuilderHasServiceDefinitionWithTag(
782
            TestQueryType::class,
783
            QueryTypePass::QUERY_TYPE_SERVICE_TAG
784
        );
785
    }
786
787
    /**
788
     * Prepare Core Container for compilation by mocking required parameters and compile it.
789
     */
790
    private function compileCoreContainer(): void
791
    {
792
        $this->disableCheckExceptionOnInvalidReferenceBehaviorPass();
793
        $this->container->setParameter('webroot_dir', __DIR__);
794
        $this->container->setParameter('kernel.project_dir', __DIR__);
795
        $this->container->setParameter('kernel.cache_dir', __DIR__ . '/cache');
796
        $this->container->setParameter('kernel.debug', false);
797
        $this->compile();
798
    }
799
800
    final public function disableCheckExceptionOnInvalidReferenceBehaviorPass(): void
801
    {
802
        $compilerPassConfig = $this->container->getCompilerPassConfig();
803
        $compilerPassConfig->setAfterRemovingPasses(
804
            array_filter(
805
                $compilerPassConfig->getAfterRemovingPasses(),
806
                static function (CompilerPassInterface $pass) {
807
                    return !($pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass);
808
                }
809
            )
810
        );
811
    }
812
813
    protected function getCoreExtension(): EzPublishCoreExtension
814
    {
815
        if (null !== $this->extension) {
816
            return $this->extension;
817
        }
818
819
        $this->extension = new EzPublishCoreExtension([new Common(), new Content()]);
820
821
        return $this->extension;
822
    }
823
}
824