From f9d66595d4e2ac9e66dcf38e9d055020993f9f7a Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Mon, 7 Jan 2019 18:03:15 +0100 Subject: [PATCH 01/10] OPENEUROPA-1414: Update config schema. --- .../config/schema/webtools_analytics_rule.schema.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml index dca53f5f..91632555 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml @@ -8,8 +8,11 @@ oe_webtools_analytics_rules.webtools_analytics_rule.*: section: type: label label: 'Section' + multilingual: + type: boolean + label: 'Support multilingual aliases' regex: type: string label: Regex uuid: - type: string \ No newline at end of file + type: string From 7584841343896592a55cff8d98402ac8aa4b87e9 Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Mon, 7 Jan 2019 18:08:07 +0100 Subject: [PATCH 02/10] OPENEUROPA-1414: Implementation of supporting multilingual aliases. --- .../oe_webtools_analytics_rules.services.yml | 2 +- .../src/Entity/WebtoolsAnalyticsRule.php | 16 +++++ .../Entity/WebtoolsAnalyticsRuleInterface.php | 8 +++ .../WebtoolsAnalyticsEventSubscriber.php | 68 ++++++++++++++++--- .../src/Form/WebtoolsAnalyticsRuleForm.php | 7 ++ .../src/WebtoolsAnalyticsRuleListBuilder.php | 2 + 6 files changed, 92 insertions(+), 11 deletions(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml index c78d85f7..b6871ec4 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml @@ -1,7 +1,7 @@ services: oe_webtools_analytics_rules.event_subscriber: class: Drupal\oe_webtools_analytics_rules\EventSubscriber\WebtoolsAnalyticsEventSubscriber - arguments: ['@entity_type.manager', '@request_stack', '@cache.webtools_analytics_rules'] + arguments: ['@entity_type.manager', '@request_stack', '@router.route_provider', '@cache.webtools_analytics_rules'] tags: - { name: event_subscriber } cache_context.webtools_analytics_section: diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php index 3e268e25..40e1167b 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php @@ -29,6 +29,7 @@ * entity_keys = { * "id" = "id", * "section" = "section", + * "multilingual" = "multilingual", * "regex" = "regex", * "uuid" = "uuid" * }, @@ -57,6 +58,13 @@ class WebtoolsAnalyticsRule extends ConfigEntityBase implements WebtoolsAnalytic */ protected $section = ''; + /** + * The Webtools Analytics rule applicable for multilingual aliases. + * + * @var bool + */ + protected $multilingual = FALSE; + /** * The regular expression to be applied. * @@ -78,4 +86,12 @@ public function getRegex(): string { return $this->regex; } + /** + * {@inheritdoc} + */ + public function isSupportMultilingualAliases(): bool { + $moduleHandler = \Drupal::service('module_handler'); + return (bool) $this->multilingual && $moduleHandler->moduleExists('path'); + } + } diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php index 3330ddb0..e3e1e34f 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php @@ -19,6 +19,14 @@ interface WebtoolsAnalyticsRuleInterface extends ConfigEntityInterface { */ public function getSection(): string; + /** + * Returns true, if rule support multilingual aliases. + * + * @return bool + * Is this rule applicable for all aliases in other languages. + */ + public function isSupportMultilingualAliases(): bool; + /** * Returns the regular expression. * diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php index cf551bcb..4ff89ed1 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php @@ -11,8 +11,10 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\oe_webtools_analytics\Event\AnalyticsEvent; use Drupal\oe_webtools_analytics\AnalyticsEventInterface; +use Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Cmf\Component\Routing\RouteProviderInterface; /** * Event subscriber for the Webtools Analytics event. @@ -33,6 +35,13 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { */ private $requestStack; + /** + * The route provider. + * + * @var \Symfony\Cmf\Component\Routing\RouteProviderInterface + */ + protected $routeProvider; + /** * A cache backend interface. * @@ -47,12 +56,15 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * The entity type manager. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack * The request stack. + * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider + * The Route provider. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * A cache backend used to store webtools rules for uris. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, CacheBackendInterface $cache) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, RouteProviderInterface $route_provider, CacheBackendInterface $cache) { $this->entityTypeManager = $entityTypeManager; $this->requestStack = $requestStack; + $this->routeProvider = $route_provider; $this->cache = $cache; } @@ -63,8 +75,8 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Reque * Response event. */ public function analyticsEventHandler(AnalyticsEventInterface $event): void { - $current_uri = $this->requestStack->getCurrentRequest()->getRequestUri(); - if ($cache = $this->cache->get($current_uri)) { + $current_path = $this->requestStack->getCurrentRequest()->getPathInfo(); + if ($cache = $this->cache->get($current_path)) { // If there is no cached data there is no section that applies to the uri. if ($cache->data === NULL) { return; @@ -76,6 +88,24 @@ public function analyticsEventHandler(AnalyticsEventInterface $event): void { } } + /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface|false $rule */ + if ($rule = $this->getRuleByPath($current_path)) { + $event->setSiteSection($rule->getSection()); + $this->cache->set($current_path, ['section' => $rule->getSection()], Cache::PERMANENT, $rule->getCacheTags()); + } + + } + + /** + * Get a rule which related to current path. + * + * @param string $path + * Current path. + * + * @return \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface|null + * Rule related to current path. + */ + private function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface { try { $storage = $this->entityTypeManager ->getStorage('webtools_analytics_rule'); @@ -96,16 +126,34 @@ public function analyticsEventHandler(AnalyticsEventInterface $event): void { $rules = $storage->loadMultiple(); /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface $rule */ foreach ($rules as $rule) { - if (preg_match($rule->getRegex(), $current_uri, $matches) === 1) { - $event->setSiteSection($rule->getSection()); - $this->cache->set($current_uri, ['section' => $rule->getSection()], Cache::PERMANENT, $rule->getCacheTags()); - // Currently there is no defined behavior for overlapping rules so we - // only take into account the first rule that applies. - return; + if ($rule->isSupportMultilingualAliases()) { + // Get source of current URI. + // But some reason we don't have correct information about current path. + // For updating information + // we have to run $this->routeProvider->getRouteCollectionForRequest(). + $this->routeProvider->getRouteCollectionForRequest($this->requestStack->getCurrentRequest()); + $source = \Drupal::service('path.current')->getPath(); + + $query = \Drupal::database()->select('url_alias', 'ua'); + $query->fields('ua', ['pid']); + $query->condition('ua.source', $source); + // As mysql doesn't support PCRE, we have to remove modifiers. + $regexp = preg_replace(['/^\//', '/\/.?$/'], '', $rule->getRegex()); + $query->condition('ua.alias', $regexp, 'REGEXP'); + $default_langcode = \Drupal::config('system.site')->get('default_langcode'); + $query->condition('ua.langcode', $default_langcode); + if ($query->execute()->fetchAll()) { + return $rule; + } + } + elseif (preg_match($rule->getRegex(), $path, $matches) === 1) { + return $rule; } } + // Cache NULL if there is no rule that applies to the uri. - $this->cache->set($current_uri, NULL, Cache::PERMANENT, $storage->getEntityType()->getListCacheTags()); + $this->cache->set($path, NULL, Cache::PERMANENT, $storage->getEntityType()->getListCacheTags()); + return NULL; } /** diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php index f52ef571..f21baae9 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php @@ -40,6 +40,13 @@ public function form(array $form, FormStateInterface $form_state): array { '#disabled' => !$rule->isNew(), ]; + $form['multilingual'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Is support multilingual aliases.'), + '#default_value' => $rule->isSupportMultilingualAliases(), + '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), + ]; + $form['regex'] = [ '#type' => 'textfield', '#title' => $this->t('Regex'), diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php index e9a953e1..9fc2a77e 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php @@ -17,6 +17,7 @@ class WebtoolsAnalyticsRuleListBuilder extends ConfigEntityListBuilder { */ public function buildHeader(): array { $header['section'] = $this->t('Section'); + $header['multilingual'] = $this->t('Multilingual'); $header['regex'] = $this->t('Regex'); return $header + parent::buildHeader(); @@ -27,6 +28,7 @@ public function buildHeader(): array { */ public function buildRow(EntityInterface $entity): array { $row['section'] = $entity->getSection(); + $row['multilingual'] = $entity->isSupportMultilingualAliases() ? $this->t('Yes') : $this->t('No'); $row['id'] = $entity->getRegex(); return $row + parent::buildRow($entity); From 18a86ee962c4bb185301510cbb14ef514f153285 Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Mon, 7 Jan 2019 18:13:00 +0100 Subject: [PATCH 03/10] OPENEUROPA-1414: Implementation of behat tests. --- runner.yml.dist | 4 +- .../Behat/WebtoolsAnalyticsConfigContext.php | 48 +++++++++++++++++++ tests/Behat/WebtoolsAnalyticsMinkContext.php | 2 +- tests/features/multilingual-aliases.feature | 39 +++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/Behat/WebtoolsAnalyticsConfigContext.php create mode 100644 tests/features/multilingual-aliases.feature diff --git a/runner.yml.dist b/runner.yml.dist index 4c2610a5..e86041cb 100755 --- a/runner.yml.dist +++ b/runner.yml.dist @@ -23,6 +23,8 @@ drupal: - "./vendor/bin/drush en oe_webtools oe_webtools_analytics_rules -y" - "./vendor/bin/drush en oe_webtools oe_webtools_laco_service -y" - "./vendor/bin/drush en oe_webtools oe_webtools_laco_widget -y" + - "./vendor/bin/drush en path -y" + - "./vendor/bin/drush en language -y" - "./vendor/bin/drush cr" settings: settings: @@ -42,4 +44,4 @@ commands: setup:phpunit: - { task: "process", source: "phpunit.xml.dist", destination: "phpunit.xml" } setup:behat: - - { task: "process", source: "behat.yml.dist", destination: "behat.yml" } \ No newline at end of file + - { task: "process", source: "behat.yml.dist", destination: "behat.yml" } diff --git a/tests/Behat/WebtoolsAnalyticsConfigContext.php b/tests/Behat/WebtoolsAnalyticsConfigContext.php new file mode 100644 index 00000000..674af699 --- /dev/null +++ b/tests/Behat/WebtoolsAnalyticsConfigContext.php @@ -0,0 +1,48 @@ +setConfig('oe_webtools_analytics.settings', 'siteID', $id); + $this->setConfig('oe_webtools_analytics.settings', 'sitePath', $sitepath); + } + + /** + * Add aliases for behat tests. + * + * @param string $path + * Source url for aliases. + * @param \Behat\Gherkin\Node\TableNode $aliasesTable + * Language and alias pairs. + * + * @Given Aliases available for the path :path: + */ + public function aliasesAvailableForPath(string $path, TableNode $aliasesTable): void { + /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */ + $path_alias_storage = \Drupal::service('path.alias_storage'); + foreach ($aliasesTable->getHash() as $row) { + $path_alias_storage->save($path, $row['url'], $row['languages']); + } + } + +} diff --git a/tests/Behat/WebtoolsAnalyticsMinkContext.php b/tests/Behat/WebtoolsAnalyticsMinkContext.php index ad223286..f6c78f67 100644 --- a/tests/Behat/WebtoolsAnalyticsMinkContext.php +++ b/tests/Behat/WebtoolsAnalyticsMinkContext.php @@ -32,7 +32,7 @@ public function analyticsJsonContainsParameter(string $parameter, string $value) $json_value = json_decode($script->getText(), TRUE); if (isset($json_value['utility']) && $json_value['utility'] == 'piwik') { $json_found = TRUE; - Assert::assertEquals($value, $json_value[$parameter]); + Assert::assertEquals($value, $json_value[$parameter] ?? ''); } } if (!$json_found) { diff --git a/tests/features/multilingual-aliases.feature b/tests/features/multilingual-aliases.feature new file mode 100644 index 00000000..cc6e4896 --- /dev/null +++ b/tests/features/multilingual-aliases.feature @@ -0,0 +1,39 @@ +@api +Feature: Webtools Analytics multilingual aliases + In order to identify separate sections of my website + As the site manager + I need to be able to create rules that allow to pair section names with regular expressions with supporting aliases + + Background: + Given I am logged in as a user with the "administer site configuration" permission + And the Webtools Analytics configuration is set to use the id '123' and the site path 'sitePath' + And the following languages are available: + | languages | + | en | + | fr | + | nl | + And Aliases available for the path "/admin/config": + | languages | url | + | en | /news | + | fr | /nouvelles | + | nl | /nieuws | + + Scenario: Create Webtools Analytics Rule with supporting multilingual aliases + Given I am on "admin/structure/webtools_analytics_rule/add" + And I fill in "Machine-readable name" with "multilingual" + And I fill in "Section" with "multilingual" + And I check the box "Is support multilingual aliases" + And I fill in "Regex" with "/news/" + When I press "Save" + # Check the rule applies + Given I am on "/admin/config" + Then the page analytics json should contain the parameter "siteSection" with the value "multilingual" + Given I am on "/news" + Then the page analytics json should contain the parameter "siteSection" with the value "multilingual" + Given I am on "/fr/nouvelles" + Then the page analytics json should contain the parameter "siteSection" with the value "multilingual" + Given I am on "/nl/nieuws" + Then the page analytics json should contain the parameter "siteSection" with the value "multilingual" + Given I am on "/admin" + Then the page analytics json should not contain the parameter "siteSection" + From c5a49ec9b3d167f93f611685ba85a6b92a848001 Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Thu, 10 Jan 2019 17:47:07 +0100 Subject: [PATCH 04/10] OPENEUROPA-1414: Adjust behat tests. --- tests/features/multilingual-aliases.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/features/multilingual-aliases.feature b/tests/features/multilingual-aliases.feature index cc6e4896..9d8e669c 100644 --- a/tests/features/multilingual-aliases.feature +++ b/tests/features/multilingual-aliases.feature @@ -22,7 +22,7 @@ Feature: Webtools Analytics multilingual aliases Given I am on "admin/structure/webtools_analytics_rule/add" And I fill in "Machine-readable name" with "multilingual" And I fill in "Section" with "multilingual" - And I check the box "Is support multilingual aliases" + And I check the box "Match on path alias for site default language." And I fill in "Regex" with "/news/" When I press "Save" # Check the rule applies From f884f7d918bce94ffd3bf02b8b680929534630e7 Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Thu, 10 Jan 2019 17:47:50 +0100 Subject: [PATCH 05/10] OPENEUROPA-1414: Update config schema. --- .../config/schema/webtools_analytics_rule.schema.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml index 91632555..a72fb36f 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/config/schema/webtools_analytics_rule.schema.yml @@ -8,9 +8,9 @@ oe_webtools_analytics_rules.webtools_analytics_rule.*: section: type: label label: 'Section' - multilingual: + match_on_site_default_language: type: boolean - label: 'Support multilingual aliases' + label: 'Match on path alias for site default language' regex: type: string label: Regex From e448100ef5066e0b7983dc80a2a6b925b455f8a4 Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Thu, 10 Jan 2019 17:51:44 +0100 Subject: [PATCH 06/10] OPENEUROPA-1414: Update Entity and Form classes according to renamed property. --- .../src/Entity/WebtoolsAnalyticsRule.php | 8 ++++---- .../src/Entity/WebtoolsAnalyticsRuleInterface.php | 2 +- .../src/Form/WebtoolsAnalyticsRuleForm.php | 15 +++++++++------ .../src/WebtoolsAnalyticsRuleListBuilder.php | 7 +++++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php index 40e1167b..9bc4522d 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php @@ -29,7 +29,7 @@ * entity_keys = { * "id" = "id", * "section" = "section", - * "multilingual" = "multilingual", + * "match_on_site_default_language" = "match_on_site_default_language", * "regex" = "regex", * "uuid" = "uuid" * }, @@ -63,7 +63,7 @@ class WebtoolsAnalyticsRule extends ConfigEntityBase implements WebtoolsAnalytic * * @var bool */ - protected $multilingual = FALSE; + protected $match_on_site_default_language = FALSE; /** * The regular expression to be applied. @@ -89,9 +89,9 @@ public function getRegex(): string { /** * {@inheritdoc} */ - public function isSupportMultilingualAliases(): bool { + public function matchOnSiteDefaultLanguage(): bool { $moduleHandler = \Drupal::service('module_handler'); - return (bool) $this->multilingual && $moduleHandler->moduleExists('path'); + return (bool) $this->match_on_site_default_language && $moduleHandler->moduleExists('path'); } } diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php index e3e1e34f..79c30626 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php @@ -25,7 +25,7 @@ public function getSection(): string; * @return bool * Is this rule applicable for all aliases in other languages. */ - public function isSupportMultilingualAliases(): bool; + public function matchOnSiteDefaultLanguage(): bool; /** * Returns the regular expression. diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php index f21baae9..d3980593 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php @@ -40,12 +40,15 @@ public function form(array $form, FormStateInterface $form_state): array { '#disabled' => !$rule->isNew(), ]; - $form['multilingual'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Is support multilingual aliases.'), - '#default_value' => $rule->isSupportMultilingualAliases(), - '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), - ]; + $moduleHandler = \Drupal::service('module_handler'); + if ($moduleHandler->moduleExists('path')) { + $form['match_on_site_default_language'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Match on path alias for site default language.'), + '#default_value' => $rule->matchOnSiteDefaultLanguage(), + '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), + ]; + } $form['regex'] = [ '#type' => 'textfield', diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php index 9fc2a77e..c4657904 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php @@ -17,7 +17,10 @@ class WebtoolsAnalyticsRuleListBuilder extends ConfigEntityListBuilder { */ public function buildHeader(): array { $header['section'] = $this->t('Section'); - $header['multilingual'] = $this->t('Multilingual'); + $moduleHandler = \Drupal::service('module_handler'); + if ($moduleHandler->moduleExists('path')) { + $header['match_on_site_default_language'] = $this->t('Match on path alias for site default language'); + } $header['regex'] = $this->t('Regex'); return $header + parent::buildHeader(); @@ -28,7 +31,7 @@ public function buildHeader(): array { */ public function buildRow(EntityInterface $entity): array { $row['section'] = $entity->getSection(); - $row['multilingual'] = $entity->isSupportMultilingualAliases() ? $this->t('Yes') : $this->t('No'); + $row['match_on_site_default_language'] = $entity->matchOnSiteDefaultLanguage() ? $this->t('Yes') : $this->t('No'); $row['id'] = $entity->getRegex(); return $row + parent::buildRow($entity); From 052fef607dba8277196be0d3b1cc02320d17a21e Mon Sep 17 00:00:00 2001 From: Sergii Pavlenko Date: Thu, 10 Jan 2019 17:54:33 +0100 Subject: [PATCH 07/10] OPENEUROPA-1414: Update Event subscriber with adding dependency injections and changed approach. --- .../oe_webtools_analytics_rules.services.yml | 2 +- .../WebtoolsAnalyticsEventSubscriber.php | 61 +++++++++++++------ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml index b6871ec4..7894849c 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml @@ -1,7 +1,7 @@ services: oe_webtools_analytics_rules.event_subscriber: class: Drupal\oe_webtools_analytics_rules\EventSubscriber\WebtoolsAnalyticsEventSubscriber - arguments: ['@entity_type.manager', '@request_stack', '@router.route_provider', '@cache.webtools_analytics_rules'] + arguments: ['@entity_type.manager', '@request_stack', '@router.route_provider', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] tags: - { name: event_subscriber } cache_context.webtools_analytics_section: diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php index 4ff89ed1..e1ad912c 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php @@ -8,13 +8,16 @@ use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Path\AliasManager; +use Drupal\Core\Path\CurrentPathStack; use Drupal\oe_webtools_analytics\Event\AnalyticsEvent; use Drupal\oe_webtools_analytics\AnalyticsEventInterface; use Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Cmf\Component\Routing\RouteProviderInterface; +use Drupal\Core\Routing\RouteProviderInterface; /** * Event subscriber for the Webtools Analytics event. @@ -38,10 +41,24 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { /** * The route provider. * - * @var \Symfony\Cmf\Component\Routing\RouteProviderInterface + * @var \Drupal\Core\Routing\RouteProviderInterface */ protected $routeProvider; + /** + * The current path service. + * + * @var \Drupal\Core\Path\CurrentPathStack + */ + protected $currentPathStack; + + /** + * The alias manager service. + * + * @var \Drupal\Core\Path\AliasManager + */ + protected $aliasManager; + /** * A cache backend interface. * @@ -49,6 +66,13 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { */ private $cache; + /** + * The configuration object. + * + * @var \Drupal\Core\Config\Config + */ + private $siteConfig; + /** * WebtoolsAnalyticsEventSubscriber constructor. * @@ -56,16 +80,25 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * The entity type manager. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack * The request stack. - * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider + * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider * The Route provider. + * @param \Drupal\Core\Path\CurrentPathStack $currentPathStack + * The current path service. + * @param \Drupal\Core\Path\AliasManager $aliasManager + * The alias manager service. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * A cache backend used to store webtools rules for uris. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * The Config Factory service. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, RouteProviderInterface $route_provider, CacheBackendInterface $cache) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, RouteProviderInterface $route_provider, CurrentPathStack $currentPathStack, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) { $this->entityTypeManager = $entityTypeManager; $this->requestStack = $requestStack; $this->routeProvider = $route_provider; + $this->currentPathStack = $currentPathStack; + $this->aliasManager = $aliasManager; $this->cache = $cache; + $this->siteConfig = $config->get('system.site'); } /** @@ -126,27 +159,17 @@ private function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface { $rules = $storage->loadMultiple(); /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface $rule */ foreach ($rules as $rule) { - if ($rule->isSupportMultilingualAliases()) { + $current_path = $path; + if ($rule->matchOnSiteDefaultLanguage()) { // Get source of current URI. // But some reason we don't have correct information about current path. // For updating information // we have to run $this->routeProvider->getRouteCollectionForRequest(). $this->routeProvider->getRouteCollectionForRequest($this->requestStack->getCurrentRequest()); - $source = \Drupal::service('path.current')->getPath(); - - $query = \Drupal::database()->select('url_alias', 'ua'); - $query->fields('ua', ['pid']); - $query->condition('ua.source', $source); - // As mysql doesn't support PCRE, we have to remove modifiers. - $regexp = preg_replace(['/^\//', '/\/.?$/'], '', $rule->getRegex()); - $query->condition('ua.alias', $regexp, 'REGEXP'); - $default_langcode = \Drupal::config('system.site')->get('default_langcode'); - $query->condition('ua.langcode', $default_langcode); - if ($query->execute()->fetchAll()) { - return $rule; - } + $current_path = $this->aliasManager->getAliasByPath($this->currentPathStack->getPath(), $this->siteConfig->get('default_langcode')); } - elseif (preg_match($rule->getRegex(), $path, $matches) === 1) { + + if (preg_match($rule->getRegex(), $current_path) === 1) { return $rule; } } From 176dbcb86e1a90237254cfab1b580db20f0fae8b Mon Sep 17 00:00:00 2001 From: upchuk Date: Mon, 14 Jan 2019 11:38:02 +0100 Subject: [PATCH 08/10] OPENEUROPA-1414: All sorts of improvements. --- .../oe_webtools_analytics_rules.info.yml | 3 +- .../oe_webtools_analytics_rules.services.yml | 4 +- .../src/Entity/WebtoolsAnalyticsRule.php | 3 +- .../WebtoolsAnalyticsEventSubscriber.php | 69 +++++-------------- .../src/Form/WebtoolsAnalyticsRuleForm.php | 15 ++-- .../src/WebtoolsAnalyticsRuleListBuilder.php | 5 +- phpunit.xml.dist | 1 + runner.yml.dist | 1 - .../Behat/WebtoolsAnalyticsConfigContext.php | 48 ------------- tests/Behat/WebtoolsConfigContext.php | 19 +++++ tests/features/multilingual-aliases.feature | 9 ++- 11 files changed, 55 insertions(+), 122 deletions(-) delete mode 100644 tests/Behat/WebtoolsAnalyticsConfigContext.php diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.info.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.info.yml index 7cca3ae0..5fb3f41c 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.info.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.info.yml @@ -4,4 +4,5 @@ package: OpenEuropa Webtools type: module core: 8.x dependencies: - - oe_webtools_analytics + - oe_webtools_analytics:oe_webtools_analytics + - drupal:path diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml index 7894849c..755ba0c7 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml @@ -1,11 +1,11 @@ services: oe_webtools_analytics_rules.event_subscriber: class: Drupal\oe_webtools_analytics_rules\EventSubscriber\WebtoolsAnalyticsEventSubscriber - arguments: ['@entity_type.manager', '@request_stack', '@router.route_provider', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] + arguments: ['@entity_type.manager', '@request_stack', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] tags: - { name: event_subscriber } cache_context.webtools_analytics_section: - class: Drupal\oe_webtools_analytics_rules\OpenEuropaWebtoolsAnalyticsSiteSectionCacheContext + class: Drupal\oe_webtools_analytics_rules\WebtoolsAnalyticsSiteSectionCacheContext arguments: ['@oe_webtools_analytics_rules.event_subscriber'] tags: - { name: cache.context } diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php index 9bc4522d..d53e0d3d 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php @@ -90,8 +90,7 @@ public function getRegex(): string { * {@inheritdoc} */ public function matchOnSiteDefaultLanguage(): bool { - $moduleHandler = \Drupal::service('module_handler'); - return (bool) $this->match_on_site_default_language && $moduleHandler->moduleExists('path'); + return (bool) $this->match_on_site_default_language; } } diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php index e1ad912c..55500106 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php @@ -4,8 +4,6 @@ namespace Drupal\oe_webtools_analytics_rules\EventSubscriber; -use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; -use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; @@ -17,7 +15,6 @@ use Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; -use Drupal\Core\Routing\RouteProviderInterface; /** * Event subscriber for the Webtools Analytics event. @@ -29,21 +26,14 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - private $entityTypeManager; + protected $entityTypeManager; /** * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack */ - private $requestStack; - - /** - * The route provider. - * - * @var \Drupal\Core\Routing\RouteProviderInterface - */ - protected $routeProvider; + protected $requestStack; /** * The current path service. @@ -64,14 +54,14 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * * @var \Drupal\Core\Cache\CacheBackendInterface */ - private $cache; + protected $cache; /** * The configuration object. * * @var \Drupal\Core\Config\Config */ - private $siteConfig; + protected $siteConfig; /** * WebtoolsAnalyticsEventSubscriber constructor. @@ -80,8 +70,6 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * The entity type manager. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack * The request stack. - * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider - * The Route provider. * @param \Drupal\Core\Path\CurrentPathStack $currentPathStack * The current path service. * @param \Drupal\Core\Path\AliasManager $aliasManager @@ -91,10 +79,9 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * @param \Drupal\Core\Config\ConfigFactoryInterface $config * The Config Factory service. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, RouteProviderInterface $route_provider, CurrentPathStack $currentPathStack, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, CurrentPathStack $currentPathStack, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) { $this->entityTypeManager = $entityTypeManager; $this->requestStack = $requestStack; - $this->routeProvider = $route_provider; $this->currentPathStack = $currentPathStack; $this->aliasManager = $aliasManager; $this->cache = $cache; @@ -109,11 +96,13 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Reque */ public function analyticsEventHandler(AnalyticsEventInterface $event): void { $current_path = $this->requestStack->getCurrentRequest()->getPathInfo(); - if ($cache = $this->cache->get($current_path)) { + $cache = $this->cache->get($current_path); + if ($cache && $cache->data === NULL) { // If there is no cached data there is no section that applies to the uri. - if ($cache->data === NULL) { - return; - } + return; + } + + if ($cache = $this->cache->get($current_path)) { // Set site section from the cached data. if (isset($cache->data['section'])) { $event->setSiteSection($cache->data['section']); @@ -121,12 +110,15 @@ public function analyticsEventHandler(AnalyticsEventInterface $event): void { } } - /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface|false $rule */ - if ($rule = $this->getRuleByPath($current_path)) { + $rule = $this->getRuleByPath($current_path); + if ($rule instanceof WebtoolsAnalyticsRuleInterface) { $event->setSiteSection($rule->getSection()); $this->cache->set($current_path, ['section' => $rule->getSection()], Cache::PERMANENT, $rule->getCacheTags()); + return; } + // Cache NULL if there is no rule that applies to the uri. + $this->cache->set($current_path, NULL, Cache::PERMANENT, ['webtools_analytics_rule_list']); } /** @@ -138,34 +130,13 @@ public function analyticsEventHandler(AnalyticsEventInterface $event): void { * @return \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface|null * Rule related to current path. */ - private function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface { - try { - $storage = $this->entityTypeManager - ->getStorage('webtools_analytics_rule'); - } - // Because of the dynamic nature how entities work in Drupal the entity type - // manager can throw exceptions if an entity type is not available or - // invalid. However since we are using our very own entity type we can - // be certain that this is defined and valid. Convert the exceptions into - // unchecked runtime exceptions so they don't need to be documented all the - // way up the call stack. - catch (InvalidPluginDefinitionException $e) { - throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); - } - catch (PluginNotFoundException $e) { - throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); - } + protected function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface { + /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface[] $rules */ + $rules = $this->entityTypeManager->getStorage('webtools_analytics_rule')->loadMultiple(); - $rules = $storage->loadMultiple(); - /** @var \Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface $rule */ foreach ($rules as $rule) { $current_path = $path; if ($rule->matchOnSiteDefaultLanguage()) { - // Get source of current URI. - // But some reason we don't have correct information about current path. - // For updating information - // we have to run $this->routeProvider->getRouteCollectionForRequest(). - $this->routeProvider->getRouteCollectionForRequest($this->requestStack->getCurrentRequest()); $current_path = $this->aliasManager->getAliasByPath($this->currentPathStack->getPath(), $this->siteConfig->get('default_langcode')); } @@ -174,8 +145,6 @@ private function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface { } } - // Cache NULL if there is no rule that applies to the uri. - $this->cache->set($path, NULL, Cache::PERMANENT, $storage->getEntityType()->getListCacheTags()); return NULL; } diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php index d3980593..82b6b466 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php @@ -40,15 +40,12 @@ public function form(array $form, FormStateInterface $form_state): array { '#disabled' => !$rule->isNew(), ]; - $moduleHandler = \Drupal::service('module_handler'); - if ($moduleHandler->moduleExists('path')) { - $form['match_on_site_default_language'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Match on path alias for site default language.'), - '#default_value' => $rule->matchOnSiteDefaultLanguage(), - '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), - ]; - } + $form['match_on_site_default_language'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Match on path alias for site default language.'), + '#default_value' => $rule->matchOnSiteDefaultLanguage(), + '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), + ]; $form['regex'] = [ '#type' => 'textfield', diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php index c4657904..83357d10 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsRuleListBuilder.php @@ -17,10 +17,7 @@ class WebtoolsAnalyticsRuleListBuilder extends ConfigEntityListBuilder { */ public function buildHeader(): array { $header['section'] = $this->t('Section'); - $moduleHandler = \Drupal::service('module_handler'); - if ($moduleHandler->moduleExists('path')) { - $header['match_on_site_default_language'] = $this->t('Match on path alias for site default language'); - } + $header['match_on_site_default_language'] = $this->t('Match on path alias for site default language'); $header['regex'] = $this->t('Regex'); return $header + parent::buildHeader(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f4b0d3bc..e771271b 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,6 +12,7 @@ ./tests/ ./modules/*/tests/ + ./modules/*/modules/*/tests/ diff --git a/runner.yml.dist b/runner.yml.dist index e86041cb..17e25a4f 100755 --- a/runner.yml.dist +++ b/runner.yml.dist @@ -23,7 +23,6 @@ drupal: - "./vendor/bin/drush en oe_webtools oe_webtools_analytics_rules -y" - "./vendor/bin/drush en oe_webtools oe_webtools_laco_service -y" - "./vendor/bin/drush en oe_webtools oe_webtools_laco_widget -y" - - "./vendor/bin/drush en path -y" - "./vendor/bin/drush en language -y" - "./vendor/bin/drush cr" settings: diff --git a/tests/Behat/WebtoolsAnalyticsConfigContext.php b/tests/Behat/WebtoolsAnalyticsConfigContext.php deleted file mode 100644 index 674af699..00000000 --- a/tests/Behat/WebtoolsAnalyticsConfigContext.php +++ /dev/null @@ -1,48 +0,0 @@ -setConfig('oe_webtools_analytics.settings', 'siteID', $id); - $this->setConfig('oe_webtools_analytics.settings', 'sitePath', $sitepath); - } - - /** - * Add aliases for behat tests. - * - * @param string $path - * Source url for aliases. - * @param \Behat\Gherkin\Node\TableNode $aliasesTable - * Language and alias pairs. - * - * @Given Aliases available for the path :path: - */ - public function aliasesAvailableForPath(string $path, TableNode $aliasesTable): void { - /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */ - $path_alias_storage = \Drupal::service('path.alias_storage'); - foreach ($aliasesTable->getHash() as $row) { - $path_alias_storage->save($path, $row['url'], $row['languages']); - } - } - -} diff --git a/tests/Behat/WebtoolsConfigContext.php b/tests/Behat/WebtoolsConfigContext.php index 46100283..518bca05 100644 --- a/tests/Behat/WebtoolsConfigContext.php +++ b/tests/Behat/WebtoolsConfigContext.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\oe_webtools\Behat; use Behat\Behat\Hook\Scope\BeforeScenarioScope; +use Behat\Gherkin\Node\TableNode; use Drupal\DrupalExtension\Context\RawDrupalContext; /** @@ -64,4 +65,22 @@ public function backupLacoConfigs() { } } + /** + * Add aliases for Behat tests. + * + * @param string $path + * Source url for aliases. + * @param \Behat\Gherkin\Node\TableNode $aliasesTable + * Language and alias pairs. + * + * @Given aliases available for the path :path: + */ + public function aliasesAvailableForPath(string $path, TableNode $aliasesTable): void { + /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */ + $path_alias_storage = \Drupal::service('path.alias_storage'); + foreach ($aliasesTable->getHash() as $row) { + $path_alias_storage->save($path, $row['url'], $row['languages']); + } + } + } diff --git a/tests/features/multilingual-aliases.feature b/tests/features/multilingual-aliases.feature index 9d8e669c..2218e00e 100644 --- a/tests/features/multilingual-aliases.feature +++ b/tests/features/multilingual-aliases.feature @@ -4,22 +4,21 @@ Feature: Webtools Analytics multilingual aliases As the site manager I need to be able to create rules that allow to pair section names with regular expressions with supporting aliases - Background: - Given I am logged in as a user with the "administer site configuration" permission + Scenario: Create Webtools Analytics Rule with supporting multilingual aliases + Given I am logged in as a user with the "administer site configuration, access administration pages" permission And the Webtools Analytics configuration is set to use the id '123' and the site path 'sitePath' And the following languages are available: | languages | | en | | fr | | nl | - And Aliases available for the path "/admin/config": + And aliases available for the path "/admin/config": | languages | url | | en | /news | | fr | /nouvelles | | nl | /nieuws | - Scenario: Create Webtools Analytics Rule with supporting multilingual aliases - Given I am on "admin/structure/webtools_analytics_rule/add" + And I am on "admin/structure/webtools_analytics_rule/add" And I fill in "Machine-readable name" with "multilingual" And I fill in "Section" with "multilingual" And I check the box "Match on path alias for site default language." From 7c7d2a08992a1ac9829f55ded93dd5bd385bd761 Mon Sep 17 00:00:00 2001 From: upchuk Date: Mon, 14 Jan 2019 15:00:35 +0100 Subject: [PATCH 09/10] OPENEUROPA-1414: Remove custom cache context. --- .../oe_webtools_analytics_rules.services.yml | 5 -- ...btoolsAnalyticsSiteSectionCacheContext.php | 60 ------------------- 2 files changed, 65 deletions(-) delete mode 100644 modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsSiteSectionCacheContext.php diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml index 755ba0c7..557d678b 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml @@ -4,11 +4,6 @@ services: arguments: ['@entity_type.manager', '@request_stack', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] tags: - { name: event_subscriber } - cache_context.webtools_analytics_section: - class: Drupal\oe_webtools_analytics_rules\WebtoolsAnalyticsSiteSectionCacheContext - arguments: ['@oe_webtools_analytics_rules.event_subscriber'] - tags: - - { name: cache.context } cache.webtools_analytics_rules: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsSiteSectionCacheContext.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsSiteSectionCacheContext.php deleted file mode 100644 index 61b934d2..00000000 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/WebtoolsAnalyticsSiteSectionCacheContext.php +++ /dev/null @@ -1,60 +0,0 @@ -analyticsEventSubscriber = $analytics_event_subscriber; - } - - /** - * {@inheritdoc} - */ - public static function getLabel() { - return t('Webtools Analytics site section'); - } - - /** - * {@inheritdoc} - */ - public function getContext() { - $event = new AnalyticsEvent(); - $this->analyticsEventSubscriber->setSection($event); - - return $event->getSiteSection(); - } - - /** - * {@inheritdoc} - */ - public function getCacheableMetadata($type = NULL) { - return new CacheableMetadata(); - } - -} From 10a247b81bd205ab4705ef95346e462eb05ad71e Mon Sep 17 00:00:00 2001 From: upchuk Date: Tue, 15 Jan 2019 15:30:39 +0100 Subject: [PATCH 10/10] OPENEUROPA-1414: Minor improvements. --- .../oe_webtools_analytics_rules.services.yml | 2 +- .../src/Entity/WebtoolsAnalyticsRule.php | 2 +- .../Entity/WebtoolsAnalyticsRuleInterface.php | 4 +- .../WebtoolsAnalyticsEventSubscriber.php | 38 ++++++------------- .../src/Form/WebtoolsAnalyticsRuleForm.php | 2 +- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml index 557d678b..a819cad0 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/oe_webtools_analytics_rules.services.yml @@ -1,7 +1,7 @@ services: oe_webtools_analytics_rules.event_subscriber: class: Drupal\oe_webtools_analytics_rules\EventSubscriber\WebtoolsAnalyticsEventSubscriber - arguments: ['@entity_type.manager', '@request_stack', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] + arguments: ['@entity_type.manager', '@path.current', '@path.alias_manager','@cache.webtools_analytics_rules', '@config.factory'] tags: - { name: event_subscriber } cache.webtools_analytics_rules: diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php index d53e0d3d..0f4f326d 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRule.php @@ -59,7 +59,7 @@ class WebtoolsAnalyticsRule extends ConfigEntityBase implements WebtoolsAnalytic protected $section = ''; /** - * The Webtools Analytics rule applicable for multilingual aliases. + * Indicates if the rule should be applied on the default site language alias. * * @var bool */ diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php index 79c30626..ecb5abfd 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Entity/WebtoolsAnalyticsRuleInterface.php @@ -20,10 +20,10 @@ interface WebtoolsAnalyticsRuleInterface extends ConfigEntityInterface { public function getSection(): string; /** - * Returns true, if rule support multilingual aliases. + * Indicates if the rule should be applied on the default site language alias. * * @return bool - * Is this rule applicable for all aliases in other languages. + * True if applies on the default site language alias. */ public function matchOnSiteDefaultLanguage(): bool; diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php index 8938aed9..689d328c 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/EventSubscriber/WebtoolsAnalyticsEventSubscriber.php @@ -8,13 +8,12 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Path\AliasManager; +use Drupal\Core\Path\AliasManagerInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\oe_webtools_analytics\Event\AnalyticsEvent; use Drupal\oe_webtools_analytics\AnalyticsEventInterface; use Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpFoundation\RequestStack; /** * Event subscriber for the Webtools Analytics event. @@ -28,24 +27,17 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { */ protected $entityTypeManager; - /** - * The request stack. - * - * @var \Symfony\Component\HttpFoundation\RequestStack - */ - protected $requestStack; - /** * The current path service. * * @var \Drupal\Core\Path\CurrentPathStack */ - protected $currentPathStack; + protected $currentPath; /** * The alias manager service. * - * @var \Drupal\Core\Path\AliasManager + * @var \Drupal\Core\Path\AliasManagerInterface */ protected $aliasManager; @@ -68,21 +60,18 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface { * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. - * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack - * The request stack. - * @param \Drupal\Core\Path\CurrentPathStack $currentPathStack + * @param \Drupal\Core\Path\CurrentPathStack $currentPath * The current path service. - * @param \Drupal\Core\Path\AliasManager $aliasManager + * @param \Drupal\Core\Path\AliasManagerInterface $aliasManager * The alias manager service. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * A cache backend used to store webtools rules for uris. * @param \Drupal\Core\Config\ConfigFactoryInterface $config * The Config Factory service. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, CurrentPathStack $currentPathStack, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, CurrentPathStack $currentPath, AliasManagerInterface $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) { $this->entityTypeManager = $entityTypeManager; - $this->requestStack = $requestStack; - $this->currentPathStack = $currentPathStack; + $this->currentPath = $currentPath; $this->aliasManager = $aliasManager; $this->cache = $cache; $this->siteConfig = $config->get('system.site'); @@ -96,19 +85,16 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Reque */ public function analyticsEventHandler(AnalyticsEventInterface $event): void { $event->addCacheTags(['webtools_analytics_rule_list']); - $current_path = $this->requestStack->getCurrentRequest()->getPathInfo(); + $current_path = $this->currentPath->getPath(); $cache = $this->cache->get($current_path); if ($cache && $cache->data === NULL) { // If there is no cached data there is no section that applies to the uri. return; } - if ($cache = $this->cache->get($current_path)) { - // Set site section from the cached data. - if (isset($cache->data['section'])) { - $event->setSiteSection($cache->data['section']); - return; - } + if (isset($cache->data['section'])) { + $event->setSiteSection($cache->data['section']); + return; } $rule = $this->getRuleByPath($current_path); @@ -138,7 +124,7 @@ protected function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface foreach ($rules as $rule) { $current_path = $path; if ($rule->matchOnSiteDefaultLanguage()) { - $current_path = $this->aliasManager->getAliasByPath($this->currentPathStack->getPath(), $this->siteConfig->get('default_langcode')); + $current_path = $this->aliasManager->getAliasByPath($this->currentPath->getPath(), $this->siteConfig->get('default_langcode')); } if (preg_match($rule->getRegex(), $current_path) === 1) { diff --git a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php index 82b6b466..f4835908 100644 --- a/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php +++ b/modules/oe_webtools_analytics/modules/oe_webtools_analytics_rules/src/Form/WebtoolsAnalyticsRuleForm.php @@ -44,7 +44,7 @@ public function form(array $form, FormStateInterface $form_state): array { '#type' => 'checkbox', '#title' => $this->t('Match on path alias for site default language.'), '#default_value' => $rule->matchOnSiteDefaultLanguage(), - '#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."), + '#description' => $this->t("If checked, the matching will be done on the path alias for the site default language. For example, if you have the /news/ regex value, the rule would be applied to the '/fr/nouvelles/*' as well as the /nl/nieuws/* paths."), ]; $form['regex'] = [