From 09336119e07b093cd2002996389b895784a40be7 Mon Sep 17 00:00:00 2001 From: Ali1 Date: Mon, 22 Apr 2019 19:45:47 +0100 Subject: [PATCH 1/4] Stop baking requirePresence validation when db default set --- src/Shell/Task/ModelTask.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Shell/Task/ModelTask.php b/src/Shell/Task/ModelTask.php index bbcdfffeb..177dfdc4d 100644 --- a/src/Shell/Task/ModelTask.php +++ b/src/Shell/Task/ModelTask.php @@ -768,10 +768,12 @@ public function fieldValidation($schema, $fieldName, array $metaData, $primaryKe 'args' => [], ]; } else { - $validation['requirePresence'] = [ - 'rule' => 'requirePresence', - 'args' => ["'create'"], - ]; + if ($metaData['default'] === null || $metaData['default'] === false) { + $validation['requirePresence'] = [ + 'rule' => 'requirePresence', + 'args' => ["'create'"], + ]; + } $validation['allowEmpty'] = [ 'rule' => $this->getAllowEmptyMethod($fieldName, $metaData), 'args' => ['false'], From 81acb0c50fb7ebb3e8997cb4a3eaf32ae5f17d1e Mon Sep 17 00:00:00 2001 From: Ali Bakir Date: Mon, 22 Apr 2019 20:44:49 +0100 Subject: [PATCH 2/4] Tests for new requirePresence behaviour --- tests/TestCase/Shell/Task/ModelTaskTest.php | 24 +++++-------------- ...akeAssociationDetectionCategoriesTable.php | 1 - ...ociationDetectionCategoriesTableSigned.php | 1 - ...keAssociationDetectionOldProductsTable.php | 1 - ...ciationDetectionOldProductsTableSigned.php | 1 - ...tBakeAssociationDetectionProductsTable.php | 1 - ...ssociationDetectionProductsTableSigned.php | 1 - 7 files changed, 6 insertions(+), 24 deletions(-) diff --git a/tests/TestCase/Shell/Task/ModelTaskTest.php b/tests/TestCase/Shell/Task/ModelTaskTest.php index e41c67f19..1eba329e5 100644 --- a/tests/TestCase/Shell/Task/ModelTaskTest.php +++ b/tests/TestCase/Shell/Task/ModelTaskTest.php @@ -848,7 +848,11 @@ public function testGetValidation() $model = TableRegistry::getTableLocator()->get('BakeArticles'); $result = $this->Task->getValidation($model); $expected = [ - 'bake_user_id' => [ + 'id' => [ + 'integer' => ['rule' => 'integer', 'args' => []], + 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ["'create'"]] + ], + 'bake_user_id' => [ 'integer' => ['rule' => 'integer', 'args' => []], 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => [ @@ -871,7 +875,6 @@ public function testGetValidation() ], 'rating' => [ 'numeric' => ['rule' => 'numeric', 'args' => []], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'greaterThanOrEqual' => [ 'rule' => 'greaterThanOrEqual', 'args' => [ @@ -885,7 +888,6 @@ public function testGetValidation() ], 'score' => [ 'decimal' => ['rule' => 'decimal', 'args' => []], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'greaterThanOrEqual' => [ 'rule' => 'greaterThanOrEqual', 'args' => [ @@ -899,18 +901,10 @@ public function testGetValidation() ], 'published' => [ 'boolean' => ['rule' => 'boolean', 'args' => []], - 'requirePresence' => [ - 'rule' => 'requirePresence', - 'args' => ["'create'" ], - ], 'allowEmpty' => [ 'rule' => 'allowEmptyString', 'args' => ['false'], ], - ], - 'id' => [ - 'integer' => ['rule' => 'integer', 'args' => []], - 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ["'create'"]] ] ]; $this->assertEquals($expected, $result); @@ -1182,8 +1176,7 @@ public function testGetValidationExcludeForeignKeys() 'allowEmpty' => [ 'rule' => 'allowEmptyString', 'args' => ['false'], - ], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], + ] ], 'score' => [ 'decimal' => ['rule' => 'decimal', 'args' => []], @@ -1193,7 +1186,6 @@ public function testGetValidationExcludeForeignKeys() 0, ], ], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => [ 'rule' => 'allowEmptyString', 'args' => ['false'], @@ -1204,10 +1196,6 @@ public function testGetValidationExcludeForeignKeys() 'rule' => 'boolean', 'args' => [], ], - 'requirePresence' => [ - 'rule' => 'requirePresence', - 'args' => ["'create'" ], - ], 'allowEmpty' => [ 'rule' => 'allowEmptyString', 'args' => ['false'], diff --git a/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTable.php b/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTable.php index 3109e71f6..16706d602 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTable.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTable.php @@ -62,7 +62,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; diff --git a/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTableSigned.php b/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTableSigned.php index fcdd1ddae..df1d1f23e 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTableSigned.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionCategoriesTableSigned.php @@ -62,7 +62,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; diff --git a/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTable.php b/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTable.php index 7bf97867e..43b0b5ee2 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTable.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTable.php @@ -54,7 +54,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; diff --git a/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTableSigned.php b/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTableSigned.php index 9a774f969..2eca2d004 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTableSigned.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionOldProductsTableSigned.php @@ -54,7 +54,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; diff --git a/tests/comparisons/Model/testBakeAssociationDetectionProductsTable.php b/tests/comparisons/Model/testBakeAssociationDetectionProductsTable.php index 45379b9dd..35bc1f25a 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionProductsTable.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionProductsTable.php @@ -66,7 +66,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; diff --git a/tests/comparisons/Model/testBakeAssociationDetectionProductsTableSigned.php b/tests/comparisons/Model/testBakeAssociationDetectionProductsTableSigned.php index 664ee1364..67e282c5b 100644 --- a/tests/comparisons/Model/testBakeAssociationDetectionProductsTableSigned.php +++ b/tests/comparisons/Model/testBakeAssociationDetectionProductsTableSigned.php @@ -66,7 +66,6 @@ public function validationDefault(Validator $validator) $validator ->scalar('name') ->maxLength('name', 100) - ->requirePresence('name', 'create') ->allowEmptyString('name', false); return $validator; From 350c0387eb7bf35a0b27d9c664d8aad30765465c Mon Sep 17 00:00:00 2001 From: Ali Bakir Date: Mon, 22 Apr 2019 20:49:02 +0100 Subject: [PATCH 3/4] Lint error --- tests/TestCase/Shell/Task/ModelTaskTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase/Shell/Task/ModelTaskTest.php b/tests/TestCase/Shell/Task/ModelTaskTest.php index 1eba329e5..b65153971 100644 --- a/tests/TestCase/Shell/Task/ModelTaskTest.php +++ b/tests/TestCase/Shell/Task/ModelTaskTest.php @@ -852,7 +852,7 @@ public function testGetValidation() 'integer' => ['rule' => 'integer', 'args' => []], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ["'create'"]] ], - 'bake_user_id' => [ + 'bake_user_id' => [ 'integer' => ['rule' => 'integer', 'args' => []], 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => [ From 89650c126c6819ea44f19e28362ec9ad4ce888f8 Mon Sep 17 00:00:00 2001 From: Ali Bakir Date: Tue, 23 Apr 2019 13:07:12 +0100 Subject: [PATCH 4/4] Fixed expecteds --- tests/TestCase/Shell/Task/ModelTaskTest.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/TestCase/Shell/Task/ModelTaskTest.php b/tests/TestCase/Shell/Task/ModelTaskTest.php index b65153971..c5103cf9c 100644 --- a/tests/TestCase/Shell/Task/ModelTaskTest.php +++ b/tests/TestCase/Shell/Task/ModelTaskTest.php @@ -976,18 +976,15 @@ public function testGetValidationSigned() ], 'rating' => [ 'numeric' => ['rule' => 'numeric', 'args' => []], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']], ], 'score' => [ 'decimal' => ['rule' => 'decimal', 'args' => []], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']] ], 'published' => [ 'boolean' => ['rule' => 'boolean', 'args' => []], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], ], 'id' => [ 'integer' => ['rule' => 'integer', 'args' => []], @@ -1047,9 +1044,9 @@ public function testGetValidationUniqueDateField() 'dateTime' => ['rule' => 'dateTime', 'args' => []], 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], 'allowEmpty' => [ - 'rule' => 'allowEmptyDateTime', - 'args' => ['false'], - ], + 'rule' => 'allowEmptyDateTime', + 'args' => ['false'], + ], ]; $this->assertEquals($expected, $result['release_date']); } @@ -1237,7 +1234,6 @@ public function testGetValidationExcludeForeignKeysSigned() 'published' => [ 'boolean' => ['rule' => 'boolean', 'args' => []], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], ], 'id' => [ 'integer' => ['rule' => 'integer', 'args' => []], @@ -1246,12 +1242,10 @@ public function testGetValidationExcludeForeignKeysSigned() 'rating' => [ 'numeric' => ['rule' => 'numeric', 'args' => []], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], ], 'score' => [ 'decimal' => ['rule' => 'decimal', 'args' => []], 'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => ['false']], - 'requirePresence' => ['rule' => 'requirePresence', 'args' => ["'create'"]], ] ]; $this->assertEquals($expected, $result);