From 23e28b8ab392c6074b205309e3dcd78eb735b385 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 20:39:47 +0100 Subject: [PATCH 01/33] Create PublicationFieldTypeValidationRulesTest.php --- .../PublicationFieldTypeValidationRulesTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php new file mode 100644 index 00000000000..231dbcf17a0 --- /dev/null +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -0,0 +1,15 @@ + Date: Mon, 19 Dec 2022 20:59:05 +0100 Subject: [PATCH 02/33] Link to the unit test --- .../Features/Publications/Models/PublicationFieldType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index a03c4f47d25..ae7b3166162 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -64,6 +64,7 @@ public function toArray(): array ]; } + /** @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest */ public function getValidationRules(bool $reload = true): Collection { $defaultRules = Collection::create(PublicationFieldTypes::values()); From 158d1f77ffc065c2f631763954f209a1c77d801c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:01:25 +0100 Subject: [PATCH 03/33] Add testing helper to make a publication type --- .../Unit/PublicationFieldTypeValidationRulesTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php index 231dbcf17a0..4efe9c2ea79 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Unit; +use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Testing\TestCase; /** @@ -11,5 +12,13 @@ */ class PublicationFieldTypeValidationRulesTest extends TestCase { - // + protected function makePublicationType(array $fields = []): PublicationType + { + return new PublicationType( + 'test', + '__createdAt', + fields: $fields, + directory: 'test-publication', + ); + } } From 7967aa66f6fab3c96ea9696edb099c0b6b02c793 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:02:29 +0100 Subject: [PATCH 04/33] Revert "Add testing helper to make a publication type" This reverts commit 158d1f77ffc065c2f631763954f209a1c77d801c. --- .../Unit/PublicationFieldTypeValidationRulesTest.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php index 4efe9c2ea79..231dbcf17a0 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Unit; -use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Testing\TestCase; /** @@ -12,13 +11,5 @@ */ class PublicationFieldTypeValidationRulesTest extends TestCase { - protected function makePublicationType(array $fields = []): PublicationType - { - return new PublicationType( - 'test', - '__createdAt', - fields: $fields, - directory: 'test-publication', - ); - } + // } From 230cf4f423ffbd707fd837fabf001944f454c419 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:18:59 +0100 Subject: [PATCH 05/33] Implement the unit test --- ...ublicationFieldTypeValidationRulesTest.php | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php index 231dbcf17a0..6e3785747fc 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -4,6 +4,8 @@ namespace Hyde\Framework\Testing\Unit; +use Hyde\Framework\Features\Publications\Models\PublicationFieldType; +use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Testing\TestCase; /** @@ -11,5 +13,58 @@ */ class PublicationFieldTypeValidationRulesTest extends TestCase { - // + public function testWithArray() + { + $rules = (new PublicationFieldType('array', 'myArray', '4', '8'))->getValidationRules(); + $this->assertSame(['array'], $rules->toArray()); + } + + public function testWithDatetime() + { + $rules = (new PublicationFieldType('datetime', 'myDatetime', '4', '8'))->getValidationRules(); + $this->assertSame(['after:4', 'before:8'], $rules->toArray()); + } + + public function testWithFloat() + { + $rules = (new PublicationFieldType('float', 'myFloat', '4', '8'))->getValidationRules(); + $this->assertSame(['between:4,8'], $rules->toArray()); + } + + public function testWithInteger() + { + $rules = (new PublicationFieldType('integer', 'myInteger', '4', '8'))->getValidationRules(); + $this->assertSame(['between:4,8'], $rules->toArray()); + } + + public function testWithString() + { + $rules = (new PublicationFieldType('string', 'myString', '4', '8'))->getValidationRules(); + $this->assertSame(['between:4,8'], $rules->toArray()); + } + + public function testWithText() + { + $rules = (new PublicationFieldType('text', 'myText', '4', '8'))->getValidationRules(); + $this->assertSame(['between:4,8'], $rules->toArray()); + } + + public function testWithImage() + { + $rules = (new PublicationFieldType('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules(); + $this->assertSame(['in:'], $rules->toArray()); + } + + public function testWithTag() + { + $this->markTestIncomplete('tags are not working yet'); + $rules = (new PublicationFieldType('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); + $this->assertSame(['in:foo'], $rules->toArray()); // TODO tags are not working + } + + public function testWithUrl() + { + $rules = (new PublicationFieldType('url', 'myUrl', '4', '8'))->getValidationRules(); + $this->assertSame([], $rules->toArray()); + } } From 790485b4bace8db5e597fea815620804dae39c35 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:19:05 +0100 Subject: [PATCH 06/33] Test with image files --- .../tests/Unit/PublicationFieldTypeValidationRulesTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php index 6e3785747fc..d88e205ceaf 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -51,8 +51,11 @@ public function testWithText() public function testWithImage() { + $this->directory('_media/foo'); + $this->file('_media/foo/bar.jpg'); + $this->file('_media/foo/baz.png'); $rules = (new PublicationFieldType('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules(); - $this->assertSame(['in:'], $rules->toArray()); + $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); } public function testWithTag() From bd3a62bf8b893787314f656e128ee1257f634efa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:19:32 +0100 Subject: [PATCH 07/33] Null coalesce to empty collection --- .../Features/Publications/Models/PublicationFieldType.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index ae7b3166162..cb1cf35d65c 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -11,6 +11,8 @@ use Illuminate\Support\Str; use InvalidArgumentException; use Rgasch\Collection\Collection; + +use function collect; use function strtolower; /** @@ -102,7 +104,7 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add("in:$valueList"); break; case 'tag': - $tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload); + $tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload) ?? collect([]); $valueList = $tagValues->implode(','); $fieldRules->add("in:$valueList"); break; From 537bd98a061fab86eba41a9c5c890ffdcca9e98c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:19:51 +0100 Subject: [PATCH 08/33] Add code review comments --- .../Publications/Models/PublicationFieldType.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index cb1cf35d65c..1acf2ad72ec 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -72,7 +72,7 @@ public function getValidationRules(bool $reload = true): Collection $defaultRules = Collection::create(PublicationFieldTypes::values()); $fieldRules = Collection::create($defaultRules->get($this->type->value)); - $doBetween = true; + $doBetween = true; // FIXME: maybe rename to useRange? // The trim command used to process the min/max input results in a string, so // we need to test both int and string values to determine required status. if (($this->min && ! $this->max) || ($this->min == '0' && $this->max == '0')) { @@ -82,7 +82,7 @@ public function getValidationRules(bool $reload = true): Collection switch ($this->type->value) { case 'array': - $fieldRules->add('array'); + $fieldRules->add('array'); // FIXME do we do range validation too? break; case 'datetime': if ($doBetween) { @@ -101,7 +101,10 @@ public function getValidationRules(bool $reload = true): Collection case 'image': $mediaFiles = PublicationService::getMediaForPubType($this->publicationType, $reload); $valueList = $mediaFiles->implode(','); - $fieldRules->add("in:$valueList"); + $fieldRules->add("in:$valueList"); // FIXME What if the list is empty? + // FIXME: Now the items look like 'in:_media/foo/bar.jpg', but do we really need the directory information? + // Wouldn't it suffice with just 'in:bar.jpg' since we already know what directory it is in? + // We could then easily qualify it within the template and/or via a helper method. break; case 'tag': $tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload) ?? collect([]); @@ -109,6 +112,7 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add("in:$valueList"); break; case 'url': + // FIXME Shouldn't we add a 'url' rule here? break; default: throw new \InvalidArgumentException( From 0161c3a35150c3e0dc2b77e60151cd9f5bf3e542 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:20:58 +0100 Subject: [PATCH 09/33] Remove default switch case that's impossible to reach due to enum --- .../Features/Publications/Models/PublicationFieldType.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index 1acf2ad72ec..5c1351acbc1 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -114,10 +114,6 @@ public function getValidationRules(bool $reload = true): Collection case 'url': // FIXME Shouldn't we add a 'url' rule here? break; - default: - throw new \InvalidArgumentException( - "Unhandled field type [{$this->type->value}]. Possible field types are: ".implode(', ', PublicationFieldTypes::values()) - ); } return $fieldRules; From dbc9230fd010accbe81954aff5e1973ac7897f15 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:21:52 +0100 Subject: [PATCH 10/33] Update skipped test --- .../tests/Unit/PublicationFieldTypeValidationRulesTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php index d88e205ceaf..ffc2d11f38a 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php @@ -60,9 +60,8 @@ public function testWithImage() public function testWithTag() { - $this->markTestIncomplete('tags are not working yet'); $rules = (new PublicationFieldType('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); - $this->assertSame(['in:foo'], $rules->toArray()); // TODO tags are not working + $this->assertSame(['in:'], $rules->toArray()); } public function testWithUrl() From 85d27456428f7bdcc32d00b76a7b5953b568d4cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 19 Dec 2022 21:22:31 +0100 Subject: [PATCH 11/33] Rename local variable doBetween to useRange --- .../Features/Publications/Models/PublicationFieldType.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index 5c1351acbc1..0283a1e75b1 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -72,12 +72,12 @@ public function getValidationRules(bool $reload = true): Collection $defaultRules = Collection::create(PublicationFieldTypes::values()); $fieldRules = Collection::create($defaultRules->get($this->type->value)); - $doBetween = true; // FIXME: maybe rename to useRange? + $useRange = true; // The trim command used to process the min/max input results in a string, so // we need to test both int and string values to determine required status. if (($this->min && ! $this->max) || ($this->min == '0' && $this->max == '0')) { $fieldRules->forget($fieldRules->search('required')); - $doBetween = false; + $useRange = false; } switch ($this->type->value) { @@ -85,7 +85,7 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add('array'); // FIXME do we do range validation too? break; case 'datetime': - if ($doBetween) { + if ($useRange) { $fieldRules->add("after:$this->min"); $fieldRules->add("before:$this->max"); } @@ -94,7 +94,7 @@ public function getValidationRules(bool $reload = true): Collection case 'integer': case 'string': case 'text': - if ($doBetween) { + if ($useRange) { $fieldRules->add("between:$this->min,$this->max"); } break; From e9d044801fc9d14b3696d0917e05eb1742187c53 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 19 Dec 2022 20:22:48 +0000 Subject: [PATCH 12/33] Apply fixes from StyleCI --- .../Features/Publications/Models/PublicationFieldType.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index 0283a1e75b1..b3d8648b37e 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Publications\Models; +use function collect; use Hyde\Framework\Features\Publications\Concerns\PublicationFieldTypes; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Support\Concerns\Serializable; @@ -11,8 +12,6 @@ use Illuminate\Support\Str; use InvalidArgumentException; use Rgasch\Collection\Collection; - -use function collect; use function strtolower; /** @@ -72,7 +71,7 @@ public function getValidationRules(bool $reload = true): Collection $defaultRules = Collection::create(PublicationFieldTypes::values()); $fieldRules = Collection::create($defaultRules->get($this->type->value)); - $useRange = true; + $useRange = true; // The trim command used to process the min/max input results in a string, so // we need to test both int and string values to determine required status. if (($this->min && ! $this->max) || ($this->min == '0' && $this->max == '0')) { From 584a3a4eff12aa57acbf9ce815c420aa1b3986b6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 20 Dec 2022 11:11:00 +0100 Subject: [PATCH 13/33] Resolve fixme --- .../Features/Publications/Models/PublicationFieldType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php index b3d8648b37e..7d72156035e 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldType.php @@ -81,7 +81,7 @@ public function getValidationRules(bool $reload = true): Collection switch ($this->type->value) { case 'array': - $fieldRules->add('array'); // FIXME do we do range validation too? + $fieldRules->add('array'); break; case 'datetime': if ($useRange) { From 64ead71609759004609f92f50dda0e4f4006e93c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 20 Dec 2022 17:35:13 +0100 Subject: [PATCH 14/33] Update PublicationFieldValidationRulesTest for renamed class --- ...> PublicationFieldValidationRulesTest.php} | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) rename packages/framework/tests/Unit/{PublicationFieldTypeValidationRulesTest.php => PublicationFieldValidationRulesTest.php} (64%) diff --git a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php similarity index 64% rename from packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php rename to packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index ffc2d11f38a..6eefb9c9d9c 100644 --- a/packages/framework/tests/Unit/PublicationFieldTypeValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -4,48 +4,48 @@ namespace Hyde\Framework\Testing\Unit; -use Hyde\Framework\Features\Publications\Models\PublicationFieldType; +use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Testing\TestCase; /** - * @covers \Hyde\Framework\Features\Publications\Models\PublicationFieldType + * @covers \Hyde\Framework\Features\Publications\Models\PublicationField */ -class PublicationFieldTypeValidationRulesTest extends TestCase +class PublicationFieldValidationRulesTest extends TestCase { public function testWithArray() { - $rules = (new PublicationFieldType('array', 'myArray', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('array', 'myArray', '4', '8'))->getValidationRules(); $this->assertSame(['array'], $rules->toArray()); } public function testWithDatetime() { - $rules = (new PublicationFieldType('datetime', 'myDatetime', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('datetime', 'myDatetime', '4', '8'))->getValidationRules(); $this->assertSame(['after:4', 'before:8'], $rules->toArray()); } public function testWithFloat() { - $rules = (new PublicationFieldType('float', 'myFloat', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('float', 'myFloat', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } public function testWithInteger() { - $rules = (new PublicationFieldType('integer', 'myInteger', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('integer', 'myInteger', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } public function testWithString() { - $rules = (new PublicationFieldType('string', 'myString', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('string', 'myString', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } public function testWithText() { - $rules = (new PublicationFieldType('text', 'myText', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('text', 'myText', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } @@ -54,19 +54,19 @@ public function testWithImage() $this->directory('_media/foo'); $this->file('_media/foo/bar.jpg'); $this->file('_media/foo/baz.png'); - $rules = (new PublicationFieldType('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules(); + $rules = (new PublicationField('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules(); $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); } public function testWithTag() { - $rules = (new PublicationFieldType('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); + $rules = (new PublicationField('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); $this->assertSame(['in:'], $rules->toArray()); } public function testWithUrl() { - $rules = (new PublicationFieldType('url', 'myUrl', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('url', 'myUrl', '4', '8'))->getValidationRules(); $this->assertSame([], $rules->toArray()); } } From b0d75d1f3942f468d5bab1a3d359467eccfcfb13 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:05:36 +0100 Subject: [PATCH 15/33] Resolve todos --- .../Features/Publications/Models/PublicationField.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 3098278c10f..fd0ced3128a 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -96,10 +96,7 @@ public function getValidationRules(bool $reload = true): Collection case 'image': $mediaFiles = PublicationService::getMediaForPubType($this->publicationType, $reload); $valueList = $mediaFiles->implode(','); - $fieldRules->add("in:$valueList"); // FIXME What if the list is empty? - // FIXME: Now the items look like 'in:_media/foo/bar.jpg', but do we really need the directory information? - // Wouldn't it suffice with just 'in:bar.jpg' since we already know what directory it is in? - // We could then easily qualify it within the template and/or via a helper method. + $fieldRules->add("in:$valueList"); break; case 'tag': $tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload) ?? collect([]); From 43086435ce1408b4f7642b84eac4880d221797fe Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:06:57 +0100 Subject: [PATCH 16/33] Add the URL rule to URLs --- .../Framework/Features/Publications/Models/PublicationField.php | 2 +- .../tests/Unit/PublicationFieldValidationRulesTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index fd0ced3128a..fad774692af 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -104,7 +104,7 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add("in:$valueList"); break; case 'url': - // FIXME Shouldn't we add a 'url' rule here? + $fieldRules->add('url'); break; } diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 6eefb9c9d9c..4d14dd88158 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -67,6 +67,6 @@ public function testWithTag() public function testWithUrl() { $rules = (new PublicationField('url', 'myUrl', '4', '8'))->getValidationRules(); - $this->assertSame([], $rules->toArray()); + $this->assertSame(['url'], $rules->toArray()); } } From 4eed23eeefa139abe9842349fdd3a1d4f6821a2c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:07:18 +0100 Subject: [PATCH 17/33] Link to the rules documentation --- .../Features/Publications/Models/PublicationField.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index fad774692af..cd6b72da7a3 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -61,7 +61,10 @@ public function toArray(): array ]; } - /** @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest */ + /** + * @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest + * @see https://laravel.com/docs/9.x/validation#available-validation-rules + */ public function getValidationRules(bool $reload = true): Collection { $defaultRules = Collection::create(PublicationFieldTypes::values()); From a601376a29b83216a74a10a44f5e2dcd3fff0f32 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:14:28 +0100 Subject: [PATCH 18/33] Rename test methods --- .../PublicationFieldValidationRulesTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 4d14dd88158..4e05555e865 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -13,43 +13,43 @@ */ class PublicationFieldValidationRulesTest extends TestCase { - public function testWithArray() + public function testGetRulesForArray() { $rules = (new PublicationField('array', 'myArray', '4', '8'))->getValidationRules(); $this->assertSame(['array'], $rules->toArray()); } - public function testWithDatetime() + public function testGetRulesForDatetime() { $rules = (new PublicationField('datetime', 'myDatetime', '4', '8'))->getValidationRules(); $this->assertSame(['after:4', 'before:8'], $rules->toArray()); } - public function testWithFloat() + public function testGetRulesForFloat() { $rules = (new PublicationField('float', 'myFloat', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } - public function testWithInteger() + public function testGetRulesForInteger() { $rules = (new PublicationField('integer', 'myInteger', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } - public function testWithString() + public function testGetRulesForString() { $rules = (new PublicationField('string', 'myString', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } - public function testWithText() + public function testGetRulesForText() { $rules = (new PublicationField('text', 'myText', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); } - public function testWithImage() + public function testGetRulesForImage() { $this->directory('_media/foo'); $this->file('_media/foo/bar.jpg'); @@ -58,13 +58,13 @@ public function testWithImage() $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); } - public function testWithTag() + public function testGetRulesForTag() { $rules = (new PublicationField('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); $this->assertSame(['in:'], $rules->toArray()); } - public function testWithUrl() + public function testGetRulesForUrl() { $rules = (new PublicationField('url', 'myUrl', '4', '8'))->getValidationRules(); $this->assertSame(['url'], $rules->toArray()); From f03816577d991e9247db5b8f83e2a07fe1c9148f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:21:12 +0100 Subject: [PATCH 19/33] Add testing helper to expect a validation exception --- .../tests/Unit/PublicationFieldValidationRulesTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 4e05555e865..ea695829e02 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -7,6 +7,7 @@ use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Testing\TestCase; +use Illuminate\Validation\ValidationException; /** * @covers \Hyde\Framework\Features\Publications\Models\PublicationField @@ -69,4 +70,10 @@ public function testGetRulesForUrl() $rules = (new PublicationField('url', 'myUrl', '4', '8'))->getValidationRules(); $this->assertSame(['url'], $rules->toArray()); } + + protected function expectValidationException(string $message): void + { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage($message); + } } From e2f1f26ad17c814b8163c4a2b46da4af0e5e0c87 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:28:54 +0100 Subject: [PATCH 20/33] Parse the datetimes to ensure they are valid and normalized --- .../Features/Publications/Models/PublicationField.php | 8 ++++++-- .../tests/Unit/PublicationFieldValidationRulesTest.php | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index cd6b72da7a3..8d6194d8b06 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Publications\Models; +use Illuminate\Support\Carbon; use function collect; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Framework\Features\Publications\PublicationService; @@ -84,8 +85,11 @@ public function getValidationRules(bool $reload = true): Collection break; case 'datetime': if ($useRange) { - $fieldRules->add("after:$this->min"); - $fieldRules->add("before:$this->max"); + // Parse the datetimes to ensure they are valid and normalized. + $dateMin = Carbon::parse($this->min); + $dateMax = Carbon::parse($this->max); + $fieldRules->add("after:$dateMin"); + $fieldRules->add("before:$dateMax"); } break; case 'float': diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index ea695829e02..be9fec066b8 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -22,8 +22,8 @@ public function testGetRulesForArray() public function testGetRulesForDatetime() { - $rules = (new PublicationField('datetime', 'myDatetime', '4', '8'))->getValidationRules(); - $this->assertSame(['after:4', 'before:8'], $rules->toArray()); + $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01', '2022-01-01'))->getValidationRules(); + $this->assertSame(['after:2021-01-01 00:00:00', 'before:2022-01-01 00:00:00'], $rules->toArray()); } public function testGetRulesForFloat() From 789486956fbec0655f0a1623bb5374215d8831e8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:37:40 +0100 Subject: [PATCH 21/33] Support only using min date values --- .../Features/Publications/Models/PublicationField.php | 3 +++ .../tests/Unit/PublicationFieldValidationRulesTest.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 8d6194d8b06..ec898deca9a 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -90,6 +90,9 @@ public function getValidationRules(bool $reload = true): Collection $dateMax = Carbon::parse($this->max); $fieldRules->add("after:$dateMin"); $fieldRules->add("before:$dateMax"); + } elseif ($this->min) { + $dateMin = Carbon::parse($this->min); + $fieldRules->add("after:$dateMin"); } break; case 'float': diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index be9fec066b8..fe9cea68a1e 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -24,6 +24,9 @@ public function testGetRulesForDatetime() { $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01', '2022-01-01'))->getValidationRules(); $this->assertSame(['after:2021-01-01 00:00:00', 'before:2022-01-01 00:00:00'], $rules->toArray()); + + $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->getValidationRules(); + $this->assertSame(['after:2021-01-01 00:00:00'], $rules->toArray()); } public function testGetRulesForFloat() From 1f4420ff1a02624c2ab64f22ad95a9157a4c18e6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:39:32 +0100 Subject: [PATCH 22/33] Remove code comment --- .../Framework/Features/Publications/Models/PublicationField.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index ec898deca9a..715d3a86fb4 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -85,7 +85,6 @@ public function getValidationRules(bool $reload = true): Collection break; case 'datetime': if ($useRange) { - // Parse the datetimes to ensure they are valid and normalized. $dateMin = Carbon::parse($this->min); $dateMax = Carbon::parse($this->max); $fieldRules->add("after:$dateMin"); From 851f6673906e430d044dd947fb122f8501148233 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:39:22 +0100 Subject: [PATCH 23/33] Evaluate min/max values for dates independently --- .../Features/Publications/Models/PublicationField.php | 9 ++++----- .../tests/Unit/PublicationFieldValidationRulesTest.php | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 715d3a86fb4..8f029d7b76c 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -84,14 +84,13 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add('array'); break; case 'datetime': - if ($useRange) { + if ($this->min) { $dateMin = Carbon::parse($this->min); - $dateMax = Carbon::parse($this->max); $fieldRules->add("after:$dateMin"); + } + if ($this->max) { + $dateMax = Carbon::parse($this->max); $fieldRules->add("before:$dateMax"); - } elseif ($this->min) { - $dateMin = Carbon::parse($this->min); - $fieldRules->add("after:$dateMin"); } break; case 'float': diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index fe9cea68a1e..39e831fc86a 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -27,6 +27,9 @@ public function testGetRulesForDatetime() $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->getValidationRules(); $this->assertSame(['after:2021-01-01 00:00:00'], $rules->toArray()); + + $rules = (new PublicationField('datetime', 'myDatetime', null, '2022-01-01'))->getValidationRules(); + $this->assertSame(['before:2022-01-01 00:00:00'], $rules->toArray()); } public function testGetRulesForFloat() From 4db1fe16cb6a4727e321b9a6130e4a28da3d114d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:40:04 +0100 Subject: [PATCH 24/33] Test array validation --- .../Unit/PublicationFieldValidationRulesTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 39e831fc86a..931fc2e2351 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -20,6 +20,18 @@ public function testGetRulesForArray() $this->assertSame(['array'], $rules->toArray()); } + public function testValidateArrayPasses() + { + $validated = (new PublicationField('array', 'myArray', '4', '8'))->validate(['foo', 'bar', 'baz']); + $this->assertSame(['my-array' => ['foo', 'bar', 'baz']], $validated); + } + + public function testValidateArrayFails() + { + $this->expectValidationException('The my-array must be an array.'); + (new PublicationField('array', 'myArray', '4', '8'))->validate('foo'); + } + public function testGetRulesForDatetime() { $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01', '2022-01-01'))->getValidationRules(); From bc32f14c95dad3ce5b303f87bd95ef2c882082c4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:47:04 +0100 Subject: [PATCH 25/33] Add the date rule to datetime fields --- .../Features/Publications/Models/PublicationField.php | 1 + .../tests/Unit/PublicationFieldValidationRulesTest.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 8f029d7b76c..25ba3054b10 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -84,6 +84,7 @@ public function getValidationRules(bool $reload = true): Collection $fieldRules->add('array'); break; case 'datetime': + $fieldRules->add('date'); if ($this->min) { $dateMin = Carbon::parse($this->min); $fieldRules->add("after:$dateMin"); diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 931fc2e2351..1e7cafa18a8 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -35,13 +35,13 @@ public function testValidateArrayFails() public function testGetRulesForDatetime() { $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01', '2022-01-01'))->getValidationRules(); - $this->assertSame(['after:2021-01-01 00:00:00', 'before:2022-01-01 00:00:00'], $rules->toArray()); + $this->assertSame(['date', 'after:2021-01-01 00:00:00', 'before:2022-01-01 00:00:00'], $rules->toArray()); $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->getValidationRules(); - $this->assertSame(['after:2021-01-01 00:00:00'], $rules->toArray()); + $this->assertSame(['date', 'after:2021-01-01 00:00:00'], $rules->toArray()); $rules = (new PublicationField('datetime', 'myDatetime', null, '2022-01-01'))->getValidationRules(); - $this->assertSame(['before:2022-01-01 00:00:00'], $rules->toArray()); + $this->assertSame(['date', 'before:2022-01-01 00:00:00'], $rules->toArray()); } public function testGetRulesForFloat() From 4492aa7baa6ba771c01dfac44604cccf3df8e712 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:49:31 +0100 Subject: [PATCH 26/33] Test datetime validation --- .../PublicationFieldValidationRulesTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 1e7cafa18a8..34c2a5699c7 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -44,6 +44,30 @@ public function testGetRulesForDatetime() $this->assertSame(['date', 'before:2022-01-01 00:00:00'], $rules->toArray()); } + public function testValidateDatetimePasses() + { + $validated = (new PublicationField('datetime', 'myDatetime'))->validate('2021-01-01'); + $this->assertSame(['my-datetime' => '2021-01-01'], $validated); + + $validated = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2021-01-02'); + $this->assertSame(['my-datetime' => '2021-01-02'], $validated); + + $validated = (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-01'); + $this->assertSame(['my-datetime' => '2021-01-01'], $validated); + } + + public function testValidateDatetimeFails() + { + $this->expectValidationException('The my-datetime is not a valid date.'); + (new PublicationField('datetime', 'myDatetime'))->validate('string'); + + $this->expectValidationException('The my-datetime must be a date after 2021-01-01 00:00:00.'); + (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2020-12-31'); + + $this->expectValidationException('The my-datetime must be a date before 2021-01-02 00:00:00.'); + (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-03'); + } + public function testGetRulesForFloat() { $rules = (new PublicationField('float', 'myFloat', '4', '8'))->getValidationRules(); From 380ae76ab01e9234dceb8f5ac46122fb7efcc6d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:50:06 +0100 Subject: [PATCH 27/33] Split out test methods --- .../tests/Unit/PublicationFieldValidationRulesTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 34c2a5699c7..f0f3ade3901 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -56,14 +56,20 @@ public function testValidateDatetimePasses() $this->assertSame(['my-datetime' => '2021-01-01'], $validated); } - public function testValidateDatetimeFails() + public function testValidateDatetimeFails1() { $this->expectValidationException('The my-datetime is not a valid date.'); (new PublicationField('datetime', 'myDatetime'))->validate('string'); + } + public function testValidateDatetimeFails2() + { $this->expectValidationException('The my-datetime must be a date after 2021-01-01 00:00:00.'); (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2020-12-31'); + } + public function testValidateDatetimeFails3() + { $this->expectValidationException('The my-datetime must be a date before 2021-01-02 00:00:00.'); (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-03'); } From a50908c979e9fb963014891b35f01eee62b42e3f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 21 Dec 2022 15:50:33 +0000 Subject: [PATCH 28/33] Apply fixes from StyleCI --- .../Framework/Features/Publications/Models/PublicationField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 25ba3054b10..208cec203e9 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -4,12 +4,12 @@ namespace Hyde\Framework\Features\Publications\Models; -use Illuminate\Support\Carbon; use function collect; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Support\Concerns\Serializable; use Hyde\Support\Contracts\SerializableContract; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; use InvalidArgumentException; use Rgasch\Collection\Collection; From 374bb41f04c458e5430b01ff90cdc1ade3cad6e9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:52:45 +0100 Subject: [PATCH 29/33] Allow only min value to be set --- .../Publications/Models/PublicationField.php | 2 +- .../framework/tests/Feature/PublicationFieldTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 208cec203e9..ef24270edc7 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -46,7 +46,7 @@ public function __construct(PublicationFieldTypes|string $type, string $name, in $this->tagGroup = $tagGroup; $this->publicationType = $publicationType; - if ($max < $min) { + if ($max < $min && $max !== '0') { throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value."); } } diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index dbf888a52af..10df3af683f 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -92,6 +92,18 @@ public function test_max_value_cannot_be_less_than_min_value() new PublicationField('string', 'test', '10', '1'); } + public function test_only_min_value_can_be_set() + { + new PublicationField('string', 'test', '1'); + $this->assertTrue(true); + } + + public function test_only_max_value_can_be_set() + { + new PublicationField('string', 'test', null, '10'); + $this->assertTrue(true); + } + public function test_integers_can_be_added_as_strings() { $field = new PublicationField('string', 'test', '1', '10'); From 76431ffd95484f4195166b009a9e5cd2e332c4a8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:54:05 +0100 Subject: [PATCH 30/33] Use class constant instead of FQCN --- packages/framework/tests/Feature/PublicationFieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index 10df3af683f..fa4254405f3 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -114,7 +114,7 @@ public function test_integers_can_be_added_as_strings() public function test_type_must_be_valid() { $this->expectException(ValueError::class); - $this->expectExceptionMessage('"invalid" is not a valid backing value for enum "Hyde\Framework\Features\Publications\PublicationFieldTypes"'); + $this->expectExceptionMessage('"invalid" is not a valid backing value for enum "'.PublicationFieldTypes::class.'"'); new PublicationField('invalid', 'test', '1', '10'); } From 22f57d53671a965e43dde1db93d2b4aff12f2892 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:55:52 +0100 Subject: [PATCH 31/33] Rename test methods to be more descriptive --- .../tests/Unit/PublicationFieldValidationRulesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index f0f3ade3901..0d0ebcf3d77 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -56,19 +56,19 @@ public function testValidateDatetimePasses() $this->assertSame(['my-datetime' => '2021-01-01'], $validated); } - public function testValidateDatetimeFails1() + public function testValidateDatetimeFailsForInvalidType() { $this->expectValidationException('The my-datetime is not a valid date.'); (new PublicationField('datetime', 'myDatetime'))->validate('string'); } - public function testValidateDatetimeFails2() + public function testValidateDatetimeFailsForInvalidMinValue() { $this->expectValidationException('The my-datetime must be a date after 2021-01-01 00:00:00.'); (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2020-12-31'); } - public function testValidateDatetimeFails3() + public function testValidateDatetimeFailsForInvalidMaxValue() { $this->expectValidationException('The my-datetime must be a date before 2021-01-02 00:00:00.'); (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-03'); From 7aeeb2d45fc8ffa5dc8f8990c952c1584f12dfb3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 16:58:18 +0100 Subject: [PATCH 32/33] Floats must be numeric to be properly range validated --- .../Features/Publications/Models/PublicationField.php | 5 +++++ .../tests/Unit/PublicationFieldValidationRulesTest.php | 1 + 2 files changed, 6 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index ef24270edc7..a892c242ce5 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -95,6 +95,11 @@ public function getValidationRules(bool $reload = true): Collection } break; case 'float': + $fieldRules->add('numeric'); + if ($useRange) { + $fieldRules->add("between:$this->min,$this->max"); + } + break; case 'integer': case 'string': case 'text': diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 0d0ebcf3d77..52aa43aa69b 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -78,6 +78,7 @@ public function testGetRulesForFloat() { $rules = (new PublicationField('float', 'myFloat', '4', '8'))->getValidationRules(); $this->assertSame(['between:4,8'], $rules->toArray()); + $this->assertSame(['numeric', 'between:4,8'], $rules->toArray()); } public function testGetRulesForInteger() From 353e66d5809eece9ea3e71166080a80f28d1b4c1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 21 Dec 2022 17:04:46 +0100 Subject: [PATCH 33/33] Remove range validation tests See https://github.com/hydephp/develop/pull/765#issuecomment-1361564784 --- .../PublicationFieldValidationRulesTest.php | 57 ++++++------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php index 52aa43aa69b..a66712ea01b 100644 --- a/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php +++ b/packages/framework/tests/Unit/PublicationFieldValidationRulesTest.php @@ -16,44 +16,32 @@ class PublicationFieldValidationRulesTest extends TestCase { public function testGetRulesForArray() { - $rules = (new PublicationField('array', 'myArray', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('array', 'myArray'))->getValidationRules(); $this->assertSame(['array'], $rules->toArray()); } public function testValidateArrayPasses() { - $validated = (new PublicationField('array', 'myArray', '4', '8'))->validate(['foo', 'bar', 'baz']); + $validated = (new PublicationField('array', 'myArray'))->validate(['foo', 'bar', 'baz']); $this->assertSame(['my-array' => ['foo', 'bar', 'baz']], $validated); } public function testValidateArrayFails() { $this->expectValidationException('The my-array must be an array.'); - (new PublicationField('array', 'myArray', '4', '8'))->validate('foo'); + (new PublicationField('array', 'myArray'))->validate('foo'); } public function testGetRulesForDatetime() { - $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01', '2022-01-01'))->getValidationRules(); - $this->assertSame(['date', 'after:2021-01-01 00:00:00', 'before:2022-01-01 00:00:00'], $rules->toArray()); - - $rules = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->getValidationRules(); - $this->assertSame(['date', 'after:2021-01-01 00:00:00'], $rules->toArray()); - - $rules = (new PublicationField('datetime', 'myDatetime', null, '2022-01-01'))->getValidationRules(); - $this->assertSame(['date', 'before:2022-01-01 00:00:00'], $rules->toArray()); + $rules = (new PublicationField('datetime', 'myDatetime'))->getValidationRules(); + $this->assertSame(['date'], $rules->toArray()); } public function testValidateDatetimePasses() { $validated = (new PublicationField('datetime', 'myDatetime'))->validate('2021-01-01'); $this->assertSame(['my-datetime' => '2021-01-01'], $validated); - - $validated = (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2021-01-02'); - $this->assertSame(['my-datetime' => '2021-01-02'], $validated); - - $validated = (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-01'); - $this->assertSame(['my-datetime' => '2021-01-01'], $validated); } public function testValidateDatetimeFailsForInvalidType() @@ -62,41 +50,28 @@ public function testValidateDatetimeFailsForInvalidType() (new PublicationField('datetime', 'myDatetime'))->validate('string'); } - public function testValidateDatetimeFailsForInvalidMinValue() - { - $this->expectValidationException('The my-datetime must be a date after 2021-01-01 00:00:00.'); - (new PublicationField('datetime', 'myDatetime', '2021-01-01'))->validate('2020-12-31'); - } - - public function testValidateDatetimeFailsForInvalidMaxValue() - { - $this->expectValidationException('The my-datetime must be a date before 2021-01-02 00:00:00.'); - (new PublicationField('datetime', 'myDatetime', null, '2021-01-02'))->validate('2021-01-03'); - } - public function testGetRulesForFloat() { - $rules = (new PublicationField('float', 'myFloat', '4', '8'))->getValidationRules(); - $this->assertSame(['between:4,8'], $rules->toArray()); - $this->assertSame(['numeric', 'between:4,8'], $rules->toArray()); + $rules = (new PublicationField('float', 'myFloat'))->getValidationRules(); + $this->assertSame(['numeric'], $rules->toArray()); } public function testGetRulesForInteger() { - $rules = (new PublicationField('integer', 'myInteger', '4', '8'))->getValidationRules(); - $this->assertSame(['between:4,8'], $rules->toArray()); + $rules = (new PublicationField('integer', 'myInteger'))->getValidationRules(); + $this->assertSame([], $rules->toArray()); } public function testGetRulesForString() { - $rules = (new PublicationField('string', 'myString', '4', '8'))->getValidationRules(); - $this->assertSame(['between:4,8'], $rules->toArray()); + $rules = (new PublicationField('string', 'myString'))->getValidationRules(); + $this->assertSame([], $rules->toArray()); } public function testGetRulesForText() { - $rules = (new PublicationField('text', 'myText', '4', '8'))->getValidationRules(); - $this->assertSame(['between:4,8'], $rules->toArray()); + $rules = (new PublicationField('text', 'myText'))->getValidationRules(); + $this->assertSame([], $rules->toArray()); } public function testGetRulesForImage() @@ -104,19 +79,19 @@ public function testGetRulesForImage() $this->directory('_media/foo'); $this->file('_media/foo/bar.jpg'); $this->file('_media/foo/baz.png'); - $rules = (new PublicationField('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules(); + $rules = (new PublicationField('image', 'myImage', publicationType: new PublicationType('foo')))->getValidationRules(); $this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray()); } public function testGetRulesForTag() { - $rules = (new PublicationField('tag', 'myTag', '4', '8', 'foo'))->getValidationRules(); + $rules = (new PublicationField('tag', 'myTag', tagGroup: 'foo'))->getValidationRules(); $this->assertSame(['in:'], $rules->toArray()); } public function testGetRulesForUrl() { - $rules = (new PublicationField('url', 'myUrl', '4', '8'))->getValidationRules(); + $rules = (new PublicationField('url', 'myUrl'))->getValidationRules(); $this->assertSame(['url'], $rules->toArray()); }