Browse Source

Merge pull request #13375 from chinpei215/fix-sort-message-and-when

Fix backward incompatibility for allowEmpty* methods
Mark Story 6 years ago
parent
commit
51dcd2ab94
2 changed files with 31 additions and 0 deletions
  1. 7 0
      src/Validation/Validator.php
  2. 24 0
      tests/TestCase/Validation/ValidatorTest.php

+ 7 - 0
src/Validation/Validator.php

@@ -814,6 +814,8 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
                 is_callable($second)
             ) && (
                 is_string($first) || $first === null
+            ) && (
+                $first !== 'create' && $first !== 'update'
             )
         ) {
             return [$first, $second];
@@ -824,6 +826,11 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
             "so that they are `message, when`."
         );
 
+        // Called without the second argument.
+        if (is_bool($second)) {
+            $second = null;
+        }
+
         // Called with `$when, $message`. Reverse the
         // order to match the expected return value.
         return [$second, $first];

+ 24 - 0
tests/TestCase/Validation/ValidatorTest.php

@@ -822,6 +822,30 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Same as testAllowEmptyDateUpdateDeprecatedArguments but without message
+     *
+     * @return void
+     */
+    public function testAllowEmptyStringDeprecatedArgumentsWithoutMessage()
+    {
+        $validator = new Validator();
+        $this->deprecated(function () use ($validator) {
+            $validator->allowEmptyString('title', 'update');
+        });
+        $this->assertFalse($validator->isEmptyAllowed('title', true));
+        $this->assertTrue($validator->isEmptyAllowed('title', false));
+
+        $data = [
+            'title' => null,
+        ];
+        $expected = [
+            'title' => ['_empty' => 'This field cannot be left empty'],
+        ];
+        $this->assertSame($expected, $validator->errors($data, true));
+        $this->assertEmpty($validator->errors($data, false));
+    }
+
+    /**
      * Tests the notEmptyString method
      *
      * @return void