Skip to content

Commit

Permalink
OEL-2002: Add TwigExtension test.
Browse files Browse the repository at this point in the history
  • Loading branch information
drishu committed Sep 26, 2022
1 parent 8ca4a35 commit 4549cd7
Showing 1 changed file with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_bootstrap_theme_helper\Kernel;

use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Tests\oe_bootstrap_theme\Kernel\AbstractKernelTestBase;
use Symfony\Component\DomCrawler\Crawler;

/**
* Test those Twig extensions that require Drupal to be bootstrapped.
*/
class TwigExtensionTest extends AbstractKernelTestBase {

/**
* Test bcl_link function.
*/
public function testBclLinkFunction(): void {
$items = $this->bclLinkDataProvider();

foreach ($items as $item) {
[$expected, $data] = $item;
$elements = [
'#type' => 'inline_template',
'#template' => '{{ bcl_link(label, path, attributes) }}',
'#context' => [
'label' => $data['label'],
'path' => $data['path'],
'attributes' => new Attribute($data['attributes'] ?? []),
],
];

$output = $this->renderRoot($elements);
$crawler = new Crawler($output);
$link = $crawler->filter('a');

$this->assertSame($expected['href'], $link->attr('href'));
$this->assertSame($expected['text'], $link->html());
}
}

/**
* Data provider for bclLinkFunction.
*
* @return array
* An array of test data arrays with assertions.
*/
public function bclLinkDataProvider(): array {
return [
'string_path_to_front' => [
[
'href' => '/',
'text' => 'My link',
],
[
'path' => '/',
'label' => 'My link',
],
],
'string_path_front_with_hash' => [
[
'href' => '/user/login#kitty',
'text' => 'Miau',
],
[
'path' => '/#kitty',
'label' => 'Miau',
],
],
'string_path_current_url_with_hash' => [
[
'href' => '/user/login#kitty',
'text' => 'Miau',
],
[
'path' => '#kitty',
'label' => 'Miau',
],
],
'just_hash' => [
[
// Is this right ?
'href' => '/user/login',
'text' => 'hash',
],
[
'path' => '#',
'label' => 'hash',
],
],
'string_path_current_url_with_parameters' => [
[
'href' => '/user/login?foo=baz',
'text' => 'My link',
],
[
'path' => '?foo=baz',
'label' => 'My link',
],
],
'string_path_internal' => [
[
'href' => '/some-path',
'text' => 'Some link',
],
[
'path' => '/some-path',
'label' => 'Some link',
],
],
'label_with_safe_markup' => [
[
'href' => '/',
'text' => 'This is <em>markup</em>!',
],
[
'path' => '/',
'label' => ['#markup' => 'This is <em>markup</em>!'],
],
],
'label_with_unsafe_markup' => [
[
'href' => '/',
'text' => 'This is &lt;script type="text/javascript"&gt;unsafe&lt;/script&gt; &lt;em&gt;markup&lt;/em&gt;!',
],
[
'path' => '/',
'label' => 'This is <script type="text/javascript">unsafe</script> <em>markup</em>!',
],
],
'url_object' => [
[
'href' => '/node/add',
'text' => 'Url object link',
],
[
'path' => Url::fromUserInput('/node/add'),
'label' => 'Url object link',
],
],
'url_object_absolute' => [
[
'href' => 'http://localhost/here',
'text' => 'Absolute url link',
],
[
'path' => Url::fromUri('base:here', ['absolute' => TRUE]),
'label' => 'Absolute url link',
],
],
];
}

}

0 comments on commit 4549cd7

Please sign in to comment.