diff --git a/.github/actions/run-tests/README.md b/.github/actions/run-tests/README.md index 96a8a18a2..404c6f8a2 100644 --- a/.github/actions/run-tests/README.md +++ b/.github/actions/run-tests/README.md @@ -32,7 +32,7 @@ The folder will be populated later during the build. There will be a few 100MB n In order to run the tests, a docker image accoring to your settings needs to be generated. This is done by calling ``` -docker-compose built dut +docker-compose build dut ``` This process will take some time as a few dependencies need to be compiled into the image. Just wait for it to succeed. diff --git a/CHANGELOG.md b/CHANGELOG.md index ceaadbe7f..e41ea0196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ [#375](https://github.com/nextcloud/cookbook/pull/375/) @seyfeb - Service to handle schema.org JSON data in strings easier [#383](https://github.com/nextcloud/cookbook/pull/383/) @christianlupus +- Unit tests for JSON object service + [#387](https://github.com/nextcloud/cookbook/pull/387) @TobiasMie ### Changed - Switch of project ownership to neextcloud organization in GitHub diff --git a/lib/Service/JsonService.php b/lib/Service/JsonService.php index 6da6cffad..8ec50a7b5 100644 --- a/lib/Service/JsonService.php +++ b/lib/Service/JsonService.php @@ -1,6 +1,6 @@ service = new JsonService(); + } + + public function testIsSchemaObject(){ + // Objects must be encoded as arrays in JSON + $testData = "notAnArray"; + $result = $this->service->isSchemaObject($testData); + self::assertFalse($result, 'The object must be an array'); + + // Objects must have a property @type + $testData = [ + "@context" => "https://schema.org/", + "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 the property @type'); + + // No typecheck will be requested + $testData = [ + "@context" => "https://schema.org/", + "@type" => "Thing", + "name" => "Schema.org Ontology", + "subjectOf" => [ + "@type" => "Book", + "name" => "The Complete History of Schema.org" + ] + ]; + $result = $this->service->isSchemaObject($testData); + self::assertTrue($result); + $result = $this->service->isSchemaObject($testData, ''); + self::assertTrue($result); + + // Check if type matches + $testData = [ + "@context" => "https://schema.org/", + "@type" => "Thing", + "name" => "Schema.org Ontology", + "subjectOf" => [ + "@type" => "Book", + "name" => "The Complete History of Schema.org" + ] + ]; + $result = $this->service->isSchemaObject($testData, 'Thing'); + self::assertTrue($result, 'The type match but it returned false'); + $result = $this->service->isSchemaObject($testData, 'Foo'); + self::assertFalse($result, 'The type does not match bat it returned true'); + } + + public function testHasProperty(){ + // The method isSchemaObject() is tested in another test and assumed as working properly + $testData = [ + "@context" => "https://schema.org/", + "@type" => "Thing", + "name" => "Schema.org Ontology", + "subjectOf" => [ + "@type" => "Book", + "name" => "The Complete History of Schema.org" + ] + ]; + $result = $this->service->hasProperty($testData, 'name'); + self::assertTrue($result, 'Property name was not found.'); + $result = $this->service->hasProperty($testData, 'Bar'); + self::assertFalse($result, 'Property Bar was falsely found.'); + + $result = $this->service->hasProperty(['foo' => 'bar'], 'foo'); + self::assertFalse($result, 'Property of a non-object must not be returned.'); + } +}