diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 076cfd3..e9ede3b 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -6,6 +6,7 @@ */ namespace Drupal\Core\Template; +use Drupal\Component\Utility\String; /** @@ -103,7 +104,7 @@ public function __toString() { $return = ''; foreach ($this->storage as $name => $value) { if (!$value->printed()) { - $rendered = is_object($value) ? $value->render() : (check_plain($name) . ' = "' . check_plain($value) . '"'); + $rendered = is_object($value) ? $value->render() : (String::checkPlain($name) . ' = "' . String::checkPlain($value) . '"'); if ($rendered) { $return .= " $rendered"; } @@ -133,8 +134,8 @@ public function getIterator() { /** * Returns the whole array. */ - public function value() { - return $this->value; + public function storage() { + return $this->storage; } } diff --git a/core/lib/Drupal/Core/Template/AttributeBoolean.php b/core/lib/Drupal/Core/Template/AttributeBoolean.php index 48ca388..ecd1dcf 100644 --- a/core/lib/Drupal/Core/Template/AttributeBoolean.php +++ b/core/lib/Drupal/Core/Template/AttributeBoolean.php @@ -6,6 +6,7 @@ */ namespace Drupal\Core\Template; +use Drupal\Component\Utility\String; /** * A class that defines a type of boolean HTML attribute. @@ -41,7 +42,7 @@ public function render() { */ public function __toString() { $this->printed = TRUE; - return $this->value === FALSE ? '' : check_plain($this->name); + return $this->value === FALSE ? '' : String::checkPlain($this->name); } } diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php new file mode 100644 index 0000000..f0abdd9 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -0,0 +1,114 @@ + 'Attribute class', + 'description' => 'Tests the template attribute class.', + 'group' => 'Template', + ); + } + + /** + * Tests the constructor of the attribute class. + */ + public function testConstructor() { + $attribute = new Attribute(array('class' => array('example-class'))); + $this->assertTrue(isset($attribute['class'])); + $this->assertEquals(new AttributeArray('class', array('example-class')), $attribute['class']); + } + + /** + * Tests set of values. + */ + public function testSet() { + $attribute = new Attribute(); + $attribute['class'] = array('example-class'); + + $this->assertTrue(isset($attribute['class'])); + $this->assertEquals(new AttributeArray('class', array('example-class')), $attribute['class']); + } + + /** + * Tests adding new values to an existing part of the attribute. + */ + public function testAdd() { + $attribute = new Attribute(array('class' => array('example-class'))); + + $attribute['class'][] = 'other-class'; + $this->assertEquals(new AttributeArray('class', array('example-class', 'other-class')), $attribute['class']); + } + + /** + * Tests removing of values. + */ + public function testRemove() { + $attribute = new Attribute(array('class' => array('example-class'))); + unset($attribute['class']); + $this->assertFalse(isset($attribute['class'])); + } + + /** + * Tests iterating on the values of the attribute. + */ + public function testIterate() { + $attribute = new Attribute(array('class' => array('example-class'), 'id' => 'example-id')); + + $counter = 0; + foreach ($attribute as $key => $value) { + if ($counter == 0) { + $this->assertEquals('class', $key); + $this->assertEquals(new AttributeArray('class', array('example-class')), $value); + } + if ($counter == 1) { + $this->assertEquals('id', $key); + $this->assertEquals(new AttributeString('id', 'example-id'), $value); + } + $counter++; + } + } + + /** + * Tests printing of an attribute. + */ + public function testPrint() { + $attribute = new Attribute(array('class' => array('example-class'), 'id' => 'example-id', 'enabled' => TRUE)); + + $html = ''; + $this->assertSelectEquals('div.example-class', '', 1, $html); + $this->assertSelectEquals('div.example-class2', '', 0, $html); + + $this->assertSelectEquals('div#example-id', '', 1, $html); + $this->assertSelectEquals('div#example-id2', '', 0, $html); + + $this->assertTrue(strpos($html, 'enabled') === 0); + } + + /** + * Tests the storage method. + */ + public function testStorage() { + $attribute = new Attribute(array('class' => array('example-class'))); + + $this->assertEquals(array('class' => new AttributeArray('class', array('example-class'))), $attribute->storage()); + } + +}