2017-02-15 19:34:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Classes WC_Deprecated_Filter_Hooks & WC_Deprecated_Action_Hooks.
|
|
|
|
* @package WooCommerce\Tests\Util
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Tests_Deprecated_Hooks.
|
|
|
|
*/
|
2017-02-15 19:34:14 +00:00
|
|
|
class WC_Tests_Deprecated_Hooks extends WC_Unit_Test_Case {
|
|
|
|
|
2021-03-09 12:50:24 +00:00
|
|
|
/**
|
|
|
|
* @var array Deprecated hook handlers.
|
|
|
|
*/
|
2017-02-15 19:34:14 +00:00
|
|
|
protected $handlers = array();
|
|
|
|
|
2017-02-15 21:50:25 +00:00
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Generic toggle value function that can be hooked to a filter for testing.
|
2021-03-09 12:50:24 +00:00
|
|
|
*
|
|
|
|
* @param bool/int $value Value to change.
|
|
|
|
*
|
|
|
|
* @return bool/int Changed value.
|
2017-02-15 21:50:25 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function toggle_value( $value ) {
|
2017-02-15 21:50:25 +00:00
|
|
|
return ! $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Generic toggle value function that can be hooked to an action for testing.
|
2021-03-09 12:50:24 +00:00
|
|
|
*
|
|
|
|
* @param bool/int $value Value to change.
|
2017-02-15 21:50:25 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function toggle_value_by_ref( &$value ) {
|
2017-02-15 21:50:25 +00:00
|
|
|
$value = ! $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Generic meta setting function that can be hooked to an action for testing.
|
2021-03-09 12:50:24 +00:00
|
|
|
*
|
|
|
|
* @param int $item1_id Post ID.
|
|
|
|
* @param int $item2_id Post ID.
|
2017-02-15 21:50:25 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function set_meta( $item1_id, $item2_id = false ) {
|
2017-02-15 21:50:25 +00:00
|
|
|
update_post_meta( $item1_id, 'wc_deprecated_hook_test_item1_meta', 1 );
|
|
|
|
if ( $item2_id ) {
|
|
|
|
update_post_meta( $item2_id, 'wc_deprecated_hook_test_item2_meta', 2 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 12:50:24 +00:00
|
|
|
/**
|
|
|
|
* Disable deprecation error trigger for this class.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2017-02-15 19:34:14 +00:00
|
|
|
add_filter( 'deprecated_function_trigger_error', '__return_false' );
|
2017-11-22 19:00:02 +00:00
|
|
|
add_filter( 'deprecated_hook_trigger_error', '__return_false' );
|
2017-02-15 19:34:14 +00:00
|
|
|
$this->handlers = WC()->deprecated_hook_handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the deprecated hook handlers are initialized.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_deprecated_hook_handlers_exist() {
|
2017-02-15 19:34:14 +00:00
|
|
|
$this->assertArrayHasKey( 'filters', $this->handlers );
|
|
|
|
$this->assertInstanceOf( 'WC_Deprecated_Filter_Hooks', $this->handlers['filters'] );
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'actions', $this->handlers );
|
|
|
|
$this->assertInstanceOf( 'WC_Deprecated_Action_Hooks', $this->handlers['actions'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the get_old_hooks method.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_get_old_hooks() {
|
2017-02-15 19:34:14 +00:00
|
|
|
$old_filters = $this->handlers['filters']->get_old_hooks( 'woocommerce_structured_data_order' );
|
|
|
|
$old_actions = $this->handlers['actions']->get_old_hooks( 'woocommerce_new_order_item' );
|
|
|
|
|
|
|
|
$this->assertContains( 'woocommerce_email_order_schema_markup', $old_filters );
|
|
|
|
$this->assertContains( 'woocommerce_order_add_shipping', $old_actions );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the hook_in method.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_hook_in() {
|
2017-02-15 19:34:14 +00:00
|
|
|
$this->assertTrue( (bool) has_filter( 'woocommerce_structured_data_order', array( $this->handlers['filters'], 'maybe_handle_deprecated_hook' ) ) );
|
|
|
|
$this->assertTrue( (bool) has_action( 'woocommerce_new_order_item', array( $this->handlers['actions'], 'maybe_handle_deprecated_hook' ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the handle_deprecated_hook method in the filters handler.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_handle_deprecated_hook_filter() {
|
2017-02-15 19:34:14 +00:00
|
|
|
$new_hook = 'wc_new_hook';
|
|
|
|
$old_hook = 'wc_old_hook';
|
2019-05-01 22:05:00 +00:00
|
|
|
$args = array( false );
|
|
|
|
$return = -1;
|
2017-02-15 19:34:14 +00:00
|
|
|
|
2017-02-15 21:50:25 +00:00
|
|
|
add_filter( $old_hook, array( $this, 'toggle_value' ) );
|
2017-02-15 19:34:14 +00:00
|
|
|
|
|
|
|
$result = $this->handlers['filters']->handle_deprecated_hook( $new_hook, $old_hook, $args, $return );
|
|
|
|
$this->assertTrue( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the handle_deprecated_hook method in the actions handler.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_handle_deprecated_hook_action() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$new_hook = 'wc_new_hook';
|
|
|
|
$old_hook = 'wc_old_hook';
|
2017-02-15 19:34:14 +00:00
|
|
|
$test_value = false;
|
2019-05-01 22:05:00 +00:00
|
|
|
$args = array( &$test_value );
|
|
|
|
$return = -1;
|
2017-02-15 19:34:14 +00:00
|
|
|
|
2017-02-15 21:50:25 +00:00
|
|
|
add_filter( $old_hook, array( $this, 'toggle_value_by_ref' ) );
|
2017-02-15 19:34:14 +00:00
|
|
|
|
|
|
|
$this->handlers['actions']->handle_deprecated_hook( $new_hook, $old_hook, $args, $return );
|
|
|
|
$this->assertTrue( $test_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test a complete deprecated filter mapping.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_filter_handler() {
|
2017-02-15 19:34:14 +00:00
|
|
|
$test_width = 1;
|
|
|
|
|
2017-02-15 21:50:25 +00:00
|
|
|
add_filter( 'woocommerce_product_width', array( $this, 'toggle_value' ) );
|
2017-02-15 19:34:14 +00:00
|
|
|
|
|
|
|
$new_width = apply_filters( 'woocommerce_product_get_width', $test_width );
|
2017-02-15 21:50:25 +00:00
|
|
|
$this->assertEquals( 0, $new_width );
|
2017-02-15 19:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test a complete deprecated action mapping.
|
2017-02-15 19:34:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2017-02-15 19:34:14 +00:00
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_action_handler() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$test_product = WC_Helper_Product::create_simple_product();
|
|
|
|
$test_order = WC_Helper_Order::create_order( 1, $test_product );
|
2017-02-15 19:34:14 +00:00
|
|
|
$test_order_id = $test_order->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$test_items = $test_order->get_items();
|
|
|
|
$test_item = reset( $test_items );
|
|
|
|
$test_item_id = $test_item->get_id();
|
2017-02-15 19:34:14 +00:00
|
|
|
|
2021-03-09 12:50:24 +00:00
|
|
|
$this->setExpectedDeprecated( 'woocommerce_order_edit_product' );
|
2017-02-15 21:50:25 +00:00
|
|
|
add_action( 'woocommerce_order_edit_product', array( $this, 'set_meta' ), 10, 2 );
|
2017-02-15 19:34:14 +00:00
|
|
|
do_action( 'woocommerce_update_order_item', $test_item_id, $test_item, $test_order_id );
|
|
|
|
|
2017-02-15 21:50:25 +00:00
|
|
|
$order_update_worked = (bool) get_post_meta( $test_order_id, 'wc_deprecated_hook_test_item1_meta', 1 );
|
2019-05-01 22:05:00 +00:00
|
|
|
$item_update_worked = (bool) get_post_meta( $test_item_id, 'wc_deprecated_hook_test_item2_meta', 2 );
|
2017-02-15 21:50:25 +00:00
|
|
|
|
2017-02-15 19:34:14 +00:00
|
|
|
$this->assertTrue( $order_update_worked );
|
2017-02-15 20:07:35 +00:00
|
|
|
$this->assertTrue( $item_update_worked );
|
2017-02-15 19:34:14 +00:00
|
|
|
}
|
2017-03-28 18:05:45 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-28 18:15:00 +00:00
|
|
|
* Test the mapping of deprecated created_* hooks to new_* hooks.
|
2017-03-28 18:05:45 +00:00
|
|
|
*
|
|
|
|
* @since 3.0
|
|
|
|
*/
|
2021-03-09 12:50:24 +00:00
|
|
|
public function test_created_actions_deprecation() {
|
2017-03-28 18:05:45 +00:00
|
|
|
add_filter( 'woocommerce_payment_token_created', '__return_true' );
|
|
|
|
add_filter( 'woocommerce_create_product_variation', '__return_true' );
|
|
|
|
|
|
|
|
$token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ );
|
|
|
|
$token->save();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$product = new WC_Product_Variation();
|
2017-03-28 18:05:45 +00:00
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$this->assertEquals( 1, did_action( 'woocommerce_payment_token_created' ) );
|
|
|
|
$this->assertEquals( 1, did_action( 'woocommerce_new_payment_token' ) );
|
|
|
|
$this->assertEquals( 1, did_action( 'woocommerce_create_product_variation' ) );
|
|
|
|
$this->assertEquals( 1, did_action( 'woocommerce_new_product_variation' ) );
|
|
|
|
}
|
2017-02-15 19:34:14 +00:00
|
|
|
}
|