diff --git a/bundle/DependencyInjection/Configuration.php b/bundle/DependencyInjection/Configuration.php index 6b8f3599..a7824c10 100644 --- a/bundle/DependencyInjection/Configuration.php +++ b/bundle/DependencyInjection/Configuration.php @@ -90,29 +90,89 @@ private function addUsePageIndexingSection(ArrayNodeDefinition $nodeDefinition): private function addPageIndexingSection(ArrayNodeDefinition $nodeDefinition): void { + $keyValidator = static function ($v) { + foreach (array_keys($v) as $key) { + if (!is_string($key)) { + return true; + } + } + return false; + }; $nodeDefinition ->children() ->arrayNode('page_indexing') ->info('Page indexing configuration') ->children() ->arrayNode('site_roots') - ->scalarPrototype()->end() - ->info('Location ids of site roots') + ->info('Site root ids') + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->validate() + ->ifTrue($keyValidator) + ->thenInvalid('Site root name must be of string type.') + ->end() + ->integerPrototype() + ->beforeNormalization()->always(static fn ($v) => is_string($v) ? (int)$v : $v)->end() + ->info('Content ID') + ->end() ->end() ->arrayNode('languages_siteaccess_map') + ->info('Language key mapped to page siteaccess') + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->validate() + ->ifTrue($keyValidator) + ->thenInvalid('Page name must be of string type.') + ->end() ->prototype('array') - ->prototype('scalar')->end() + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->validate() + ->ifTrue($keyValidator) + ->thenInvalid('Language code must be of string type.') + ->end() + ->prototype('scalar') + ->validate() + ->ifTrue(static fn ($v) => !is_string($v)) + ->thenInvalid('Siteaccess name must be of string type.') + ->end() + ->end() ->end() ->end() ->scalarNode('host') + ->info('Host to index page from, defined in .env files') + ->validate() + ->ifTrue(static fn ($v) => !is_string($v)) + ->thenInvalid('Host must be of string type.') + ->end() ->end() ->arrayNode('config') + ->info('Config for separating page text by importance of the html tags and classes, used to index content to separate solr fields') + ->validate() + ->ifTrue($keyValidator) + ->thenInvalid('Array key (level of field importance) must be of string type.') + ->end() ->prototype('array') - ->prototype('scalar')->end() + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->scalarPrototype() + ->validate() + ->ifTrue(static fn ($v) => !is_string($v)) + ->thenInvalid('HTML tag and class must be of string type.') + ->end() + ->end() ->end() ->end() ->arrayNode('allowed_content_types') - ->scalarPrototype()->end() + ->info('Content types to index') + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->scalarPrototype() + ->validate() + ->ifTrue(static fn ($v) => !is_string($v)) + ->thenInvalid('Content type identifier must be of string type.') + ->end() + ->end() ->end() ->end() ->end(); diff --git a/bundle/DependencyInjection/NetgenIbexaSearchExtraExtension.php b/bundle/DependencyInjection/NetgenIbexaSearchExtraExtension.php index 23ae1096..0d1df678 100644 --- a/bundle/DependencyInjection/NetgenIbexaSearchExtraExtension.php +++ b/bundle/DependencyInjection/NetgenIbexaSearchExtraExtension.php @@ -128,6 +128,7 @@ private function processUsePageIndexingConfiguration(array $configuration, Conta private function processPageIndexingConfiguration(array $configuration, ContainerBuilder $container): void { + $container->setParameter( 'netgen_ibexa_search_extra.page_indexing.site_roots', $configuration['page_indexing']['site_roots'] ?? [], @@ -138,7 +139,7 @@ private function processPageIndexingConfiguration(array $configuration, Containe ); $container->setParameter( 'netgen_ibexa_search_extra.page_indexing.host', - $configuration['page_indexing']['host'] ?? [], + $configuration['page_indexing']['host'] ?? null, ); $container->setParameter( 'netgen_ibexa_search_extra.page_indexing.config', diff --git a/lib/Resources/config/search/common/layouts_page_text_indexing.yaml b/lib/Resources/config/search/common/layouts_page_text_indexing.yaml index 7a8be31c..2e5809ea 100644 --- a/lib/Resources/config/search/common/layouts_page_text_indexing.yaml +++ b/lib/Resources/config/search/common/layouts_page_text_indexing.yaml @@ -5,10 +5,10 @@ services: - '@Ibexa\Contracts\Core\Persistence\Content\Handler' - '@Ibexa\Contracts\Core\Persistence\Content\Location\Handler' - '@Symfony\Component\Routing\RouterInterface' - - '%netgen.ibexa_search_extra.page_indexing.site_roots%' - - '%netgen.ibexa_search_extra.page_indexing.languages_siteaccess_map%' - - '%netgen.ibexa_search_extra.page_indexing.host%' - - '%netgen.ibexa_search_extra.page_indexing.config%' + - '%netgen_ibexa_search_extra.page_indexing.site_roots%' + - '%netgen_ibexa_search_extra.page_indexing.languages_siteaccess_map%' + - '%netgen_ibexa_search_extra.page_indexing.host%' + - '%netgen_ibexa_search_extra.page_indexing.config%' calls: - setLogger: ['@?logger'] @@ -22,9 +22,9 @@ services: - '@ibexa.api.persistence_handler' - '%netgen.ibexa_search_extra.page_indexing.allowed_content_types%' -parameters: - netgen.ibexa_search_extra.page_indexing.site_roots: [] - netgen.ibexa_search_extra.page_indexing.languages_siteaccess_map: [] - netgen.ibexa_search_extra.page_indexing.host: null - netgen.ibexa_search_extra.page_indexing.config: [] - netgen.ibexa_search_extra.page_indexing.allowed_content_types: [] \ No newline at end of file +#parameters: +# netgen.ibexa_search_extra.page_indexing.site_roots: [] +# netgen.ibexa_search_extra.page_indexing.languages_siteaccess_map: [] +# netgen.ibexa_search_extra.page_indexing.host: null +# netgen.ibexa_search_extra.page_indexing.config: [] +# netgen.ibexa_search_extra.page_indexing.allowed_content_types: [] \ No newline at end of file diff --git a/tests/bundle/DependencyInjection/NetgenIbexaSearchExtraExtensionTest.php b/tests/bundle/DependencyInjection/NetgenIbexaSearchExtraExtensionTest.php index f1a23998..d5f9a697 100644 --- a/tests/bundle/DependencyInjection/NetgenIbexaSearchExtraExtensionTest.php +++ b/tests/bundle/DependencyInjection/NetgenIbexaSearchExtraExtensionTest.php @@ -8,6 +8,8 @@ use Netgen\Bundle\IbexaSearchExtraBundle\DependencyInjection\NetgenIbexaSearchExtraExtension; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; + class NetgenIbexaSearchExtraExtensionTest extends AbstractExtensionTestCase { @@ -91,6 +93,296 @@ public function testIndexableFieldTypeDefaultConfiguration(array $configuration) ); } + + public function providerForPageIndexingConfiguration(): array + { + return [ + [ + [ + 'page_indexing' => [], + ], + [], + [], + null, + [], + [], + ], + [ + [ + 'page_indexing' => [ + 'site_roots' => [ + 'finaweb' => '42', + ], + ], + ], + [ + 'finaweb' => 42, + ], + [], + null, + [], + [], + ], + [ + [ + 'page_indexing' => [ + 'languages_siteaccess_map' => [ + 'finaweb' => [ + 'cro-HR' => 'fina_cro' + ] + ], + ], + ], + [], + [ + 'finaweb' => [ + 'cro-HR' => 'fina_cro' + ] + ], + null, + [], + [] + ], + + [ + [ + 'page_indexing' => [ + 'host' => 'string' + ], + ], + [], + [], + 'string', + [], + [] + ], + + [ + [ + 'page_indexing' => [ + 'config' => [ + 'level1' => [ + 'h1', + 'h2' + ] + ], + ], + ], + [], + [], + null, + [ + 'level1' => [ + 'h1', + 'h2' + ] + ], + [] + ], + + [ + [ + 'page_indexing' =>[ + 'allowed_content_types' => [ + 'ng_landing_page', + 'ng_frontpage' + ], + ] + ], + [], + [], + null, + [], + [ + 'ng_landing_page', + 'ng_frontpage' + ] + ], + + [ + [ + 'page_indexing' => [ + 'site_roots' => [ + 'finaweb' => '42', + ], + 'languages_siteaccess_map' => [ + 'finaweb' => [ + 'cro-HR' => 'fina_cro' + ] + ], + 'host' => 'string', + 'config' => [ + 'level1' => [ + 'h1', + 'h2' + ] + ], + 'allowed_content_types' => [ + 'ng_landing_page', + 'ng_frontpage' + ], + ], + ], + [ + 'finaweb' => 42, + ], + [ + 'finaweb' => [ + 'cro-HR' => 'fina_cro' + ] + ], + 'string', + [ + 'level1' => [ + 'h1', + 'h2' + ] + ], + [ + 'ng_landing_page', + 'ng_frontpage' + ] + ], + ]; + } + + + /** + * @dataProvider providerForPageIndexingConfiguration + */ + public function testPageIndexingConfiguration( + array $configuration, + array $expectedSiteRoots, + array $expectedLanguagesSiteaccessMap, + ?string $expectedHost, + array $expectedConfig, + array $expectedAllowedContentTypes + ): void { + $this->load($configuration); + + $this->assertContainerBuilderHasParameter( + 'netgen_ibexa_search_extra.page_indexing.site_roots', + $expectedSiteRoots, + + ); + $this->assertContainerBuilderHasParameter( + 'netgen_ibexa_search_extra.page_indexing.languages_siteaccess_map', + $expectedLanguagesSiteaccessMap, + ); + + $this->assertContainerBuilderHasParameter( + 'netgen_ibexa_search_extra.page_indexing.host', + $expectedHost, + ); + + $this->assertContainerBuilderHasParameter( + 'netgen_ibexa_search_extra.page_indexing.config', + $expectedConfig, + ); + + $this->assertContainerBuilderHasParameter( + 'netgen_ibexa_search_extra.page_indexing.allowed_content_types', + $expectedAllowedContentTypes + ); + } + + + public function providerForPageIndexingDefaultConfigurationInvalidCases(): array + { + return [ + [ + [ + 'page_indexing' => [ + 'site_roots' => [ + 'finaweb' => [], + ] + ], + ], + InvalidConfigurationException::class, + 'Expected "int", but got "array"', + ], + [ + [ + 'page_indexing' => [ + 'site_roots' => [ + 'finaweb' => true, + ] + ], + ], + InvalidConfigurationException::class, + 'Expected "int", but got "bool"', + ], + [ + [ + 'page_indexing' => [ + 'languages_siteaccess_map' => [ + 'finaweb' => [ + 'cro-HR' => 5 + ] + ] + ], + ], + InvalidConfigurationException::class, + 'Expected "string", but got "int"', + ], + [ + [ + 'page_indexing' => [ + 'host' => [] + ], + ], + InvalidConfigurationException::class, + 'Expected "string", but got "array"', + ], + [ + [ + 'page_indexing' => [ + 'config' => [ + 'level1' => 'a' + ] + ], + ], + InvalidConfigurationException::class, + 'Expected "array", but got "string"', + ], + [ + [ + 'page_indexing' => [ + 'config' => [ + ['h1', 'h2'] + ] + ], + ], + InvalidConfigurationException::class, + 'Array key (field importance level) must be of string type' + ], + [ + [ + 'page_indexing' => [ + 'allowed_content_types' => [ + 34, + 52 + ], + ], + ], + InvalidConfigurationException::class, + 'Expected "string", but got "int"' + ], + ]; + + } + + /** + * @dataProvider providerForPageIndexingDefaultConfigurationInvalidCases + */ + public function testInvalidPageIndexingDefaultConfiguration(array $siteRootsConfig): void + { + $this->expectException(InvalidConfigurationException::class); + $this->load($siteRootsConfig); + } + + + protected function getContainerExtensions(): array { return [