diff --git a/typo3/sysext/core/Classes/MetaTag/TwitterCardMetaTagManager.php b/typo3/sysext/core/Classes/MetaTag/TwitterCardMetaTagManager.php index 7be0bc67baad0997e9e3e7cf65812460b681ccf4..eca5519565dc0f5cf2659eba2bc40c36ddf7945f 100644 --- a/typo3/sysext/core/Classes/MetaTag/TwitterCardMetaTagManager.php +++ b/typo3/sysext/core/Classes/MetaTag/TwitterCardMetaTagManager.php @@ -27,39 +27,39 @@ class TwitterCardMetaTagManager extends AbstractMetaTagManager 'twitter:card' => [], 'twitter:site' => [ 'allowedSubProperties' => [ - 'id', + 'id' => [], ] ], 'twitter:creator' => [ 'allowedSubProperties' => [ - 'id', + 'id' => [], ] ], 'twitter:description' => [], 'twitter:title' => [], 'twitter:image' => [ 'allowedSubProperties' => [ - 'alt', + 'alt' => [], ] ], 'twitter:player' => [ 'allowedSubProperties' => [ - 'width', - 'height', - 'stream', + 'width' => [], + 'height' => [], + 'stream' => [], ] ], 'twitter:app' => [ 'allowedSubProperties' => [ - 'name:iphone', - 'id:iphone', - 'url:iphone', - 'name:ipad', - 'id:ipad', - 'url:ipad', - 'name:googleplay', - 'id:googleplay', - 'url:googleplay', + 'name:iphone' => [], + 'id:iphone' => [], + 'url:iphone' => [], + 'name:ipad' => [], + 'id:ipad' => [], + 'url:ipad' => [], + 'name:googleplay' => [], + 'id:googleplay' => [], + 'url:googleplay' => [], ] ], ]; diff --git a/typo3/sysext/core/Tests/Unit/MetaTag/TwitterCardMetaTagManagerTest.php b/typo3/sysext/core/Tests/Unit/MetaTag/TwitterCardMetaTagManagerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..25c7fd13beadb47425dee3e2e76dc44fad827b23 --- /dev/null +++ b/typo3/sysext/core/Tests/Unit/MetaTag/TwitterCardMetaTagManagerTest.php @@ -0,0 +1,212 @@ +<?php +declare(strict_types = 1); +namespace TYPO3\CMS\Core\Tests\Unit\MetaTag; + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +use TYPO3\CMS\Core\MetaTag\TwitterCardMetaTagManager; + +/** + * Test case + */ +class TwitterCardMetaTagManagerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase +{ + /** + * @test + */ + public function checkIfGetAllHandledPropertiesReturnsNonEmptyArray() + { + $manager = new TwitterCardMetaTagManager(); + $handledProperties = $manager->getAllHandledProperties(); + + $this->assertNotEmpty($handledProperties); + } + + /** + * @dataProvider propertiesProvider + * + * @test + * + * @param array $property + * @param array $expected + * @param string $expectedRenderedTag + */ + public function checkIfPropertyIsStoredAfterAddingProperty(array $property, array $expected, string $expectedRenderedTag) + { + $manager = new TwitterCardMetaTagManager(); + $manager->addProperty( + $property['property'], + $property['content'], + (array)$property['subProperties'] + ); + + $this->assertEquals($expected, $manager->getProperty($property['property'])); + $this->assertEquals($expectedRenderedTag, $manager->renderProperty($property['property'])); + } + + /** + * @return array + */ + public function propertiesProvider() + { + return [ + 'title is set' => [ + [ + 'property' => 'twitter:title', + 'content' => 'Test title', + 'subProperties' => [] + ], + [ + [ + 'content' => 'Test title', + 'subProperties' => [] + ] + ], + '<meta name="twitter:title" content="Test title" />' + ], + 'image path is set' => [ + [ + 'property' => 'twitter:image', + 'content' => '/path/to/image', + 'subProperties' => [] + ], + [ + [ + 'content' => '/path/to/image', + 'subProperties' => [] + ] + ], + '<meta name="twitter:image" content="/path/to/image" />' + ], + 'remove not used subproperties' => [ + [ + 'property' => 'twitter:image', + 'content' => '/path/to/image', + 'subProperties' => ['width' => [400], 'height' => [400]] + ], + [ + [ + 'content' => '/path/to/image', + 'subProperties' => [] + ] + ], + '<meta name="twitter:image" content="/path/to/image" />' + ], + 'set alt to twitter:image' => [ + [ + 'property' => 'twitter:image', + 'content' => '/path/to/image', + 'subProperties' => ['alt' => ['Alternative title']] + ], + [ + [ + 'content' => '/path/to/image', + 'subProperties' => [ + 'alt' => ['Alternative title'] + ] + ] + ], + '<meta name="twitter:image" content="/path/to/image" />' . PHP_EOL . + '<meta name="twitter:image:alt" content="Alternative title" />' + ] + ]; + } + + /** + * @test + */ + public function checkIfAddingOnlySubPropertyAndNoMainPropertyIsReturningException() + { + $manager = new TwitterCardMetaTagManager(); + + $this->expectException(\UnexpectedValueException::class); + $manager->addProperty('og:image:width', '400'); + } + + /** + * @test + */ + public function checkRenderAllPropertiesRendersCorrectMetaTags() + { + $properties = [ + [ + 'property' => 'twitter:title', + 'content' => 'This is a title', + 'subProperties' => [], + 'replace' => false, + 'type' => '' + ], + [ + 'property' => 'twitter:image', + 'content' => '/path/to/image', + 'subProperties' => [ + 'width' => 400 + ], + 'replace' => false, + 'type' => '' + ], + [ + 'property' => 'twitter:title', + 'content' => 'This is the new title', + 'subProperties' => [], + 'replace' => true, + 'type' => '' + ], + [ + 'property' => 'twitter:image', + 'content' => '/path/to/image2', + 'subProperties' => [], + 'replace' => false, + 'type' => '' + ], + ]; + + $manager = new TwitterCardMetaTagManager(); + foreach ($properties as $property) { + $manager->addProperty( + $property['property'], + $property['content'], + $property['subProperties'], + $property['replace'], + $property['type'] + ); + } + + $expected = '<meta name="twitter:image" content="/path/to/image" />' . PHP_EOL . + '<meta name="twitter:title" content="This is the new title" />'; + + $this->assertEquals($expected, $manager->renderAllProperties()); + } + + /** + * @test + */ + public function checkIfRemovePropertyReallyRemovesProperty() + { + $manager = new TwitterCardMetaTagManager(); + $manager->addProperty('twitter:title', 'Title'); + $this->assertEquals([['content' => 'Title', 'subProperties' => []]], $manager->getProperty('twitter:title')); + + $manager->removeProperty('twitter:title'); + $this->assertEquals([], $manager->getProperty('twitter:title')); + + $manager->addProperty('twitter:title', 'Title'); + $manager->addProperty('twitter:description', 'Description'); + + $manager->removeAllProperties(); + + $this->assertEquals([], $manager->getProperty('twitter:title')); + $this->assertEquals([], $manager->getProperty('twitter:description')); + } +}