‪TYPO3CMS  ‪main
TwitterCardMetaTagManagerTest.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 ‪TwitterCardMetaTagManagerTest extends UnitTestCase
26 {
27  #[Test]
29  {
30  $manager = new ‪TwitterCardMetaTagManager();
31  $handledProperties = $manager->getAllHandledProperties();
32 
33  self::assertNotEmpty($handledProperties);
34  }
35 
36  #[DataProvider('propertiesProvider')]
37  #[Test]
38  public function ‪checkIfPropertyIsStoredAfterAddingProperty(array $property, array $expected, string $expectedRenderedTag): void
39  {
40  $manager = new ‪TwitterCardMetaTagManager();
41  $manager->addProperty(
42  $property['property'],
43  $property['content'],
44  (array)$property['subProperties']
45  );
46 
47  self::assertEquals($expected, $manager->getProperty($property['property']));
48  self::assertEquals($expectedRenderedTag, $manager->renderProperty($property['property']));
49  }
50 
51  public static function ‪propertiesProvider(): array
52  {
53  return [
54  'title is set' => [
55  [
56  'property' => 'twitter:title',
57  'content' => 'Test title',
58  'subProperties' => [],
59  ],
60  [
61  [
62  'content' => 'Test title',
63  'subProperties' => [],
64  ],
65  ],
66  '<meta name="twitter:title" content="Test title" />',
67  ],
68  'image path is set' => [
69  [
70  'property' => 'twitter:image',
71  'content' => '/path/to/image',
72  'subProperties' => [],
73  ],
74  [
75  [
76  'content' => '/path/to/image',
77  'subProperties' => [],
78  ],
79  ],
80  '<meta name="twitter:image" content="/path/to/image" />',
81  ],
82  'remove not used subproperties' => [
83  [
84  'property' => 'twitter:image',
85  'content' => '/path/to/image',
86  'subProperties' => ['width' => [400], 'height' => [400]],
87  ],
88  [
89  [
90  'content' => '/path/to/image',
91  'subProperties' => [],
92  ],
93  ],
94  '<meta name="twitter:image" content="/path/to/image" />',
95  ],
96  'set alt to twitter:image' => [
97  [
98  'property' => 'twitter:image',
99  'content' => '/path/to/image',
100  'subProperties' => ['alt' => ['Alternative title']],
101  ],
102  [
103  [
104  'content' => '/path/to/image',
105  'subProperties' => [
106  'alt' => ['Alternative title'],
107  ],
108  ],
109  ],
110  '<meta name="twitter:image" content="/path/to/image" />' . PHP_EOL .
111  '<meta name="twitter:image:alt" content="Alternative title" />',
112  ],
113  ];
114  }
115 
116  #[Test]
118  {
119  $manager = new ‪TwitterCardMetaTagManager();
120 
121  $this->expectException(\UnexpectedValueException::class);
122  $manager->addProperty('og:image:width', '400');
123  }
124 
125  #[Test]
127  {
128  $properties = [
129  [
130  'property' => 'twitter:title',
131  'content' => 'This is a title',
132  'subProperties' => [],
133  'replace' => false,
134  'type' => '',
135  ],
136  [
137  'property' => 'twitter:image',
138  'content' => '/path/to/image',
139  'subProperties' => [
140  'width' => 400,
141  ],
142  'replace' => false,
143  'type' => '',
144  ],
145  [
146  'property' => 'twitter:title',
147  'content' => 'This is the new title',
148  'subProperties' => [],
149  'replace' => true,
150  'type' => '',
151  ],
152  [
153  'property' => 'twitter:image',
154  'content' => '/path/to/image2',
155  'subProperties' => [],
156  'replace' => false,
157  'type' => '',
158  ],
159  ];
160 
161  $manager = new ‪TwitterCardMetaTagManager();
162  foreach ($properties as $property) {
163  $manager->addProperty(
164  $property['property'],
165  $property['content'],
166  $property['subProperties'],
167  $property['replace'],
168  $property['type']
169  );
170  }
171 
172  $expected = '<meta name="twitter:image" content="/path/to/image" />' . PHP_EOL .
173  '<meta name="twitter:title" content="This is the new title" />';
174 
175  self::assertEquals($expected, $manager->renderAllProperties());
176  }
177 
178  #[Test]
180  {
181  $manager = new ‪TwitterCardMetaTagManager();
182  $manager->addProperty('twitter:title', 'Title');
183  self::assertEquals([['content' => 'Title', 'subProperties' => []]], $manager->getProperty('twitter:title'));
184 
185  $manager->removeProperty('twitter:title');
186  self::assertEquals([], $manager->getProperty('twitter:title'));
187 
188  $manager->addProperty('twitter:title', 'Title');
189  $manager->addProperty('twitter:description', 'Description');
190 
191  $manager->removeAllProperties();
192 
193  self::assertEquals([], $manager->getProperty('twitter:title'));
194  self::assertEquals([], $manager->getProperty('twitter:description'));
195  }
196 }
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\propertiesProvider
‪static propertiesProvider()
Definition: TwitterCardMetaTagManagerTest.php:51
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest
Definition: TwitterCardMetaTagManagerTest.php:26
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\checkIfGetAllHandledPropertiesReturnsNonEmptyArray
‪checkIfGetAllHandledPropertiesReturnsNonEmptyArray()
Definition: TwitterCardMetaTagManagerTest.php:28
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\checkRenderAllPropertiesRendersCorrectMetaTags
‪checkRenderAllPropertiesRendersCorrectMetaTags()
Definition: TwitterCardMetaTagManagerTest.php:126
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\checkIfPropertyIsStoredAfterAddingProperty
‪checkIfPropertyIsStoredAfterAddingProperty(array $property, array $expected, string $expectedRenderedTag)
Definition: TwitterCardMetaTagManagerTest.php:38
‪TYPO3\CMS\Seo\MetaTag\TwitterCardMetaTagManager
Definition: TwitterCardMetaTagManager.php:26
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\checkIfAddingOnlySubPropertyAndNoMainPropertyIsReturningException
‪checkIfAddingOnlySubPropertyAndNoMainPropertyIsReturningException()
Definition: TwitterCardMetaTagManagerTest.php:117
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag
Definition: OpenGraphMetaTagManagerTest.php:18
‪TYPO3\CMS\Seo\Tests\Unit\MetaTag\TwitterCardMetaTagManagerTest\checkIfRemovePropertyReallyRemovesProperty
‪checkIfRemovePropertyReallyRemovesProperty()
Definition: TwitterCardMetaTagManagerTest.php:179