TYPO3 CMS  TYPO3_7-6
TagBuilderTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
18 {
22  public function constructorSetsTagName()
23  {
24  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('someTagName');
25  $this->assertEquals('someTagName', $tagBuilder->getTagName());
26  }
27 
31  public function constructorSetsTagContent()
32  {
33  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('', '<some text>');
34  $this->assertEquals('<some text>', $tagBuilder->getContent());
35  }
36 
40  public function setContentDoesNotEscapeValue()
41  {
42  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
43  $tagBuilder->setContent('<to be escaped>', false);
44  $this->assertEquals('<to be escaped>', $tagBuilder->getContent());
45  }
46 
51  {
52  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('', 'foo');
53  $this->assertTrue($tagBuilder->hasContent());
54  }
55 
60  {
61  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
62  $tagBuilder->setContent(null);
63  $this->assertFalse($tagBuilder->hasContent());
64  }
65 
70  {
71  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
72  $tagBuilder->setContent('');
73  $this->assertFalse($tagBuilder->hasContent());
74  }
75 
80  {
81  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
82  $this->assertEquals('', $tagBuilder->render());
83  }
84 
89  {
90  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
91  $this->assertEquals('<tag />', $tagBuilder->render());
92  }
93 
97  public function contentCanBeRemoved()
98  {
99  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag', 'some content');
100  $tagBuilder->setContent(null);
101  $this->assertEquals('<tag />', $tagBuilder->render());
102  }
103 
108  {
109  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
110  $tagBuilder->forceClosingTag(true);
111  $this->assertEquals('<tag></tag>', $tagBuilder->render());
112  }
113 
118  {
119  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
120  $tagBuilder->addAttribute('attribute1', 'attribute1value');
121  $tagBuilder->addAttribute('attribute2', 'attribute2value');
122  $tagBuilder->addAttribute('attribute3', 'attribute3value');
123  $this->assertEquals('<tag attribute1="attribute1value" attribute2="attribute2value" attribute3="attribute3value" />', $tagBuilder->render());
124  }
125 
130  {
131  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
132  $tagBuilder->addAttribute('foo', '<to be escaped>');
133  $this->assertEquals('<tag foo="&lt;to be escaped&gt;" />', $tagBuilder->render());
134  }
135 
140  {
141  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
142  $tagBuilder->addAttribute('foo', '<not to be escaped>', false);
143  $this->assertEquals('<tag foo="<not to be escaped>" />', $tagBuilder->render());
144  }
145 
149  public function attributesCanBeRemoved()
150  {
151  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
152  $tagBuilder->addAttribute('attribute1', 'attribute1value');
153  $tagBuilder->addAttribute('attribute2', 'attribute2value');
154  $tagBuilder->addAttribute('attribute3', 'attribute3value');
155  $tagBuilder->removeAttribute('attribute2');
156  $this->assertEquals('<tag attribute1="attribute1value" attribute3="attribute3value" />', $tagBuilder->render());
157  }
158 
162  public function attributesCanBeAccessed()
163  {
164  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
165  $tagBuilder->addAttribute('attribute1', 'attribute1value');
166  $attributeValue = $tagBuilder->getAttribute('attribute1');
167  $this->assertSame('attribute1value', $attributeValue);
168  }
169 
174  {
175  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('tag');
176  $attributeValue = $tagBuilder->getAttribute('missingattribute');
177  $this->assertNull($attributeValue);
178  }
179 
183  public function resetResetsTagBuilder()
184  {
185  $tagBuilder = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, ['dummy']);
186  $tagBuilder->setTagName('tagName');
187  $tagBuilder->setContent('some content');
188  $tagBuilder->forceClosingTag(true);
189  $tagBuilder->addAttribute('attribute1', 'attribute1value');
190  $tagBuilder->addAttribute('attribute2', 'attribute2value');
191  $tagBuilder->reset();
192 
193  $this->assertEquals('', $tagBuilder->_get('tagName'));
194  $this->assertEquals('', $tagBuilder->_get('content'));
195  $this->assertEquals([], $tagBuilder->_get('attributes'));
196  $this->assertFalse($tagBuilder->_get('forceClosingTag'));
197  }
198 
202  public function tagNameCanBeOverridden()
203  {
204  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('foo');
205  $tagBuilder->setTagName('bar');
206  $this->assertEquals('<bar />', $tagBuilder->render());
207  }
208 
212  public function tagContentCanBeOverridden()
213  {
214  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('foo', 'some content');
215  $tagBuilder->setContent('');
216  $this->assertEquals('<foo />', $tagBuilder->render());
217  }
218 
223  {
224  $tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder('foo');
225  $tagBuilder->setTagName('');
226  $this->assertEquals('', $tagBuilder->render());
227  }
228 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)