[BUGFIX] Ensure result is string in __toString of CropVariantCollection 87/59887/2
authorSusanne Moog <susanne.moog@typo3.com>
Wed, 6 Mar 2019 17:57:59 +0000 (18:57 +0100)
committerDaniel Goerz <daniel.goerz@posteo.de>
Sat, 16 Mar 2019 11:24:09 +0000 (12:24 +0100)
CropVariantCollection returns output from json_encode in its __toString
method. Json_encode may return false if it fails to properly encode the
given data (mostly when non-UTF8 data is given). To ensure that no
exception is thrown to the user in that case, the __toString method
has been adjusted to return the same result as in the empty variants case.

Resolves: #79823
Releases: master, 9.5
Change-Id: Idfbdbbbd867251c8480a0729412dd6d652f57ee1
Reviewed-on: https://review.typo3.org/c/59887
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Reviewed-by: Stephan GroƟberndt <stephan.grossberndt@typo3.org>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariantCollection.php
typo3/sysext/core/Tests/Unit/Imaging/ImageManipulation/CropVariantCollectionTest.php

index 4981213..38400a7 100644 (file)
@@ -118,7 +118,7 @@ class CropVariantCollection
         foreach ($this->cropVariants as $id => $cropVariant) {
             $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY);
         }
         foreach ($this->cropVariants as $id => $cropVariant) {
             $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY);
         }
-        return json_encode($cropVariantsAsArray);
+        return json_encode($cropVariantsAsArray) ?: '[]';
     }
 
     /**
     }
 
     /**
index 6daaa7c..67aa0c3 100644 (file)
@@ -111,4 +111,13 @@ class CropVariantCollectionTest extends UnitTestCase
     {
         $this->assertTrue(CropVariantCollection::create('')->getCropArea()->isEmpty());
     }
     {
         $this->assertTrue(CropVariantCollection::create('')->getCropArea()->isEmpty());
     }
+
+    /**
+     * @test
+     */
+    public function castToStringReturnsJsonArrayOnEmptyInput(): void
+    {
+        $variants = new CropVariantCollection([]);
+        $this->assertSame('[]', (string)$variants);
+    }
 }
 }