View source
<?php
namespace Drupal\Tests\Core\Template;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\AttributeArray;
use Drupal\Core\Template\AttributeString;
use Drupal\Core\Template\Loader\StringLoader;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Render\MarkupInterface;
use Twig\Environment;
class AttributeTest extends UnitTestCase {
public function testConstructor() {
$attribute = new Attribute([
'class' => [
'example-class',
],
]);
$this
->assertTrue(isset($attribute['class']));
$this
->assertEquals(new AttributeArray('class', [
'example-class',
]), $attribute['class']);
$attribute = new Attribute([
'selected' => TRUE,
'checked' => FALSE,
]);
$this
->assertTrue($attribute['selected']
->value());
$this
->assertFalse($attribute['checked']
->value());
$attribute = new Attribute([
'class' => 'example-class',
]);
$this
->assertTrue(isset($attribute['class']));
$this
->assertEquals(new AttributeArray('class', [
'example-class',
]), $attribute['class']);
$safe_string = $this
->prophesize(MarkupInterface::class);
$safe_string
->__toString()
->willReturn('example-class');
$attribute = new Attribute([
'class' => $safe_string
->reveal(),
]);
$this
->assertTrue(isset($attribute['class']));
$this
->assertEquals(new AttributeArray('class', [
'example-class',
]), $attribute['class']);
}
public function testSet() {
$attribute = new Attribute();
$attribute['class'] = [
'example-class',
];
$this
->assertTrue(isset($attribute['class']));
$this
->assertEquals(new AttributeArray('class', [
'example-class',
]), $attribute['class']);
}
public function testAdd() {
$attribute = new Attribute([
'class' => [
'example-class',
],
]);
$attribute['class'][] = 'other-class';
$this
->assertEquals(new AttributeArray('class', [
'example-class',
'other-class',
]), $attribute['class']);
}
public function testRemove() {
$attribute = new Attribute([
'class' => [
'example-class',
],
]);
unset($attribute['class']);
$this
->assertFalse(isset($attribute['class']));
}
public function testSetAttribute() {
$attribute = new Attribute();
$attributes = [
'alt',
'id',
'src',
'title',
'value',
];
foreach ($attributes as $key) {
foreach ([
'kitten',
'',
] as $value) {
$attribute = new Attribute();
$attribute
->setAttribute($key, $value);
$this
->assertEquals($value, $attribute[$key]);
}
}
$attribute = new Attribute();
$attribute
->setAttribute('class', [
'kitten',
'cat',
]);
$this
->assertEquals([
'kitten',
'cat',
], $attribute['class']
->value());
$attribute = new Attribute();
$attribute['checked'] = TRUE;
$this
->assertTrue($attribute['checked']
->value());
}
public function testRemoveAttribute() {
$attributes = [
'alt' => 'Alternative text',
'id' => 'bunny',
'src' => 'zebra',
'style' => 'color: pink;',
'title' => 'kitten',
'value' => 'ostrich',
'checked' => TRUE,
];
$attribute = new Attribute($attributes);
$attribute
->removeAttribute('alt');
$this
->assertEmpty($attribute['alt']);
$attribute
->removeAttribute('id', 'src');
$this
->assertEmpty($attribute['id']);
$this
->assertEmpty($attribute['src']);
$attribute
->removeAttribute([
'style',
]);
$this
->assertEmpty($attribute['style']);
$attribute
->removeAttribute('checked');
$this
->assertEmpty($attribute['checked']);
$attribute
->removeAttribute([
'title',
'value',
]);
$this
->assertEmpty((string) $attribute);
}
public function testAddClasses() {
$attribute = new Attribute();
$attribute
->addClass();
$this
->assertEmpty($attribute['class']);
foreach ([
NULL,
FALSE,
'',
[],
] as $value) {
$attribute
->addClass($value);
$this
->assertEmpty((string) $attribute);
$attribute
->addClass($value, $value);
$this
->assertEmpty((string) $attribute);
$attribute
->addClass([
$value,
]);
$this
->assertEmpty((string) $attribute);
$attribute
->addClass([
$value,
], [
$value,
]);
$this
->assertEmpty((string) $attribute);
}
$attribute
->addClass('banana');
$this
->assertEquals([
'banana',
], $attribute['class']
->value());
$attribute
->addClass('aa');
$this
->assertEquals([
'banana',
'aa',
], $attribute['class']
->value());
$attribute
->addClass('xx', 'yy');
$this
->assertEquals([
'banana',
'aa',
'xx',
'yy',
], $attribute['class']
->value());
$attribute
->addClass([
'red',
'green',
'blue',
]);
$this
->assertEquals([
'banana',
'aa',
'xx',
'yy',
'red',
'green',
'blue',
], $attribute['class']
->value());
$attribute
->addClass([
'red',
'green',
'blue',
], [
'aa',
'aa',
'banana',
], 'yy');
$this
->assertEquals('banana aa xx yy red green blue', (string) $attribute['class']);
}
public function testRemoveClasses() {
$classes = [
'example-class',
'aa',
'xx',
'yy',
'red',
'green',
'blue',
'red',
];
$attribute = new Attribute([
'class' => $classes,
]);
$attribute
->removeClass('example-class');
$this
->assertNotContains('example-class', $attribute['class']
->value());
$attribute
->removeClass('xx', 'yy');
$this
->assertNotContains([
'xx',
'yy',
], $attribute['class']
->value());
$attribute
->removeClass([
'red',
'green',
'blue',
]);
$this
->assertNotContains([
'red',
'green',
'blue',
], $attribute['class']
->value());
$attribute
->removeClass('gg');
$this
->assertNotContains([
'gg',
], $attribute['class']
->value());
$this
->assertEquals([
'aa',
], $attribute['class']
->value());
$attribute
->removeClass('aa');
$this
->assertEmpty((string) $attribute);
}
public function testHasClass() {
$attribute = new Attribute();
$this
->assertFalse($attribute
->hasClass('a-class-nowhere-to-be-found'));
$attribute
->addClass('we-totally-have-this-class');
$this
->assertTrue($attribute
->hasClass('we-totally-have-this-class'));
}
public function testChainAddRemoveClasses() {
$attribute = new Attribute([
'class' => [
'example-class',
'red',
'green',
'blue',
],
]);
$attribute
->removeClass([
'red',
'green',
'pink',
])
->addClass([
'apple',
'lime',
'grapefruit',
])
->addClass([
'banana',
]);
$expected = [
'example-class',
'blue',
'apple',
'lime',
'grapefruit',
'banana',
];
$this
->assertEquals($expected, $attribute['class']
->value(), 'Attributes chained');
}
public function testTwigAddRemoveClasses($template, $expected, $seed_attributes = []) {
$loader = new StringLoader();
$twig = new Environment($loader);
$data = [
'attributes' => new Attribute($seed_attributes),
];
$result = $twig
->createTemplate($template)
->render($data);
$this
->assertEquals($expected, $result);
}
public function providerTestAttributeClassHelpers() {
return [
[
"{{ attributes.class }}",
'',
],
[
"{{ attributes.addClass('everest').class }}",
'everest',
],
[
"{{ attributes.addClass(['k2', 'kangchenjunga']).class }}",
'k2 kangchenjunga',
],
[
"{{ attributes.addClass('lhotse', 'makalu', 'cho-oyu').class }}",
'lhotse makalu cho-oyu',
],
[
"{{ attributes.addClass('nanga-parbat').class }}",
'dhaulagiri manaslu nanga-parbat',
[
'class' => [
'dhaulagiri',
'manaslu',
],
],
],
[
"{{ attributes.removeClass('annapurna').class }}",
'gasherbrum-i',
[
'class' => [
'annapurna',
'gasherbrum-i',
],
],
],
[
"{{ attributes.removeClass(['broad peak']).class }}",
'gasherbrum-ii',
[
'class' => [
'broad peak',
'gasherbrum-ii',
],
],
],
[
"{{ attributes.removeClass('gyachung-kang', 'shishapangma').class }}",
'',
[
'class' => [
'shishapangma',
'gyachung-kang',
],
],
],
[
"{{ attributes.removeClass('nuptse').addClass('annapurna-ii').class }}",
'himalchuli annapurna-ii',
[
'class' => [
'himalchuli',
'nuptse',
],
],
],
[
"{{ attributes.addClass('rakaposhi', '').class }}",
'rakaposhi',
],
];
}
public function testIterate() {
$attribute = new Attribute([
'class' => [
'example-class',
],
'id' => 'example-id',
]);
$counter = 0;
foreach ($attribute as $key => $value) {
if ($counter == 0) {
$this
->assertEquals('class', $key);
$this
->assertEquals(new AttributeArray('class', [
'example-class',
]), $value);
}
if ($counter == 1) {
$this
->assertEquals('id', $key);
$this
->assertEquals(new AttributeString('id', 'example-id'), $value);
}
$counter++;
}
}
public function testPrint() {
$attribute = new Attribute([
'class' => [
'example-class',
],
'id' => 'example-id',
'enabled' => TRUE,
]);
$content = $this
->randomMachineName();
$html = '<div' . (string) $attribute . '>' . $content . '</div>';
$this
->assertClass('example-class', $html);
$this
->assertNoClass('example-class2', $html);
$this
->assertID('example-id', $html);
$this
->assertNoID('example-id2', $html);
$this
->assertStringContainsString('enabled', $html);
}
public function testAttributeValues(array $attributes, $expected) {
$this
->assertEquals($expected, (new Attribute($attributes))
->__toString());
}
public function providerTestAttributeValues() {
$data = [];
$string = '"> <script>alert(123)</script>"';
$data['safe-object-xss1'] = [
[
'title' => Markup::create($string),
],
' title=""> alert(123)""',
];
$data['non-safe-object-xss1'] = [
[
'title' => $string,
],
' title="' . Html::escape($string) . '"',
];
$string = '"><script>alert(123)</script>';
$data['safe-object-xss2'] = [
[
'title' => Markup::create($string),
],
' title="">alert(123)"',
];
$data['non-safe-object-xss2'] = [
[
'title' => $string,
],
' title="' . Html::escape($string) . '"',
];
return $data;
}
protected function assertClass($class, $html) {
$xpath = "//*[@class='{$class}']";
self::assertTrue((bool) $this
->getXPathResultCount($xpath, $html));
}
protected function assertNoClass($class, $html) {
$xpath = "//*[@class='{$class}']";
self::assertFalse((bool) $this
->getXPathResultCount($xpath, $html));
}
protected function assertID($id, $html) {
$xpath = "//*[@id='{$id}']";
self::assertTrue((bool) $this
->getXPathResultCount($xpath, $html));
}
protected function assertNoID($id, $html) {
$xpath = "//*[@id='{$id}']";
self::assertFalse((bool) $this
->getXPathResultCount($xpath, $html));
}
protected function getXPathResultCount($query, $html) {
$document = new \DOMDocument();
$document
->loadHTML($html);
$xpath = new \DOMXPath($document);
return $xpath
->query($query)->length;
}
public function testStorage() {
$attribute = new Attribute([
'class' => [
'example-class',
],
]);
$this
->assertEquals([
'class' => new AttributeArray('class', [
'example-class',
]),
], $attribute
->storage());
}
public function providerTestHasAttribute() {
return [
[
[
'class' => [
'example-class',
],
],
'class',
TRUE,
],
[
[],
'class',
FALSE,
],
[
[
'class' => [
'example-class',
],
],
'id',
FALSE,
],
[
[
'class' => [
'example-class',
],
'id' => 'foo',
],
'id',
TRUE,
],
[
[
'id' => 'foo',
],
'class',
FALSE,
],
];
}
public function testHasAttribute(array $test_data, $test_attribute, $expected) {
$attributes = new Attribute($test_data);
$this
->assertSame($expected, $attributes
->hasAttribute($test_attribute));
}
public function providerTestMerge() {
return [
[
new Attribute([]),
new Attribute([
'class' => [
'class1',
],
]),
new Attribute([
'class' => [
'class1',
],
]),
],
[
new Attribute([
'class' => [
'example-class',
],
]),
new Attribute([
'class' => [
'class1',
],
]),
new Attribute([
'class' => [
'example-class',
'class1',
],
]),
],
[
new Attribute([
'class' => [
'example-class',
],
]),
new Attribute([
'id' => 'foo',
'href' => 'bar',
]),
new Attribute([
'class' => [
'example-class',
],
'id' => 'foo',
'href' => 'bar',
]),
],
];
}
public function testMerge($original, $merge, $expected) {
$this
->assertEquals($expected, $original
->merge($merge));
}
public function testMergeArgumentException() {
$attributes = new Attribute([
'class' => [
'example-class',
],
]);
$this
->expectException(\TypeError::class);
$attributes
->merge('not an array');
}
}