Merge commit 'd435f549fe9bbfbea64ed9be36104e7a23f9603c' as 'libraries/action-scheduler'
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\Migration\ActionMigrator;
|
||||
use Action_Scheduler\Migration\LogMigrator;
|
||||
|
||||
/**
|
||||
* Class ActionMigrator_Test
|
||||
* @group migration
|
||||
*/
|
||||
class ActionMigrator_Test extends ActionScheduler_UnitTestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
if ( ! taxonomy_exists( ActionScheduler_wpPostStore::GROUP_TAXONOMY ) ) {
|
||||
// register the post type and taxonomy necessary for the store to work.
|
||||
$store = new ActionScheduler_wpPostStore();
|
||||
$store->init();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_migrate_from_wpPost_to_db() {
|
||||
$source = new ActionScheduler_wpPostStore();
|
||||
$destination = new ActionScheduler_DBStore();
|
||||
$migrator = new ActionMigrator( $source, $destination, $this->get_log_migrator() );
|
||||
|
||||
$time = as_get_datetime_object();
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule, 'my_group' );
|
||||
$action_id = $source->save_action( $action );
|
||||
|
||||
$new_id = $migrator->migrate( $action_id );
|
||||
|
||||
// ensure we get the same record out of the new store as we stored in the old.
|
||||
$retrieved = $destination->fetch_action( $new_id );
|
||||
$this->assertEquals( $action->get_hook(), $retrieved->get_hook() );
|
||||
$this->assertEqualSets( $action->get_args(), $retrieved->get_args() );
|
||||
$this->assertEquals( $action->get_schedule()->get_date()->format( 'U' ), $retrieved->get_schedule()->get_date()->format( 'U' ) );
|
||||
$this->assertEquals( $action->get_group(), $retrieved->get_group() );
|
||||
$this->assertEquals( \ActionScheduler_Store::STATUS_PENDING, $destination->get_status( $new_id ) );
|
||||
|
||||
// ensure that the record in the old store does not exist.
|
||||
$old_action = $source->fetch_action( $action_id );
|
||||
$this->assertInstanceOf( 'ActionScheduler_NullAction', $old_action );
|
||||
}
|
||||
|
||||
public function test_does_not_migrate_missing_action_from_wpPost_to_db() {
|
||||
$source = new ActionScheduler_wpPostStore();
|
||||
$destination = new ActionScheduler_DBStore();
|
||||
$migrator = new ActionMigrator( $source, $destination, $this->get_log_migrator() );
|
||||
|
||||
$action_id = wp_rand( 100, 100000 );
|
||||
|
||||
$new_id = $migrator->migrate( $action_id );
|
||||
$this->assertEquals( 0, $new_id );
|
||||
|
||||
// ensure we get the same record out of the new store as we stored in the old.
|
||||
$retrieved = $destination->fetch_action( $new_id );
|
||||
$this->assertInstanceOf( 'ActionScheduler_NullAction', $retrieved );
|
||||
}
|
||||
|
||||
public function test_migrate_completed_action_from_wpPost_to_db() {
|
||||
$source = new ActionScheduler_wpPostStore();
|
||||
$destination = new ActionScheduler_DBStore();
|
||||
$migrator = new ActionMigrator( $source, $destination, $this->get_log_migrator() );
|
||||
|
||||
$time = as_get_datetime_object();
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule, 'my_group' );
|
||||
$action_id = $source->save_action( $action );
|
||||
$source->mark_complete( $action_id );
|
||||
|
||||
$new_id = $migrator->migrate( $action_id );
|
||||
|
||||
// ensure we get the same record out of the new store as we stored in the old.
|
||||
$retrieved = $destination->fetch_action( $new_id );
|
||||
$this->assertEquals( $action->get_hook(), $retrieved->get_hook() );
|
||||
$this->assertEqualSets( $action->get_args(), $retrieved->get_args() );
|
||||
$this->assertEquals( $action->get_schedule()->get_date()->format( 'U' ), $retrieved->get_schedule()->get_date()->format( 'U' ) );
|
||||
$this->assertEquals( $action->get_group(), $retrieved->get_group() );
|
||||
$this->assertTrue( $retrieved->is_finished() );
|
||||
$this->assertEquals( \ActionScheduler_Store::STATUS_COMPLETE, $destination->get_status( $new_id ) );
|
||||
|
||||
// ensure that the record in the old store does not exist.
|
||||
$old_action = $source->fetch_action( $action_id );
|
||||
$this->assertInstanceOf( 'ActionScheduler_NullAction', $old_action );
|
||||
}
|
||||
|
||||
public function test_migrate_failed_action_from_wpPost_to_db() {
|
||||
$source = new ActionScheduler_wpPostStore();
|
||||
$destination = new ActionScheduler_DBStore();
|
||||
$migrator = new ActionMigrator( $source, $destination, $this->get_log_migrator() );
|
||||
|
||||
$time = as_get_datetime_object();
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule, 'my_group' );
|
||||
$action_id = $source->save_action( $action );
|
||||
$source->mark_failure( $action_id );
|
||||
|
||||
$new_id = $migrator->migrate( $action_id );
|
||||
|
||||
// ensure we get the same record out of the new store as we stored in the old.
|
||||
$retrieved = $destination->fetch_action( $new_id );
|
||||
$this->assertEquals( $action->get_hook(), $retrieved->get_hook() );
|
||||
$this->assertEqualSets( $action->get_args(), $retrieved->get_args() );
|
||||
$this->assertEquals( $action->get_schedule()->get_date()->format( 'U' ), $retrieved->get_schedule()->get_date()->format( 'U' ) );
|
||||
$this->assertEquals( $action->get_group(), $retrieved->get_group() );
|
||||
$this->assertTrue( $retrieved->is_finished() );
|
||||
$this->assertEquals( \ActionScheduler_Store::STATUS_FAILED, $destination->get_status( $new_id ) );
|
||||
|
||||
// ensure that the record in the old store does not exist.
|
||||
$old_action = $source->fetch_action( $action_id );
|
||||
$this->assertInstanceOf( 'ActionScheduler_NullAction', $old_action );
|
||||
}
|
||||
|
||||
public function test_migrate_canceled_action_from_wpPost_to_db() {
|
||||
$source = new ActionScheduler_wpPostStore();
|
||||
$destination = new ActionScheduler_DBStore();
|
||||
$migrator = new ActionMigrator( $source, $destination, $this->get_log_migrator() );
|
||||
|
||||
$time = as_get_datetime_object();
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule, 'my_group' );
|
||||
$action_id = $source->save_action( $action );
|
||||
$source->cancel_action( $action_id );
|
||||
|
||||
$new_id = $migrator->migrate( $action_id );
|
||||
|
||||
// ensure we get the same record out of the new store as we stored in the old.
|
||||
$retrieved = $destination->fetch_action( $new_id );
|
||||
$this->assertEquals( $action->get_hook(), $retrieved->get_hook() );
|
||||
$this->assertEqualSets( $action->get_args(), $retrieved->get_args() );
|
||||
$this->assertEquals( $action->get_schedule()->get_date()->format( 'U' ), $retrieved->get_schedule()->get_date()->format( 'U' ) );
|
||||
$this->assertEquals( $action->get_group(), $retrieved->get_group() );
|
||||
$this->assertTrue( $retrieved->is_finished() );
|
||||
$this->assertEquals( \ActionScheduler_Store::STATUS_CANCELED, $destination->get_status( $new_id ) );
|
||||
|
||||
// ensure that the record in the old store does not exist.
|
||||
$old_action = $source->fetch_action( $action_id );
|
||||
$this->assertInstanceOf( 'ActionScheduler_NullAction', $old_action );
|
||||
}
|
||||
|
||||
private function get_log_migrator() {
|
||||
return new LogMigrator( \ActionScheduler::logger(), new ActionScheduler_DBLogger() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\Migration\BatchFetcher;
|
||||
use ActionScheduler_wpPostStore as PostStore;
|
||||
|
||||
/**
|
||||
* Class BatchFetcher_Test
|
||||
* @group migration
|
||||
*/
|
||||
class BatchFetcher_Test extends ActionScheduler_UnitTestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
if ( ! taxonomy_exists( PostStore::GROUP_TAXONOMY ) ) {
|
||||
// register the post type and taxonomy necessary for the store to work.
|
||||
$store = new PostStore();
|
||||
$store->init();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_nothing_to_migrate() {
|
||||
$store = new PostStore();
|
||||
$batch_fetcher = new BatchFetcher( $store );
|
||||
|
||||
$actions = $batch_fetcher->fetch();
|
||||
$this->assertEmpty( $actions );
|
||||
}
|
||||
|
||||
public function test_get_due_before_future() {
|
||||
$store = new PostStore();
|
||||
$due = array();
|
||||
$future = array();
|
||||
|
||||
for ( $i = 0; $i < 5; $i ++ ) {
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$future[] = $store->save_action( $action );
|
||||
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$due[] = $store->save_action( $action );
|
||||
}
|
||||
|
||||
$batch_fetcher = new BatchFetcher( $store );
|
||||
|
||||
$actions = $batch_fetcher->fetch();
|
||||
|
||||
$this->assertEqualSets( $due, $actions );
|
||||
}
|
||||
|
||||
public function test_get_future_before_complete() {
|
||||
$store = new PostStore();
|
||||
$future = array();
|
||||
$complete = array();
|
||||
|
||||
for ( $i = 0; $i < 5; $i ++ ) {
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$future[] = $store->save_action( $action );
|
||||
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_FinishedAction( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$complete[] = $store->save_action( $action );
|
||||
}
|
||||
|
||||
$batch_fetcher = new BatchFetcher( $store );
|
||||
|
||||
$actions = $batch_fetcher->fetch();
|
||||
|
||||
$this->assertEqualSets( $future, $actions );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\Migration\Config;
|
||||
|
||||
/**
|
||||
* Class Config_Test
|
||||
* @group migration
|
||||
*/
|
||||
class Config_Test extends ActionScheduler_UnitTestCase {
|
||||
public function test_source_store_required() {
|
||||
$config = new Config();
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$config->get_source_store();
|
||||
}
|
||||
|
||||
public function test_source_logger_required() {
|
||||
$config = new Config();
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$config->get_source_logger();
|
||||
}
|
||||
|
||||
public function test_destination_store_required() {
|
||||
$config = new Config();
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$config->get_destination_store();
|
||||
}
|
||||
|
||||
public function test_destination_logger_required() {
|
||||
$config = new Config();
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$config->get_destination_logger();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* Contains tests for the Migration Controller.
|
||||
*
|
||||
* @package test_cases\migration
|
||||
*/
|
||||
|
||||
use ActionScheduler_StoreSchema as Schema;
|
||||
use Action_Scheduler\Migration\Controller;
|
||||
use Action_Scheduler\Migration\Scheduler;
|
||||
|
||||
/**
|
||||
* Test the migration controller.
|
||||
*
|
||||
* @group migration
|
||||
*/
|
||||
class Controller_Test extends ActionScheduler_UnitTestCase {
|
||||
/**
|
||||
* Test to ensure the Migration Controller will schedule the migration.
|
||||
*/
|
||||
public function test_schedules_migration() {
|
||||
as_unschedule_action( Scheduler::HOOK );
|
||||
Controller::instance()->schedule_migration();
|
||||
|
||||
$this->assertTrue(
|
||||
as_next_scheduled_action( Scheduler::HOOK ) > 0,
|
||||
'Confirm that the Migration Controller scheduled the migration.'
|
||||
);
|
||||
|
||||
as_unschedule_action( Scheduler::HOOK );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to ensure that if an essential table is missing, the Migration
|
||||
* Controller will not schedule a migration.
|
||||
*
|
||||
* @see https://github.com/woocommerce/action-scheduler/issues/653
|
||||
*/
|
||||
public function test_migration_not_scheduled_if_tables_are_missing() {
|
||||
as_unschedule_action( Scheduler::HOOK );
|
||||
$this->rename_claims_table();
|
||||
Controller::instance()->schedule_migration();
|
||||
|
||||
$this->assertFalse(
|
||||
as_next_scheduled_action( Scheduler::HOOK ),
|
||||
'When required tables are missing, the migration will not be scheduled.'
|
||||
);
|
||||
|
||||
$this->restore_claims_table_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename the claims table, so that it cannot be used by the library.
|
||||
*/
|
||||
private function rename_claims_table() {
|
||||
global $wpdb;
|
||||
$normal_table_name = $wpdb->prefix . Schema::CLAIMS_TABLE;
|
||||
$modified_table_name = $normal_table_name . 'x';
|
||||
|
||||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
$wpdb->query( "RENAME TABLE {$normal_table_name} TO {$modified_table_name}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the expected name of the claims table, so that it can be used by the library
|
||||
* and any further tests.
|
||||
*/
|
||||
private function restore_claims_table_name() {
|
||||
global $wpdb;
|
||||
$normal_table_name = $wpdb->prefix . Schema::CLAIMS_TABLE;
|
||||
$modified_table_name = $normal_table_name . 'x';
|
||||
|
||||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
$wpdb->query( "RENAME TABLE {$modified_table_name} TO {$normal_table_name}" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\Migration\LogMigrator;
|
||||
|
||||
/**
|
||||
* Class LogMigrator_Test
|
||||
* @group migration
|
||||
*/
|
||||
class LogMigrator_Test extends ActionScheduler_UnitTestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
if ( ! taxonomy_exists( ActionScheduler_wpPostStore::GROUP_TAXONOMY ) ) {
|
||||
// register the post type and taxonomy necessary for the store to work.
|
||||
$store = new ActionScheduler_wpPostStore();
|
||||
$store->init();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_migrate_from_wpComment_to_db() {
|
||||
$source = new ActionScheduler_wpCommentLogger();
|
||||
$destination = new ActionScheduler_DBLogger();
|
||||
$migrator = new LogMigrator( $source, $destination );
|
||||
$source_action_id = wp_rand( 10, 10000 );
|
||||
$destination_action_id = wp_rand( 10, 10000 );
|
||||
|
||||
$logs = array();
|
||||
for ( $i = 0; $i < 3; $i++ ) {
|
||||
for ( $j = 0; $j < 5; $j++ ) {
|
||||
$logs[ $i ][ $j ] = md5( wp_rand() );
|
||||
if ( 1 === $i ) {
|
||||
$source->log( $source_action_id, $logs[ $i ][ $j ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$migrator->migrate( $source_action_id, $destination_action_id );
|
||||
|
||||
$migrated = $destination->get_logs( $destination_action_id );
|
||||
$this->assertEqualSets(
|
||||
$logs[1],
|
||||
array_map(
|
||||
function( $log ) {
|
||||
return $log->get_message();
|
||||
},
|
||||
$migrated
|
||||
)
|
||||
);
|
||||
|
||||
// no API for deleting logs, so we leave them for manual cleanup later.
|
||||
$this->assertCount( 5, $source->get_logs( $source_action_id ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Action_Scheduler\Migration\Config;
|
||||
use Action_Scheduler\Migration\Runner;
|
||||
use ActionScheduler_wpCommentLogger as CommentLogger;
|
||||
use ActionScheduler_wpPostStore as PostStore;
|
||||
|
||||
/**
|
||||
* Class Runner_Test
|
||||
* @group migration
|
||||
*/
|
||||
class Runner_Test extends ActionScheduler_UnitTestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
if ( ! taxonomy_exists( PostStore::GROUP_TAXONOMY ) ) {
|
||||
// register the post type and taxonomy necessary for the store to work.
|
||||
$store = new PostStore();
|
||||
$store->init();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_migrate_batches() {
|
||||
$source_store = new PostStore();
|
||||
$destination_store = new ActionScheduler_DBStore();
|
||||
$source_logger = new CommentLogger();
|
||||
$destination_logger = new ActionScheduler_DBLogger();
|
||||
|
||||
$config = new Config();
|
||||
$config->set_source_store( $source_store );
|
||||
$config->set_source_logger( $source_logger );
|
||||
$config->set_destination_store( $destination_store );
|
||||
$config->set_destination_logger( $destination_logger );
|
||||
|
||||
$runner = new Runner( $config );
|
||||
|
||||
$due = array();
|
||||
$future = array();
|
||||
$complete = array();
|
||||
|
||||
for ( $i = 0; $i < 5; $i ++ ) {
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$future[] = $source_store->save_action( $action );
|
||||
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$due[] = $source_store->save_action( $action );
|
||||
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_FinishedAction( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$complete[] = $source_store->save_action( $action );
|
||||
}
|
||||
|
||||
$created = $source_store->query_actions( array( 'per_page' => 0 ) );
|
||||
$this->assertCount( 15, $created );
|
||||
|
||||
$runner->run( 10 );
|
||||
|
||||
$args = array(
|
||||
'per_page' => 0,
|
||||
'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK,
|
||||
);
|
||||
|
||||
// due actions should migrate in the first batch.
|
||||
$migrated = $destination_store->query_actions( $args );
|
||||
$this->assertCount( 5, $migrated );
|
||||
|
||||
$remaining = $source_store->query_actions( $args );
|
||||
$this->assertCount( 10, $remaining );
|
||||
|
||||
$runner->run( 10 );
|
||||
|
||||
// pending actions should migrate in the second batch.
|
||||
$migrated = $destination_store->query_actions( $args );
|
||||
$this->assertCount( 10, $migrated );
|
||||
|
||||
$remaining = $source_store->query_actions( $args );
|
||||
$this->assertCount( 5, $remaining );
|
||||
|
||||
$runner->run( 10 );
|
||||
|
||||
// completed actions should migrate in the third batch.
|
||||
$migrated = $destination_store->query_actions( $args );
|
||||
$this->assertCount( 15, $migrated );
|
||||
|
||||
$remaining = $source_store->query_actions( $args );
|
||||
$this->assertCount( 0, $remaining );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
use Action_Scheduler\Migration\Scheduler;
|
||||
use ActionScheduler_wpPostStore as PostStore;
|
||||
|
||||
/**
|
||||
* Class Scheduler_Test
|
||||
* @group migration
|
||||
*/
|
||||
class Scheduler_Test extends ActionScheduler_UnitTestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
if ( ! taxonomy_exists( PostStore::GROUP_TAXONOMY ) ) {
|
||||
// register the post type and taxonomy necessary for the store to work.
|
||||
$store = new PostStore();
|
||||
$store->init();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_migration_is_complete() {
|
||||
ActionScheduler_DataController::mark_migration_complete();
|
||||
$this->assertTrue( ActionScheduler_DataController::is_migration_complete() );
|
||||
}
|
||||
|
||||
public function test_migration_is_not_complete() {
|
||||
$this->assertFalse( ActionScheduler_DataController::is_migration_complete() );
|
||||
update_option( ActionScheduler_DataController::STATUS_FLAG, 'something_random' );
|
||||
$this->assertFalse( ActionScheduler_DataController::is_migration_complete() );
|
||||
}
|
||||
|
||||
public function test_migration_is_scheduled() {
|
||||
// Clear the any existing migration hooks that have already been setup.
|
||||
as_unschedule_all_actions( Scheduler::HOOK );
|
||||
|
||||
$scheduler = new Scheduler();
|
||||
$this->assertFalse(
|
||||
$scheduler->is_migration_scheduled(),
|
||||
'Migration is not automatically scheduled when a new ' . Scheduler::class . ' instance is created.'
|
||||
);
|
||||
|
||||
$scheduler->schedule_migration();
|
||||
$this->assertTrue(
|
||||
$scheduler->is_migration_scheduled(),
|
||||
'Migration is scheduled only after schedule_migration() has been called.'
|
||||
);
|
||||
}
|
||||
|
||||
public function test_scheduler_runs_migration() {
|
||||
$source_store = new PostStore();
|
||||
$destination_store = new ActionScheduler_DBStore();
|
||||
|
||||
$return_5 = function () {
|
||||
return 5;
|
||||
};
|
||||
add_filter( 'action_scheduler/migration_batch_size', $return_5 );
|
||||
|
||||
// Make sure successive migration actions are delayed so all actions aren't migrated at once on separate hooks.
|
||||
$return_60 = function () {
|
||||
return 60;
|
||||
};
|
||||
add_filter( 'action_scheduler/migration_interval', $return_60 );
|
||||
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$future[] = $source_store->save_action( $action );
|
||||
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$due[] = $source_store->save_action( $action );
|
||||
}
|
||||
|
||||
$this->assertCount( 20, $source_store->query_actions( array( 'per_page' => 0 ) ) );
|
||||
|
||||
$scheduler = new Scheduler();
|
||||
$scheduler->unschedule_migration();
|
||||
$scheduler->schedule_migration( time() - 1 );
|
||||
|
||||
$queue_runner = ActionScheduler_Mocker::get_queue_runner( $destination_store );
|
||||
$queue_runner->run();
|
||||
|
||||
// 5 actions should have moved from the source store when the queue runner triggered the migration action.
|
||||
$args = array(
|
||||
'per_page' => 0,
|
||||
'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK,
|
||||
);
|
||||
$this->assertCount( 15, $source_store->query_actions( $args ) );
|
||||
|
||||
remove_filter( 'action_scheduler/migration_batch_size', $return_5 );
|
||||
remove_filter( 'action_scheduler/migration_interval', $return_60 );
|
||||
}
|
||||
|
||||
public function test_scheduler_marks_itself_complete() {
|
||||
$source_store = new PostStore();
|
||||
$destination_store = new ActionScheduler_DBStore();
|
||||
|
||||
for ( $i = 0; $i < 5; $i ++ ) {
|
||||
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
|
||||
$schedule = new ActionScheduler_SimpleSchedule( $time );
|
||||
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
|
||||
$due[] = $source_store->save_action( $action );
|
||||
}
|
||||
|
||||
$this->assertCount( 5, $source_store->query_actions( array( 'per_page' => 0 ) ) );
|
||||
|
||||
$scheduler = new Scheduler();
|
||||
$scheduler->unschedule_migration();
|
||||
$scheduler->schedule_migration( time() - 1 );
|
||||
|
||||
$queue_runner = ActionScheduler_Mocker::get_queue_runner( $destination_store );
|
||||
$queue_runner->run();
|
||||
|
||||
// All actions should have moved from the source store when the queue runner triggered the migration action.
|
||||
$args = array(
|
||||
'per_page' => 0,
|
||||
'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK,
|
||||
);
|
||||
$this->assertCount( 0, $source_store->query_actions( $args ) );
|
||||
|
||||
// schedule another so we can get it to run immediately.
|
||||
$scheduler->unschedule_migration();
|
||||
$scheduler->schedule_migration( time() - 1 );
|
||||
|
||||
// run again so it knows that there's nothing left to process.
|
||||
$queue_runner->run();
|
||||
|
||||
$scheduler->unhook();
|
||||
|
||||
// ensure the flag is set marking migration as complete.
|
||||
$this->assertTrue( ActionScheduler_DataController::is_migration_complete() );
|
||||
|
||||
// ensure that another instance has not been scheduled.
|
||||
$this->assertFalse( $scheduler->is_migration_scheduled() );
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user