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