2019-01-12 01:20:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Generation Batch Queue Tests
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\Reports
|
2019-01-12 01:20:08 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
|
2022-02-17 18:48:31 +00:00
|
|
|
use Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler;
|
|
|
|
use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
|
2019-08-01 22:06:00 +00:00
|
|
|
|
2019-01-12 01:20:08 +00:00
|
|
|
/**
|
|
|
|
* Reports Generation Batch Queue Test Class
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\Reports
|
2019-01-12 01:20:08 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
|
|
* Queue batch size.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2019-06-26 20:16:33 +00:00
|
|
|
public $queue_batch_size = 100;
|
2019-01-12 01:20:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Customers batch size.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $customers_batch_size = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force known values for batch size.
|
|
|
|
*
|
|
|
|
* @param int $batch_size Batch size.
|
2020-01-02 16:00:37 +00:00
|
|
|
* @param string $scheduler_name Scheduler name.
|
2019-01-12 01:20:08 +00:00
|
|
|
* @param string $action Action.
|
|
|
|
* @return int
|
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public function filter_batch_size( $batch_size, $scheduler_name, $action ) {
|
|
|
|
switch ( "{$scheduler_name}_{$action}" ) {
|
|
|
|
case 'customers_queue_batches':
|
|
|
|
case 'orders_queue_batches':
|
2019-01-12 01:20:08 +00:00
|
|
|
return $this->queue_batch_size;
|
2020-01-02 16:00:37 +00:00
|
|
|
case 'customers_import_batch':
|
2019-01-12 01:20:08 +00:00
|
|
|
return $this->customers_batch_size;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->queue = new WC_Admin_Test_Action_Queue();
|
2020-01-02 16:00:37 +00:00
|
|
|
CustomersScheduler::set_queue( $this->queue );
|
|
|
|
OrdersScheduler::set_queue( $this->queue );
|
|
|
|
add_filter( 'woocommerce_admin_scheduler_batch_size', array( $this, 'filter_batch_size' ), 10, 3 );
|
2019-01-12 01:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down.
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2020-01-02 16:00:37 +00:00
|
|
|
CustomersScheduler::set_queue( null );
|
|
|
|
OrdersScheduler::set_queue( null );
|
2019-01-12 01:20:08 +00:00
|
|
|
$this->queue->actions = array();
|
2020-01-02 16:00:37 +00:00
|
|
|
remove_filter( 'woocommerce_admin_scheduler_batch_size', array( $this, 'filter_batch_size' ), 10, 3 );
|
2019-01-12 01:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that large batches get split properly.
|
|
|
|
*/
|
|
|
|
public function test_queue_batches_splits_into_batches_correctly() {
|
|
|
|
$num_customers = 1234; // 1234 / 5 = 247 batches
|
2019-06-26 20:23:56 +00:00
|
|
|
$num_batches = (int) ceil( $num_customers / $this->customers_batch_size );
|
|
|
|
$chunk_size = (int) ceil( $num_batches / $this->queue_batch_size );
|
|
|
|
$num_chunks = (int) ceil( $num_batches / $chunk_size );
|
2019-01-12 01:20:08 +00:00
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
CustomersScheduler::queue_batches( 1, $num_batches, 'import_batch' );
|
2019-01-12 01:20:08 +00:00
|
|
|
|
2019-06-26 20:23:56 +00:00
|
|
|
$this->assertCount( $num_chunks, $this->queue->actions );
|
2019-01-12 01:20:08 +00:00
|
|
|
$this->assertArraySubset(
|
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => CustomersScheduler::get_action( 'queue_batches' ),
|
|
|
|
'args' => array( 1, $chunk_size, 'import_batch' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
),
|
|
|
|
$this->queue->actions[0]
|
|
|
|
);
|
|
|
|
$this->assertArraySubset(
|
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => CustomersScheduler::get_action( 'queue_batches' ),
|
|
|
|
'args' => array( 247, 247, 'import_batch' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
),
|
2019-06-26 20:23:56 +00:00
|
|
|
$this->queue->actions[ $num_chunks - 1 ]
|
2019-01-12 01:20:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that small enough batches have their "single" action queued.
|
|
|
|
*/
|
|
|
|
public function test_queue_batches_schedules_single_actions() {
|
|
|
|
$num_customers = 45; // 45 / 5 = 9 batches (which is less than the batch queue size)
|
|
|
|
$num_batches = ceil( $num_customers / $this->customers_batch_size );
|
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
CustomersScheduler::queue_batches( 1, $num_batches, 'import_batch' );
|
2019-01-12 01:20:08 +00:00
|
|
|
|
|
|
|
$this->assertCount( 9, $this->queue->actions );
|
|
|
|
$this->assertArraySubset(
|
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => CustomersScheduler::get_action( 'import_batch' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
'args' => array( 1 ),
|
|
|
|
),
|
|
|
|
$this->queue->actions[0]
|
|
|
|
);
|
|
|
|
$this->assertArraySubset(
|
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => CustomersScheduler::get_action( 'import_batch' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
'args' => array( 9 ),
|
|
|
|
),
|
|
|
|
$this->queue->actions[8]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that batch dependencies work.
|
|
|
|
*/
|
|
|
|
public function test_queue_dependent_action() {
|
2020-01-02 16:00:37 +00:00
|
|
|
// Reset back to using a real queue.
|
|
|
|
CustomersScheduler::set_queue( null );
|
|
|
|
OrdersScheduler::set_queue( null );
|
|
|
|
|
|
|
|
// Schedule an action that depends on blocking job.
|
|
|
|
OrdersScheduler::schedule_action( 'import_batch_init', array( 1, false ) );
|
|
|
|
// Insert a blocking job.
|
|
|
|
CustomersScheduler::schedule_action( 'import_batch_init', array( 1, false ) );
|
|
|
|
// Verify that the action was properly blocked.
|
|
|
|
$this->assertCount(
|
|
|
|
1,
|
|
|
|
OrdersScheduler::queue()->search(
|
2019-01-12 01:20:08 +00:00
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => OrdersScheduler::get_action( 'import_batch_init' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2020-01-02 16:00:37 +00:00
|
|
|
// Verify that a second follow up action was queued.
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
2019-01-12 01:20:08 +00:00
|
|
|
$this->assertCount(
|
2020-01-02 16:00:37 +00:00
|
|
|
2,
|
|
|
|
OrdersScheduler::queue()->search(
|
2019-01-12 01:20:08 +00:00
|
|
|
array(
|
2020-01-02 16:00:37 +00:00
|
|
|
'hook' => OrdersScheduler::get_action( 'import_batch_init' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
// Queue an action that isn't blocked.
|
|
|
|
OrdersScheduler::schedule_action( 'import', array( 0 ) );
|
|
|
|
// Verify that the dependent action was queued.
|
2019-01-12 01:20:08 +00:00
|
|
|
$this->assertCount(
|
|
|
|
1,
|
2020-01-02 16:00:37 +00:00
|
|
|
OrdersScheduler::queue()->search(
|
2019-01-12 01:20:08 +00:00
|
|
|
array(
|
2020-03-12 13:46:13 +00:00
|
|
|
'status' => 'pending',
|
|
|
|
'hook' => OrdersScheduler::get_action( 'import' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2020-01-02 16:00:37 +00:00
|
|
|
// Verify that no follow up action was queued.
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
$this->assertCount(
|
2020-03-12 13:46:13 +00:00
|
|
|
0,
|
2020-01-02 16:00:37 +00:00
|
|
|
OrdersScheduler::queue()->search(
|
2019-01-12 01:20:08 +00:00
|
|
|
array(
|
2020-03-12 13:46:13 +00:00
|
|
|
'status' => 'pending',
|
|
|
|
'hook' => OrdersScheduler::get_action( 'import' ),
|
2019-01-12 01:20:08 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// clean up.
|
2020-01-02 16:00:37 +00:00
|
|
|
CustomersScheduler::clear_queued_actions();
|
|
|
|
OrdersScheduler::clear_queued_actions();
|
2019-01-12 01:20:08 +00:00
|
|
|
}
|
|
|
|
}
|