From 71ddb6a392104e8c9ab37ac9e0ff5fcc8a3470be Mon Sep 17 00:00:00 2001 From: Abel Santos <abel.santos.corral@gmail.com> Date: Mon, 6 Sep 2021 18:21:54 +0200 Subject: [PATCH 1/6] OEL-450: Add block--system-branding-block.html.twig component. --- .../block--system-branding-block.html.twig | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 templates/overrides/navigation/block--system-branding-block.html.twig diff --git a/templates/overrides/navigation/block--system-branding-block.html.twig b/templates/overrides/navigation/block--system-branding-block.html.twig new file mode 100644 index 00000000..8997f69c --- /dev/null +++ b/templates/overrides/navigation/block--system-branding-block.html.twig @@ -0,0 +1,30 @@ +{% extends "block.html.twig" %} +{# +/** + * @file + * Theme override for a branding block. + * + * Each branding element variable (logo, name, slogan) is only available if + * enabled in the block configuration. + * + * Available variables: + * - site_logo: Logo for site as defined in Appearance or theme settings. + * - site_name: Name for site as defined in Site information settings. + * - site_slogan: Slogan for site as defined in Site information settings. + */ +#} +{% block content %} + {% if site_logo %} + <a href="{{ path('<front>') }}" rel="home" class="site-logo"> + <img src="{{ site_logo }}" alt="{{ 'Home'|t }}" /> + </a> + {% endif %} + {% if site_name %} + <div class="site-name h1 text-white text-decoration-none align-bottom"> + <a href="{{ path('<front>') }}" rel="home">{{ site_name }}</a> + </div> + {% endif %} + {% if site_slogan %} + <div class="site-slogan">{{ site_slogan }}</div> + {% endif %} +{% endblock %} -- GitLab From 56702b0fa454df7be39d6ac9107c082d74507f02 Mon Sep 17 00:00:00 2001 From: Abel Santos <abel.santos.corral@gmail.com> Date: Tue, 7 Sep 2021 11:24:52 +0200 Subject: [PATCH 2/6] OEL-450: Add test SiteBrandingBlockTest. --- tests/Functional/SiteBrandingBlockTest.php | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/Functional/SiteBrandingBlockTest.php diff --git a/tests/Functional/SiteBrandingBlockTest.php b/tests/Functional/SiteBrandingBlockTest.php new file mode 100644 index 00000000..3627aae2 --- /dev/null +++ b/tests/Functional/SiteBrandingBlockTest.php @@ -0,0 +1,78 @@ +<?php + +declare(strict_types = 1); + +namespace Drupal\Tests\oe_whitelabel\Functional; + +use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\WebAssert; + +/** + * Tests of Site Branding Block. + */ +class SiteBrandingBlockTest extends BrowserTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = [ + 'config', + 'system', + 'ui_patterns', + 'ui_patterns_library', + 'ui_patterns_settings', + 'components', + 'field_ui', + 'toolbar', + 'block', + ]; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + // Enable and set OpenEuropa Whitelabel Theme as default. + \Drupal::service('theme_installer')->install(['oe_whitelabel']); + \Drupal::configFactory() + ->getEditable('system.theme') + ->set('default', 'oe_whitelabel') + ->save(); + + /* Rebuild the ui_pattern definitions to collect the ones provided by + oe_whitelabel itself. */ + \Drupal::service('plugin.manager.ui_patterns')->clearCachedDefinitions(); + + // Ensure that the system breadcrumb is placed as well. + $this->drupalPlaceBlock('osystem_branding_block', [ + 'region' => 'header', + ]); + + } + + /** + * Asserts classes contained at site branding block. + */ + public function testClassesSiteName(): void { + ini_set('xdebug.var_display_max_depth', '10'); + ini_set('xdebug.var_display_max_children', '256'); + ini_set('xdebug.var_display_max_data', '10024'); + $this->drupalGet('<front>'); + $assert_session = $this->assertSession(); + + $webAssert = new WebAssert($this->getSession()); + $element = $webAssert->elementExists('css', 'html'); + $actual = $element->getHtml(); + var_dump($actual); + + $assert_session->elementExists('css', 'div.site-name.h1.text-white.text-decoration-none.align-bottom'); + + } + +} -- GitLab From bcc49b977c77f65bd8ebe45d796451795a8cecc2 Mon Sep 17 00:00:00 2001 From: Abel Santos <abel.santos.corral@gmail.com> Date: Tue, 7 Sep 2021 15:44:40 +0200 Subject: [PATCH 3/6] OEL-450: Fix typo in block name and clean SiteBrandingBlockTest.php component for CS. --- tests/Functional/SiteBrandingBlockTest.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/Functional/SiteBrandingBlockTest.php b/tests/Functional/SiteBrandingBlockTest.php index 3627aae2..bab61e5f 100644 --- a/tests/Functional/SiteBrandingBlockTest.php +++ b/tests/Functional/SiteBrandingBlockTest.php @@ -5,7 +5,6 @@ declare(strict_types = 1); namespace Drupal\Tests\oe_whitelabel\Functional; use Drupal\Tests\BrowserTestBase; -use Drupal\Tests\WebAssert; /** * Tests of Site Branding Block. @@ -22,6 +21,7 @@ class SiteBrandingBlockTest extends BrowserTestBase { 'ui_patterns_library', 'ui_patterns_settings', 'components', + 'field', 'field_ui', 'toolbar', 'block', @@ -49,8 +49,8 @@ class SiteBrandingBlockTest extends BrowserTestBase { oe_whitelabel itself. */ \Drupal::service('plugin.manager.ui_patterns')->clearCachedDefinitions(); - // Ensure that the system breadcrumb is placed as well. - $this->drupalPlaceBlock('osystem_branding_block', [ + // Ensure that the system branding block is placed as well. + $this->drupalPlaceBlock('system_branding_block', [ 'region' => 'header', ]); @@ -60,19 +60,9 @@ class SiteBrandingBlockTest extends BrowserTestBase { * Asserts classes contained at site branding block. */ public function testClassesSiteName(): void { - ini_set('xdebug.var_display_max_depth', '10'); - ini_set('xdebug.var_display_max_children', '256'); - ini_set('xdebug.var_display_max_data', '10024'); $this->drupalGet('<front>'); $assert_session = $this->assertSession(); - - $webAssert = new WebAssert($this->getSession()); - $element = $webAssert->elementExists('css', 'html'); - $actual = $element->getHtml(); - var_dump($actual); - $assert_session->elementExists('css', 'div.site-name.h1.text-white.text-decoration-none.align-bottom'); - } } -- GitLab From 21f18a3bc2f9e679e501a266dfcbdb5f01d9ef6e Mon Sep 17 00:00:00 2001 From: drishu <aszilagyi@live.com> Date: Wed, 8 Sep 2021 07:36:56 +0000 Subject: [PATCH 4/6] OEL-450: Add kernel test. --- templates/README.txt | 2 - templates/overrides/README.txt | 2 - tests/src/Kernel/RenderingTest.php | 167 +++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 4 deletions(-) delete mode 100644 templates/README.txt delete mode 100644 templates/overrides/README.txt create mode 100644 tests/src/Kernel/RenderingTest.php diff --git a/templates/README.txt b/templates/README.txt deleted file mode 100644 index f208fb7a..00000000 --- a/templates/README.txt +++ /dev/null @@ -1,2 +0,0 @@ - -This directory should be used to place template files. diff --git a/templates/overrides/README.txt b/templates/overrides/README.txt deleted file mode 100644 index 2cfba3ea..00000000 --- a/templates/overrides/README.txt +++ /dev/null @@ -1,2 +0,0 @@ -Place overrides template files here, -please, try to keep same folder structure as base-theme. \ No newline at end of file diff --git a/tests/src/Kernel/RenderingTest.php b/tests/src/Kernel/RenderingTest.php new file mode 100644 index 00000000..b60d5a74 --- /dev/null +++ b/tests/src/Kernel/RenderingTest.php @@ -0,0 +1,167 @@ +<?php + +declare(strict_types = 1); + +namespace Drupal\Tests\oe_whitelabel\Kernel; + +use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Tests\token\Kernel\KernelTestBase; +use Symfony\Component\DomCrawler\Crawler; +use Symfony\Component\Yaml\Yaml; + +/** + * Tests that rendering of elements follows the theme implementation. + */ +class RenderingTest extends KernelTestBase implements FormInterface { + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oe_whitelabel_rendering_test_form'; + } + + /** + * Form constructor. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $structure + * The structure of the form, read from the fixtures files. + * + * @return array + * The form structure. + */ + public function buildForm(array $form, FormStateInterface $form_state, array $structure = NULL): array { + $form['test'] = $structure; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) {} + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) {} + + /** + * Test rendering of elements. + * + * @param array $structure + * A render array. + * @param array $assertions + * Test assertions. + * + * @throws \Exception + * + * @dataProvider renderingDataProvider + */ + public function testRendering(array $structure, array $assertions): void { + // Wrap all the test structure inside a form. This will allow proper + // processing of form elements and invocation of form alter hooks. + // Even if the elements being tested are not form related, the form can + // host them without causing any issues. + $form_state = new FormState(); + $form_state->addBuildInfo('args', [$structure]); + $form_state->setProgrammed(); + + $form = $this->container->get('form_builder')->buildForm($this, $form_state); + $this->assertRendering($this->renderRoot($form), $assertions); + } + + /** + * Data provider for rendering tests. + * + * The actual data is read from fixtures stored in a YAML configuration. + * + * @return array + * A set of dump data for testing. + */ + public function renderingDataProvider(): array { + return $this->getFixtureContent('rendering.yml'); + } + + /** + * Run various assertion on given HTML string via CSS selectors. + * + * Specifically: + * + * - 'count': assert how many times the given HTML elements occur. + * - 'equals': assert content of given HTML elements. + * - 'contains': assert content contained in given HTML elements. + * + * Assertions array has to be provided in the following format: + * + * [ + * 'count' => [ + * '.ecl-page-header' => 1, + * ], + * 'equals' => [ + * '.ecl-page-header__identity' => 'Digital single market', + * ], + * 'contains' => [ + * 'Digital', + * 'single', + * 'market', + * ], + * ] + * + * @param string $html + * A render array. + * @param array $assertions + * Test assertions. + */ + protected function assertRendering(string $html, array $assertions): void { + $crawler = new Crawler($html); + + // Assert presence of given strings. + if (isset($assertions['contains'])) { + foreach ($assertions['contains'] as $string) { + $message = "String '{$string}' not found in:" . PHP_EOL . $html; + $this->assertContains($string, $html, $message); + } + } + + // Assert occurrences of given elements. + if (isset($assertions['count'])) { + foreach ($assertions['count'] as $name => $expected) { + $message = "Wrong number of occurrences found for element '{$name}' in:" . PHP_EOL . $html; + $this->assertCount($expected, $crawler->filter($name), $message); + } + } + + // Assert that a given element content equals a given string. + if (isset($assertions['equals'])) { + foreach ($assertions['equals'] as $name => $expected) { + try { + $actual = trim($crawler->filter($name)->html()); + } + catch (\InvalidArgumentException $exception) { + $this->fail(sprintf('Element "%s" not found (exception: "%s") in: ' . PHP_EOL . ' %s', $name, $exception->getMessage(), $html)); + } + $this->assertEquals($expected, $actual); + } + } + } + + /** + * Get fixture content. + * + * @param string $filepath + * File path. + * + * @return array + * A set of test data. + */ + protected function getFixtureContent(string $filepath): array { + return Yaml::parse(file_get_contents(__DIR__ . "/fixtures/{$filepath}")); + } + +} -- GitLab From 760db5ad3caaf64223203a055ef6ee45caa39974 Mon Sep 17 00:00:00 2001 From: drishu <aszilagyi@live.com> Date: Wed, 8 Sep 2021 10:18:18 +0000 Subject: [PATCH 5/6] OEL-450: Kernel block rendering test. --- tests/Functional/SiteBrandingBlockTest.php | 68 --------- tests/src/Kernel/RenderingTest.php | 167 --------------------- tests/src/Kernel/SiteBrandingBlockTest.php | 74 +++++++++ 3 files changed, 74 insertions(+), 235 deletions(-) delete mode 100644 tests/Functional/SiteBrandingBlockTest.php delete mode 100644 tests/src/Kernel/RenderingTest.php create mode 100644 tests/src/Kernel/SiteBrandingBlockTest.php diff --git a/tests/Functional/SiteBrandingBlockTest.php b/tests/Functional/SiteBrandingBlockTest.php deleted file mode 100644 index bab61e5f..00000000 --- a/tests/Functional/SiteBrandingBlockTest.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -declare(strict_types = 1); - -namespace Drupal\Tests\oe_whitelabel\Functional; - -use Drupal\Tests\BrowserTestBase; - -/** - * Tests of Site Branding Block. - */ -class SiteBrandingBlockTest extends BrowserTestBase { - - /** - * {@inheritdoc} - */ - public static $modules = [ - 'config', - 'system', - 'ui_patterns', - 'ui_patterns_library', - 'ui_patterns_settings', - 'components', - 'field', - 'field_ui', - 'toolbar', - 'block', - ]; - - /** - * {@inheritdoc} - */ - protected $defaultTheme = 'stark'; - - /** - * {@inheritdoc} - */ - protected function setUp(): void { - parent::setUp(); - - // Enable and set OpenEuropa Whitelabel Theme as default. - \Drupal::service('theme_installer')->install(['oe_whitelabel']); - \Drupal::configFactory() - ->getEditable('system.theme') - ->set('default', 'oe_whitelabel') - ->save(); - - /* Rebuild the ui_pattern definitions to collect the ones provided by - oe_whitelabel itself. */ - \Drupal::service('plugin.manager.ui_patterns')->clearCachedDefinitions(); - - // Ensure that the system branding block is placed as well. - $this->drupalPlaceBlock('system_branding_block', [ - 'region' => 'header', - ]); - - } - - /** - * Asserts classes contained at site branding block. - */ - public function testClassesSiteName(): void { - $this->drupalGet('<front>'); - $assert_session = $this->assertSession(); - $assert_session->elementExists('css', 'div.site-name.h1.text-white.text-decoration-none.align-bottom'); - } - -} diff --git a/tests/src/Kernel/RenderingTest.php b/tests/src/Kernel/RenderingTest.php deleted file mode 100644 index b60d5a74..00000000 --- a/tests/src/Kernel/RenderingTest.php +++ /dev/null @@ -1,167 +0,0 @@ -<?php - -declare(strict_types = 1); - -namespace Drupal\Tests\oe_whitelabel\Kernel; - -use Drupal\Core\Form\FormInterface; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Tests\token\Kernel\KernelTestBase; -use Symfony\Component\DomCrawler\Crawler; -use Symfony\Component\Yaml\Yaml; - -/** - * Tests that rendering of elements follows the theme implementation. - */ -class RenderingTest extends KernelTestBase implements FormInterface { - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'oe_whitelabel_rendering_test_form'; - } - - /** - * Form constructor. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * @param array $structure - * The structure of the form, read from the fixtures files. - * - * @return array - * The form structure. - */ - public function buildForm(array $form, FormStateInterface $form_state, array $structure = NULL): array { - $form['test'] = $structure; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) {} - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) {} - - /** - * Test rendering of elements. - * - * @param array $structure - * A render array. - * @param array $assertions - * Test assertions. - * - * @throws \Exception - * - * @dataProvider renderingDataProvider - */ - public function testRendering(array $structure, array $assertions): void { - // Wrap all the test structure inside a form. This will allow proper - // processing of form elements and invocation of form alter hooks. - // Even if the elements being tested are not form related, the form can - // host them without causing any issues. - $form_state = new FormState(); - $form_state->addBuildInfo('args', [$structure]); - $form_state->setProgrammed(); - - $form = $this->container->get('form_builder')->buildForm($this, $form_state); - $this->assertRendering($this->renderRoot($form), $assertions); - } - - /** - * Data provider for rendering tests. - * - * The actual data is read from fixtures stored in a YAML configuration. - * - * @return array - * A set of dump data for testing. - */ - public function renderingDataProvider(): array { - return $this->getFixtureContent('rendering.yml'); - } - - /** - * Run various assertion on given HTML string via CSS selectors. - * - * Specifically: - * - * - 'count': assert how many times the given HTML elements occur. - * - 'equals': assert content of given HTML elements. - * - 'contains': assert content contained in given HTML elements. - * - * Assertions array has to be provided in the following format: - * - * [ - * 'count' => [ - * '.ecl-page-header' => 1, - * ], - * 'equals' => [ - * '.ecl-page-header__identity' => 'Digital single market', - * ], - * 'contains' => [ - * 'Digital', - * 'single', - * 'market', - * ], - * ] - * - * @param string $html - * A render array. - * @param array $assertions - * Test assertions. - */ - protected function assertRendering(string $html, array $assertions): void { - $crawler = new Crawler($html); - - // Assert presence of given strings. - if (isset($assertions['contains'])) { - foreach ($assertions['contains'] as $string) { - $message = "String '{$string}' not found in:" . PHP_EOL . $html; - $this->assertContains($string, $html, $message); - } - } - - // Assert occurrences of given elements. - if (isset($assertions['count'])) { - foreach ($assertions['count'] as $name => $expected) { - $message = "Wrong number of occurrences found for element '{$name}' in:" . PHP_EOL . $html; - $this->assertCount($expected, $crawler->filter($name), $message); - } - } - - // Assert that a given element content equals a given string. - if (isset($assertions['equals'])) { - foreach ($assertions['equals'] as $name => $expected) { - try { - $actual = trim($crawler->filter($name)->html()); - } - catch (\InvalidArgumentException $exception) { - $this->fail(sprintf('Element "%s" not found (exception: "%s") in: ' . PHP_EOL . ' %s', $name, $exception->getMessage(), $html)); - } - $this->assertEquals($expected, $actual); - } - } - } - - /** - * Get fixture content. - * - * @param string $filepath - * File path. - * - * @return array - * A set of test data. - */ - protected function getFixtureContent(string $filepath): array { - return Yaml::parse(file_get_contents(__DIR__ . "/fixtures/{$filepath}")); - } - -} diff --git a/tests/src/Kernel/SiteBrandingBlockTest.php b/tests/src/Kernel/SiteBrandingBlockTest.php new file mode 100644 index 00000000..5a5fbb6d --- /dev/null +++ b/tests/src/Kernel/SiteBrandingBlockTest.php @@ -0,0 +1,74 @@ +<?php + +declare(strict_types = 1); + +namespace Drupal\Tests\oe_whitelabel\Kernel; + +use Drupal\Tests\token\Kernel\KernelTestBase; +use Symfony\Component\DomCrawler\Crawler; + +/** + * Tests the Site Branding Block rendering. + */ +class SiteBrandingBlockTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['block', 'system']; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->container->get('theme_installer')->install(['oe_whitelabel']); + $this->container->get('theme_handler')->setDefault('oe_whitelabel'); + $this->container->set('theme.registry', NULL); + $this->config('system.site') + ->set('name', 'Site name') + ->set('slogan', 'Slogan') + ->save(); + + $this->container->get('cache.render')->deleteAll(); + } + + /** + * Tests the rendering of blocks. + */ + public function testBlockRendering(): void { + $entity_type_manager = $this->container + ->get('entity_type.manager') + ->getStorage('block'); + $entity = $entity_type_manager->create([ + 'id' => 'test_block', + 'theme' => 'oe_whitelabel', + 'plugin' => 'system_branding_block', + 'settings' => [ + 'id' => 'system_branding_block', + 'label' => 'Site branding', + 'provider' => 'system', + 'label_display' => '0', + 'use_site_logo' => TRUE, + 'use_site_name' => TRUE, + 'use_site_slogan' => FALSE, + ], + ]); + $entity->save(); + $builder = \Drupal::entityTypeManager()->getViewBuilder('block'); + $build = $builder->view($entity, 'block'); + $render = $this->container->get('renderer')->renderRoot($build); + $crawler = new Crawler($render->__toString()); + + $actual = $crawler->filter('.site-name.h1.text-white.text-decoration-none.align-bottom'); + $this->assertCount(1, $actual); + $actual = $crawler->filter('.site-logo'); + $this->assertCount(1, $actual); + $logo = $actual->filter('img'); + $this->assertCount(1, $logo); + $expected = '/' . drupal_get_path('theme', 'oe_whitelabel') . '/logo.svg'; + $this->assertSame($expected, $logo->attr('src')); + } + +} -- GitLab From 4a610f7833f3019697d51ef4617b9a162b34c5cb Mon Sep 17 00:00:00 2001 From: drishu <aszilagyi@live.com> Date: Wed, 8 Sep 2021 10:22:10 +0000 Subject: [PATCH 6/6] OEL-450: Move classes to link. --- .../navigation/block--system-branding-block.html.twig | 4 ++-- tests/src/Kernel/SiteBrandingBlockTest.php | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/templates/overrides/navigation/block--system-branding-block.html.twig b/templates/overrides/navigation/block--system-branding-block.html.twig index 8997f69c..7b43dd7c 100644 --- a/templates/overrides/navigation/block--system-branding-block.html.twig +++ b/templates/overrides/navigation/block--system-branding-block.html.twig @@ -20,8 +20,8 @@ </a> {% endif %} {% if site_name %} - <div class="site-name h1 text-white text-decoration-none align-bottom"> - <a href="{{ path('<front>') }}" rel="home">{{ site_name }}</a> + <div class="site-name"> + <a class="h1 text-white text-decoration-none align-bottom" href="{{ path('<front>') }}" rel="home">{{ site_name }}</a> </div> {% endif %} {% if site_slogan %} diff --git a/tests/src/Kernel/SiteBrandingBlockTest.php b/tests/src/Kernel/SiteBrandingBlockTest.php index 5a5fbb6d..a491a95d 100644 --- a/tests/src/Kernel/SiteBrandingBlockTest.php +++ b/tests/src/Kernel/SiteBrandingBlockTest.php @@ -61,8 +61,10 @@ class SiteBrandingBlockTest extends KernelTestBase { $render = $this->container->get('renderer')->renderRoot($build); $crawler = new Crawler($render->__toString()); - $actual = $crawler->filter('.site-name.h1.text-white.text-decoration-none.align-bottom'); + $actual = $crawler->filter('.site-name'); $this->assertCount(1, $actual); + $link = $actual->filter('.h1.text-white.text-decoration-none.align-bottom'); + $this->assertCount(1, $link); $actual = $crawler->filter('.site-logo'); $this->assertCount(1, $actual); $logo = $actual->filter('img'); -- GitLab