Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions __tests__/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function _manually_load_plugin() {
)
);

require dirname( dirname( __FILE__ ) ) . '/cron-control.php';
require_once dirname( dirname( __FILE__ ) ) . '/cron-control.php';

// Plugin loads after `wp_install()` is called, so we compensate.
if ( ! Cron_Control\Events_Store::is_installed() ) {
Expand All @@ -46,20 +46,20 @@ function _manually_load_plugin() {
require_once __DIR__ . '/utils.php';

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
require_once $_tests_dir . '/includes/bootstrap.php';

// Setup WP-CLI dependencies.
if ( ! defined( 'WP_CLI_ROOT' ) ) {
define( 'WP_CLI_ROOT', __DIR__ . '/../vendor/wp-cli/wp-cli' );
}

include WP_CLI_ROOT . '/php/utils.php';
include WP_CLI_ROOT . '/php/dispatcher.php';
include WP_CLI_ROOT . '/php/class-wp-cli.php';
include WP_CLI_ROOT . '/php/class-wp-cli-command.php';
include_once WP_CLI_ROOT . '/php/utils.php';
include_once WP_CLI_ROOT . '/php/dispatcher.php';
include_once WP_CLI_ROOT . '/php/class-wp-cli.php';
include_once WP_CLI_ROOT . '/php/class-wp-cli-command.php';

\WP_CLI\Utils\load_dependencies();

// WP_CLI wasn't defined during plugin bootup, so bootstrap our cli classes manually
require dirname( dirname( __FILE__ ) ) . '/includes/wp-cli.php';
require_once dirname( dirname( __FILE__ ) ) . '/includes/wp-cli.php';
Cron_Control\CLI\prepare_environment();
16 changes: 6 additions & 10 deletions __tests__/unit-tests/test-cli-orchestrate-sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
use WP_Site;

class Orchestrate_Sites_Tests extends \WP_UnitTestCase {
function setUp(): void {
public function setUp(): void {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Skipping tests that only run on multisites.' );
}

parent::setUp();
}

function tearDown(): void {
parent::tearDown();
}

function test_list_sites_removes_inactive_subsites() {
public function test_list_sites_removes_inactive_subsites() {
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );

// The archived/spam/deleted subsites should not be returned.
Expand All @@ -29,7 +25,7 @@ function test_list_sites_removes_inactive_subsites() {
remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
}

function test_list_sites_2_hosts() {
public function test_list_sites_2_hosts() {
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );

// With two hosts, all active sites should still be returned.
Expand All @@ -41,7 +37,7 @@ function test_list_sites_2_hosts() {
remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
}

function test_list_sites_7_hosts() {
public function test_list_sites_7_hosts() {
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );

// With seven hosts, our current request should only be given two of the active sites.
Expand All @@ -53,7 +49,7 @@ function test_list_sites_7_hosts() {
remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
}

function mock_hosts_list( $number_of_hosts ) {
public function mock_hosts_list( $number_of_hosts ) {
// Always have the "current" host.
$heartbeats = [ gethostname() => time() ];

Expand All @@ -66,7 +62,7 @@ function mock_hosts_list( $number_of_hosts ) {
wp_cache_set( CLI\Orchestrate_Sites::RUNNER_HOST_HEARTBEAT_KEY, $heartbeats );
}

function mock_get_sites( $site_data, $query_class ) {
public function mock_get_sites( $site_data, $query_class ) {
if ( $query_class->query_vars['count'] ) {
return 7;
}
Expand Down
26 changes: 13 additions & 13 deletions __tests__/unit-tests/test-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use WP_Error;

class Event_Tests extends \WP_UnitTestCase {
function setUp(): void {
public function setUp(): void {
parent::setUp();
Utils::clear_cron_table();
}

function tearDown(): void {
public function tearDown(): void {
Utils::clear_cron_table();
parent::tearDown();
}

function test_run() {
public function test_run() {
$called = 0;
add_action( 'test_run_event_action', function () use ( &$called ) {
$called++;
Expand All @@ -30,7 +30,7 @@ function test_run() {
$this->assertEquals( 1, $called, 'event callback was triggered once' );
}

function test_complete() {
public function test_complete() {
// Mock up an event, but try to complete it before saving.
$event = new Event();
$event->set_action( 'test_complete' );
Expand All @@ -47,7 +47,7 @@ function test_complete() {
$this->assertNotEquals( Event::create_instance_hash( [ 'test', 'args' ] ), $event->get_instance(), 'the instance was updated/randomized' );
}

function test_reschedule() {
public function test_reschedule() {
// Try to reschedule a non-recurring event.
$event = new Event();
$event->set_action( 'test_reschedule' );
Expand All @@ -70,10 +70,10 @@ function test_reschedule() {
$result = $event->reschedule();
$this->assertTrue( $result, 'event was successfully rescheduled' );
$this->assertEquals( Events_Store::STATUS_PENDING, $event->get_status() );
$this->assertEquals( time() + HOUR_IN_SECONDS, $event->get_timestamp() );
$this->assertEqualsWithDelta( time() + HOUR_IN_SECONDS, $event->get_timestamp(), 1 );
}

function test_exists() {
public function test_exists() {
$event = new Event();
$event->set_action( 'test_exists' );
$event->set_timestamp( time() );
Expand All @@ -83,15 +83,15 @@ function test_exists() {
$this->assertTrue( $event->exists() );
}

function test_create_instance_hash() {
public function test_create_instance_hash() {
$empty_args = Event::create_instance_hash( [] );
$this->assertEquals( md5( serialize( [] ) ), $empty_args );

$has_args = Event::create_instance_hash( [ 'some', 'data' ] );
$this->assertEquals( md5( serialize( [ 'some', 'data' ] ) ), $has_args );
}

function test_get_wp_event_format() {
public function test_get_wp_event_format() {
$event = new Event();
$event->set_action( 'test_get_wp_event_format' );
$event->set_timestamp( 123 );
Expand All @@ -117,7 +117,7 @@ function test_get_wp_event_format() {
], $event->get_wp_event_format() );
}

function test_get() {
public function test_get() {
$test_event = new Event();
$test_event->set_action( 'test_get_action' );
$test_event->set_timestamp( 1637447875 );
Expand All @@ -132,7 +132,7 @@ function test_get() {
$this->assertNull( $event, 'could not find event by ID' );
}

function test_find() {
public function test_find() {
$test_event = new Event();
$test_event->set_action( 'test_find_action' );
$test_event->set_timestamp( 1637447876 );
Expand All @@ -147,7 +147,7 @@ function test_find() {
$this->assertNull( $event, 'could not find event by args' );
}

function test_validate_props() {
public function test_validate_props() {
// Invalid status.
$this->run_event_save_test( [
'creation' => [
Expand Down Expand Up @@ -218,7 +218,7 @@ function test_validate_props() {
}

// Run through various flows of event saving.
function test_event_save() {
public function test_event_save() {
// Create event w/ bare information to test the defaults.
// Then update the timestamp.
$this->run_event_save_test( [
Expand Down
22 changes: 11 additions & 11 deletions __tests__/unit-tests/test-events-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
use Automattic\WP\Cron_Control\Event;

class Events_Store_Tests extends \WP_UnitTestCase {
function setUp(): void {
public function setUp(): void {
parent::setUp();
Utils::clear_cron_table();
}

function tearDown(): void {
public function tearDown(): void {
Utils::clear_cron_table();
parent::tearDown();
}

function test_table_exists() {
public function test_table_exists() {
global $wpdb;

$table_name = Utils::get_table_name();
Expand All @@ -25,7 +25,7 @@ function test_table_exists() {
$this->assertTrue( Events_Store::is_installed() );
}

function test_event_creation() {
public function test_event_creation() {
$store = Events_Store::instance();

// We don't validate fields here, so not much to test other than return values.
Expand All @@ -43,7 +43,7 @@ function test_event_creation() {
$this->assertTrue( 0 === $empty_result, 'empty event was not inserted' );
}

function test_event_updates() {
public function test_event_updates() {
$store = Events_Store::instance();

// Make a valid event.
Expand All @@ -63,7 +63,7 @@ function test_event_updates() {
$this->assertFalse( $failed_result, 'event was not updated due to invalid args' );
}

function test_get_raw_event() {
public function test_get_raw_event() {
$store = Events_Store::instance();

$result = $store->_get_event_raw( -1 );
Expand Down Expand Up @@ -174,7 +174,7 @@ public function test_query_raw_events() {
$this->assertEquals( $event_two->get_timestamp(), $result[0]->timestamp, 'found the right event' );
$this->assertEquals( $event_three->get_timestamp(), $result[1]->timestamp, 'found the right event' );

$event_five = Utils::create_test_event( array_merge( $args, [ 'timestamp' => time() + 5 ] ) );
Utils::create_test_event( array_merge( $args, [ 'timestamp' => time() + 5 ] ) );

// Should find all but the last event that is not due yet.
$result = $store->_query_events_raw( [
Expand Down Expand Up @@ -207,10 +207,10 @@ public function test_query_raw_events() {
public function test_query_raw_events_orderby() {
$store = Events_Store::instance();

$event_one = Utils::create_test_event( [ 'timestamp' => 5, 'action' => 'test_query_raw_events_orderby' ] );
$event_two = Utils::create_test_event( [ 'timestamp' => 2, 'action' => 'test_query_raw_events_orderby' ] );
$event_three = Utils::create_test_event( [ 'timestamp' => 3, 'action' => 'test_query_raw_events_orderby' ] );
$event_four = Utils::create_test_event( [ 'timestamp' => 1, 'action' => 'test_query_raw_events_orderby' ] );
$event_one = Utils::create_test_event( [ 'timestamp' => 5, 'action' => 'test_query_raw_events_orderby' ] );
Utils::create_test_event( [ 'timestamp' => 2, 'action' => 'test_query_raw_events_orderby' ] );
Utils::create_test_event( [ 'timestamp' => 3, 'action' => 'test_query_raw_events_orderby' ] );
$event_four = Utils::create_test_event( [ 'timestamp' => 1, 'action' => 'test_query_raw_events_orderby' ] );

// Default orderby should be timestamp ASC
$result = $store->_query_events_raw();
Expand Down
12 changes: 6 additions & 6 deletions __tests__/unit-tests/test-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
use Automattic\WP\Cron_Control\Event;

class Events_Tests extends \WP_UnitTestCase {
function setUp(): void {
public function setUp(): void {
parent::setUp();
Utils::clear_cron_table();
}

function tearDown(): void {
public function tearDown(): void {
Utils::clear_cron_table();
parent::tearDown();
}

// The actual query functionality is largely tested in the data store already, so here we just ensure the returns are as expected.
function test_query() {
public function test_query() {
// Create 2 test events.
Utils::create_test_event( [ 'action' => 'test_query_action', 'args' => [ 'first' ], 'timestamp' => 1 ] );
Utils::create_test_event( [ 'action' => 'test_query_action', 'args' => [ 'second' ], 'timestamp' => 2 ] );
Expand All @@ -33,7 +33,7 @@ function test_query() {
$this->assertEquals( $events, [], 'Returns empty array when no results found' );
}

function test_format_events_for_wp() {
public function test_format_events_for_wp() {
$events = $this->create_test_events();

$expected_format = [
Expand Down Expand Up @@ -73,7 +73,7 @@ function test_format_events_for_wp() {
$this->assertEquals( $empty_formatted, [], 'Returns empty array when no events to format' );
}

function test_flatten_wp_events_array() {
public function test_flatten_wp_events_array() {
// Setup an events array the way WP gives it to us.
$events = $this->create_test_events();
$formatted = Events::format_events_for_wp( array_values( $events ) );
Expand Down Expand Up @@ -122,7 +122,7 @@ private function create_test_events() {
return $events;
}

function test_get_events() {
public function test_get_events() {
$events = Events::instance();

$test_events = $this->register_active_events_for_listing();
Expand Down
14 changes: 7 additions & 7 deletions __tests__/unit-tests/test-internal-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
use Automattic\WP\Cron_Control;

class Internal_Events_Tests extends \WP_UnitTestCase {
function setUp(): void {
public function setUp(): void {
parent::setUp();
Utils::clear_cron_table();
}

function tearDown(): void {
public function tearDown(): void {
Utils::clear_cron_table();
parent::tearDown();
}

function test_internal_events_are_scheduled() {
public function test_internal_events_are_scheduled() {
Cron_Control\Internal_Events::instance()->schedule_internal_events();
$scheduled_events = Cron_Control\Events::query( [ 'limit' => 100 ] );

Expand All @@ -28,7 +28,7 @@ function test_internal_events_are_scheduled() {
}
}

function test_migrate_legacy_cron_events() {
public function test_migrate_legacy_cron_events() {
global $wpdb;

// Ensure we start with an empty cron option.
Expand All @@ -43,7 +43,7 @@ function test_migrate_legacy_cron_events() {
$cron_array['version'] = 2;

// Put the legacy event directly into the cron option, avoiding our special filtering. @codingStandardsIgnoreLine
$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)", 'cron', serialize( $cron_array ), 'yes' ) );
$wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)", 'cron', serialize( $cron_array ), 'yes' ) );
wp_cache_delete( 'alloptions', 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'cron', 'options' );
Expand All @@ -62,7 +62,7 @@ function test_migrate_legacy_cron_events() {
$this->assertNull( $cron_row, 'cron option was deleted' );
}

function test_prune_duplicate_events() {
public function test_prune_duplicate_events() {
// We don't prune single events, even if duplicates.
$original_single_event = Utils::create_test_event( [ 'timestamp' => time(), 'action' => 'single_event', 'args' => [ 'same' ] ] );
$duplicate_single_event = Utils::create_test_event( [ 'timestamp' => time() + 100, 'action' => 'single_event', 'args' => [ 'same' ] ] );
Expand Down Expand Up @@ -108,7 +108,7 @@ function test_prune_duplicate_events() {
$this->assertEquals( $duplicate_recurring_2->get_status(), Cron_Control\Events_Store::STATUS_COMPLETED, 'duplicate recurring event 2 was marked as completed' );
}

function test_force_publish_missed_schedules() {
public function test_force_publish_missed_schedules() {
// Define the filter callback to override post status.
$future_insert_filter = function ( $data ) {
if ( 'publish' === $data['post_status'] ) {
Expand Down
6 changes: 5 additions & 1 deletion __tests__/unit-tests/test-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use WP_CRON_CONTROL_SECRET;

class REST_API_Tests extends \WP_UnitTestCase {
/** @var WP_REST_Server|null */
private $server = null;

public function setUp(): void {
parent::setUp();

Expand All @@ -19,9 +22,10 @@ public function setUp(): void {
Utils::clear_cron_table();
}

function tearDown(): void {
public function tearDown(): void {
global $wp_rest_server;
$wp_rest_server = null;
$this->server = null;

Utils::clear_cron_table();
parent::tearDown();
Expand Down
Loading
Loading