2019-11-15 00:38:46 -05:00
<? php
2023-06-21 13:21:29 -04:00
2022-12-07 15:04:39 -07:00
/**
* Remove the "extensions" payload from GraphQL results
* so that tests can make assertions without worrying about what's in the extensions payload
*/
2020-11-20 10:29:10 -05:00
add_filter (
'graphql_request_results' ,
2023-11-30 15:41:32 +02:00
static function ( $response ) {
2020-11-20 10:29:10 -05:00
unset ( $response [ 'extensions' ] );
return $response ;
2022-12-07 15:04:39 -07:00
},
99
2020-11-20 10:29:10 -05:00
);
2023-06-21 13:21:29 -04:00
/**
* Helper method to drop custom tables if present.
*/
function delete_order_custom_tables () {
2023-12-22 18:06:12 -05:00
$features_controller = wc_get_container () -> get ( \Automattic\WooCommerce\Internal\Features\FeaturesController :: class );
2023-06-21 13:21:29 -04:00
$features_controller -> change_feature_enable ( 'custom_order_tables' , true );
$synchronizer = wc_get_container ()
2023-12-22 18:06:12 -05:00
-> get ( \Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer :: class );
2023-06-21 13:21:29 -04:00
if ( $synchronizer -> check_orders_table_exists () ) {
$synchronizer -> delete_database_tables ();
}
}
/**
* Enables or disables the custom orders table across WP temporarily.
*
* @param boolean $enabled TRUE to enable COT or FALSE to disable.
* @return void
*/
function toggle_cot ( bool $enabled ) {
2023-12-22 18:06:12 -05:00
$features_controller = wc_get_container () -> get ( \Automattic\WooCommerce\Internal\Features\FeaturesController :: class );
2023-06-21 13:21:29 -04:00
$features_controller -> change_feature_enable ( 'custom_order_tables' , $enabled );
2023-12-22 18:06:12 -05:00
update_option (
\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController :: CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION ,
wc_bool_to_string ( $enabled )
);
2023-06-21 13:21:29 -04:00
// Confirm things are really correct.
$wc_data_store = WC_Data_Store :: load ( 'order' );
2023-12-22 18:06:12 -05:00
assert (
is_a (
$wc_data_store -> get_current_class_name (),
\Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore :: class ,
true
) === $enabled
);
2023-06-21 13:21:29 -04:00
}
/**
* Helper method to create custom tables if not present.
*/
function create_order_custom_table_if_not_exist () {
2023-12-22 18:06:12 -05:00
$features_controller = wc_get_container () -> get ( \Automattic\WooCommerce\Internal\Features\FeaturesController :: class );
2023-06-21 13:21:29 -04:00
$features_controller -> change_feature_enable ( 'custom_order_tables' , true );
2026-03-30 21:42:00 -04:00
update_option ( \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController :: CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION , 'yes' );
wp_cache_flush ();
$wc_data_store = WC_Data_Store :: load ( 'order' );
assert ( is_a ( $wc_data_store -> get_current_class_name (), \Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore :: class , true ) === true , 'data store\'s classname is "' . $wc_data_store -> get_current_class_name () . '", but HPOS is enabled' );
2023-12-22 18:06:12 -05:00
$synchronizer = wc_get_container () -> get ( \Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer :: class );
2023-06-21 13:21:29 -04:00
if ( ! $synchronizer -> check_orders_table_exists () ) {
$synchronizer -> create_database_tables ();
}
}
/**
* Initialize HPOS if tests need to run in HPOS context.
*
* @return void
*/
function initialize_hpos () {
delete_order_custom_tables ();
create_order_custom_table_if_not_exist ();
toggle_cot ( true );
}
if ( defined ( 'HPOS' ) ) {
\codecept_debug ( 'HPOS activated!!!' );
2026-03-30 21:42:00 -04:00
initialize_hpos ();
2023-12-22 18:06:12 -05:00
//add_action( 'woocommerce_init', 'initialize_hpos' );
2023-06-21 13:21:29 -04:00
}
2026-03-26 17:20:55 -04:00
class WC_Unit_Tests_Bootstrap {}