Skip to content

Commit

Permalink
OPENEUROPA-1414: Updates from the master branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Jan 14, 2019
2 parents 7c7d2a0 + 670fbb7 commit 7c0dc48
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 21 deletions.
3 changes: 3 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ default:
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\Tests\oe_webtools\Behat\WebtoolsAnalyticsMinkContext
- Drupal\Tests\oe_webtools\Behat\WebtoolsConfigContext
- OpenEuropa\Behat\TransformationContext:
pages:
Webtools Analytics configuration: '/admin/config/system/oe_webtools_analytics'
extensions:
Behat\MinkExtension:
goutte: ~
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
siteID: ''
sitePath: ''
instance: ''
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ oe_webtools_analytics.settings:
instance:
type: string
label: 'Instance'
description: 'The test server instance. e.g. testing, ec.europa.eu or europa.eu.'
description: 'The server instance. e.g. testing, ec.europa.eu or europa.eu.'
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Reque
* Response event.
*/
public function analyticsEventHandler(AnalyticsEventInterface $event): void {
$event->addCacheTags(['webtools_analytics_rule_list']);
$current_path = $this->requestStack->getCurrentRequest()->getPathInfo();
$cache = $this->cache->get($current_path);
if ($cache && $cache->data === NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ package: OpenEuropa Webtools
type: module
version: 1.0
core: 8.x
configure: oe_webtools_analytics.settings
dependencies:
- oe_webtools
20 changes: 20 additions & 0 deletions modules/oe_webtools_analytics/oe_webtools_analytics.install
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,23 @@ function oe_webtools_analytics_requirements($phase) {

return $requirements;
}

/**
* Implements hook_update_N().
*
* Set default config.
*/
function oe_webtools_analytics_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable(AnalyticsEventInterface::CONFIG_NAME);
if (is_null($config->get('siteID'))) {
$config->set('siteID', '');
}
if (is_null($config->get('sitePath'))) {
$config->set('sitePath', '');
}
if (is_null($config->get('instance'))) {
$config->set('instance', '');
}
$config->save(TRUE);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
oe_webtools_analytics.settings:
title: Webtools Analytics
description: 'Configure Webtools Analytics.'
route_name: oe_webtools_analytics.settings
parent: system.admin_config_system
28 changes: 18 additions & 10 deletions modules/oe_webtools_analytics/oe_webtools_analytics.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@
declare(strict_types = 1);

use Drupal\oe_webtools_analytics\Event\AnalyticsEvent;
use Drupal\Core\Cache\CacheableMetadata;

/**
* Implements hook_page_attachments().
*
* Collects the Analytics settings and sets them as a JSON snippet.
*/
function oe_webtools_analytics_page_attachments(array &$attachments) {
$import_settings_event = new AnalyticsEvent();
$event = new AnalyticsEvent();
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch(AnalyticsEvent::NAME, $import_settings_event);
$event_dispatcher->dispatch(AnalyticsEvent::NAME, $event);

if (!$import_settings_event->isValid()) {
$existing_cache = CacheableMetadata::createFromRenderArray($attachments);
$event_cache = CacheableMetadata::createFromObject($event);

if (!$event->isValid()) {
$cache = $existing_cache->merge($event_cache);
$cache->applyTo($attachments);
return;
}

$analytics_attachment = [
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $event,
'#attributes' => ['type' => 'application/json'],
];

$event_cache->applyTo($attachments);
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $import_settings_event,
'#attributes' => ['type' => 'application/json'],
'#cache' => ['contexts' => ['url']],
],
$analytics_attachment,
// A key, to make it possible to recognize this when altering.
'oe_webtools_analytics',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer webtools analytics:
title: 'Administer Webtools Analytics'
restrict access: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
oe_webtools_analytics.settings:
path: '/admin/config/system/oe_webtools_analytics'
defaults:
_form: 'Drupal\oe_webtools_analytics\Form\WebtoolsAnalyticsSettingsForm'
_title: 'Webtools Analytics settings'
requirements:
_permission: 'administer webtools analytics'
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Drupal\oe_webtools_analytics;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use JsonSerializable;
use Drupal\oe_webtools_analytics\Search\SearchParametersInterface;

/**
* Provides an interface for AnalyticsEvent accordingly to the documentation.
*/
interface AnalyticsEventInterface extends JsonSerializable {
interface AnalyticsEventInterface extends JsonSerializable, RefinableCacheableDependencyInterface {
/**
* The Webtools analytics entrance in settings.php.
*/
Expand Down
8 changes: 6 additions & 2 deletions modules/oe_webtools_analytics/src/Event/AnalyticsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@

namespace Drupal\oe_webtools_analytics\Event;

use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\oe_webtools_analytics\AnalyticsEventInterface;
use Drupal\oe_webtools_analytics\Search\SearchParameters;
use Drupal\oe_webtools_analytics\Search\SearchParametersInterface;
use JsonSerializable;
use Symfony\Component\EventDispatcher\Event;
use Drupal\oe_webtools_analytics\AnalyticsEventInterface;
use Drupal\oe_webtools_analytics\Search\SearchParametersInterface;

/**
* Class WebtoolsImportDataEvent.
*
* @package Drupal\oe_webtools_analytics\Event
*/
class AnalyticsEvent extends Event implements JsonSerializable, AnalyticsEventInterface {

use RefinableCacheableDependencyTrait;

/**
* This event allows you to set the Analytics variable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function __construct(ConfigFactoryInterface $configFactory, RequestStack
* Response event.
*/
public function onSetSiteDefaults(AnalyticsEventInterface $event) {
$event->addCacheableDependency($this->config);
$event->addCacheContexts(['url.path']);

// SiteID must exist and be an integer.
$site_id = $this->config->get(AnalyticsEventInterface::SITE_ID);
if (!is_numeric($site_id)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_webtools_analytics\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Settings form for module.
*/
class WebtoolsAnalyticsSettingsForm extends ConfigFormBase {

/**
* Name of the config being edited.
*/
const CONFIG_NAME = 'oe_webtools_analytics.settings';

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oe_webtools_analytics_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['siteID'] = [
'#type' => 'number',
'#title' => $this->t('Site ID'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('siteID'),
'#description' => $this->t('The site unique numeric identifier.'),
];
$form['sitePath'] = [
'#type' => 'textfield',
'#title' => $this->t('Site path'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('sitePath'),
'#description' => $this->t('The domain + root path without protocol.'),
];
$form['instance'] = [
'#type' => 'textfield',
'#title' => $this->t('Instance'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('instance'),
'#description' => $this->t('The server instance. e.g. testing, ec.europa.eu or europa.eu.'),
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config(static::CONFIG_NAME)
->set('siteID', $form_state->getValue('siteID'))
->set('sitePath', $form_state->getValue('sitePath'))
->set('instance', $form_state->getValue('instance'))
->save();
parent::submitForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['oe_webtools_analytics.settings'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Drupal\Tests\oe_webtools_analytics\Kernel;

use Drupal\Core\Cache\Cache;
use Drupal\oe_webtools_analytics\AnalyticsEventInterface;
use Drupal\Tests\BrowserTestBase;

Expand Down Expand Up @@ -36,12 +35,6 @@ public function testLibraryLoading(): void {
->set("instance", "testing");
$config->save();

foreach (Cache::getBins() as $service_id => $cache_backend) {
if ('dynamic_page_cache' === $service_id || 'page' === $service_id) {
$cache_backend->deleteAll();
}
}

$this->drupalGet('<front>');
$this->assertSession()
->responseContains('<script type="application/json">{"utility":"piwik","siteID":"123","sitePath":["ec.europa.eu"],"instance":"testing"}</script>');
Expand All @@ -53,6 +46,17 @@ public function testLibraryLoading(): void {
$this->drupalGet('admin');
$this->assertSession()
->responseContains('<script type="application/json">{"utility":"piwik","siteID":"123","sitePath":["ec.europa.eu"],"is403":true,"instance":"testing"}</script>');

// Test the cache invalidation.
$config = \Drupal::configFactory()
->getEditable(AnalyticsEventInterface::CONFIG_NAME)
->set("siteID", "1234");
$config->save();

$this->drupalGet('<front>');
$this->assertSession()
->responseContains('<script type="application/json">{"utility":"piwik","siteID":"1234","sitePath":["ec.europa.eu"],"instance":"testing"}</script>');

}

}
13 changes: 13 additions & 0 deletions tests/Behat/WebtoolsConfigContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ public function aliasesAvailableForPath(string $path, TableNode $aliasesTable):
}
}

/**
* Backup configs that need to be reverted in AfterScenario by ConfigContext.
*
* @BeforeScenario @BackupAnalyticsConfigs
*/
public function backupAnalyticsConfigs() {
$name = 'oe_webtools_analytics.settings';
$configs = $this->getDriver()->getCore()->configGet($name);
foreach ($configs as $key => $value) {
$this->configContext->setConfig($name, $key, $value);
}
}

}
19 changes: 19 additions & 0 deletions tests/features/analytics.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@api
Feature: Webtools Analytics
In order to provide analytics
As the site manager
I need to be able to configure the settings

@BackupAnalyticsConfigs
Scenario: Create Webtools Analytics settings
Given I am logged in as a user with the "administer webtools analytics" permission
And I am on "the Webtools Analytics configuration page"
Then I should see "Webtools Analytics settings"
When I fill in "Site ID" with "123456"
And I fill in "Site path" with "ec.europa.eu/info"
And I fill in "Instance" with "ec.europa.eu"
And I press "Save configuration"
Then I should see the message "The configuration options have been saved."
And the "Site ID" field should contain "123456"
And the "Site path" field should contain "ec.europa.eu/info"
And the "Instance" field should contain "ec.europa.eu"

0 comments on commit 7c0dc48

Please sign in to comment.