Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,41 @@ function dokan_truncate_table_data(): void {
'dokan_announcement',
'dokan_refund',
'dokan_vendor_balance',
'wc_orders', // WooCommerce HPOS orders table
'wc_order_addresses', // WooCommerce HPOS addresses table
'wc_order_operational_data', // WooCommerce HPOS operational data table
];
global $wpdb;
foreach ( $tables as $table_name ) {
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}{$table_name}" );
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . $table_name ) );
if ( $table_exists ) {
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}{$table_name}" );
}
}
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

// Enable WooCommerce HPOS for tests
tests_add_filter(
'muplugins_loaded', function () {
// Enable WooCommerce HPOS feature
add_filter(
'woocommerce_feature_enabled', function ( $enabled, $feature ) {
if ( 'custom_order_tables' === $feature ) {
return true;
}
return $enabled;
}, 10, 2
);

// Enable WooCommerce order data sync
add_filter( 'pre_option_woocommerce_custom_orders_table_enabled', '__return_true' );
add_filter( 'pre_option_woocommerce_custom_orders_table_data_sync_enabled', '__return_true' );
}, 1
);

function _manually_load_plugin() {
define( 'WC_TAX_ROUNDING_MODE', 'auto' );
define( 'WC_USE_TRANSACTIONS', false );
Expand All @@ -52,6 +77,26 @@ function install_wc() {

WC_Install::install();

// Enable HPOS and create custom order tables
update_option( 'woocommerce_custom_orders_table_enabled', 'yes' );
update_option( 'woocommerce_custom_orders_table_data_sync_enabled', 'yes' );

// Create all WooCommerce database tables including HPOS
WC_Install::create_tables();

// Create HPOS tables specifically
if ( class_exists( 'Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) ) {
$controller = wc_get_container()->get( 'Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' );
if ( method_exists( $controller, 'create_database_tables' ) ) {
$controller->create_database_tables();
}
}

// Create WooCommerce Analytics tables
if ( class_exists( 'Automattic\WooCommerce\Admin\Install' ) ) {
\Automattic\WooCommerce\Admin\Install::create_tables();
}

// WC()->init();

// Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374.
Expand All @@ -62,7 +107,7 @@ function install_wc() {
wp_roles();
}

echo esc_html( 'Installing WooCommerce...' . PHP_EOL );
echo esc_html( 'Installing WooCommerce with HPOS enabled...' . PHP_EOL );
}

/**
Expand Down
17 changes: 9 additions & 8 deletions tests/php/src/Analytics/Reports/OrderQueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* Class OrderStatsTest
* @group analytics
* @group analytics-order
*
* Unit tests for Order statistics in the Dokan plugin.
Expand Down Expand Up @@ -57,7 +58,7 @@ public function test_orders_hook_registered() {
public function test_filter_hooks_are_applied_for_orders_query() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -95,7 +96,7 @@ public function test_dokan_order_stats_fields_are_selected_for_seller1( $expecte

$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'should_filter_by_vendor_id',
Expand Down Expand Up @@ -152,7 +153,7 @@ public function test_dokan_order_stats_fields_are_selected_for_admin( $expected_

$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -190,7 +191,7 @@ public function test_orders_for_dokan_suborder_refund() {

$refund = $this->create_refund( $sub_ids[0] );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -221,7 +222,7 @@ public function test_orders_for_dokan_parent_order_refund() {

$parent_refund = $this->create_refund( $sub_ids[0], true, true );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -253,7 +254,7 @@ public function test_orders_for_dokan_single_order_refund() {

$refund = $this->create_refund( $order_id );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -281,7 +282,7 @@ public function test_orders_analytics_for_vendor_filter_as_a_admin() {
$order_id = $this->create_single_vendor_order( $this->seller_id1 );
$order_id2 = $this->create_single_vendor_order( $this->seller_id2 );

$this->run_all_pending();
$this->run_all_pending_queue();

$_GET['sellers'] = $this->seller_id1;

Expand All @@ -303,7 +304,7 @@ public function test_orders_analytics_for_vendor_filter_as_a_vendor() {
$order_id = $this->create_single_vendor_order( $this->seller_id1 );
$order_id2 = $this->create_single_vendor_order( $this->seller_id2 );

$this->run_all_pending();
$this->run_all_pending_queue();

// Ignore seller filter if a seller pass another seller ID as filter .
$_GET['seller'] = $this->seller_id1;
Expand Down
4 changes: 2 additions & 2 deletions tests/php/src/Analytics/Reports/OrderStatsQueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function test_order_stats_hook_registered() {
public function test_dokan_order_states_query_filter_hooks_are_order_stats_update() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -101,7 +101,7 @@ public function test_dokan_order_stats_added_to_wc_select_query_for_total( array
$this->set_mock_commission( $data['vendor_earning'], $data['admin_commission'] );
$parent_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$orders_query = new \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Query( [], 'orders-stats' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_dokan_order_states_update_hook_execute_on_order_stats_updat
->once()
->andReturn( 1 );

$this->run_all_pending();
$this->run_all_pending_queue();
}

/**
Expand All @@ -50,7 +50,7 @@ public function test_dokan_order_states_update_hook_execute_on_order_stats_updat
public function test_data_is_inserted_in_dokan_order_stats_table() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$wc_order_stats_table = \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::get_db_table_name();

Expand Down
16 changes: 8 additions & 8 deletions tests/php/src/Analytics/Reports/Orders/QueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function test_orders_hook_registered() {
public function test_filter_hooks_are_applied_for_orders_query() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -97,7 +97,7 @@ public function test_dokan_order_stats_fields_are_selected_for_seller( $expected

$this->set_order_meta_for_dokan( $order_id, $expected_data );

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'should_filter_by_vendor_id',
Expand Down Expand Up @@ -149,7 +149,7 @@ public function test_dokan_order_stats_fields_are_selected_for_admin( $expected_

$this->set_order_meta_for_dokan( $order_id, $expected_data );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -187,7 +187,7 @@ public function test_orders_for_dokan_suborder_refund() {

$refund = $this->create_refund( $sub_ids[0] );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

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

$parent_refund = $this->create_refund( $sub_ids[0], true, true );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -250,7 +250,7 @@ public function test_orders_for_dokan_single_order_refund() {

$refund = $this->create_refund( $order_id );

$this->run_all_pending();
$this->run_all_pending_queue();

remove_filter( 'woocommerce_analytics_clauses_where_orders_subquery', [ $this->sut, 'add_where_subquery' ], 30 );

Expand Down Expand Up @@ -278,7 +278,7 @@ public function test_orders_analytics_for_seller_filter_as_a_admin() {
$order_id = $this->create_single_vendor_order( $this->seller_id1 );
$order_id2 = $this->create_single_vendor_order( $this->seller_id2 );

$this->run_all_pending();
$this->run_all_pending_queue();

$_GET['sellers'] = $this->seller_id1;

Expand All @@ -300,7 +300,7 @@ public function test_orders_analytics_for_seller_filter_as_a_vendor() {
$order_id = $this->create_single_vendor_order( $this->seller_id1 );
$order_id2 = $this->create_single_vendor_order( $this->seller_id2 );

$this->run_all_pending();
$this->run_all_pending_queue();

// Ignore seller filter if a seller pass another seller ID as filter .
$_GET['seller'] = $this->seller_id1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function test_order_stats_hook_registered() {
public function test_dokan_order_states_query_filter_hooks_are_order_stats_update() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -97,7 +97,7 @@ public function test_dokan_order_stats_added_to_wc_select_query_for_seller( arra
$this->set_mock_commission( $data['vendor_earning'], $data['admin_commission'] );
$parent_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$filter = Mockery::mock( QueryFilter::class . '[should_filter_by_vendor_id]' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function test_dokan_order_states_update_hook_execute_on_order_stats_updat
->once()
->andReturn( 1 );

$this->run_all_pending();
$this->run_all_pending_queue();
}

/**
Expand All @@ -53,7 +53,7 @@ public function test_dokan_order_states_update_hook_execute_on_order_stats_updat
public function test_data_is_inserted_in_dokan_order_stats_table() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$wc_order_stats_table = \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::get_db_table_name();

Expand Down
6 changes: 3 additions & 3 deletions tests/php/src/Analytics/Reports/ProductQueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_products_hook_registered() {
public function test_filter_hooks_are_applied_for_products_query() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -88,7 +88,7 @@ function ( $clauses ) {
public function test_dokan_products_fields_are_selected_for_seller() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'should_filter_by_vendor_id',
Expand Down Expand Up @@ -134,7 +134,7 @@ public function test_dokan_products_fields_are_selected_for_seller() {
public function test_dokan_products_fields_are_selected_for_admin() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'should_filter_by_vendor_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function tearDown(): void {
public function test_products_stats_hook_registered() {
$order_stats_query_filter = dokan_get_container()->get( QueryFilter::class );
$order_stats_query_filter->register_hooks();

// Assert the Join Clause filters are registered
self::assertNotFalse( has_filter( 'woocommerce_analytics_clauses_join_products_stats_total', [ $order_stats_query_filter, 'add_join_subquery' ] ) );
self::assertNotFalse( has_filter( 'woocommerce_analytics_clauses_join_products_stats_interval', [ $order_stats_query_filter, 'add_join_subquery' ] ) );
Expand All @@ -57,7 +57,7 @@ public function test_products_stats_hook_registered() {
public function test_dokan_products_states_query_filter_hooks_are_order_stats_update() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -91,7 +91,7 @@ function ( $clauses ) {
public function test_dokan_products_stats_added_to_wc_select_query_for_seller() {
$parent_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$filter = Mockery::mock( QueryFilter::class . '[should_filter_by_vendor_id]' );

Expand Down Expand Up @@ -134,7 +134,7 @@ public function test_dokan_products_stats_added_to_wc_select_query_for_seller()
public function test_dokan_products_stats_added_to_wc_select_query_for_admin() {
$parent_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$filter = Mockery::mock( QueryFilter::class . '[should_filter_by_vendor_id]' );

Expand Down
4 changes: 2 additions & 2 deletions tests/php/src/Analytics/Reports/Products/QueryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_products_hook_registered() {
public function test_filter_hooks_are_applied_for_products_query() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'add_join_subquery',
Expand Down Expand Up @@ -89,7 +89,7 @@ function ( $clauses ) {
public function test_dokan_products_fields_are_selected_for_seller() {
$order_id = $this->create_multi_vendor_order();

$this->run_all_pending();
$this->run_all_pending_queue();

$mocking_methods = [
'should_filter_by_vendor_id',
Expand Down
Loading
Loading