View source
<?php
namespace Drupal\Tests\Core\EventSubscriber;
use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\ActiveLinkResponseFilter;
use Drupal\Core\Template\Attribute;
use Drupal\Tests\UnitTestCase;
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 = array(
0 => array(
'prefix' => '<div><p>',
'suffix' => '</p></div>',
),
1 => array(
'prefix' => '<div><p>',
'suffix' => '</p>' . $edge_case_html5 . '</div>',
),
2 => array(
'prefix' => '<div><p>αβγδεζηθικλμνξοσὠ</p><p>',
'suffix' => '</p></div>',
),
);
$tags = array(
'a',
'li',
'foo',
);
$contents = array(
'test',
'☆ 3 × 4 = €12 and 4 × 3 = €12 ☆',
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ',
'data-drupal-link-system-path="<front>"',
);
$situations = array();
$context = array(
'path' => 'myfrontpage',
'front' => TRUE,
'language' => 'en',
'query' => array(),
);
$markup = '<foo>bar</foo>';
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => array(),
);
$attributes = array(
'data-drupal-link-system-path' => 'myfrontpage',
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
),
);
$attributes = array(
'data-drupal-link-system-path' => '<front>',
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
),
);
$context = array(
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => array(),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => array(),
);
$attributes = array(
'data-drupal-link-system-path' => 'llama',
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$attributes = array(
'data-drupal-link-system-path' => '<front>',
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => '{"foo":"bar"}',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$context = array(
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => array(
'foo' => 'bar',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => array(),
);
$attributes = array(
'data-drupal-link-system-path' => 'llama',
'data-drupal-link-query' => Json::encode(array(
'foo' => 'bar',
)),
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$attributes = array(
'data-drupal-link-system-path' => '<front>',
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$context = array(
'path' => 'llama',
'front' => FALSE,
'language' => 'nl',
'query' => array(
'foo' => 'bar',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => array(),
);
$attributes = array(
'data-drupal-link-system-path' => 'llama',
'data-drupal-link-query' => Json::encode(array(
'foo' => 'bar',
)),
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$attributes = array(
'data-drupal-link-system-path' => '<front>',
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
'data-drupal-link-query' => TRUE,
),
);
$context = array(
'path' => 'myfrontpage',
'front' => TRUE,
'language' => 'en',
'query' => array(
'foo' => 'bar',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => array(),
);
$attributes = array(
'data-drupal-link-system-path' => 'myfrontpage',
'data-drupal-link-query' => Json::encode(array(
'foo' => 'bar',
)),
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
),
);
$attributes = array(
'data-drupal-link-system-path' => '<front>',
'data-drupal-link-query' => Json::encode(array(
'foo' => 'bar',
)),
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes,
);
$situations[] = array(
'context' => $context,
'is active' => TRUE,
'attributes' => $attributes + array(
'hreflang' => 'en',
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'nl',
),
);
unset($attributes['data-drupal-link-query']);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'data-drupal-link-query' => TRUE,
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => "",
),
);
$situations[] = array(
'context' => $context,
'is active' => FALSE,
'attributes' => $attributes + array(
'hreflang' => 'en',
'data-drupal-link-query' => TRUE,
),
);
$data = array();
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'] = array();
}
$active_attributes['class'][] = 'is-active';
$target_markup = $create_markup(new Attribute($active_attributes));
}
$data[] = array(
$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));
}
}