Skip to content

Commit

Permalink
Use tests_add_filter targetting only the orders table
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Dec 11, 2024
1 parent 28a17b8 commit d7f8c0b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
27 changes: 1 addition & 26 deletions tests/php/TestHealthCheckElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,6 @@
* Health check elasticsearch test class
*/
class TestHealthCheckElasticsearch extends WP_Ajax_UnitTestCase {
/**
* Set up the test case.
*
* @var obj
* @since 5.1.4
*/
public function set_up() {
parent::set_up();

remove_filter( 'query', [ $this, '_create_temporary_tables' ] );
remove_filter( 'query', [ $this, '_drop_temporary_tables' ] );
}

/**
* Clean up the test case.
*
* @var obj
* @since 5.1.4
*/
public function tear_down() {
parent::tear_down();

add_filter( 'query', [ $this, '_create_temporary_tables' ] );
add_filter( 'query', [ $this, '_drop_temporary_tables' ] );
}

/**
* Test if the test is registered
Expand All @@ -55,7 +30,7 @@ public function testIsRegistered() {
/**
* Test ajax output.
*/
public function testAjaxOutput_1() {
public function testAjaxOutput() {
$admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $admin_id );

Expand Down
41 changes: 40 additions & 1 deletion tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,48 @@ function skip_translations_api() {
'translations' => [],
];
}

tests_add_filter( 'translations_api', __NAMESPACE__ . '\skip_translations_api' );

/**
* Make sure the wc_orders table is NOT temporary
*
* As WC does a subselect with that table, it can not be temporary.
*
* @param string $query SQL Query
* @return string
*/
function create_wc_order_table( $query ) {
if ( ! str_contains( $query, 'wc_orders' ) ) {
return $query;
}

if ( ! str_starts_with( trim( $query ), 'CREATE TEMPORARY TABLE' ) ) {
return $query;
}

return substr_replace( trim( $query ), 'CREATE TABLE', 0, 22 );
}
tests_add_filter( 'query', __NAMESPACE__ . '\create_wc_order_table', 11 );

/**
* Complement the change applied in create_wc_order_table, so the table is dropped correctly
*
* @param string $query SQL Query
* @return string
*/
function drop_wc_order_table( $query ) {
if ( ! str_contains( $query, 'wc_orders' ) ) {
return $query;
}

if ( ! str_starts_with( trim( $query ), 'DROP TEMPORARY TABLE' ) ) {
return $query;
}

return substr_replace( trim( $query ), 'DROP TABLE', 0, 22 );
}
tests_add_filter( 'query', __NAMESPACE__ . '\drop_wc_order_table', 11 );

require_once $_tests_dir . '/includes/bootstrap.php';

require_once __DIR__ . '/includes/classes/factory/PostFactory.php';
Expand Down
16 changes: 0 additions & 16 deletions tests/php/includes/classes/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ public function set_up() {
\ElasticPress\setup_roles();

parent::set_up();

remove_filter( 'query', [ $this, '_create_temporary_tables' ] );
remove_filter( 'query', [ $this, '_drop_temporary_tables' ] );
}

/**
* Clean up the test case.
*
* @var obj
* @since 5.1.4
*/
public function tear_down() {
parent::tear_down();

add_filter( 'query', [ $this, '_create_temporary_tables' ] );
add_filter( 'query', [ $this, '_drop_temporary_tables' ] );
}

/**
Expand Down

0 comments on commit d7f8c0b

Please sign in to comment.