diff --git a/CHANGELOG.md b/CHANGELOG.md index 642b096fa..6d56cc2c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ [#544](https://github.com/nextcloud/cookbook/pull/544/) @seyfeb - Refreshing left navigation pane after downloading recipe data, closes #465 [#547](https://github.com/nextcloud/cookbook/pull/547/) @seyfeb +- Check for existing `@context` setting in json checker + [#554](https://github.com/nextcloud/cookbook/pull/554) @christianlupus ### Removed - Removal of old contoller no longer in use diff --git a/lib/Service/JsonService.php b/lib/Service/JsonService.php index e6162f128..fb7db78ff 100644 --- a/lib/Service/JsonService.php +++ b/lib/Service/JsonService.php @@ -25,6 +25,11 @@ public function isSchemaObject($obj, string $type = null) : bool { return false; } + if (!isset($obj['@context']) || ! preg_match('@^https?://schema\.org/?$@', $obj['@context'])) { + // We have no correct context property + return false; + } + if (!isset($obj['@type'])) { // Objects must have a property @type return false; diff --git a/tests/Unit/Service/JsonServiceTest.php b/tests/Unit/Service/JsonServiceTest.php index db8d410e8..f6d10c57a 100644 --- a/tests/Unit/Service/JsonServiceTest.php +++ b/tests/Unit/Service/JsonServiceTest.php @@ -22,6 +22,31 @@ public function testIsSchemaObject() { $result = $this->service->isSchemaObject($testData); self::assertFalse($result, 'The object must be an array'); + // Objects must have a property @context + $testData = [ + "@type" => "Recipe", + "name" => "Schema.org Ontology", + "subjectOf" => [ + "@type" => "Book", + "name" => "The Complete History of Schema.org" + ] + ]; + $result = $this->service->isSchemaObject($testData); + self::assertFalse($result, 'The object must have a context'); + + // Context must be in schema.org domain + $testData = [ + "@context" => "https://schema.com/", + '@type' => 'Recipe', + "name" => "Schema.org Ontology", + "subjectOf" => [ + "@type" => "Book", + "name" => "The Complete History of Schema.org" + ] + ]; + $result = $this->service->isSchemaObject($testData); + self::assertFalse($result, 'The object must be in the correct context'); + // Objects must have a property @type $testData = [ "@context" => "https://schema.org/", @@ -33,7 +58,7 @@ public function testIsSchemaObject() { ]; $result = $this->service->isSchemaObject($testData); self::assertFalse($result, 'The object must have the property @type'); - + // No typecheck will be requested $testData = [ "@context" => "https://schema.org/",