Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Common / AttributesTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Common;
4
5 use Drupal\Core\Template\Attribute;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the Drupal\Core\Template\Attribute functionality.
10  *
11  * @group Common
12  */
13 class AttributesTest extends UnitTestCase {
14
15   /**
16    * Provides data for the Attribute test.
17    *
18    * @return array
19    */
20   public function providerTestAttributeData() {
21     return [
22       // Verify that special characters are HTML encoded.
23       [['&"\'<>' => 'value'], ' &amp;&quot;&#039;&lt;&gt;="value"', 'HTML encode attribute names.'],
24       [['title' => '&"\'<>'], ' title="&amp;&quot;&#039;&lt;&gt;"', 'HTML encode attribute values.'],
25       // Verify multi-value attributes are concatenated with spaces.
26       [['class' => ['first', 'last']], ' class="first last"', 'Concatenate multi-value attributes.'],
27       // Verify boolean attribute values are rendered correctly.
28       [['disabled' => TRUE], ' disabled', 'Boolean attribute is rendered.'],
29       [['disabled' => FALSE], '', 'Boolean attribute is not rendered.'],
30       // Verify empty attribute values are rendered.
31       [['alt' => ''], ' alt=""', 'Empty attribute value #1.'],
32       [['alt' => NULL], '', 'Null attribute value #2.'],
33       // Verify multiple attributes are rendered.
34       [
35         [
36           'id' => 'id-test',
37           'class' => ['first', 'last'],
38           'alt' => 'Alternate',
39         ],
40         ' id="id-test" class="first last" alt="Alternate"',
41         'Multiple attributes.',
42       ],
43       // Verify empty attributes array is rendered.
44       [[], '', 'Empty attributes array.'],
45     ];
46   }
47
48   /**
49    * Tests casting an Attribute object to a string.
50    *
51    * @see \Drupal\Core\Template\Attribute::__toString()
52    *
53    * @dataProvider providerTestAttributeData
54    */
55   public function testDrupalAttributes($attributes, $expected, $message) {
56     $this->assertSame($expected, (string) new Attribute($attributes), $message);
57   }
58
59   /**
60    * Test attribute iteration
61    */
62   public function testAttributeIteration() {
63     $attribute = new Attribute(['key1' => 'value1']);
64     foreach ($attribute as $value) {
65       $this->assertSame((string) $value, 'value1', 'Iterate over attribute.');
66     }
67   }
68
69   /**
70    * Test AttributeValueBase copy.
71    */
72   public function testAttributeValueBaseCopy() {
73     $original_attributes = new Attribute([
74       'checked' => TRUE,
75       'class' => ['who', 'is', 'on'],
76       'id' => 'first',
77     ]);
78     $attributes['selected'] = $original_attributes['checked'];
79     $attributes['id'] = $original_attributes['id'];
80     $attributes = new Attribute($attributes);
81     $this->assertSame(' checked class="who is on" id="first"', (string) $original_attributes, 'Original boolean value used with original name.');
82     $this->assertSame(' selected id="first"', (string) $attributes, 'Original boolean value used with new name.');
83   }
84
85 }