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