Skip to content

Commit

Permalink
Merge branch 'trunk' into phpcs/ActionScheduler_CanceledSchedule.php
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Oct 23, 2024
2 parents cd2e90e + d3c54dd commit c55e87d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 52 deletions.
14 changes: 10 additions & 4 deletions classes/abstracts/ActionScheduler_Abstract_Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
abstract class ActionScheduler_Abstract_Schema {

/**
* @var int Increment this value in derived class to trigger a schema update.
* Increment this value in derived class to trigger a schema update.
*
* @var int
*/
protected $schema_version = 1;

/**
* @var string Schema version stored in database.
* Schema version stored in database.
*
* @var string
*/
protected $db_version;

/**
* @var array Names of tables that will be registered by this class.
* Names of tables that will be registered by this class.
*
* @var array
*/
protected $tables = array();

Expand Down Expand Up @@ -137,7 +143,7 @@ private function update_table( $table ) {
$updated = dbDelta( $definition );
foreach ( $updated as $updated_table => $update_description ) {
if ( strpos( $update_description, 'Created table' ) === 0 ) {
do_action( 'action_scheduler/created_table', $updated_table, $table );
do_action( 'action_scheduler/created_table', $updated_table, $table ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions classes/abstracts/ActionScheduler_Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
*/
abstract class ActionScheduler_Lock {

/** @var ActionScheduler_Lock */
private static $locker = NULL;
/**
* Instance.
*
* @var ActionScheduler_Lock
*/
private static $locker = null;

/** @var int */
/**
* Duration of lock.
*
* @var int
*/
protected static $lock_duration = MINUTE_IN_SECONDS;

/**
Expand Down Expand Up @@ -52,11 +60,13 @@ protected function get_duration( $lock_type ) {
}

/**
* Get instance.
*
* @return ActionScheduler_Lock
*/
public static function instance() {
if ( empty( self::$locker ) ) {
$class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' );
$class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' );
self::$locker = new $class();
}
return self::$locker;
Expand Down
60 changes: 41 additions & 19 deletions classes/abstracts/ActionScheduler_Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Class ActionScheduler_Store
*
* @codeCoverageIgnore
*/
abstract class ActionScheduler_Store extends ActionScheduler_Store_Deprecated {
Expand All @@ -12,21 +13,31 @@ abstract class ActionScheduler_Store extends ActionScheduler_Store_Deprecated {
const STATUS_CANCELED = 'canceled';
const DEFAULT_CLASS = 'ActionScheduler_wpPostStore';

/** @var ActionScheduler_Store */
private static $store = NULL;
/**
* ActionScheduler_Store instance.
*
* @var ActionScheduler_Store
*/
private static $store = null;

/** @var int */
/**
* Maximum length of args.
*
* @var int
*/
protected static $max_args_length = 191;

/**
* Save action.
*
* @param ActionScheduler_Action $action Action to save.
* @param null|DateTime $scheduled_date Optional Date of the first instance
* to store. Otherwise uses the first date of the action's
* schedule.
*
* @return int The action ID
*/
abstract public function save_action( ActionScheduler_Action $action, DateTime $scheduled_date = NULL );
abstract public function save_action( ActionScheduler_Action $action, DateTime $scheduled_date = null );

/**
* Get action.
Expand Down Expand Up @@ -143,10 +154,13 @@ abstract public function action_counts();
public function extra_action_counts() {
$extra_actions = array();

$pastdue_action_counts = (int) $this->query_actions( array(
'status' => self::STATUS_PENDING,
'date' => as_get_datetime_object(),
), 'count' );
$pastdue_action_counts = (int) $this->query_actions(
array(
'status' => self::STATUS_PENDING,
'date' => as_get_datetime_object(),
),
'count'
);

if ( $pastdue_action_counts ) {
$extra_actions['past-due'] = $pastdue_action_counts;
Expand Down Expand Up @@ -270,9 +284,10 @@ abstract public function find_actions_by_claim_id( $claim_id );
* @return string
*/
protected function validate_sql_comparator( $comparison_operator ) {
if ( in_array( $comparison_operator, array('!=', '>', '>=', '<', '<=', '=') ) ) {
if ( in_array( $comparison_operator, array( '!=', '>', '>=', '<', '<=', '=' ), true ) ) {
return $comparison_operator;
}

return '=';
}

Expand All @@ -283,11 +298,13 @@ protected function validate_sql_comparator( $comparison_operator ) {
* @param null|DateTime $scheduled_date Action's schedule date (optional).
* @return string
*/
protected function get_scheduled_date_string( ActionScheduler_Action $action, DateTime $scheduled_date = NULL ) {
$next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date;
protected function get_scheduled_date_string( ActionScheduler_Action $action, DateTime $scheduled_date = null ) {
$next = is_null( $scheduled_date ) ? $action->get_schedule()->get_date() : $scheduled_date;

if ( ! $next ) {
$next = date_create();
}

$next->setTimezone( new DateTimeZone( 'UTC' ) );

return $next->format( 'Y-m-d H:i:s' );
Expand All @@ -300,8 +317,9 @@ protected function get_scheduled_date_string( ActionScheduler_Action $action, Da
* @param null|DateTime $scheduled_date Action's scheduled date (optional).
* @return string
*/
protected function get_scheduled_date_string_local( ActionScheduler_Action $action, DateTime $scheduled_date = NULL ) {
$next = null === $scheduled_date ? $action->get_schedule()->get_date() : $scheduled_date;
protected function get_scheduled_date_string_local( ActionScheduler_Action $action, DateTime $scheduled_date = null ) {
$next = is_null( $scheduled_date ) ? $action->get_schedule()->get_date() : $scheduled_date;

if ( ! $next ) {
$next = date_create();
}
Expand Down Expand Up @@ -448,11 +466,13 @@ public function get_status_labels() {
* @return string
*/
public function has_pending_actions_due() {
$pending_actions = $this->query_actions( array(
'date' => as_get_datetime_object(),
'status' => self::STATUS_PENDING,
'orderby' => 'none',
) );
$pending_actions = $this->query_actions(
array(
'date' => as_get_datetime_object(),
'status' => self::STATUS_PENDING,
'orderby' => 'none',
)
);

return ! empty( $pending_actions );
}
Expand All @@ -470,11 +490,13 @@ public function init() {}
public function mark_migrated( $action_id ) {}

/**
* Get instance.
*
* @return ActionScheduler_Store
*/
public static function instance() {
if ( empty( self::$store ) ) {
$class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS );
$class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS );
self::$store = new $class();
}
return self::$store;
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
</rule>

<rule ref="PEAR.NamingConventions.ValidClassName">
<exclude-pattern>classes/data-stores/ActionScheduler_wpPostStore.php</exclude-pattern>
<exclude-pattern>classes/ActionScheduler_wcSystemStatus.php</exclude-pattern>
<exclude-pattern>tests/phpunit/procedural_api/wc_get_scheduled_actions_Test.php</exclude-pattern>
</rule>

Expand Down
22 changes: 16 additions & 6 deletions tests/ActionScheduler_UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php
// phpcs:disable WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set

/**
* Class ActionScheduler_UnitTestCase
*/
class ActionScheduler_UnitTestCase extends WP_UnitTestCase {

/**
* Timezone string.
*
* @var string
*/
protected $existing_timezone;

/**
Expand All @@ -29,28 +35,32 @@ public function tear_down() {
* @return int
*/
public function count(): int {
return 'UTC' == date_default_timezone_get() ? 2 : 3;
return ( 'UTC' === date_default_timezone_get() ) ? 2 : 3;
}

/**
* We want to run every test multiple times using a different timezone to make sure
* that they are unaffected by changes to PHP's timezone.
*
* @param null|\PHPUnit\Framework\TestResult $result Test result.
*/
public function run( PHPUnit\Framework\TestResult $result = NULL ): \PHPUnit\Framework\TestResult {
public function run( PHPUnit\Framework\TestResult $result = null ): \PHPUnit\Framework\TestResult {

if ($result === NULL) {
if ( is_null( $result ) ) {
$result = $this->createResult();
}

if ( 'UTC' != ( $this->existing_timezone = date_default_timezone_get() ) ) {
$this->existing_timezone = date_default_timezone_get();

if ( 'UTC' !== $this->existing_timezone ) {
date_default_timezone_set( 'UTC' );
$result->run( $this );
}

date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12
date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12.
$result->run( $this );

date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place
date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place.
$result->run( $this );

date_default_timezone_set( $this->existing_timezone );
Expand Down
41 changes: 22 additions & 19 deletions tests/phpunit/migration/Runner_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Runner_Test extends ActionScheduler_UnitTestCase {
public function setUp() {
parent::setUp();
if ( ! taxonomy_exists( PostStore::GROUP_TAXONOMY ) ) {
// register the post type and taxonomy necessary for the store to work
// register the post type and taxonomy necessary for the store to work.
$store = new PostStore();
$store->init();
}
Expand All @@ -34,57 +34,60 @@ public function test_migrate_batches() {

$runner = new Runner( $config );

$due = [];
$future = [];
$complete = [];
$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, [], $schedule );
$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, [], $schedule );
$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, [], $schedule );
$action = new ActionScheduler_FinishedAction( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
$complete[] = $source_store->save_action( $action );
}

$created = $source_store->query_actions( [ 'per_page' => 0 ] );
$created = $source_store->query_actions( array( 'per_page' => 0 ) );
$this->assertCount( 15, $created );

$runner->run( 10 );

// due actions should migrate in the first batch
$migrated = $destination_store->query_actions( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
$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( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
$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( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
// pending actions should migrate in the second batch.
$migrated = $destination_store->query_actions( $args );
$this->assertCount( 10, $migrated );

$remaining = $source_store->query_actions( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
$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( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
// completed actions should migrate in the third batch.
$migrated = $destination_store->query_actions( $args );
$this->assertCount( 15, $migrated );

$remaining = $source_store->query_actions( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] );
$remaining = $source_store->query_actions( $args );
$this->assertCount( 0, $remaining );

}
Expand Down

0 comments on commit c55e87d

Please sign in to comment.