Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Jan 14, 2025
1 parent 06515f4 commit 256f100
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
30 changes: 27 additions & 3 deletions includes/classes/Feature/SearchTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function scripts() {
* Setup REST endpoints
*/
public function setup_endpoint() {
$controller = new \ElasticPressLabs\REST\SearchTemplates();
$controller = new \ElasticPressLabs\REST\SearchTemplates( $this );
$controller->register_routes();
}

Expand All @@ -191,21 +191,45 @@ public function admin_page() {
* @return void
*/
public function delete_all_search_templates() {
$response = \ElasticPress\Elasticsearch::factory()->remote_request( 'api/v1/search/posts/templates' );
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->get_search_templates_endpoint() );
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( ! empty( $response_body ) ) {
foreach ( $response_body as $index_name => $templates ) {
foreach ( $templates as $template ) {
\ElasticPress\Elasticsearch::factory()->remote_request(
'api/v1/search/posts/' . $index_name . '/template?template_name=' . $template,
$this->get_search_template_endpoint( $index_name ) . '?template_name=' . $template,
[ 'method' => 'DELETE' ]
);
}
}
}
}

/**
* EP.io search templates endpoint.
*
* @return string
*/
public function get_search_templates_endpoint(): string {
return 'api/v1/search/posts/templates';
}

/**
* EP.io (single) search template endpoint.
*
* @param null|string $index_name Index name.
* @return string
*/
public function get_search_template_endpoint( $index_name = null ): string {
if ( ! $index_name ) {
$index_name = \ElasticPress\Indexables::factory()->get( 'post' )->get_index_name();
}

return "api/v1/search/posts/{$index_name}/template";
}


/**
* Set the `settings_schema` attribute
*/
Expand Down
45 changes: 21 additions & 24 deletions includes/classes/REST/SearchTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@
namespace ElasticPressLabs\REST;

use ElasticPress\Utils;
use ElasticPressLabs\Feature\SearchTemplates as SearchTemplatesFeature;

/**
* Search Templates API controller class.
*/
class SearchTemplates {
/**
* The SearchTemplatesFeature instance.
*
* @var SearchTemplatesFeature
*/
protected $feature;

/**
* Class constructor
*
* @param SearchTemplatesFeature $feature The feature instance.
*/
public function __construct( SearchTemplatesFeature $feature ) {
$this->feature = $feature;
}

/**
* Register routes.
*
Expand Down Expand Up @@ -70,33 +87,13 @@ public function check_permission() {
return current_user_can( $capability );
}

/**
* EP.io search templates endpoint.
*
* @return string
*/
protected function get_search_templates_endpoint(): string {
return 'api/v1/search/posts/templates';
}

/**
* EP.io (single) search template endpoint.
*
* @return string
*/
protected function get_search_template_endpoint(): string {
$index_name = \ElasticPress\Indexables::factory()->get( 'post' )->get_index_name();

return "api/v1/search/posts/{$index_name}/template";
}

/**
* List search templates handler.
*
* @return array|\WP_Error
*/
public function get_search_templates() {
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->get_search_templates_endpoint() );
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $this->feature->get_search_templates_endpoint() );

if ( is_wp_error( $response ) ) {
return $response;
Expand All @@ -119,7 +116,7 @@ public function get_search_templates() {
* @return object|\WP_Error
*/
public function get_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request( $path );

if ( is_wp_error( $response ) ) {
Expand All @@ -140,7 +137,7 @@ public function get_search_template( \WP_REST_Request $request ) {
* @return object|\WP_Error
*/
public function update_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request(
$path,
[
Expand Down Expand Up @@ -175,7 +172,7 @@ public function update_search_template( \WP_REST_Request $request ) {
* @return object|\WP_Error
*/
public function delete_search_template( \WP_REST_Request $request ) {
$path = $this->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$path = $this->feature->get_search_template_endpoint() . '?template_name=' . $request['template_name'];
$response = \ElasticPress\Elasticsearch::factory()->remote_request(
$path,
[
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/REST/TestSearchTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestSearchTemplates extends \ElasticPressLabsTest\BaseTestCase {
public function set_up() {
parent::set_up();

$this->controller = new SearchTemplates();
$this->controller = new SearchTemplates( \ElasticPress\Features::factory()->get_registered_feature( 'search_templates' ) );
add_filter( 'ep_intercept_remote_request', '__return_true' );
}

Expand Down Expand Up @@ -218,7 +218,7 @@ public function test_update_search_template_invalid_status_code() {

$error = $this->controller->update_search_template( new \WP_REST_Request() );
$this->assertEquals( 500, $error->get_error_code() );
$this->assertEquals( 'Testing message', $error->get_error_message() );
$this->assertEquals( 'Testing body message', $error->get_error_message() );
}

/**
Expand Down Expand Up @@ -326,6 +326,7 @@ protected function send_invalid_http_status_code() {
'code' => 500,
'message' => 'Testing message',
],
'body' => 'Testing body message',

Check warning on line 329 in tests/phpunit/REST/TestSearchTemplates.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Array double arrow not aligned correctly; expected 5 space(s) between "'body'" and double arrow, but found 1.
];
};
add_filter( 'ep_do_intercept_request', $return_http_code );
Expand Down

0 comments on commit 256f100

Please sign in to comment.