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