Skip to content

Commit

Permalink
(chore): add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinsiebel committed Feb 26, 2021
1 parent 85d113b commit 9935238
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}":
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions docs/restapi.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Rest API
# REST API

## Available endpoints

Expand Down Expand Up @@ -39,4 +39,4 @@ Parameters:

### Endpoint of searching

`https://url/wp-json/owc/openpub/v1/search`
`https://url/wp-json/owc/openpub/v1/search`
1 change: 0 additions & 1 deletion src/Base/RestAPI/ItemFields/CommentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 3 additions & 11 deletions src/Base/RestAPI/ItemFields/SynonymsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?: '';
}
}
40 changes: 34 additions & 6 deletions tests/Unit/Base/RestAPI/ItemFields/CommentFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []
]
]
]
Expand All @@ -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());
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Base/RestAPI/ItemFields/NotesFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace OWC\OpenPub\Tests\Base\RestAPI\ItemFields;

use Mockery as m;
use OWC\OpenPub\Base\Foundation\Config;
use OWC\OpenPub\Base\Foundation\Loader;
use OWC\OpenPub\Base\Foundation\Plugin;
use OWC\OpenPub\Base\RestAPI\ItemFields\NotesField;
use OWC\OpenPub\Tests\TestCase;
use WP_Mock;
use WP_Post;

class NotesFieldTest extends TestCase
{
protected $post;

protected $plugin;

protected function setUp(): void
{
WP_Mock::setUp();

$config = m::mock(Config::class);
$this->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);
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Base/RestAPI/ItemFields/SynonymsFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace OWC\OpenPub\Tests\Base\RestAPI\ItemFields;

use Mockery as m;
use OWC\OpenPub\Base\Foundation\Config;
use OWC\OpenPub\Base\Foundation\Loader;
use OWC\OpenPub\Base\Foundation\Plugin;
use OWC\OpenPub\Base\RestAPI\ItemFields\SynonymsField;
use OWC\OpenPub\Tests\TestCase;
use WP_Mock;
use WP_Post;

class SynonymsFieldTest extends TestCase
{
protected $post;

protected $plugin;

protected function setUp(): void
{
WP_Mock::setUp();

$config = m::mock(Config::class);
$this->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);
}
}

0 comments on commit 9935238

Please sign in to comment.