From a09cd8662a57ddc7a20fc72b49dfa4ca888d242c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:39:56 +0100 Subject: [PATCH 01/49] Create crude validation method for publication types --- .../src/Models/PublicationType.php | 66 ++++++++++++++++++- .../tests/Feature/PublicationTypeTest.php | 11 ++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 944c83684ac..9f0d1d8304b 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -4,6 +4,7 @@ namespace Hyde\Publications\Models; +use Hyde\Facades\Filesystem; use function array_filter; use function array_merge; use function dirname; @@ -84,7 +85,7 @@ public static function fromFile(string $schemaFile): static } public function __construct( - string $name, + string $name, // todo get from directory name if not set in schema? string $canonicalField = '__createdAt', string $detailTemplate = 'detail.blade.php', string $listTemplate = 'list.blade.php', @@ -217,4 +218,67 @@ protected function withoutNullValues(array $array): array { return array_filter($array, fn (mixed $value): bool => ! is_null($value)); } + + /** + * Validate the schema.json file is valid. + * + * @internal This method is experimental and may be removed without notice + */ + public function validateSchemaFile(): void + { + $schema = json_decode(Filesystem::getContents($this->getSchemaFile())); + + if (isset($schema->directory)) { + throw new RuntimeException("The 'directory' property is not allowed in the schema file as that may cause unexpected issues."); + } + + $shouldBeStrings = ['name', 'canonicalField', 'detailTemplate', 'listTemplate', 'sortField']; + $shouldBeBooleans = ['sortAscending']; + $shouldBeIntegers = ['pageSize']; + $shouldBeArrays = ['fields']; + + foreach ($schema as $key => $value) { + if (in_array($key, $shouldBeStrings) && ! is_string($value)) { + throw new RuntimeException("The '$key' property must be a string."); + } + + if (in_array($key, $shouldBeBooleans) && ! is_bool($value)) { + throw new RuntimeException("The '$key' property must be a boolean."); + } + + if (in_array($key, $shouldBeIntegers) && ! is_int($value)) { + throw new RuntimeException("The '$key' property must be an integer."); + } + + if (in_array($key, $shouldBeArrays) && ! is_array($value)) { + throw new RuntimeException("The '$key' property must be an array."); + } + } + + foreach ($schema->fields as $field) { + if (! isset($field->type)) { + throw new RuntimeException("The 'type' property is required for each field."); + } + + if (! isset($field->name)) { + throw new RuntimeException("The 'name' property is required for each field."); + } + + if (! is_string($field->type)) { + throw new RuntimeException("The 'type' property must be a string."); + } + + if (! is_string($field->name)) { + throw new RuntimeException("The 'name' property must be a string."); + } + + if (isset($field->rules) && ! is_array($field->rules)) { + throw new RuntimeException("The 'rules' property must be an array."); + } + + if (isset($field->tagGroup) && ! is_string($field->tagGroup)) { + throw new RuntimeException("The 'tagGroup' property must be a string."); + } + } + } } diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 4d7974b6528..e9171087f9c 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -428,6 +428,17 @@ public function testJsonRepresentationWithDefaultValues() JSON, $publicationType->toJson()); } + public function testValidateSchemaFile() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $publicationType->validateSchemaFile(); + + $this->assertTrue(true); + } + protected function getTestData(array $mergeData = []): array { return array_merge([ From c09a121c24617e4fbadebf03a49c6d6cb3b75cf1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:40:57 +0100 Subject: [PATCH 02/49] Add todos --- .../publications/src/Models/PublicationType.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 9f0d1d8304b..a0f693fe8f4 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -95,7 +95,7 @@ public function __construct( array $fields = [], ?string $directory = null ) { - $this->name = $name; + $this->name = $name; // todo get from directory name if not set in schema? $this->canonicalField = $canonicalField; $this->detailTemplate = $detailTemplate; $this->listTemplate = $listTemplate; @@ -238,6 +238,8 @@ public function validateSchemaFile(): void $shouldBeArrays = ['fields']; foreach ($schema as $key => $value) { + // TODO throw (custom?) validation exception instead? + if (in_array($key, $shouldBeStrings) && ! is_string($value)) { throw new RuntimeException("The '$key' property must be a string."); } @@ -255,6 +257,14 @@ public function validateSchemaFile(): void } } + // TODO warn if fields are empty? + + // TODO warn if canonicalField does not match meta field or actual? + + // TODO Warn if template files do not exist (assuming files not vendor views)? + + // TODO warn if pageSize is less than 0 (as that equals no pagination)? + foreach ($schema->fields as $field) { if (! isset($field->type)) { throw new RuntimeException("The 'type' property is required for each field."); @@ -278,6 +288,7 @@ public function validateSchemaFile(): void if (isset($field->tagGroup) && ! is_string($field->tagGroup)) { throw new RuntimeException("The 'tagGroup' property must be a string."); + // TODO check tag group exists? } } } From 1a8b68ea58a5bf376a9a0c90c7cb57fffaee9981 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:44:49 +0100 Subject: [PATCH 03/49] Test with invalid schema --- .../tests/Feature/PublicationTypeTest.php | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index e9171087f9c..ba339966d3f 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -438,7 +438,31 @@ public function testValidateSchemaFile() $this->assertTrue(true); } - + + public function testValidateSchemaFileWithInvalidSchema() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": 123 + } + JSON + ); + + $this->expectException(RuntimeException::class); + $publicationType->validateSchemaFile(); + } + protected function getTestData(array $mergeData = []): array { return array_merge([ From c76de85590789f7d15418909040f27dad9751559 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:55:17 +0100 Subject: [PATCH 04/49] Refactor to use the Illuminate validator --- .../src/Models/PublicationType.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index a0f693fe8f4..7351e5a5769 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -5,6 +5,7 @@ namespace Hyde\Publications\Models; use Hyde\Facades\Filesystem; +use Illuminate\Contracts\Validation\Validator; use function array_filter; use function array_merge; use function dirname; @@ -23,6 +24,7 @@ use function json_encode; use RuntimeException; use function str_starts_with; +use function validator; /** * @see \Hyde\Publications\Testing\Feature\PublicationTypeTest @@ -238,22 +240,20 @@ public function validateSchemaFile(): void $shouldBeArrays = ['fields']; foreach ($schema as $key => $value) { - // TODO throw (custom?) validation exception instead? + if (in_array($key, $shouldBeStrings)) { + validator([$key => $value], [$key => 'string'])->validate(); + } - if (in_array($key, $shouldBeStrings) && ! is_string($value)) { - throw new RuntimeException("The '$key' property must be a string."); + if (in_array($key, $shouldBeBooleans)) { + validator([$key => $value], [$key => 'boolean'])->validate(); } - if (in_array($key, $shouldBeBooleans) && ! is_bool($value)) { - throw new RuntimeException("The '$key' property must be a boolean."); + if (in_array($key, $shouldBeIntegers)) { + validator([$key => $value], [$key => 'integer'])->validate(); } - if (in_array($key, $shouldBeIntegers) && ! is_int($value)) { - throw new RuntimeException("The '$key' property must be an integer."); - } - - if (in_array($key, $shouldBeArrays) && ! is_array($value)) { - throw new RuntimeException("The '$key' property must be an array."); + if (in_array($key, $shouldBeArrays)) { + validator([$key => $value], [$key => 'array'])->validate(); } } From ba05c6fed3b29b2e8b1c23f9ed22e8d486f50575 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:55:56 +0100 Subject: [PATCH 05/49] Test with fields --- packages/publications/tests/Feature/PublicationTypeTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index ba339966d3f..f28d34d047d 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -431,7 +431,9 @@ public function testJsonRepresentationWithDefaultValues() public function testValidateSchemaFile() { $this->directory('test-publication'); - $publicationType = new PublicationType('test-publication'); + $publicationType = new PublicationType('test-publication', fields: [ + ['name' => 'myField', 'type' => 'string'], + ]); $publicationType->save(); $publicationType->validateSchemaFile(); From 54dd0387fed8e0885ac08053af32d6646e411fb6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 20:56:56 +0100 Subject: [PATCH 06/49] Test with invalid fields --- .../tests/Feature/PublicationTypeTest.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index f28d34d047d..278d6fac386 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -465,6 +465,39 @@ public function testValidateSchemaFileWithInvalidSchema() $publicationType->validateSchemaFile(); } + public function testValidateSchemaFileWithInvalidFields() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": "test-publication", + "canonicalField": "__createdAt", + "detailTemplate": "detail.blade.php", + "listTemplate": "list.blade.php", + "sortField": "__createdAt", + "sortAscending": true, + "pageSize": 0, + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ] + } + JSON + ); + + $this->expectException(RuntimeException::class); + $publicationType->validateSchemaFile(); + } + protected function getTestData(array $mergeData = []): array { return array_merge([ From 8b755eabcda99371612f3ec25e3558215d2d05d7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:00:43 +0100 Subject: [PATCH 07/49] Refactor to use the Illuminate validator --- .../src/Models/PublicationType.php | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 7351e5a5769..65542dd8560 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -266,30 +266,19 @@ public function validateSchemaFile(): void // TODO warn if pageSize is less than 0 (as that equals no pagination)? foreach ($schema->fields as $field) { - if (! isset($field->type)) { - throw new RuntimeException("The 'type' property is required for each field."); - } - - if (! isset($field->name)) { - throw new RuntimeException("The 'name' property is required for each field."); - } - - if (! is_string($field->type)) { - throw new RuntimeException("The 'type' property must be a string."); - } - - if (! is_string($field->name)) { - throw new RuntimeException("The 'name' property must be a string."); - } - - if (isset($field->rules) && ! is_array($field->rules)) { - throw new RuntimeException("The 'rules' property must be an array."); - } - - if (isset($field->tagGroup) && ! is_string($field->tagGroup)) { - throw new RuntimeException("The 'tagGroup' property must be a string."); - // TODO check tag group exists? - } + validator([ + 'type' => $field->type ?? null, + 'name' => $field->name ?? null, + 'rules' => $field->type ?? null, + 'tagGroup' => $field->name ?? null, + ], [ + 'type' => 'required|string', + 'name' => 'required|string', + 'rules' => 'nullable|array', + 'tagGroup' => 'nullable|string', + ])->validate(); + + // TODO check tag group exists? } } } From 8c4e69fca6755af32d1406e0f7da881e619d5c33 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:03:33 +0100 Subject: [PATCH 08/49] Merge Illuminate validator calls --- .../src/Models/PublicationType.php | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 65542dd8560..e024964d24d 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -234,28 +234,25 @@ public function validateSchemaFile(): void throw new RuntimeException("The 'directory' property is not allowed in the schema file as that may cause unexpected issues."); } - $shouldBeStrings = ['name', 'canonicalField', 'detailTemplate', 'listTemplate', 'sortField']; - $shouldBeBooleans = ['sortAscending']; - $shouldBeIntegers = ['pageSize']; - $shouldBeArrays = ['fields']; - - foreach ($schema as $key => $value) { - if (in_array($key, $shouldBeStrings)) { - validator([$key => $value], [$key => 'string'])->validate(); - } - - if (in_array($key, $shouldBeBooleans)) { - validator([$key => $value], [$key => 'boolean'])->validate(); - } - - if (in_array($key, $shouldBeIntegers)) { - validator([$key => $value], [$key => 'integer'])->validate(); - } - - if (in_array($key, $shouldBeArrays)) { - validator([$key => $value], [$key => 'array'])->validate(); - } - } + validator([ + 'name' => $schema->name, + 'canonicalField' => $schema->canonicalField, + 'detailTemplate' => $schema->detailTemplate, + 'listTemplate' => $schema->listTemplate, + 'sortField' => $schema->sortField, + 'sortAscending' => $schema->sortAscending, + 'pageSize' => $schema->pageSize, + 'fields' => $schema->fields, + ], [ + 'name' => 'required|string', + 'canonicalField' => 'nullable|string', + 'detailTemplate' => 'nullable|string', + 'listTemplate' => 'nullable|string', + 'sortField' => 'nullable|string', + 'sortAscending' => 'nullable|boolean', + 'pageSize' => 'nullable|integer', + 'fields' => 'nullable|array', + ])->validate(); // TODO warn if fields are empty? From 03a2a42bc9578953aff0438d70acf99620e93687 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:05:42 +0100 Subject: [PATCH 09/49] Coalesce to null for optional fields --- .../publications/src/Models/PublicationType.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index e024964d24d..417d1dd137a 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -235,14 +235,14 @@ public function validateSchemaFile(): void } validator([ - 'name' => $schema->name, - 'canonicalField' => $schema->canonicalField, - 'detailTemplate' => $schema->detailTemplate, - 'listTemplate' => $schema->listTemplate, - 'sortField' => $schema->sortField, - 'sortAscending' => $schema->sortAscending, - 'pageSize' => $schema->pageSize, - 'fields' => $schema->fields, + 'name' => $schema->name ?? null, + 'canonicalField' => $schema->canonicalField ?? null, + 'detailTemplate' => $schema->detailTemplate ?? null, + 'listTemplate' => $schema->listTemplate ?? null, + 'sortField' => $schema->sortField ?? null, + 'sortAscending' => $schema->sortAscending ?? null, + 'pageSize' => $schema->pageSize ?? null, + 'fields' => $schema->fields ?? null, ], [ 'name' => 'required|string', 'canonicalField' => 'nullable|string', From 7b6a69df80f74ec1eea78140cea7794b1cbf5dc6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:07:10 +0100 Subject: [PATCH 10/49] Use prohibited rule to disallow directory schema property --- packages/publications/src/Models/PublicationType.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 417d1dd137a..21ae8b2824f 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -230,10 +230,6 @@ public function validateSchemaFile(): void { $schema = json_decode(Filesystem::getContents($this->getSchemaFile())); - if (isset($schema->directory)) { - throw new RuntimeException("The 'directory' property is not allowed in the schema file as that may cause unexpected issues."); - } - validator([ 'name' => $schema->name ?? null, 'canonicalField' => $schema->canonicalField ?? null, @@ -243,6 +239,7 @@ public function validateSchemaFile(): void 'sortAscending' => $schema->sortAscending ?? null, 'pageSize' => $schema->pageSize ?? null, 'fields' => $schema->fields ?? null, + 'directory' => $schema->directory ?? null, ], [ 'name' => 'required|string', 'canonicalField' => 'nullable|string', @@ -252,6 +249,7 @@ public function validateSchemaFile(): void 'sortAscending' => 'nullable|boolean', 'pageSize' => 'nullable|integer', 'fields' => 'nullable|array', + 'directory' => 'prohibited', ])->validate(); // TODO warn if fields are empty? From d435d7240d810ffb39f6f1c52604b9c08e276de6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:09:04 +0100 Subject: [PATCH 11/49] Make directory rule nullable --- packages/publications/src/Models/PublicationType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 21ae8b2824f..280b1cceb15 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -249,7 +249,7 @@ public function validateSchemaFile(): void 'sortAscending' => 'nullable|boolean', 'pageSize' => 'nullable|integer', 'fields' => 'nullable|array', - 'directory' => 'prohibited', + 'directory' => 'nullable|prohibited', ])->validate(); // TODO warn if fields are empty? From 94c1d6d5f9b15595c60e235f62595fe894afa01f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:10:06 +0100 Subject: [PATCH 12/49] Specify allowed array keys --- packages/publications/src/Models/PublicationType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 280b1cceb15..4bd9fca34b5 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -248,7 +248,7 @@ public function validateSchemaFile(): void 'sortField' => 'nullable|string', 'sortAscending' => 'nullable|boolean', 'pageSize' => 'nullable|integer', - 'fields' => 'nullable|array', + 'fields' => 'nullable|array:type,name,rules,tagGroup', 'directory' => 'nullable|prohibited', ])->validate(); From baed80c1a97ffe7d592227a4e1e37d18e93ec1a6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:11:05 +0100 Subject: [PATCH 13/49] Revert "Specify allowed array keys" This reverts commit 94c1d6d5f9b15595c60e235f62595fe894afa01f. --- packages/publications/src/Models/PublicationType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 4bd9fca34b5..280b1cceb15 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -248,7 +248,7 @@ public function validateSchemaFile(): void 'sortField' => 'nullable|string', 'sortAscending' => 'nullable|boolean', 'pageSize' => 'nullable|integer', - 'fields' => 'nullable|array:type,name,rules,tagGroup', + 'fields' => 'nullable|array', 'directory' => 'nullable|prohibited', ])->validate(); From 53892351116c6840a53b378ded9f38d465106a33 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:13:01 +0100 Subject: [PATCH 14/49] Fix mismatched property names --- packages/publications/src/Models/PublicationType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 280b1cceb15..ebb926fcbcf 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -264,8 +264,8 @@ public function validateSchemaFile(): void validator([ 'type' => $field->type ?? null, 'name' => $field->name ?? null, - 'rules' => $field->type ?? null, - 'tagGroup' => $field->name ?? null, + 'rules' => $field->rules ?? null, + 'tagGroup' => $field->tagGroup ?? null, ], [ 'type' => 'required|string', 'name' => 'required|string', From 743b13e0277f4acbb392144085f6b5a8cf3a48df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:22:58 +0100 Subject: [PATCH 15/49] Test with invalid directory --- packages/publications/tests/Feature/PublicationTypeTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 278d6fac386..3008eac6c9f 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -456,7 +456,8 @@ public function testValidateSchemaFileWithInvalidSchema() "sortField": 123, "sortAscending": 123, "pageSize": "123", - "fields": 123 + "fields": 123, + "directory": "foo" } JSON ); From 52ef4389ead45ac0f84eb2e583984fbc6e14e2dd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:23:41 +0100 Subject: [PATCH 16/49] Expect ValidationExceptions --- packages/publications/tests/Feature/PublicationTypeTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 3008eac6c9f..03f41822d4c 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -4,6 +4,7 @@ namespace Hyde\Publications\Testing\Feature; +use Illuminate\Validation\ValidationException; use function array_merge; use function array_reverse; use Hyde\Framework\Features\Paginator; @@ -462,7 +463,7 @@ public function testValidateSchemaFileWithInvalidSchema() JSON ); - $this->expectException(RuntimeException::class); + $this->expectException(ValidationException::class); $publicationType->validateSchemaFile(); } @@ -495,7 +496,7 @@ public function testValidateSchemaFileWithInvalidFields() JSON ); - $this->expectException(RuntimeException::class); + $this->expectException(ValidationException::class); $publicationType->validateSchemaFile(); } From caf36bb55f23371cce96c48f53418db706ec2811 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Jan 2023 22:29:02 +0100 Subject: [PATCH 17/49] Add option to buffer and return validation failures --- .../src/Models/PublicationType.php | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index ebb926fcbcf..f4f1d1a93c4 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -226,11 +226,12 @@ protected function withoutNullValues(array $array): array * * @internal This method is experimental and may be removed without notice */ - public function validateSchemaFile(): void + public function validateSchemaFile(bool $throw = true): array { $schema = json_decode(Filesystem::getContents($this->getSchemaFile())); + $errors = []; - validator([ + $schemaValidator = validator([ 'name' => $schema->name ?? null, 'canonicalField' => $schema->canonicalField ?? null, 'detailTemplate' => $schema->detailTemplate ?? null, @@ -250,7 +251,13 @@ public function validateSchemaFile(): void 'pageSize' => 'nullable|integer', 'fields' => 'nullable|array', 'directory' => 'nullable|prohibited', - ])->validate(); + ]); + + $errors['schema'] = $schemaValidator->errors()->toArray(); + + if ($throw) { + $schemaValidator->validate(); + } // TODO warn if fields are empty? @@ -260,8 +267,10 @@ public function validateSchemaFile(): void // TODO warn if pageSize is less than 0 (as that equals no pagination)? + $errors['fields'] = []; + foreach ($schema->fields as $field) { - validator([ + $fieldValidator = validator([ 'type' => $field->type ?? null, 'name' => $field->name ?? null, 'rules' => $field->rules ?? null, @@ -271,9 +280,17 @@ public function validateSchemaFile(): void 'name' => 'required|string', 'rules' => 'nullable|array', 'tagGroup' => 'nullable|string', - ])->validate(); + ]); // TODO check tag group exists? + + $errors['fields'][] = $fieldValidator->errors()->toArray(); + + if ($throw) { + $fieldValidator->validate(); + } } + + return $errors; } } From 43d112404484c4cd0ca1a105577bf70a8f479bdc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 10:27:57 +0100 Subject: [PATCH 18/49] Test complete buffered output --- .../tests/Feature/PublicationTypeTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 03f41822d4c..46bf77fb3f9 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -500,6 +500,58 @@ public function testValidateSchemaFileWithInvalidFields() $publicationType->validateSchemaFile(); } + public function testValidateSchemaFileWithInvalidDataBuffered() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ], + "directory": "foo" + } + JSON + ); + + $errors = $publicationType->validateSchemaFile(false); + + $this->assertSame([ + 'schema' => [ + 'name' => ['The name must be a string.'], + 'canonicalField' => ['The canonical field must be a string.'], + 'detailTemplate' => ['The detail template must be a string.'], + 'listTemplate' => ['The list template must be a string.'], + 'sortField' => ['The sort field must be a string.'], + 'sortAscending' => ['The sort ascending field must be true or false.'], + 'directory' => ['The directory field is prohibited.'], + ], + 'fields' => [[ + 'type' => ['The type must be a string.'], + 'name' => ['The name must be a string.'] + ], [ + 'type' => ['The type field is required.'], + 'name' => ['The name field is required.'], + ]], + ], $errors); + } + protected function getTestData(array $mergeData = []): array { return array_merge([ From 2956011d36698ec71beb20aaac400e7555db40eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 10:50:02 +0100 Subject: [PATCH 19/49] Move publication schema validation logic to service --- .../src/Models/PublicationType.php | 68 +--------- .../publications/src/PublicationService.php | 76 +++++++++++ .../tests/Feature/PublicationServiceTest.php | 124 ++++++++++++++++++ .../tests/Feature/PublicationTypeTest.php | 110 ---------------- 4 files changed, 201 insertions(+), 177 deletions(-) diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index f4f1d1a93c4..532dbdeeafe 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -4,8 +4,6 @@ namespace Hyde\Publications\Models; -use Hyde\Facades\Filesystem; -use Illuminate\Contracts\Validation\Validator; use function array_filter; use function array_merge; use function dirname; @@ -24,7 +22,6 @@ use function json_encode; use RuntimeException; use function str_starts_with; -use function validator; /** * @see \Hyde\Publications\Testing\Feature\PublicationTypeTest @@ -228,69 +225,6 @@ protected function withoutNullValues(array $array): array */ public function validateSchemaFile(bool $throw = true): array { - $schema = json_decode(Filesystem::getContents($this->getSchemaFile())); - $errors = []; - - $schemaValidator = validator([ - 'name' => $schema->name ?? null, - 'canonicalField' => $schema->canonicalField ?? null, - 'detailTemplate' => $schema->detailTemplate ?? null, - 'listTemplate' => $schema->listTemplate ?? null, - 'sortField' => $schema->sortField ?? null, - 'sortAscending' => $schema->sortAscending ?? null, - 'pageSize' => $schema->pageSize ?? null, - 'fields' => $schema->fields ?? null, - 'directory' => $schema->directory ?? null, - ], [ - 'name' => 'required|string', - 'canonicalField' => 'nullable|string', - 'detailTemplate' => 'nullable|string', - 'listTemplate' => 'nullable|string', - 'sortField' => 'nullable|string', - 'sortAscending' => 'nullable|boolean', - 'pageSize' => 'nullable|integer', - 'fields' => 'nullable|array', - 'directory' => 'nullable|prohibited', - ]); - - $errors['schema'] = $schemaValidator->errors()->toArray(); - - if ($throw) { - $schemaValidator->validate(); - } - - // TODO warn if fields are empty? - - // TODO warn if canonicalField does not match meta field or actual? - - // TODO Warn if template files do not exist (assuming files not vendor views)? - - // TODO warn if pageSize is less than 0 (as that equals no pagination)? - - $errors['fields'] = []; - - foreach ($schema->fields as $field) { - $fieldValidator = validator([ - 'type' => $field->type ?? null, - 'name' => $field->name ?? null, - 'rules' => $field->rules ?? null, - 'tagGroup' => $field->tagGroup ?? null, - ], [ - 'type' => 'required|string', - 'name' => 'required|string', - 'rules' => 'nullable|array', - 'tagGroup' => 'nullable|string', - ]); - - // TODO check tag group exists? - - $errors['fields'][] = $fieldValidator->errors()->toArray(); - - if ($throw) { - $fieldValidator->validate(); - } - } - - return $errors; + return PublicationService::validateSchemaFile($this->getSchemaFile(), $throw); } } diff --git a/packages/publications/src/PublicationService.php b/packages/publications/src/PublicationService.php index baf8741dc9a..65d748ce297 100644 --- a/packages/publications/src/PublicationService.php +++ b/packages/publications/src/PublicationService.php @@ -4,6 +4,7 @@ namespace Hyde\Publications; +use Hyde\Facades\Filesystem; use function glob; use Hyde\Hyde; use Hyde\Publications\Models\PublicationPage; @@ -11,6 +12,8 @@ use Hyde\Publications\Models\PublicationType; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use function json_decode; +use function validator; /** * @see \Hyde\Publications\Testing\Feature\PublicationServiceTest @@ -89,6 +92,79 @@ public static function publicationTypeExists(string $pubTypeName): bool return static::getPublicationTypes()->has(Str::slug($pubTypeName)); } + /** + * Validate the schema.json file is valid. + * + * @internal This method is experimental and may be removed without notice + */ + public static function validateSchemaFile(string $schemaFile, bool $throw = true): array + { + $schema = json_decode(Filesystem::getContents($schemaFile)); + $errors = []; + + $schemaValidator = validator([ + 'name' => $schema->name ?? null, + 'canonicalField' => $schema->canonicalField ?? null, + 'detailTemplate' => $schema->detailTemplate ?? null, + 'listTemplate' => $schema->listTemplate ?? null, + 'sortField' => $schema->sortField ?? null, + 'sortAscending' => $schema->sortAscending ?? null, + 'pageSize' => $schema->pageSize ?? null, + 'fields' => $schema->fields ?? null, + 'directory' => $schema->directory ?? null, + ], [ + 'name' => 'required|string', + 'canonicalField' => 'nullable|string', + 'detailTemplate' => 'nullable|string', + 'listTemplate' => 'nullable|string', + 'sortField' => 'nullable|string', + 'sortAscending' => 'nullable|boolean', + 'pageSize' => 'nullable|integer', + 'fields' => 'nullable|array', + 'directory' => 'nullable|prohibited', + ]); + + $errors['schema'] = $schemaValidator->errors()->toArray(); + + if ($throw) { + $schemaValidator->validate(); + } + + // TODO warn if fields are empty? + + // TODO warn if canonicalField does not match meta field or actual? + + // TODO Warn if template files do not exist (assuming files not vendor views)? + + // TODO warn if pageSize is less than 0 (as that equals no pagination)? + + $errors['fields'] = []; + + foreach ($schema->fields as $field) { + $fieldValidator = validator([ + 'type' => $field->type ?? null, + 'name' => $field->name ?? null, + 'rules' => $field->rules ?? null, + 'tagGroup' => $field->tagGroup ?? null, + ], [ + 'type' => 'required|string', + 'name' => 'required|string', + 'rules' => 'nullable|array', + 'tagGroup' => 'nullable|string', + ]); + + // TODO check tag group exists? + + $errors['fields'][] = $fieldValidator->errors()->toArray(); + + if ($throw) { + $fieldValidator->validate(); + } + } + + return $errors; + } + protected static function getSchemaFiles(): array { return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); diff --git a/packages/publications/tests/Feature/PublicationServiceTest.php b/packages/publications/tests/Feature/PublicationServiceTest.php index 5eec530f549..1295c31b6ab 100644 --- a/packages/publications/tests/Feature/PublicationServiceTest.php +++ b/packages/publications/tests/Feature/PublicationServiceTest.php @@ -4,6 +4,7 @@ namespace Hyde\Publications\Testing\Feature; +use Illuminate\Validation\ValidationException; use function file_put_contents; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Hyde; @@ -214,6 +215,129 @@ public function testGetValuesForTagName() $this->assertSame(['bar', 'baz'], PublicationService::getValuesForTagName('foo')->toArray()); } + public function testValidateSchemaFile() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication', fields: [ + ['name' => 'myField', 'type' => 'string'], + ]); + $publicationType->save(); + + $publicationType->validateSchemaFile(); + + $this->assertTrue(true); + } + + public function testValidateSchemaFileWithInvalidSchema() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": 123, + "directory": "foo" + } + JSON + ); + + $this->expectException(ValidationException::class); + $publicationType->validateSchemaFile(); + } + + public function testValidateSchemaFileWithInvalidFields() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": "test-publication", + "canonicalField": "__createdAt", + "detailTemplate": "detail.blade.php", + "listTemplate": "list.blade.php", + "sortField": "__createdAt", + "sortAscending": true, + "pageSize": 0, + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ] + } + JSON + ); + + $this->expectException(ValidationException::class); + $publicationType->validateSchemaFile(); + } + + public function testValidateSchemaFileWithInvalidDataBuffered() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ], + "directory": "foo" + } + JSON + ); + + $errors = $publicationType->validateSchemaFile(false); + + $this->assertSame([ + 'schema' => [ + 'name' => ['The name must be a string.'], + 'canonicalField' => ['The canonical field must be a string.'], + 'detailTemplate' => ['The detail template must be a string.'], + 'listTemplate' => ['The list template must be a string.'], + 'sortField' => ['The sort field must be a string.'], + 'sortAscending' => ['The sort ascending field must be true or false.'], + 'directory' => ['The directory field is prohibited.'], + ], + 'fields' => [[ + 'type' => ['The type must be a string.'], + 'name' => ['The name must be a string.'] + ], [ + 'type' => ['The type field is required.'], + 'name' => ['The name field is required.'], + ]], + ], $errors); + } + protected function createPublicationType(): void { (new PublicationType('test-publication'))->save(); diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 46bf77fb3f9..71e7e549223 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -442,116 +442,6 @@ public function testValidateSchemaFile() $this->assertTrue(true); } - public function testValidateSchemaFileWithInvalidSchema() - { - $this->directory('test-publication'); - $publicationType = new PublicationType('test-publication'); - $publicationType->save(); - - $this->file('test-publication/schema.json', <<<'JSON' - { - "name": 123, - "canonicalField": 123, - "detailTemplate": 123, - "listTemplate": 123, - "sortField": 123, - "sortAscending": 123, - "pageSize": "123", - "fields": 123, - "directory": "foo" - } - JSON - ); - - $this->expectException(ValidationException::class); - $publicationType->validateSchemaFile(); - } - - public function testValidateSchemaFileWithInvalidFields() - { - $this->directory('test-publication'); - $publicationType = new PublicationType('test-publication'); - $publicationType->save(); - - $this->file('test-publication/schema.json', <<<'JSON' - { - "name": "test-publication", - "canonicalField": "__createdAt", - "detailTemplate": "detail.blade.php", - "listTemplate": "list.blade.php", - "sortField": "__createdAt", - "sortAscending": true, - "pageSize": 0, - "fields": [ - { - "name": 123, - "type": 123 - }, - { - "noName": "myField", - "noType": "string" - } - ] - } - JSON - ); - - $this->expectException(ValidationException::class); - $publicationType->validateSchemaFile(); - } - - public function testValidateSchemaFileWithInvalidDataBuffered() - { - $this->directory('test-publication'); - $publicationType = new PublicationType('test-publication'); - $publicationType->save(); - - $this->file('test-publication/schema.json', <<<'JSON' - { - "name": 123, - "canonicalField": 123, - "detailTemplate": 123, - "listTemplate": 123, - "sortField": 123, - "sortAscending": 123, - "pageSize": "123", - "fields": [ - { - "name": 123, - "type": 123 - }, - { - "noName": "myField", - "noType": "string" - } - ], - "directory": "foo" - } - JSON - ); - - $errors = $publicationType->validateSchemaFile(false); - - $this->assertSame([ - 'schema' => [ - 'name' => ['The name must be a string.'], - 'canonicalField' => ['The canonical field must be a string.'], - 'detailTemplate' => ['The detail template must be a string.'], - 'listTemplate' => ['The list template must be a string.'], - 'sortField' => ['The sort field must be a string.'], - 'sortAscending' => ['The sort ascending field must be true or false.'], - 'directory' => ['The directory field is prohibited.'], - ], - 'fields' => [[ - 'type' => ['The type must be a string.'], - 'name' => ['The name must be a string.'] - ], [ - 'type' => ['The type field is required.'], - 'name' => ['The name field is required.'], - ]], - ], $errors); - } - protected function getTestData(array $mergeData = []): array { return array_merge([ From d7f2a53a0d51c801d57ef95f0baf9c741fe7097c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 10:57:13 +0100 Subject: [PATCH 20/49] Add option to only validate the schemas --- .../Commands/ValidatePublicationsCommand.php | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationsCommand.php b/packages/publications/src/Commands/ValidatePublicationsCommand.php index 08c2d909d14..be1324e3323 100644 --- a/packages/publications/src/Commands/ValidatePublicationsCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationsCommand.php @@ -4,6 +4,8 @@ namespace Hyde\Publications\Commands; +use Hyde\Hyde; +use function basename; use function collect; use Exception; use function filled; @@ -15,6 +17,7 @@ use Illuminate\Support\Collection; use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; +use function glob; use function microtime; use function str_repeat; use function strlen; @@ -35,7 +38,8 @@ class ValidatePublicationsCommand extends ValidatingCommand /** @var string */ protected $signature = 'validate:publications {publicationType? : The name of the publication type to validate.} - {--json : Display results as JSON.}'; + {--json : Display results as JSON.} + {--schemas : Only validate the publication schema files.}'; /** @var string */ protected $description = 'Validate all or the specified publication type(s)'; @@ -61,6 +65,17 @@ public function safeHandle(): int $this->title('Validating publications!'); } + if ($this->option('schemas')) { + $this->validateSchemaFiles(); + + $this->newLine(); + $this->info(sprintf('All done in %sms using %sMB peak memory!', + round((microtime(true) - $timeStart) * 1000), + round(memory_get_peak_usage() / 1024 / 1024) + )); + return Command::SUCCESS; + } + $publicationTypesToValidate = $this->getPublicationTypesToValidate(); foreach ($publicationTypesToValidate as $publicationType) { @@ -249,4 +264,30 @@ protected function outputJson(): void { $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); } + + protected function validateSchemaFiles(): void + { + /** @see PublicationService::getSchemaFiles() */ + $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + + foreach ($schemaFiles as $number => $schemaFile) { + $name = basename(dirname($schemaFile)); + $this->infoComment('Validating schema file for', $name); + + $errors = PublicationService::validateSchemaFile($schemaFile, false); + + if (empty($errors['schema'])) { + $this->line(' No top-level schema errors found'); + } else { + $this->line(sprintf(" Found %s errors:", count($errors['schema']))); + foreach ($errors['schema'] as $error) { + $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + } + } + + if ($number !== count($schemaFiles) - 1) { + $this->newLine(); + } + } + } } From c8d16248b2fe4b308476f604314061b196397a37 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:04:12 +0100 Subject: [PATCH 21/49] Display field errors --- .../src/Commands/ValidatePublicationsCommand.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationsCommand.php b/packages/publications/src/Commands/ValidatePublicationsCommand.php index be1324e3323..5238040711d 100644 --- a/packages/publications/src/Commands/ValidatePublicationsCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationsCommand.php @@ -5,6 +5,7 @@ namespace Hyde\Publications\Commands; use Hyde\Hyde; +use function array_filter; use function basename; use function collect; use Exception; @@ -279,12 +280,25 @@ protected function validateSchemaFiles(): void if (empty($errors['schema'])) { $this->line(' No top-level schema errors found'); } else { - $this->line(sprintf(" Found %s errors:", count($errors['schema']))); + $this->line(sprintf(" Found %s top-level schema errors:", count($errors['schema']))); foreach ($errors['schema'] as $error) { $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); } } + if (empty(array_filter($errors['fields']))) { + $this->line(' No field-level schema errors found'); + } else { + $this->newLine(); + $this->line(sprintf(" Found errors in %s field definitions:", count($errors['fields']))); + foreach ($errors['fields'] as $fieldNumber => $fieldErrors) { + $this->line(sprintf(" Field #%s:", $fieldNumber+1)); + foreach ($fieldErrors as $error) { + $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + } + } + } + if ($number !== count($schemaFiles) - 1) { $this->newLine(); } From 4be5a479970ceecb4296890d34a90c0ca5e4166e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:09:56 +0100 Subject: [PATCH 22/49] Split out publication type validation to new command class --- .../ValidatePublicationTypesCommand.php | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 packages/publications/src/Commands/ValidatePublicationTypesCommand.php diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php new file mode 100644 index 00000000000..3338ae8fccb --- /dev/null +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -0,0 +1,136 @@ +json = $this->option('json'); + + if (! $this->json) { + $this->title('Validating publications!'); + } + + $this->validateSchemaFiles(); + + if ($this->json) { + $this->outputJson(); + } else { + $this->displayResults(); + + $this->outputSummary($timeStart); + } + + if (count(array_filter($this->results))) { + return Command::FAILURE; + } + + return Command::SUCCESS; + } + + protected function displayResults(): void + { + // TODO: Split out display logic + } + + protected function outputSummary($timeStart): void + { + $this->newLine(); + $this->info(sprintf('All done in %sms using %sMB peak memory!', + round((microtime(true) - $timeStart) * 1000), + round(memory_get_peak_usage() / 1024 / 1024) + )); + } + + protected function outputJson(): void + { + $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); + } + + protected function validateSchemaFiles(): void + { + /** @see PublicationService::getSchemaFiles() */ + $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + + foreach ($schemaFiles as $number => $schemaFile) { + $name = basename(dirname($schemaFile)); + $this->infoComment('Validating schema file for', $name); + + $errors = PublicationService::validateSchemaFile($schemaFile, false); + + if (empty($errors['schema'])) { + $this->line(' No top-level schema errors found'); + } else { + $this->line(sprintf(" Found %s top-level schema errors:", count($errors['schema']))); + foreach ($errors['schema'] as $error) { + $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + } + } + + if (empty(array_filter($errors['fields']))) { + $this->line(' No field-level schema errors found'); + } else { + $this->newLine(); + $this->line(sprintf(" Found errors in %s field definitions:", count($errors['fields']))); + foreach ($errors['fields'] as $fieldNumber => $fieldErrors) { + $this->line(sprintf(" Field #%s:", $fieldNumber+1)); + foreach ($fieldErrors as $error) { + $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + } + } + } + + if ($number !== count($schemaFiles) - 1) { + $this->newLine(); + } + } + } +} From 3dda1acbb37acfeb991b2239ad06261084a9c453 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:11:04 +0100 Subject: [PATCH 23/49] Introduce local variables --- .../Commands/ValidatePublicationTypesCommand.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 3338ae8fccb..286def96e81 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -78,7 +78,7 @@ public function safeHandle(): int protected function displayResults(): void { - // TODO: Split out display logic + // TODO: Split out display logic } protected function outputSummary($timeStart): void @@ -106,21 +106,23 @@ protected function validateSchemaFiles(): void $errors = PublicationService::validateSchemaFile($schemaFile, false); - if (empty($errors['schema'])) { + $schemaErrors = $errors['schema']; + if (empty($schemaErrors)) { $this->line(' No top-level schema errors found'); } else { - $this->line(sprintf(" Found %s top-level schema errors:", count($errors['schema']))); - foreach ($errors['schema'] as $error) { + $this->line(sprintf(" Found %s top-level schema errors:", count($schemaErrors))); + foreach ($schemaErrors as $error) { $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); } } - if (empty(array_filter($errors['fields']))) { + $schemaFields = $errors['fields']; + if (empty(array_filter($schemaFields))) { $this->line(' No field-level schema errors found'); } else { $this->newLine(); - $this->line(sprintf(" Found errors in %s field definitions:", count($errors['fields']))); - foreach ($errors['fields'] as $fieldNumber => $fieldErrors) { + $this->line(sprintf(" Found errors in %s field definitions:", count($schemaFields))); + foreach ($schemaFields as $fieldNumber => $fieldErrors) { $this->line(sprintf(" Field #%s:", $fieldNumber+1)); foreach ($fieldErrors as $error) { $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); From 210e347abeee0b1a46171957d441c22ef091d23e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:12:25 +0100 Subject: [PATCH 24/49] Update title --- .../src/Commands/ValidatePublicationTypesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 286def96e81..c2e9506ce3e 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -56,7 +56,7 @@ public function safeHandle(): int $this->json = $this->option('json'); if (! $this->json) { - $this->title('Validating publications!'); + $this->title('Validating publication schemas!'); } $this->validateSchemaFiles(); From d9a6366e353cf08435977e18d1ca18038274a22c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:16:12 +0100 Subject: [PATCH 25/49] Split out display logic --- .../ValidatePublicationTypesCommand.php | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index c2e9506ce3e..3ed62122ccc 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -78,41 +78,16 @@ public function safeHandle(): int protected function displayResults(): void { - // TODO: Split out display logic - } - - protected function outputSummary($timeStart): void - { - $this->newLine(); - $this->info(sprintf('All done in %sms using %sMB peak memory!', - round((microtime(true) - $timeStart) * 1000), - round(memory_get_peak_usage() / 1024 / 1024) - )); - } - - protected function outputJson(): void - { - $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); - } - - protected function validateSchemaFiles(): void - { - /** @see PublicationService::getSchemaFiles() */ - $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); - - foreach ($schemaFiles as $number => $schemaFile) { - $name = basename(dirname($schemaFile)); + foreach ($this->results as $name => $errors) { $this->infoComment('Validating schema file for', $name); - $errors = PublicationService::validateSchemaFile($schemaFile, false); - $schemaErrors = $errors['schema']; if (empty($schemaErrors)) { $this->line(' No top-level schema errors found'); } else { - $this->line(sprintf(" Found %s top-level schema errors:", count($schemaErrors))); + $this->line(sprintf(' Found %s top-level schema errors:', count($schemaErrors))); foreach ($schemaErrors as $error) { - $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); } } @@ -121,18 +96,44 @@ protected function validateSchemaFiles(): void $this->line(' No field-level schema errors found'); } else { $this->newLine(); - $this->line(sprintf(" Found errors in %s field definitions:", count($schemaFields))); + $this->line(sprintf(' Found errors in %s field definitions:', count($schemaFields))); foreach ($schemaFields as $fieldNumber => $fieldErrors) { - $this->line(sprintf(" Field #%s:", $fieldNumber+1)); + $this->line(sprintf(' Field #%s:', $fieldNumber + 1)); foreach ($fieldErrors as $error) { - $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + $this->line(sprintf(' %s %s', self::CROSS_MARK, + implode(' ', $error))); } } } - if ($number !== count($schemaFiles) - 1) { + if (next($this->results)) { $this->newLine(); } } } + + protected function outputSummary($timeStart): void + { + $this->newLine(); + $this->info(sprintf('All done in %sms using %sMB peak memory!', + round((microtime(true) - $timeStart) * 1000), + round(memory_get_peak_usage() / 1024 / 1024) + )); + } + + protected function outputJson(): void + { + $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); + } + + protected function validateSchemaFiles(): void + { + /** @see PublicationService::getSchemaFiles() */ + $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + + foreach ($schemaFiles as $schemaFile) { + $name = basename(dirname($schemaFile)); + $this->results[$name] = PublicationService::validateSchemaFile($schemaFile, false); + } + } } From 85dece498558b6a691c35b8f555140751cde8381 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:16:30 +0100 Subject: [PATCH 26/49] Reorder helper methods --- .../ValidatePublicationTypesCommand.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 3ed62122ccc..a27a87be33e 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -76,6 +76,17 @@ public function safeHandle(): int return Command::SUCCESS; } + protected function validateSchemaFiles(): void + { + /** @see PublicationService::getSchemaFiles() */ + $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + + foreach ($schemaFiles as $schemaFile) { + $name = basename(dirname($schemaFile)); + $this->results[$name] = PublicationService::validateSchemaFile($schemaFile, false); + } + } + protected function displayResults(): void { foreach ($this->results as $name => $errors) { @@ -125,15 +136,4 @@ protected function outputJson(): void { $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); } - - protected function validateSchemaFiles(): void - { - /** @see PublicationService::getSchemaFiles() */ - $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); - - foreach ($schemaFiles as $schemaFile) { - $name = basename(dirname($schemaFile)); - $this->results[$name] = PublicationService::validateSchemaFile($schemaFile, false); - } - } } From 4c4d284d1e1497423b991f11b1bc2c76b603be13 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:16:49 +0100 Subject: [PATCH 27/49] Import used functions --- .../src/Commands/ValidatePublicationTypesCommand.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index a27a87be33e..c14a8444ec3 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -17,10 +17,14 @@ use function array_filter; use function basename; use function collect; +use function dirname; use function filled; use function glob; +use function implode; +use function json_encode; use function memory_get_peak_usage; use function microtime; +use function next; use function round; use function sprintf; use function str_repeat; From a84e601c09c67ed2d3303b564e9151397bb55b7b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:17:03 +0100 Subject: [PATCH 28/49] Remove unused imports --- .../src/Commands/ValidatePublicationTypesCommand.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index c14a8444ec3..4a2df3b4051 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -4,21 +4,12 @@ namespace Hyde\Publications\Commands; -use Exception; use Hyde\Hyde; -use Hyde\Publications\Actions\ValidatesPublicationField; -use Hyde\Publications\Models\PublicationFieldDefinition; -use Hyde\Publications\Models\PublicationPage; -use Hyde\Publications\Models\PublicationType; use Hyde\Publications\PublicationService; -use Illuminate\Support\Collection; -use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; use function array_filter; use function basename; -use function collect; use function dirname; -use function filled; use function glob; use function implode; use function json_encode; @@ -27,8 +18,6 @@ use function next; use function round; use function sprintf; -use function str_repeat; -use function strlen; /** * Hyde Command to validate all publication schema file.. From 2747ff4af9bec5953d8296f7a8b0a675a11ced9e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:19:36 +0100 Subject: [PATCH 29/49] Register the command --- packages/publications/src/PublicationsServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/publications/src/PublicationsServiceProvider.php b/packages/publications/src/PublicationsServiceProvider.php index 50148162da4..654e501bf8f 100644 --- a/packages/publications/src/PublicationsServiceProvider.php +++ b/packages/publications/src/PublicationsServiceProvider.php @@ -23,6 +23,7 @@ public function register(): void Commands\MakePublicationTypeCommand::class, Commands\MakePublicationCommand::class, + Commands\ValidatePublicationTypesCommand::class, Commands\ValidatePublicationsCommand::class, Commands\SeedPublicationCommand::class, ]); From f1635e3a18c545e19df1f0304251325d159c34fd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:20:13 +0100 Subject: [PATCH 30/49] Inline local variables --- .../src/Commands/ValidatePublicationTypesCommand.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 4a2df3b4051..5fccea1ac07 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -72,11 +72,8 @@ public function safeHandle(): int protected function validateSchemaFiles(): void { /** @see PublicationService::getSchemaFiles() */ - $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); - - foreach ($schemaFiles as $schemaFile) { - $name = basename(dirname($schemaFile)); - $this->results[$name] = PublicationService::validateSchemaFile($schemaFile, false); + foreach (glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json') as $schemaFile) { + $this->results[basename(dirname($schemaFile))] = PublicationService::validateSchemaFile($schemaFile, false); } } From 5e97c07dc19feae4208f4e43871e5778477cf932 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:20:57 +0100 Subject: [PATCH 31/49] Clarify PHPDoc comment --- .../src/Commands/ValidatePublicationTypesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 5fccea1ac07..e3e1f013f16 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -71,7 +71,7 @@ public function safeHandle(): int protected function validateSchemaFiles(): void { - /** @see PublicationService::getSchemaFiles() */ + /** Uses the same glob pattern as {@see PublicationService::getSchemaFiles()} */ foreach (glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json') as $schemaFile) { $this->results[basename(dirname($schemaFile))] = PublicationService::validateSchemaFile($schemaFile, false); } From d83f72b4c5714745552f7e89a501250a7ced3c0a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:21:55 +0100 Subject: [PATCH 32/49] Remove unused class constants --- .../src/Commands/ValidatePublicationTypesCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index e3e1f013f16..84364d583cf 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -28,9 +28,7 @@ */ class ValidatePublicationTypesCommand extends ValidatingCommand { - protected const CHECKMARK = "\u{2713}"; protected const CROSS_MARK = 'x'; - protected const WARNING = "\u{26A0}"; /** @var string */ protected $signature = 'validate:publicationTypes {--json : Display results as JSON.}'; From ccdab4a8f467888ebad82e51a12744c292f55897 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:22:44 +0100 Subject: [PATCH 33/49] Replace class property with local variable --- .../src/Commands/ValidatePublicationTypesCommand.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 84364d583cf..14d606e59fd 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -36,23 +36,21 @@ class ValidatePublicationTypesCommand extends ValidatingCommand /** @var string */ protected $description = 'Validate all publication schema files.'; - protected bool $json; - protected array $results = []; public function safeHandle(): int { $timeStart = microtime(true); - $this->json = $this->option('json'); + $json = $this->option('json'); - if (! $this->json) { + if (!$json) { $this->title('Validating publication schemas!'); } $this->validateSchemaFiles(); - if ($this->json) { + if ($json) { $this->outputJson(); } else { $this->displayResults(); From 6b19fcc65695ff354da10b1278358392dc87947e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 11:23:58 +0100 Subject: [PATCH 34/49] Reorder execution flow to merge conditionals --- .../src/Commands/ValidatePublicationTypesCommand.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 14d606e59fd..c95803d13f2 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -42,19 +42,13 @@ public function safeHandle(): int { $timeStart = microtime(true); - $json = $this->option('json'); - - if (!$json) { - $this->title('Validating publication schemas!'); - } - $this->validateSchemaFiles(); - if ($json) { + if ($this->option('json')) { $this->outputJson(); } else { + $this->title('Validating publication schemas!'); $this->displayResults(); - $this->outputSummary($timeStart); } From d388934cecf16a6d6fe54efb3a829e6acd3aa5c8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:03:24 +0100 Subject: [PATCH 35/49] Throw if there are no schema files to validate --- .../src/Commands/ValidatePublicationTypesCommand.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index c95803d13f2..4f6082526db 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -6,6 +6,7 @@ use Hyde\Hyde; use Hyde\Publications\PublicationService; +use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; use function array_filter; use function basename; @@ -62,7 +63,13 @@ public function safeHandle(): int protected function validateSchemaFiles(): void { /** Uses the same glob pattern as {@see PublicationService::getSchemaFiles()} */ - foreach (glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json') as $schemaFile) { + $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + + if (empty($schemaFiles)) { + throw new InvalidArgumentException('No publication types to validate!'); + } + + foreach ($schemaFiles as $schemaFile) { $this->results[basename(dirname($schemaFile))] = PublicationService::validateSchemaFile($schemaFile, false); } } From c483f740b8bea56262a9f18f531d9751be9e498c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:04:08 +0100 Subject: [PATCH 36/49] Bring out title to run before validation --- .../src/Commands/ValidatePublicationTypesCommand.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 4f6082526db..916f3bc6f6c 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -43,12 +43,15 @@ public function safeHandle(): int { $timeStart = microtime(true); + if (! $this->option('json')) { + $this->title('Validating publication schemas!'); + } + $this->validateSchemaFiles(); if ($this->option('json')) { $this->outputJson(); } else { - $this->title('Validating publication schemas!'); $this->displayResults(); $this->outputSummary($timeStart); } From 5b2c320a4ef6fdb95f8920ba5e8487433e2dd8d5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:19:42 +0100 Subject: [PATCH 37/49] Create custom function to get accurate error count --- .../Commands/ValidatePublicationTypesCommand.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 916f3bc6f6c..96e80e138c7 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -56,7 +56,7 @@ public function safeHandle(): int $this->outputSummary($timeStart); } - if (count(array_filter($this->results))) { + if ($this->countErrors() > 0) { return Command::FAILURE; } @@ -126,4 +126,16 @@ protected function outputJson(): void { $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); } + + protected function countErrors(): int + { + $errors = 0; + + foreach ($this->results as $results) { + $errors += count($results['schema']); + $errors += count(array_filter($results['fields'])); + } + + return $errors; + } } From b662cd89c599fe3795b1d68709b226642c7f3dc6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:23:44 +0100 Subject: [PATCH 38/49] Create ValidatePublicationTypesCommandTest.php --- .../ValidatePublicationTypesCommandTest.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php new file mode 100644 index 00000000000..80ce2d87595 --- /dev/null +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -0,0 +1,70 @@ +artisan('validate:publicationTypes') + ->expectsOutput('Error: No publication types to validate!') + ->assertExitCode(1); + } + + public function testWithValidSchemaFile() + { + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication', fields: [ + ['name' => 'myField', 'type' => 'string'], + ]); + $publicationType->save(); + + $this->artisan('validate:publicationTypes') + ->assertExitCode(0); + } + + + public function testWithInvalidSchemaFile() + { + config(['app.throw_on_console_exception' => true]); + + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->file('test-publication/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ], + "directory": "foo" + } + JSON + ); + + $this->artisan('validate:publicationTypes') + ->assertExitCode(1); + } +} From 835ae27344241ac2a3f2a0dc0c12ae152198efca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:28:12 +0100 Subject: [PATCH 39/49] Add output expectations --- .../ValidatePublicationTypesCommandTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index 80ce2d87595..c341c813c61 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -15,6 +15,7 @@ class ValidatePublicationTypesCommandTest extends TestCase public function testWithNoPublicationTypes() { $this->artisan('validate:publicationTypes') + ->expectsOutputToContain('Validating publication schemas!') ->expectsOutput('Error: No publication types to validate!') ->assertExitCode(1); } @@ -28,6 +29,11 @@ public function testWithValidSchemaFile() $publicationType->save(); $this->artisan('validate:publicationTypes') + ->expectsOutputToContain('Validating publication schemas!') + ->expectsOutput('Validating schema file for [test-publication]') + ->expectsOutput(' No top-level schema errors found') + ->expectsOutput(' No field-level schema errors found') + ->expectsOutputToContain('All done') ->assertExitCode(0); } @@ -65,6 +71,24 @@ public function testWithInvalidSchemaFile() ); $this->artisan('validate:publicationTypes') + ->expectsOutputToContain('Validating publication schemas!') + ->expectsOutput('Validating schema file for [test-publication]') + ->expectsOutput(' Found 7 top-level schema errors:') + ->expectsOutput(' x The name must be a string.') + ->expectsOutput(' x The canonical field must be a string.') + ->expectsOutput(' x The detail template must be a string.') + ->expectsOutput(' x The list template must be a string.') + ->expectsOutput(' x The sort field must be a string.') + ->expectsOutput(' x The sort ascending field must be true or false.') + ->expectsOutput(' x The directory field is prohibited.') + ->expectsOutput(' Found errors in 2 field definitions:') + ->expectsOutput(' Field #1:') + ->expectsOutput(' x The type must be a string.') + ->expectsOutput(' x The name must be a string.') + ->expectsOutput(' Field #2:') + ->expectsOutput(' x The type field is required.') + ->expectsOutput(' x The name field is required.') + ->expectsOutputToContain('All done') ->assertExitCode(1); } } From 88f10581b7c5837ec2ccfde7f17e39ce9c16ae22 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:31:52 +0100 Subject: [PATCH 40/49] Test with multiple publication types --- .../ValidatePublicationTypesCommandTest.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index c341c813c61..dbc22a1bcbf 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -91,4 +91,64 @@ public function testWithInvalidSchemaFile() ->expectsOutputToContain('All done') ->assertExitCode(1); } + + public function testWithMultiplePublicationTypes() + { + config(['app.throw_on_console_exception' => true]); + + $this->directory('test-publication-1'); + $publicationType = new PublicationType('test-publication-1', fields: [ + ['name' => 'myField', 'type' => 'string'], + ]); + $publicationType->save(); + + $this->directory('test-publication-2'); + $this->file('test-publication-2/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ], + "directory": "foo" + } + JSON + ); + + $this->artisan('validate:publicationTypes') + ->expectsOutputToContain('Validating publication schemas!') + ->expectsOutput('Validating schema file for [test-publication-1]') + ->expectsOutput(' No top-level schema errors found') + ->expectsOutput(' No field-level schema errors found') + ->expectsOutput('Validating schema file for [test-publication-2]') + ->expectsOutput(' Found 7 top-level schema errors:') + ->expectsOutput(' x The name must be a string.') + ->expectsOutput(' x The canonical field must be a string.') + ->expectsOutput(' x The detail template must be a string.') + ->expectsOutput(' x The list template must be a string.') + ->expectsOutput(' x The sort field must be a string.') + ->expectsOutput(' x The sort ascending field must be true or false.') + ->expectsOutput(' x The directory field is prohibited.') + ->expectsOutput(' Found errors in 2 field definitions:') + ->expectsOutput(' Field #1:') + ->expectsOutput(' x The type must be a string.') + ->expectsOutput(' x The name must be a string.') + ->expectsOutput(' Field #2:') + ->expectsOutput(' x The type field is required.') + ->expectsOutput(' x The name field is required.') + ->expectsOutputToContain('All done') + ->assertExitCode(1); + } } From f9ec8928d32b8031eb4abca2a86b26045f436518 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:33:09 +0100 Subject: [PATCH 41/49] Test with no field definitions --- .../ValidatePublicationTypesCommandTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index dbc22a1bcbf..701eb048f8f 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -151,4 +151,21 @@ public function testWithMultiplePublicationTypes() ->expectsOutputToContain('All done') ->assertExitCode(1); } + + public function testWithNoFields() + { + config(['app.throw_on_console_exception' => true]); + + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->artisan('validate:publicationTypes') + ->expectsOutputToContain('Validating publication schemas!') + ->expectsOutput('Validating schema file for [test-publication]') + ->expectsOutput(' No top-level schema errors found') + ->expectsOutput(' No field-level schema errors found') + ->expectsOutputToContain('All done') + ->assertExitCode(0); + } } From 2e8ab1f88e87558c0234c498b66a2c888ead57a7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:37:01 +0100 Subject: [PATCH 42/49] Test with Json output --- .../ValidatePublicationTypesCommandTest.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index 701eb048f8f..de55b1c9920 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -168,4 +168,97 @@ public function testWithNoFields() ->expectsOutputToContain('All done') ->assertExitCode(0); } + + public function testJsonOutputOption() + { + config(['app.throw_on_console_exception' => true]); + + $this->directory('test-publication-1'); + $publicationType = new PublicationType('test-publication-1', fields: [ + ['name' => 'myField', 'type' => 'string'], + ]); + $publicationType->save(); + + $this->directory('test-publication-2'); + $this->file('test-publication-2/schema.json', <<<'JSON' + { + "name": 123, + "canonicalField": 123, + "detailTemplate": 123, + "listTemplate": 123, + "sortField": 123, + "sortAscending": 123, + "pageSize": "123", + "fields": [ + { + "name": 123, + "type": 123 + }, + { + "noName": "myField", + "noType": "string" + } + ], + "directory": "foo" + } + JSON + ); + + $this->artisan('validate:publicationTypes --json') + ->expectsOutput( + <<<'JSON' + { + "test-publication-1": { + "schema": [], + "fields": [ + [] + ] + }, + "test-publication-2": { + "schema": { + "name": [ + "The name must be a string." + ], + "canonicalField": [ + "The canonical field must be a string." + ], + "detailTemplate": [ + "The detail template must be a string." + ], + "listTemplate": [ + "The list template must be a string." + ], + "sortField": [ + "The sort field must be a string." + ], + "sortAscending": [ + "The sort ascending field must be true or false." + ], + "directory": [ + "The directory field is prohibited." + ] + }, + "fields": [ + { + "type": [ + "The type must be a string." + ], + "name": [ + "The name must be a string." + ] + }, + { + "type": [ + "The type field is required." + ], + "name": [ + "The name field is required." + ] + } + ] + } + } + JSON + )->assertExitCode(1); + } } From 9a343df4a4034bf2ca2b0845ab7287cc1c9a399a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:38:11 +0100 Subject: [PATCH 43/49] Add additional Json test --- .../ValidatePublicationTypesCommandTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index de55b1c9920..7cc02332599 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -173,6 +173,26 @@ public function testJsonOutputOption() { config(['app.throw_on_console_exception' => true]); + $this->directory('test-publication'); + $publicationType = new PublicationType('test-publication'); + $publicationType->save(); + + $this->artisan('validate:publicationTypes --json') + ->expectsOutput(<<<'JSON' + { + "test-publication": { + "schema": [], + "fields": [] + } + } + JSON) + ->assertExitCode(0); + } + + public function testMultipleTypesWithJsonOutput() + { + config(['app.throw_on_console_exception' => true]); + $this->directory('test-publication-1'); $publicationType = new PublicationType('test-publication-1', fields: [ ['name' => 'myField', 'type' => 'string'], From 24d4c211ca9923ffbc0df8148e94d33f41cfbe1c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:39:45 +0100 Subject: [PATCH 44/49] Extract code to setup method --- .../ValidatePublicationTypesCommandTest.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index 7cc02332599..beb5e31b480 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -12,8 +12,17 @@ */ class ValidatePublicationTypesCommandTest extends TestCase { + protected function setUp(): void + { + parent::setUp(); + + config(['app.throw_on_console_exception' => true]); + } + public function testWithNoPublicationTypes() { + config(['app.throw_on_console_exception' => false]); + $this->artisan('validate:publicationTypes') ->expectsOutputToContain('Validating publication schemas!') ->expectsOutput('Error: No publication types to validate!') @@ -22,6 +31,8 @@ public function testWithNoPublicationTypes() public function testWithValidSchemaFile() { + config(['app.throw_on_console_exception' => false]); + $this->directory('test-publication'); $publicationType = new PublicationType('test-publication', fields: [ ['name' => 'myField', 'type' => 'string'], @@ -40,8 +51,6 @@ public function testWithValidSchemaFile() public function testWithInvalidSchemaFile() { - config(['app.throw_on_console_exception' => true]); - $this->directory('test-publication'); $publicationType = new PublicationType('test-publication'); $publicationType->save(); @@ -94,8 +103,6 @@ public function testWithInvalidSchemaFile() public function testWithMultiplePublicationTypes() { - config(['app.throw_on_console_exception' => true]); - $this->directory('test-publication-1'); $publicationType = new PublicationType('test-publication-1', fields: [ ['name' => 'myField', 'type' => 'string'], @@ -154,8 +161,6 @@ public function testWithMultiplePublicationTypes() public function testWithNoFields() { - config(['app.throw_on_console_exception' => true]); - $this->directory('test-publication'); $publicationType = new PublicationType('test-publication'); $publicationType->save(); @@ -171,8 +176,6 @@ public function testWithNoFields() public function testJsonOutputOption() { - config(['app.throw_on_console_exception' => true]); - $this->directory('test-publication'); $publicationType = new PublicationType('test-publication'); $publicationType->save(); @@ -191,8 +194,6 @@ public function testJsonOutputOption() public function testMultipleTypesWithJsonOutput() { - config(['app.throw_on_console_exception' => true]); - $this->directory('test-publication-1'); $publicationType = new PublicationType('test-publication-1', fields: [ ['name' => 'myField', 'type' => 'string'], From 618f20238d446ead28791cfee17688df8e16be43 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 18 Jan 2023 12:40:33 +0000 Subject: [PATCH 45/49] Apply fixes from StyleCI --- .../Commands/ValidatePublicationTypesCommand.php | 8 ++++---- .../src/Commands/ValidatePublicationsCommand.php | 15 ++++++++------- packages/publications/src/PublicationService.php | 2 +- .../tests/Feature/PublicationServiceTest.php | 4 ++-- .../tests/Feature/PublicationTypeTest.php | 1 - .../ValidatePublicationTypesCommandTest.php | 1 - 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index 96e80e138c7..a8969cac5b8 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -4,16 +4,16 @@ namespace Hyde\Publications\Commands; -use Hyde\Hyde; -use Hyde\Publications\PublicationService; -use InvalidArgumentException; -use LaravelZero\Framework\Commands\Command; use function array_filter; use function basename; use function dirname; use function glob; +use Hyde\Hyde; +use Hyde\Publications\PublicationService; use function implode; +use InvalidArgumentException; use function json_encode; +use LaravelZero\Framework\Commands\Command; use function memory_get_peak_usage; use function microtime; use function next; diff --git a/packages/publications/src/Commands/ValidatePublicationsCommand.php b/packages/publications/src/Commands/ValidatePublicationsCommand.php index 5238040711d..82d29e9bbd0 100644 --- a/packages/publications/src/Commands/ValidatePublicationsCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationsCommand.php @@ -4,12 +4,13 @@ namespace Hyde\Publications\Commands; -use Hyde\Hyde; use function array_filter; use function basename; use function collect; use Exception; use function filled; +use function glob; +use Hyde\Hyde; use Hyde\Publications\Actions\ValidatesPublicationField; use Hyde\Publications\Models\PublicationFieldDefinition; use Hyde\Publications\Models\PublicationPage; @@ -18,7 +19,6 @@ use Illuminate\Support\Collection; use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; -use function glob; use function microtime; use function str_repeat; use function strlen; @@ -74,6 +74,7 @@ public function safeHandle(): int round((microtime(true) - $timeStart) * 1000), round(memory_get_peak_usage() / 1024 / 1024) )); + return Command::SUCCESS; } @@ -280,9 +281,9 @@ protected function validateSchemaFiles(): void if (empty($errors['schema'])) { $this->line(' No top-level schema errors found'); } else { - $this->line(sprintf(" Found %s top-level schema errors:", count($errors['schema']))); + $this->line(sprintf(' Found %s top-level schema errors:', count($errors['schema']))); foreach ($errors['schema'] as $error) { - $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); } } @@ -290,11 +291,11 @@ protected function validateSchemaFiles(): void $this->line(' No field-level schema errors found'); } else { $this->newLine(); - $this->line(sprintf(" Found errors in %s field definitions:", count($errors['fields']))); + $this->line(sprintf(' Found errors in %s field definitions:', count($errors['fields']))); foreach ($errors['fields'] as $fieldNumber => $fieldErrors) { - $this->line(sprintf(" Field #%s:", $fieldNumber+1)); + $this->line(sprintf(' Field #%s:', $fieldNumber + 1)); foreach ($fieldErrors as $error) { - $this->line(sprintf(" %s %s", self::CROSS_MARK, implode(' ', $error))); + $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); } } } diff --git a/packages/publications/src/PublicationService.php b/packages/publications/src/PublicationService.php index 65d748ce297..3a9a470f0a7 100644 --- a/packages/publications/src/PublicationService.php +++ b/packages/publications/src/PublicationService.php @@ -4,8 +4,8 @@ namespace Hyde\Publications; -use Hyde\Facades\Filesystem; use function glob; +use Hyde\Facades\Filesystem; use Hyde\Hyde; use Hyde\Publications\Models\PublicationPage; use Hyde\Publications\Models\PublicationTags; diff --git a/packages/publications/tests/Feature/PublicationServiceTest.php b/packages/publications/tests/Feature/PublicationServiceTest.php index 1295c31b6ab..693ea849be3 100644 --- a/packages/publications/tests/Feature/PublicationServiceTest.php +++ b/packages/publications/tests/Feature/PublicationServiceTest.php @@ -4,7 +4,6 @@ namespace Hyde\Publications\Testing\Feature; -use Illuminate\Validation\ValidationException; use function file_put_contents; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Hyde; @@ -14,6 +13,7 @@ use Hyde\Testing\TestCase; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; +use Illuminate\Validation\ValidationException; use function json_encode; use function mkdir; @@ -330,7 +330,7 @@ public function testValidateSchemaFileWithInvalidDataBuffered() ], 'fields' => [[ 'type' => ['The type must be a string.'], - 'name' => ['The name must be a string.'] + 'name' => ['The name must be a string.'], ], [ 'type' => ['The type field is required.'], 'name' => ['The name field is required.'], diff --git a/packages/publications/tests/Feature/PublicationTypeTest.php b/packages/publications/tests/Feature/PublicationTypeTest.php index 71e7e549223..208fc51ff3a 100644 --- a/packages/publications/tests/Feature/PublicationTypeTest.php +++ b/packages/publications/tests/Feature/PublicationTypeTest.php @@ -4,7 +4,6 @@ namespace Hyde\Publications\Testing\Feature; -use Illuminate\Validation\ValidationException; use function array_merge; use function array_reverse; use Hyde\Framework\Features\Paginator; diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index beb5e31b480..b0d7fc471e6 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -48,7 +48,6 @@ public function testWithValidSchemaFile() ->assertExitCode(0); } - public function testWithInvalidSchemaFile() { $this->directory('test-publication'); From 8ec026711269189d099c8e243c787a3b762b6d1e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:53:11 +0100 Subject: [PATCH 46/49] Revert "Display field errors" This reverts commit c8d16248b2fe4b308476f604314061b196397a37. --- .../Commands/ValidatePublicationsCommand.php | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationsCommand.php b/packages/publications/src/Commands/ValidatePublicationsCommand.php index 82d29e9bbd0..5a3dc4b159b 100644 --- a/packages/publications/src/Commands/ValidatePublicationsCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationsCommand.php @@ -4,7 +4,7 @@ namespace Hyde\Publications\Commands; -use function array_filter; +use Hyde\Hyde; use function basename; use function collect; use Exception; @@ -281,25 +281,12 @@ protected function validateSchemaFiles(): void if (empty($errors['schema'])) { $this->line(' No top-level schema errors found'); } else { - $this->line(sprintf(' Found %s top-level schema errors:', count($errors['schema']))); + $this->line(sprintf(" Found %s errors:", count($errors['schema']))); foreach ($errors['schema'] as $error) { $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); } } - if (empty(array_filter($errors['fields']))) { - $this->line(' No field-level schema errors found'); - } else { - $this->newLine(); - $this->line(sprintf(' Found errors in %s field definitions:', count($errors['fields']))); - foreach ($errors['fields'] as $fieldNumber => $fieldErrors) { - $this->line(sprintf(' Field #%s:', $fieldNumber + 1)); - foreach ($fieldErrors as $error) { - $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); - } - } - } - if ($number !== count($schemaFiles) - 1) { $this->newLine(); } From dee1660c15dee6bba38e65550c721889522f776f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:53:29 +0100 Subject: [PATCH 47/49] Revert "Add option to only validate the schemas" This reverts commit d7f2a53a0d51c801d57ef95f0baf9c741fe7097c. --- .../Commands/ValidatePublicationsCommand.php | 45 +------------------ 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationsCommand.php b/packages/publications/src/Commands/ValidatePublicationsCommand.php index 5a3dc4b159b..08c2d909d14 100644 --- a/packages/publications/src/Commands/ValidatePublicationsCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationsCommand.php @@ -4,13 +4,9 @@ namespace Hyde\Publications\Commands; -use Hyde\Hyde; -use function basename; use function collect; use Exception; use function filled; -use function glob; -use Hyde\Hyde; use Hyde\Publications\Actions\ValidatesPublicationField; use Hyde\Publications\Models\PublicationFieldDefinition; use Hyde\Publications\Models\PublicationPage; @@ -39,8 +35,7 @@ class ValidatePublicationsCommand extends ValidatingCommand /** @var string */ protected $signature = 'validate:publications {publicationType? : The name of the publication type to validate.} - {--json : Display results as JSON.} - {--schemas : Only validate the publication schema files.}'; + {--json : Display results as JSON.}'; /** @var string */ protected $description = 'Validate all or the specified publication type(s)'; @@ -66,18 +61,6 @@ public function safeHandle(): int $this->title('Validating publications!'); } - if ($this->option('schemas')) { - $this->validateSchemaFiles(); - - $this->newLine(); - $this->info(sprintf('All done in %sms using %sMB peak memory!', - round((microtime(true) - $timeStart) * 1000), - round(memory_get_peak_usage() / 1024 / 1024) - )); - - return Command::SUCCESS; - } - $publicationTypesToValidate = $this->getPublicationTypesToValidate(); foreach ($publicationTypesToValidate as $publicationType) { @@ -266,30 +249,4 @@ protected function outputJson(): void { $this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); } - - protected function validateSchemaFiles(): void - { - /** @see PublicationService::getSchemaFiles() */ - $schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); - - foreach ($schemaFiles as $number => $schemaFile) { - $name = basename(dirname($schemaFile)); - $this->infoComment('Validating schema file for', $name); - - $errors = PublicationService::validateSchemaFile($schemaFile, false); - - if (empty($errors['schema'])) { - $this->line(' No top-level schema errors found'); - } else { - $this->line(sprintf(" Found %s errors:", count($errors['schema']))); - foreach ($errors['schema'] as $error) { - $this->line(sprintf(' %s %s', self::CROSS_MARK, implode(' ', $error))); - } - } - - if ($number !== count($schemaFiles) - 1) { - $this->newLine(); - } - } - } } From 9333ae92dc241f0ee0052db5c3f6c4abaa587ff3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 13:59:44 +0100 Subject: [PATCH 48/49] Refactor to use the publication name as argument --- .../src/Commands/ValidatePublicationTypesCommand.php | 3 ++- packages/publications/src/Models/PublicationType.php | 2 +- packages/publications/src/PublicationService.php | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php index a8969cac5b8..11376d4cc8a 100644 --- a/packages/publications/src/Commands/ValidatePublicationTypesCommand.php +++ b/packages/publications/src/Commands/ValidatePublicationTypesCommand.php @@ -73,7 +73,8 @@ protected function validateSchemaFiles(): void } foreach ($schemaFiles as $schemaFile) { - $this->results[basename(dirname($schemaFile))] = PublicationService::validateSchemaFile($schemaFile, false); + $publicationName = basename(dirname($schemaFile)); + $this->results[$publicationName] = PublicationService::validateSchemaFile($publicationName, false); } } diff --git a/packages/publications/src/Models/PublicationType.php b/packages/publications/src/Models/PublicationType.php index 532dbdeeafe..0b0bbe5cc3e 100644 --- a/packages/publications/src/Models/PublicationType.php +++ b/packages/publications/src/Models/PublicationType.php @@ -225,6 +225,6 @@ protected function withoutNullValues(array $array): array */ public function validateSchemaFile(bool $throw = true): array { - return PublicationService::validateSchemaFile($this->getSchemaFile(), $throw); + return PublicationService::validateSchemaFile($this->getIdentifier(), $throw); } } diff --git a/packages/publications/src/PublicationService.php b/packages/publications/src/PublicationService.php index 3a9a470f0a7..c681850cbec 100644 --- a/packages/publications/src/PublicationService.php +++ b/packages/publications/src/PublicationService.php @@ -97,9 +97,9 @@ public static function publicationTypeExists(string $pubTypeName): bool * * @internal This method is experimental and may be removed without notice */ - public static function validateSchemaFile(string $schemaFile, bool $throw = true): array + public static function validateSchemaFile(string $pubTypeName, bool $throw = true): array { - $schema = json_decode(Filesystem::getContents($schemaFile)); + $schema = json_decode(Filesystem::getContents("$pubTypeName/schema.json")); $errors = []; $schemaValidator = validator([ From 858469c93b8c5be277236cb5be4f91406d9e0f42 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 Jan 2023 14:06:37 +0100 Subject: [PATCH 49/49] Inline local variable --- .../publications/tests/Feature/PublicationServiceTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/publications/tests/Feature/PublicationServiceTest.php b/packages/publications/tests/Feature/PublicationServiceTest.php index 693ea849be3..981d59e2cf8 100644 --- a/packages/publications/tests/Feature/PublicationServiceTest.php +++ b/packages/publications/tests/Feature/PublicationServiceTest.php @@ -316,8 +316,6 @@ public function testValidateSchemaFileWithInvalidDataBuffered() JSON ); - $errors = $publicationType->validateSchemaFile(false); - $this->assertSame([ 'schema' => [ 'name' => ['The name must be a string.'], @@ -335,7 +333,7 @@ public function testValidateSchemaFileWithInvalidDataBuffered() 'type' => ['The type field is required.'], 'name' => ['The name field is required.'], ]], - ], $errors); + ], $publicationType->validateSchemaFile(false)); } protected function createPublicationType(): void