[TASK] Add validation for name option in FontAwesomeProvider 03/42903/9
authorFrank Nägler <frank.naegler@typo3.org>
Fri, 28 Aug 2015 09:13:36 +0000 (11:13 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Fri, 28 Aug 2015 18:22:27 +0000 (20:22 +0200)
Resolves: #69384
Releases: master
Change-Id: I87703486b5233dd91eb7a40dd93633af114ea5ff
Reviewed-on: http://review.typo3.org/42903
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Classes/Imaging/IconProvider/FontawesomeIconProvider.php
typo3/sysext/core/Classes/Imaging/IconRegistry.php
typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php
typo3/sysext/core/Tests/Unit/Imaging/IconProvider/FontawesomeIconProviderTest.php

index e535f74..0759697 100644 (file)
@@ -34,9 +34,16 @@ class FontawesomeIconProvider implements IconProviderInterface {
         * @param Icon $icon
         * @param array $options
         *
+        * @throws \InvalidArgumentException
         * @return string
         */
        protected function generateMarkup(Icon $icon, array $options) {
+               if (empty($options['name'])) {
+                       throw new \InvalidArgumentException('The option "name" is required and must not be empty', 1440754978);
+               }
+               if (preg_match('/^[a-zA-Z0-9\\-]+$/', $options['name']) !== 1) {
+                       throw new \InvalidArgumentException('The option "name" must only contain characters a-z, A-Z, 0-9 or -', 1440754979);
+               }
                $additionalClasses = (!empty($options['additionalClasses'])) ? ' ' . $options['additionalClasses'] : '';
                return '<span class="icon-unify"><i class="fa fa-' . htmlspecialchars($options['name']) . htmlspecialchars($additionalClasses) . '"></i></span>';
        }
index 9cf3ab1..ef0fd2b 100644 (file)
@@ -125,7 +125,8 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                'actions-edit-copy-release' => array(
                        'provider' => FontawesomeIconProvider::class,
                        'options' => array(
-                               'name' => 'copy text-danger',
+                               'name' => 'copy',
+                               'additionalClasses' => 'text-danger',
                        )
                ),
                'actions-edit-cut' => array(
@@ -137,7 +138,8 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                'actions-edit-cut-release' => array(
                        'provider' => FontawesomeIconProvider::class,
                        'options' => array(
-                               'name' => 'scissors text-danger',
+                               'name' => 'scissors',
+                               'additionalClasses' => 'text-danger',
                        )
                ),
                'actions-edit-download' => array(
@@ -223,7 +225,8 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                'status-status-current' => array(
                        'provider' => FontawesomeIconProvider::class,
                        'options' => array(
-                               'name' => 'caret-right text-danger',
+                               'name' => 'caret-right',
+                               'additionalClasses' => 'text-danger',
                        )
                ),
 
index 2b46e12..16378b8 100644 (file)
@@ -149,6 +149,7 @@ class IconFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         * @dataProvider differentSizesDataProvider
+        * @param string $size
         */
        public function getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
                $this->assertContains('<span class="icon-overlay icon-overlay-read-only">',
index 6825c44..0b0937a 100644 (file)
@@ -59,4 +59,29 @@ class FontawesomeIconProviderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->subject->prepareIconMarkup($this->icon, array('name' => 'times', 'additionalClasses' => 'foo'));
                $this->assertEquals('<span class="icon-unify"><i class="fa fa-times foo"></i></span>', $this->icon->getMarkup());
        }
+
+       /**
+        * DataProvider for icon names
+        *
+        * @return array
+        */
+       public function wrongNamesDataProvider() {
+               return [
+                       'name with spaces' => ['name with spaces'],
+                       'name with spaces and umlauts' => ['name with spaces äöü'],
+                       'name umlauts' => ['häuser'],
+                       'name with underscore' => ['name_with_underscore'],
+                       'name is empty' => [''],
+               ];
+       }
+
+       /**
+        * @expectedException \InvalidArgumentException
+        * @dataProvider wrongNamesDataProvider
+        * @param string $name
+        * @test
+        */
+       public function prepareIconMarkupWithInvalidNameThrowsException($name) {
+               $this->subject->prepareIconMarkup($this->icon, array('name' => $name));
+       }
 }