Skip to content

Commit

Permalink
Adds unit tests for REST endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dsawardekar committed Dec 3, 2022
1 parent 134041c commit cdabb10
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
5 changes: 2 additions & 3 deletions includes/classes/RESTSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RESTSupport {
* Registers with WordPress REST api.
*/
public function register() {
add_action( 'rest_api_init', [ $this, 'register_endpoint' ] );
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
}

/**
Expand All @@ -31,7 +31,7 @@ public function can_register() {
/**
* Registes the REST endpoint with WP.
*/
public function register_endpoint() {
public function register_endpoints() {
register_rest_route(
'block-catalog/v1',
'/posts/',
Expand Down Expand Up @@ -108,7 +108,6 @@ public function register_endpoint() {
* @return array
*/
public function get_terms() {

$term_opts = [
'taxonomy' => BLOCK_CATALOG_TAXONOMY,
'hide_empty' => false,
Expand Down
125 changes: 125 additions & 0 deletions tests/classes/RESTSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace BlockCatalog;

class RESTSupportTest extends \WP_UnitTestCase {

public $rest;

function setUp() {
parent::setUp();


// Initiating the REST API.
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server();
do_action('rest_api_init');

$this->builder = new CatalogBuilder();
$this->rest = new RESTSupport();
}

function tearDown() {
parent::tearDown();

global $wp_rest_server;
$wp_rest_server = null;
}

function test_it_can_register_endpoints() {
$this->rest->register_endpoints();

$endpoints = $this->server->get_routes();
$this->assertArrayHasKey( '/block-catalog/v1/posts', $endpoints );
$this->assertArrayHasKey( '/block-catalog/v1/index', $endpoints );
$this->assertArrayHasKey( '/block-catalog/v1/terms', $endpoints );
$this->assertArrayHasKey( '/block-catalog/v1/delete-index', $endpoints );
}

function test_it_can_load_all_block_catalog_terms() {
$content = file_get_contents( FIXTURES_DIR . '/nested-blocks.html' );
$post_id = $this->factory->post->create( [ 'post_content' => $content ] );

$this->builder->catalog( $post_id );

$actual = $this->rest->get_terms()['terms'];
$actual = array_column( $actual, 'name' );

$this->assertContains( 'Column', $actual );
$this->assertContains( 'Column', $actual );
$this->assertContains( 'Columns', $actual );
$this->assertContains( 'List', $actual );
$this->assertContains( 'List item', $actual );
$this->assertContains( 'Paragraph', $actual );
$this->assertContains( 'Quote', $actual );
$this->assertContains( 'Core', $actual );
}

function test_it_can_get_all_posts_to_be_indexed() {
$total = 5;
$post_ids = [];

for ( $i = 0; $i < $total; $i++ ) {
$content = file_get_contents( FIXTURES_DIR . '/nested-blocks.html' );
$post_id = $this->factory->post->create( [ 'post_content' => $content ] );

$this->builder->catalog( $post_id );

$post_ids[] = $post_id;
}

$request = new \WP_REST_Request( 'POST', '/block-catalog/v1/posts' );
$actual = $this->rest->get_posts( $request )['posts'];

$this->assertEquals( $total, count( $actual ) );
$this->assertEmpty( array_diff( $post_ids, $actual ) );
}

function test_it_can_index_posts_over_rest() {
$total = 5;
$post_ids = [];

for ( $i = 0; $i < $total; $i++ ) {
$content = file_get_contents( FIXTURES_DIR . '/nested-blocks.html' );
$post_id = $this->factory->post->create( [ 'post_content' => $content ] );

$post_ids[] = $post_id;
}

$request = new \WP_REST_Request( 'POST', '/block-catalog/v1/index' );
$request->set_param( 'post_ids', $post_ids );

$actual = $this->rest->index( $request );

$this->assertEquals( 10, $actual['updated'] );
$this->assertEquals( 0, $actual['errors'] );
}

function test_it_delete_index_over_rest() {
$total = 5;
$post_ids = [];

for ( $i = 0; $i < $total; $i++ ) {
$content = file_get_contents( FIXTURES_DIR . '/nested-blocks.html' );
$post_id = $this->factory->post->create( [ 'post_content' => $content ] );

$post_ids[] = $post_id;
}

$request = new \WP_REST_Request( 'POST', '/block-catalog/v1/index' );
$request->set_param( 'post_ids', $post_ids );

$actual = $this->rest->index( $request );

$term_ids = get_terms( [ 'taxonomy' => BLOCK_CATALOG_TAXONOMY, 'fields' => 'ids' ] );

$request = new \WP_REST_Request( 'POST', '/block-catalog/v1/delete-index' );
$request->set_param( 'term_ids', $term_ids );

$actual = $this->rest->delete_index( $request );

$this->assertEquals( 7, $actual['removed'] );
$this->assertEquals( 0, $actual['errors'] );
}

}

0 comments on commit cdabb10

Please sign in to comment.