[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / MetaTag / GenericMetaTagManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\MetaTag;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\MetaTag\GenericMetaTagManager;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 class GenericMetaTagManagerTest extends UnitTestCase
21 {
22 /**
23 * @test
24 */
25 public function checkIfGetAllHandledPropertiesReturnsNonEmptyArray()
26 {
27 $manager = new GenericMetaTagManager();
28 $handledProperties = $manager->getAllHandledProperties();
29
30 $this->assertEmpty($handledProperties);
31 }
32
33 /**
34 * @test
35 */
36 public function checkIfMethodCanHandlePropertyAlwaysReturnsTrue()
37 {
38 $manager = new GenericMetaTagManager();
39 $this->assertTrue($manager->canHandleProperty('custom-meta-tag'));
40 $this->assertTrue($manager->canHandleProperty('description'));
41 $this->assertTrue($manager->canHandleProperty('og:title'));
42 }
43
44 /**
45 * @dataProvider propertiesProvider
46 *
47 * @test
48 */
49 public function checkIfPropertyIsStoredAfterAddingProperty($property, $expected, $expectedRenderedTag)
50 {
51 $manager = new GenericMetaTagManager();
52 $manager->addProperty(
53 $property['property'],
54 $property['content'],
55 (array)$property['subProperties'],
56 $property['replace'],
57 $property['type']
58 );
59
60 $this->assertEquals($expected, $manager->getProperty($property['property'], $property['type']));
61 $this->assertEquals($expectedRenderedTag, $manager->renderProperty($property['property']));
62 }
63
64 /**
65 * @test
66 */
67 public function checkRenderAllPropertiesRendersCorrectMetaTags()
68 {
69 $properties = [
70 [
71 'property' => 'description',
72 'content' => 'This is a description',
73 'subProperties' => [],
74 'replace' => false,
75 'type' => ''
76 ],
77 [
78 'property' => 'og:image',
79 'content' => '/path/to/image',
80 'subProperties' => [
81 'width' => 400
82 ],
83 'replace' => false,
84 'type' => 'property'
85 ],
86 [
87 'property' => 'og:image:height',
88 'content' => '200',
89 'subProperties' => [],
90 'replace' => false,
91 'type' => 'property'
92 ],
93 [
94 'property' => 'twitter:card',
95 'content' => 'This is the Twitter card',
96 'subProperties' => [],
97 'replace' => false,
98 'type' => ''
99 ],
100 [
101 'property' => 'og:image',
102 'content' => '/path/to/image2',
103 'subProperties' => [],
104 'replace' => true,
105 'type' => 'property'
106 ],
107 ];
108
109 $manager = new GenericMetaTagManager();
110 foreach ($properties as $property) {
111 $manager->addProperty(
112 $property['property'],
113 $property['content'],
114 $property['subProperties'],
115 $property['replace'],
116 $property['type']
117 );
118 }
119
120 $expected = '<meta name="description" content="This is a description" />' . PHP_EOL .
121 '<meta property="og:image" content="/path/to/image2" />' . PHP_EOL .
122 '<meta property="og:image:height" content="200" />' . PHP_EOL .
123 '<meta name="twitter:card" content="This is the Twitter card" />';
124
125 $this->assertEquals($expected, $manager->renderAllProperties());
126 }
127
128 /**
129 * @test
130 */
131 public function checkIfRemovePropertyReallyRemovesProperty()
132 {
133 $manager = new GenericMetaTagManager();
134 $manager->addProperty('description', 'Description');
135 $this->assertEquals([['content' => 'Description', 'subProperties' => []]], $manager->getProperty('description'));
136
137 $manager->removeProperty('description');
138 $this->assertEquals([], $manager->getProperty('description'));
139
140 $manager->addProperty('description', 'Description 1', [], false, 'property');
141 $manager->addProperty('description', 'Description 2', [], false, '');
142 $manager->addProperty('description', 'Description 3', []);
143
144 $this->assertEquals([['content' => 'Description 1', 'subProperties' => []]], $manager->getProperty('description', 'property'));
145
146 $manager->removeProperty('description', 'property');
147 $this->assertEquals([], $manager->getProperty('description', 'property'));
148 $this->assertEquals(
149 [
150 ['content' => 'Description 2', 'subProperties' => []],
151 ['content' => 'Description 3', 'subProperties' => []]
152 ],
153 $manager->getProperty('description')
154 );
155
156 $manager->addProperty('description', 'Title', [], false, 'property');
157 $manager->addProperty('description', 'Title', [], false, 'name');
158 $manager->addProperty('twitter:card', 'Twitter card');
159
160 $manager->removeAllProperties();
161
162 $this->assertEquals([], $manager->getProperty('description'));
163 $this->assertEquals([], $manager->getProperty('description', 'name'));
164 $this->assertEquals([], $manager->getProperty('description', 'property'));
165 $this->assertEquals([], $manager->getProperty('twitter:card'));
166 }
167
168 /**
169 * @return array
170 */
171 public function propertiesProvider()
172 {
173 return [
174 [
175 [
176 'property' => 'custom-tag',
177 'content' => 'Test title',
178 'subProperties' => [],
179 'replace' => false,
180 'type' => ''
181 ],
182 [
183 [
184 'content' => 'Test title',
185 'subProperties' => []
186 ]
187 ],
188 '<meta name="custom-tag" content="Test title" />'
189 ],
190 [
191 [
192 'property' => 'description',
193 'content' => 'Custom description',
194 'subProperties' => [],
195 'replace' => false,
196 'type' => ''
197 ],
198 [
199 [
200 'content' => 'Custom description',
201 'subProperties' => []
202 ]
203 ],
204 '<meta name="description" content="Custom description" />'
205 ],
206 [
207 [
208 'property' => 'og:image',
209 'content' => '/path/to/image',
210 'subProperties' => [],
211 'replace' => false,
212 'type' => 'property'
213 ],
214 [
215 [
216 'content' => '/path/to/image',
217 'subProperties' => []
218 ]
219 ],
220 '<meta property="og:image" content="/path/to/image" />'
221 ],
222 [
223 [
224 'property' => 'og:image',
225 'content' => '/path/to/image',
226 'subProperties' => ['width' => 100],
227 'replace' => false,
228 'type' => 'property'
229 ],
230 [
231 [
232 'content' => '/path/to/image',
233 'subProperties' => ['width' => 100]
234 ]
235 ],
236 '<meta property="og:image" content="/path/to/image" />' . PHP_EOL .
237 '<meta property="og:image:width" content="100" />'
238 ],
239 [
240 [
241 'property' => 'og:image:width',
242 'content' => '100',
243 'subProperties' => [],
244 'replace' => false,
245 'type' => 'property'
246 ],
247 [
248 [
249 'content' => '100',
250 'subProperties' => []
251 ]
252 ],
253 '<meta property="og:image:width" content="100" />'
254 ]
255 ];
256 }
257 }