View source
<?php
namespace Drupal\Tests\Core\EventSubscriber;
use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\ActiveLinkResponseFilter;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Template\Attribute;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class ActiveLinkResponseFilterTest extends UnitTestCase {
public function providerTestSetLinkActiveClass() {
$edge_case_html5 = '<audio src="foo.ogg">
<track kind="captions" src="foo.en.vtt" srclang="en" label="English">
<track kind="captions" src="foo.sv.vtt" srclang="sv" label="Svenska">
</audio>';
$html = [
0 => [
'prefix' => '<div><p>',
'suffix' => '</p></div>',
],
1 => [
'prefix' => '<div><p>',
'suffix' => '</p>' . $edge_case_html5 . '</div>',
],
2 => [
'prefix' => '<div><p>αβγδεζηθικλμνξοσὠ</p><p>',
'suffix' => '</p></div>',
],
];
$tags = [
'a',
'li',
'foo',
];
$contents = [
'test',
'☆ 3 × 4 = €12 and 4 × 3 = €12 ☆',
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ',
'data-drupal-link-system-path="<front>"',
];
$situations = [];
$context = [
'path' => 'myfrontpage',
'front' => TRUE,
'language' => 'en',
'query' => [],
];
$markup = '<foo>bar</foo>';
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => [],
];
$attributes = [
'data-drupal-link-system-path' => 'myfrontpage',
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
],
];
$attributes = [
'data-drupal-link-system-path' => '<front>',
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
],
];
$context = [
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => [],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => [],
];
$attributes = [
'data-drupal-link-system-path' => 'llama',
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$attributes = [
'data-drupal-link-system-path' => '<front>',
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => '{"foo":"bar"}',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$context = [
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => [
'foo' => 'bar',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => [],
];
$attributes = [
'data-drupal-link-system-path' => 'llama',
'data-drupal-link-query' => Json::encode([
'foo' => 'bar',
]),
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$attributes = [
'data-drupal-link-system-path' => '<front>',
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$context = [
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => [
'foo' => 'bar',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => [],
];
$attributes = [
'data-drupal-link-system-path' => 'llama',
'data-drupal-link-query' => Json::encode([
'foo' => 'bar',
]),
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$attributes = [
'data-drupal-link-system-path' => '<front>',
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
],
];
$context = [
'path' => 'myfrontpage',
'front' => TRUE,
'language' => 'en',
'query' => [
'foo' => 'bar',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => [],
];
$attributes = [
'data-drupal-link-system-path' => 'myfrontpage',
'data-drupal-link-query' => Json::encode([
'foo' => 'bar',
]),
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
],
];
$attributes = [
'data-drupal-link-system-path' => '<front>',
'data-drupal-link-query' => Json::encode([
'foo' => 'bar',
]),
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + [
'hreflang' => 'en',
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'nl',
],
];
unset($attributes['data-drupal-link-query']);
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'data-drupal-link-query' => TRUE,
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => "",
],
];
$situations[] = [
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + [
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
],
];
$context = [
'path' => 'myfrontpage',
'front' => TRUE,
'language' => 'en',
'query' => [
'foo' => 'bar',
'baz' => 'qux',
],
];
$attributes = [
'data-drupal-link-system-path' => 'myfrontpage',
'data-drupal-link-query' => Json::encode([
'baz' => 'qux',
'foo' => 'bar',
]),
];
$situations[] = [
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
];
$data = [];
for ($h = 0; $h < count($html); $h++) {
$html_prefix = $html[$h]['prefix'];
$html_suffix = $html[$h]['suffix'];
for ($t = 0; $t < count($tags); $t++) {
$tag = $tags[$t];
for ($c = 0; $c < count($contents); $c++) {
$tag_content = $contents[$c];
$create_markup = function (Attribute $attributes) use ($html_prefix, $html_suffix, $tag, $tag_content) {
return $html_prefix . '<' . $tag . $attributes . '>' . $tag_content . '</' . $tag . '>' . $html_suffix;
};
for ($s = 0; $s < count($situations); $s++) {
$situation = $situations[$s];
$source_markup = $create_markup(new Attribute($situation['attributes']));
$target_markup = NULL;
if (!$situation['is active']) {
$target_markup = $source_markup;
}
else {
$active_attributes = $situation['attributes'];
if (!isset($active_attributes['class'])) {
$active_attributes['class'] = [];
}
$active_attributes['class'][] = 'is-active';
$target_markup = $create_markup(new Attribute($active_attributes));
}
$data[] = [
$source_markup,
$situation['context']['path'],
$situation['context']['front'],
$situation['context']['language'],
$situation['context']['query'],
$target_markup,
];
}
}
}
}
$data[] = [
0 => '<a data-drupal-link-system-path="<front>">Once</a> <a data-drupal-link-system-path="<front>">Twice</a>',
1 => '',
2 => TRUE,
3 => 'en',
4 => [],
5 => '<a data-drupal-link-system-path="<front>" class="is-active">Once</a> <a data-drupal-link-system-path="<front>" class="is-active">Twice</a>',
];
$front_special_link = '<a data-drupal-link-system-path="<front>">Front</a>';
$front_special_link_active = '<a data-drupal-link-system-path="<front>" class="is-active">Front</a>';
$front_path_link = '<a data-drupal-link-system-path="myfrontpage">Front Path</a>';
$front_path_link_active = '<a data-drupal-link-system-path="myfrontpage" class="is-active">Front Path</a>';
$data[] = [
0 => $front_path_link . ' ' . $front_special_link,
1 => 'myfrontpage',
2 => TRUE,
3 => 'en',
4 => [],
5 => $front_path_link_active . ' ' . $front_special_link_active,
];
$data[] = [
0 => $front_special_link . ' ' . $front_path_link,
1 => 'myfrontpage',
2 => TRUE,
3 => 'en',
4 => [],
5 => $front_special_link_active . ' ' . $front_path_link_active,
];
$other_link = '<a data-drupal-link-system-path="otherpage">Other page</a>';
$other_link_active = '<a data-drupal-link-system-path="otherpage" class="is-active">Other page</a>';
$data['<front>-and-other-link-on-other-path'] = [
0 => $front_special_link . ' ' . $other_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $front_special_link . ' ' . $other_link_active,
];
$data['front-and-other-link-on-other-path'] = [
0 => $front_path_link . ' ' . $other_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $front_path_link . ' ' . $other_link_active,
];
$data['other-and-<front>-link-on-other-path'] = [
0 => $other_link . ' ' . $front_special_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $other_link_active . ' ' . $front_special_link,
];
$data['other-and-front-link-on-other-path'] = [
0 => $other_link . ' ' . $front_path_link,
1 => 'otherpage',
2 => FALSE,
3 => 'en',
4 => [],
5 => $other_link_active . ' ' . $front_path_link,
];
return $data;
}
public function testSetLinkActiveClass($html_markup, $current_path, $is_front, $url_language, array $query, $expected_html_markup) {
$this
->assertSame($expected_html_markup, ActiveLinkResponseFilter::setLinkActiveClass($html_markup, $current_path, $is_front, $url_language, $query));
}
public function testOnlyHtml() {
$session = new AnonymousUserSession();
$language_manager = new LanguageManager(new LanguageDefault([]));
$request_stack = new RequestStack();
$request_stack
->push(new Request());
$current_path_stack = new CurrentPathStack($request_stack);
$path_matcher = $this
->prophesize(PathMatcherInterface::class);
$path_matcher
->isFrontPage()
->shouldNotBeCalled();
$subscriber = new ActiveLinkResponseFilter($session, $current_path_stack, $path_matcher
->reveal(), $language_manager);
$content = '<a data-drupal-link-system-path="otherpage">Other page</a>';
$response = new Response();
$response
->setContent($content);
$response->headers
->get('Content-Type', 'application/json');
$subscriber
->onResponse(new FilterResponseEvent($this
->prophesize(KernelInterface::class)
->reveal(), $request_stack
->getCurrentRequest(), HttpKernelInterface::MASTER_REQUEST, $response));
$this
->assertSame($response
->getContent(), $content);
}
public function testSkipCertainResponseTypes() {
$session = new AnonymousUserSession();
$language_manager = new LanguageManager(new LanguageDefault([]));
$request_stack = new RequestStack();
$request_stack
->push(new Request());
$current_path_stack = new CurrentPathStack($request_stack);
$path_matcher = $this
->prophesize(PathMatcherInterface::class);
$path_matcher
->isFrontPage()
->shouldNotBeCalled();
$subscriber = new ActiveLinkResponseFilter($session, $current_path_stack, $path_matcher
->reveal(), $language_manager);
$response = new BinaryFileResponse(__FILE__, 200, [
'Content-Type' => 'text/html',
]);
$subscriber
->onResponse(new FilterResponseEvent($this
->prophesize(KernelInterface::class)
->reveal(), $request_stack
->getCurrentRequest(), HttpKernelInterface::MASTER_REQUEST, $response));
$response = new StreamedResponse(function () {
echo 'Success!';
}, 200, [
'Content-Type' => 'text/html',
]);
$subscriber
->onResponse(new FilterResponseEvent($this
->prophesize(KernelInterface::class)
->reveal(), $request_stack
->getCurrentRequest(), HttpKernelInterface::MASTER_REQUEST, $response));
}
}