‪TYPO3CMS  11.5
GenericMetaTagManagerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
23 class ‪GenericMetaTagManagerTest extends UnitTestCase
24 {
29  {
30  $manager = new ‪GenericMetaTagManager();
31  $handledProperties = $manager->getAllHandledProperties();
32 
33  self::assertEmpty($handledProperties);
34  }
35 
40  {
41  $manager = new ‪GenericMetaTagManager();
42  self::assertTrue($manager->canHandleProperty('custom-meta-tag'));
43  self::assertTrue($manager->canHandleProperty('description'));
44  self::assertTrue($manager->canHandleProperty('og:title'));
45  }
46 
52  public function ‪checkIfPropertyIsStoredAfterAddingProperty($property, $expected, $expectedRenderedTag): void
53  {
54  $manager = new ‪GenericMetaTagManager();
55  $manager->addProperty(
56  $property['property'],
57  $property['content'],
58  (array)$property['subProperties'],
59  $property['replace'],
60  $property['type']
61  );
62 
63  self::assertEquals($expected, $manager->getProperty($property['property'], $property['type']));
64  self::assertEquals($expectedRenderedTag, $manager->renderProperty($property['property']));
65  }
66 
71  {
72  $properties = [
73  [
74  'property' => 'description',
75  'content' => 'This is a description',
76  'subProperties' => [],
77  'replace' => false,
78  'type' => '',
79  ],
80  [
81  'property' => 'og:image',
82  'content' => '/path/to/image',
83  'subProperties' => [
84  'width' => 400,
85  ],
86  'replace' => false,
87  'type' => 'property',
88  ],
89  [
90  'property' => 'og:image:height',
91  'content' => '200',
92  'subProperties' => [],
93  'replace' => false,
94  'type' => 'property',
95  ],
96  [
97  'property' => 'twitter:card',
98  'content' => 'This is the Twitter card',
99  'subProperties' => [],
100  'replace' => false,
101  'type' => '',
102  ],
103  [
104  'property' => 'og:image',
105  'content' => '/path/to/image2',
106  'subProperties' => [],
107  'replace' => true,
108  'type' => 'property',
109  ],
110  ];
111 
112  $manager = new ‪GenericMetaTagManager();
113  foreach ($properties as $property) {
114  $manager->addProperty(
115  $property['property'],
116  $property['content'],
117  $property['subProperties'],
118  $property['replace'],
119  $property['type']
120  );
121  }
122 
123  $expected = '<meta name="description" content="This is a description" />' . PHP_EOL .
124  '<meta property="og:image" content="/path/to/image2" />' . PHP_EOL .
125  '<meta property="og:image:height" content="200" />' . PHP_EOL .
126  '<meta name="twitter:card" content="This is the Twitter card" />';
127 
128  self::assertEquals($expected, $manager->renderAllProperties());
129  }
130 
135  {
136  $manager = new ‪GenericMetaTagManager();
137  $manager->addProperty('description', 'Description');
138  self::assertEquals([['content' => 'Description', 'subProperties' => []]], $manager->getProperty('description'));
139 
140  $manager->removeProperty('description');
141  self::assertEquals([], $manager->getProperty('description'));
142 
143  $manager->addProperty('description', 'Description 1', [], false, 'property');
144  $manager->addProperty('description', 'Description 2', [], false, '');
145  $manager->addProperty('description', 'Description 3', []);
146 
147  self::assertEquals([['content' => 'Description 1', 'subProperties' => []]], $manager->getProperty('description', 'property'));
148 
149  $manager->removeProperty('Description', 'Property');
150  self::assertEquals([], $manager->getProperty('description', 'property'));
151  self::assertEquals(
152  [
153  ['content' => 'Description 2', 'subProperties' => []],
154  ['content' => 'Description 3', 'subProperties' => []],
155  ],
156  $manager->getProperty('description')
157  );
158 
159  $manager->addProperty('description', 'Title', [], false, 'property');
160  $manager->addProperty('description', 'Title', [], false, 'name');
161  $manager->addProperty('twitter:card', 'Twitter card');
162 
163  $manager->removeAllProperties();
164 
165  self::assertEquals([], $manager->getProperty('description'));
166  self::assertEquals([], $manager->getProperty('description', 'name'));
167  self::assertEquals([], $manager->getProperty('description', 'property'));
168  self::assertEquals([], $manager->getProperty('twitter:card'));
169  }
170 
174  public function ‪propertiesProvider(): array
175  {
176  return [
177  [
178  [
179  'property' => 'custom-tag',
180  'content' => 'Test title',
181  'subProperties' => [],
182  'replace' => false,
183  'type' => '',
184  ],
185  [
186  [
187  'content' => 'Test title',
188  'subProperties' => [],
189  ],
190  ],
191  '<meta name="custom-tag" content="Test title" />',
192  ],
193  [
194  [
195  'property' => 'description',
196  'content' => 'Custom description',
197  'subProperties' => [],
198  'replace' => false,
199  'type' => '',
200  ],
201  [
202  [
203  'content' => 'Custom description',
204  'subProperties' => [],
205  ],
206  ],
207  '<meta name="description" content="Custom description" />',
208  ],
209  [
210  [
211  'property' => 'og:image',
212  'content' => '/path/to/image',
213  'subProperties' => [],
214  'replace' => false,
215  'type' => 'property',
216  ],
217  [
218  [
219  'content' => '/path/to/image',
220  'subProperties' => [],
221  ],
222  ],
223  '<meta property="og:image" content="/path/to/image" />',
224  ],
225  [
226  [
227  'property' => 'og:image',
228  'content' => '/path/to/image',
229  'subProperties' => ['width' => 100],
230  'replace' => false,
231  'type' => 'property',
232  ],
233  [
234  [
235  'content' => '/path/to/image',
236  'subProperties' => ['width' => 100],
237  ],
238  ],
239  '<meta property="og:image" content="/path/to/image" />' . PHP_EOL .
240  '<meta property="og:image:width" content="100" />',
241  ],
242  [
243  [
244  'property' => 'og:image:width',
245  'content' => '100',
246  'subProperties' => [],
247  'replace' => false,
248  'type' => 'property',
249  ],
250  [
251  [
252  'content' => '100',
253  'subProperties' => [],
254  ],
255  ],
256  '<meta property="og:image:width" content="100" />',
257  ],
258  ];
259  }
260 }
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\checkIfGetAllHandledPropertiesReturnsNonEmptyArray
‪checkIfGetAllHandledPropertiesReturnsNonEmptyArray()
Definition: GenericMetaTagManagerTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest
Definition: GenericMetaTagManagerTest.php:24
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\checkRenderAllPropertiesRendersCorrectMetaTags
‪checkRenderAllPropertiesRendersCorrectMetaTags()
Definition: GenericMetaTagManagerTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\checkIfMethodCanHandlePropertyAlwaysReturnsTrue
‪checkIfMethodCanHandlePropertyAlwaysReturnsTrue()
Definition: GenericMetaTagManagerTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\propertiesProvider
‪array propertiesProvider()
Definition: GenericMetaTagManagerTest.php:174
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\checkIfPropertyIsStoredAfterAddingProperty
‪checkIfPropertyIsStoredAfterAddingProperty($property, $expected, $expectedRenderedTag)
Definition: GenericMetaTagManagerTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\MetaTag\GenericMetaTagManagerTest\checkIfRemovePropertyReallyRemovesProperty
‪checkIfRemovePropertyReallyRemovesProperty()
Definition: GenericMetaTagManagerTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\MetaTag
Definition: GenericMetaTagManagerTest.php:18
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager
Definition: GenericMetaTagManager.php:25