[BUGFIX] Correctly define allowedSubProperties of TwitterCardMetaTagManager 99/57099/9
authorRichard Haeser <richard@maxserv.com>
Wed, 30 May 2018 20:56:29 +0000 (22:56 +0200)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Sat, 2 Jun 2018 21:50:40 +0000 (23:50 +0200)
The properties within the allowedSubProperties have to be arrays
because the AbstractMetaTagManager checks if the property as a key
exists in the allowSubProperties.

Resolves: #85126
Releases: master
Change-Id: Ie57a77f8d4100e0861958de73faa56d80709daad
Reviewed-on: https://review.typo3.org/57099
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Tested-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/core/Classes/MetaTag/TwitterCardMetaTagManager.php
typo3/sysext/core/Tests/Unit/MetaTag/TwitterCardMetaTagManagerTest.php [new file with mode: 0644]

index 7be0bc6..eca5519 100644 (file)
@@ -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 (file)
index 0000000..25c7fd1
--- /dev/null
@@ -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'));
+    }
+}