Browse Source

Entity::isEmpty() / Entity::hasValue() add string zero test case

Backport changes from #15340 to 3.x
mtak3 5 years ago
parent
commit
dac33104e4
2 changed files with 13 additions and 3 deletions
  1. 9 3
      src/Datasource/EntityTrait.php
  2. 4 0
      tests/TestCase/ORM/EntityTest.php

+ 9 - 3
src/Datasource/EntityTrait.php

@@ -392,9 +392,15 @@ trait EntityTrait
     {
         $value = $this->get($property);
         if (
-            $value === null
-            || (is_array($value) && empty($value)
-            || (is_string($value) && empty($value)))
+            $value === null ||
+            (
+                is_array($value) &&
+                empty($value) ||
+                (
+                    is_string($value) &&
+                    $value === ''
+                )
+            )
         ) {
             return true;
         }

+ 4 - 0
tests/TestCase/ORM/EntityTest.php

@@ -1817,6 +1817,7 @@ class EntityTest extends TestCase
             'emptyArray' => [],
             'object' => new \stdClass(),
             'string' => 'string',
+            'stringZero' => '0',
             'emptyString' => '',
             'intZero' => 0,
             'intNotZero' => 1,
@@ -1829,6 +1830,7 @@ class EntityTest extends TestCase
         $this->assertTrue($entity->isEmpty('emptyArray'));
         $this->assertFalse($entity->isEmpty('object'));
         $this->assertFalse($entity->isEmpty('string'));
+        $this->assertFalse($entity->isEmpty('stringZero'));
         $this->assertTrue($entity->isEmpty('emptyString'));
         $this->assertFalse($entity->isEmpty('intZero'));
         $this->assertFalse($entity->isEmpty('intNotZero'));
@@ -1849,6 +1851,7 @@ class EntityTest extends TestCase
             'emptyArray' => [],
             'object' => new \stdClass(),
             'string' => 'string',
+            'stringZero' => '0',
             'emptyString' => '',
             'intZero' => 0,
             'intNotZero' => 1,
@@ -1861,6 +1864,7 @@ class EntityTest extends TestCase
         $this->assertFalse($entity->hasValue('emptyArray'));
         $this->assertTrue($entity->hasValue('object'));
         $this->assertTrue($entity->hasValue('string'));
+        $this->assertTrue($entity->hasValue('stringZero'));
         $this->assertFalse($entity->hasValue('emptyString'));
         $this->assertTrue($entity->hasValue('intZero'));
         $this->assertTrue($entity->hasValue('intNotZero'));