[FEATURE] Fluid Tag Builder: Allow empty attributes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / ViewHelper / AbstractTagBasedViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\ViewHelper;
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
14 /**
15 * Test case
16 */
17 class AbstractTagBasedViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
18
19 /**
20 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject
21 */
22 protected $viewHelper;
23
24 protected function setUp() {
25 $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper::class, array('dummy'), array(), '', FALSE);
26 }
27
28 /**
29 * @test
30 */
31 public function initializeResetsUnderlyingTagBuilder() {
32 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('reset'), array(), '', FALSE);
33 $mockTagBuilder->expects($this->once())->method('reset');
34 $this->viewHelper->_set('tag', $mockTagBuilder);
35
36 $this->viewHelper->initialize();
37 }
38
39 /**
40 * @test
41 */
42 public function oneTagAttributeIsRenderedCorrectly() {
43 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('addAttribute'), array(), '', FALSE);
44 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('foo', 'bar');
45 $this->viewHelper->_set('tag', $mockTagBuilder);
46
47 $this->viewHelper->_call('registerTagAttribute', 'foo', 'string', 'Description', FALSE);
48 $arguments = array('foo' => 'bar');
49 $this->viewHelper->setArguments($arguments);
50 $this->viewHelper->initialize();
51 }
52
53 /**
54 * @test
55 */
56 public function emptyTagAttributeCallsTagBuilder() {
57 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('addAttribute'), array(), '', FALSE);
58 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('foo', '');
59 $this->viewHelper->_set('tag', $mockTagBuilder);
60
61 $this->viewHelper->_call('registerTagAttribute', 'foo', 'string', 'Description', FALSE);
62 $arguments = array('foo' => '');
63 $this->viewHelper->setArguments($arguments);
64 $this->viewHelper->initialize();
65 }
66
67 /**
68 * @test
69 */
70 public function additionalTagAttributesAreRenderedCorrectly() {
71 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('addAttribute'), array(), '', FALSE);
72 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('foo', 'bar');
73 $this->viewHelper->_set('tag', $mockTagBuilder);
74
75 $this->viewHelper->_call('registerTagAttribute', 'foo', 'string', 'Description', FALSE);
76 $arguments = array('additionalAttributes' => array('foo' => 'bar'));
77 $this->viewHelper->setArguments($arguments);
78 $this->viewHelper->initialize();
79 }
80
81 /**
82 * @test
83 */
84 public function dataAttributesAreRenderedCorrectly() {
85 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('addAttribute'), array(), '', FALSE);
86 $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('data-foo', 'bar');
87 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('data-baz', 'foos');
88 $this->viewHelper->_set('tag', $mockTagBuilder);
89
90 $arguments = array('data' => array('foo' => 'bar', 'baz' => 'foos'));
91 $this->viewHelper->setArguments($arguments);
92 $this->viewHelper->initializeArguments();
93 $this->viewHelper->initialize();
94 }
95
96 /**
97 * @test
98 */
99 public function standardTagAttributesAreRegistered() {
100 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('addAttribute'), array(), '', FALSE);
101 $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('class', 'classAttribute');
102 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('dir', 'dirAttribute');
103 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('id', 'idAttribute');
104 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('lang', 'langAttribute');
105 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('style', 'styleAttribute');
106 $mockTagBuilder->expects($this->at(5))->method('addAttribute')->with('title', 'titleAttribute');
107 $mockTagBuilder->expects($this->at(6))->method('addAttribute')->with('accesskey', 'accesskeyAttribute');
108 $mockTagBuilder->expects($this->at(7))->method('addAttribute')->with('tabindex', 'tabindexAttribute');
109 $this->viewHelper->_set('tag', $mockTagBuilder);
110
111 $arguments = array(
112 'class' => 'classAttribute',
113 'dir' => 'dirAttribute',
114 'id' => 'idAttribute',
115 'lang' => 'langAttribute',
116 'style' => 'styleAttribute',
117 'title' => 'titleAttribute',
118 'accesskey' => 'accesskeyAttribute',
119 'tabindex' => 'tabindexAttribute'
120 );
121 $this->viewHelper->_call('registerUniversalTagAttributes');
122 $this->viewHelper->setArguments($arguments);
123 $this->viewHelper->initializeArguments();
124 $this->viewHelper->initialize();
125 }
126
127 /**
128 * @test
129 */
130 public function registerTagAttributeRegistersArgumentWithDefaultValue() {
131 $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper::class, array('registerArgument'), array(), '', FALSE);
132 $this->viewHelper->expects($this->once())->method('registerArgument')->with('foo', 'string', 'Description', FALSE, 'defaultValue');
133 $this->viewHelper->_call('registerTagAttribute', 'foo', 'string', 'Description', FALSE, 'defaultValue');
134 }
135
136 /**
137 * @test
138 */
139 public function registerTagAttributeRegistersArgumentWithNullIfNoDefaultValueIsSet() {
140 $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper::class, array('registerArgument'), array(), '', FALSE);
141 $this->viewHelper->expects($this->once())->method('registerArgument')->with('foo', 'string', 'Description', FALSE, NULL);
142 $this->viewHelper->_call('registerTagAttribute', 'foo', 'string', 'Description', FALSE);
143 }
144
145 }