diff --git a/README.md b/README.md index cc79268..3444f42 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ This README documents whatever steps are necessary to get this plugin up and run See [Hooks](/docs/hooks.md) -## Rest api ## +## REST API ## -See [Rest Api](/docs/restapi.md) +See [REST API](/docs/restapi.md) ## Translations ## -If you want to use your own set of labels/names/descriptions and so on you can do so. +If you want to use your own set of labels/names/descriptions and so on you can do so. All text output in this plugin is controlled via the gettext methods. Please use your preferred way to make your own translations from the /wp-content/plugins/openpub-base/languages/openpub-base.pot file @@ -52,7 +52,7 @@ phpunit --coverage-html ./tests/coverage ### Writing tests ### -Have a look at the code coverage reports to see where more coverage can be obtained. +Have a look at the code coverage reports to see where more coverage can be obtained. Write tests Create a Pull request to the OWC repository diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 6596d90..be4b762 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -9,7 +9,8 @@ pipelines: - composer - vendor-directory script: - - composer install --no-interaction --no-progress --prefer-dist --ignore-platform-reqs + - composer install --verbose --prefer-dist --optimize-autoloader --no-progress --no-interaction + - ./vendor/bin/phpunit --testsuite "Unit Test Suite" branches: "{master,develop}": @@ -19,7 +20,7 @@ pipelines: - composer - vendor-directory script: - - composer install --no-interaction --no-progress --prefer-dist --ignore-platform-reqs + - composer install --verbose --prefer-dist --optimize-autoloader --no-progress --no-interaction - ./vendor/bin/phpunit --testsuite "Unit Test Suite" definitions: diff --git a/docs/restapi.md b/docs/restapi.md index a80d15d..05e9dc1 100644 --- a/docs/restapi.md +++ b/docs/restapi.md @@ -1,4 +1,4 @@ -# Rest API +# REST API ## Available endpoints @@ -39,4 +39,4 @@ Parameters: ### Endpoint of searching -`https://url/wp-json/owc/openpub/v1/search` \ No newline at end of file +`https://url/wp-json/owc/openpub/v1/search` diff --git a/src/Base/RestAPI/ItemFields/CommentField.php b/src/Base/RestAPI/ItemFields/CommentField.php index c699b8e..c62bc0d 100644 --- a/src/Base/RestAPI/ItemFields/CommentField.php +++ b/src/Base/RestAPI/ItemFields/CommentField.php @@ -58,7 +58,6 @@ protected function getComments(int $postID): array return []; } - return array_map(function ($comment) { return $this->format($comment, $this->getChild($comment)); }, array_values($comments)); diff --git a/src/Base/RestAPI/ItemFields/SynonymsField.php b/src/Base/RestAPI/ItemFields/SynonymsField.php index ff9c115..0dc9abc 100644 --- a/src/Base/RestAPI/ItemFields/SynonymsField.php +++ b/src/Base/RestAPI/ItemFields/SynonymsField.php @@ -9,25 +9,17 @@ class SynonymsField extends CreatesFields { /** * Generate the synonyms field. - * - * @param WP_Post $post - * - * @return string */ public function create(WP_Post $post): string { - return esc_attr(strip_tags($this->getSynonyms($post))); + return \esc_attr(\strip_tags($this->getSynonyms($post))); } /** * Get synonyms of a post, if URL & title are present. - * - * @param WP_Post $post - * - * @return string */ - private function getSynonyms(WP_Post $post) + private function getSynonyms(WP_Post $post): string { - return get_post_meta($post->ID, '_owc_openpub_tags', true) ?: ''; + return \get_post_meta($post->ID, '_owc_openpub_tags', true) ?: ''; } } diff --git a/tests/Unit/Base/RestAPI/ItemFields/CommentFieldTest.php b/tests/Unit/Base/RestAPI/ItemFields/CommentFieldTest.php index 0e66a21..c29e634 100644 --- a/tests/Unit/Base/RestAPI/ItemFields/CommentFieldTest.php +++ b/tests/Unit/Base/RestAPI/ItemFields/CommentFieldTest.php @@ -153,12 +153,12 @@ public function it_returns_the_comments_if_comments_are_enabled_and_there_are_co 'date' => '12-01-2020', 'replies' => [ [ -'id' => 4, - 'parentid' => 3, - 'author' => 'child author 2', - 'content' => 'child comment 2', - 'date' => '12-01-2020', - 'replies' => [] + 'id' => 4, + 'parentid' => 3, + 'author' => 'child author 2', + 'content' => 'child comment 2', + 'date' => '12-01-2020', + 'replies' => [] ] ] ] @@ -168,4 +168,32 @@ public function it_returns_the_comments_if_comments_are_enabled_and_there_are_co ]; $this->assertEquals($expected, $actual); } + + /** @test */ + public function it_returns_nothing_if_comments_are_not_allow_to_be_outputted_in_the_api() + { + WP_Mock::userFunction('get_comments', [ + 'return' => [] + ]); + + $commentField = new CommentField($this->plugin); + $actual = $commentField->executeCondition(); + + $this->assertInstanceOf('Closure', $actual); + $this->assertFalse($actual()); + } + + /** @test */ + public function it_returns_true_if_comments_are_allow_to_be_outputted_in_the_api() + { + WP_Mock::userFunction('get_comments', [ + 'return' => [] + ]); + + $_REQUEST['with'] = 'comments'; + $commentField = new CommentField($this->plugin); + $actual = $commentField->executeCondition(); + + $this->assertTrue($actual()); + } } diff --git a/tests/Unit/Base/RestAPI/ItemFields/NotesFieldTest.php b/tests/Unit/Base/RestAPI/ItemFields/NotesFieldTest.php new file mode 100644 index 0000000..89c9e73 --- /dev/null +++ b/tests/Unit/Base/RestAPI/ItemFields/NotesFieldTest.php @@ -0,0 +1,68 @@ +plugin = m::mock(Plugin::class); + + $this->plugin->config = $config; + $this->plugin->loader = m::mock(Loader::class); + + $this->post = m::mock(WP_Post::class); + $this->post->ID = 1; + } + + protected function tearDown(): void + { + WP_Mock::tearDown(); + } + + /** @test */ + public function it_returns_empty_if_no_synonyms_are_used() + { + WP_Mock::userFunction('get_post_meta', [ + 'return' => false + ]); + + $notesField = new NotesField($this->plugin); + $actual = $notesField->create($this->post); + + $this->assertEquals('', $actual); + } + + /** @test */ + public function it_returns_the_notes_if_notes_are_used() + { + WP_Mock::userFunction('get_post_meta', [ + 'return' => '\" sofa couch divan' + ]); + + WP_Mock::userFunction('esc_attr', [ + 'return' => 'sofa couch divan' + ]); + + $notesField = new NotesField($this->plugin); + $actual = $notesField->create($this->post); + + $this->assertEquals('sofa couch divan', $actual); + } +} diff --git a/tests/Unit/Base/RestAPI/ItemFields/SynonymsFieldTest.php b/tests/Unit/Base/RestAPI/ItemFields/SynonymsFieldTest.php new file mode 100644 index 0000000..9a985bb --- /dev/null +++ b/tests/Unit/Base/RestAPI/ItemFields/SynonymsFieldTest.php @@ -0,0 +1,68 @@ +plugin = m::mock(Plugin::class); + + $this->plugin->config = $config; + $this->plugin->loader = m::mock(Loader::class); + + $this->post = m::mock(WP_Post::class); + $this->post->ID = 1; + } + + protected function tearDown(): void + { + WP_Mock::tearDown(); + } + + /** @test */ + public function it_returns_empty_if_no_synonyms_are_used() + { + WP_Mock::userFunction('get_post_meta', [ + 'return' => false + ]); + + $synonymsField = new SynonymsField($this->plugin); + $actual = $synonymsField->create($this->post); + + $this->assertEquals('', $actual); + } + + /** @test */ + public function it_returns_the_synonyms_if_there_are_synonyms_used() + { + WP_Mock::userFunction('get_post_meta', [ + 'return' => '\" sofa couch divan' + ]); + + WP_Mock::userFunction('esc_attr', [ + 'return' => 'sofa couch divan' + ]); + + $synonymsField = new SynonymsField($this->plugin); + $actual = $synonymsField->create($this->post); + + $this->assertEquals('sofa couch divan', $actual); + } +}