Skip to content

Commit 59634cc

Browse files
authored
Merge pull request #400 from Automattic/fix/issues
fix: issues that cause tests to fail
2 parents 7e32816 + 1fd7f3a commit 59634cc

14 files changed

+446
-363
lines changed

__tests__/bootstrap.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function _manually_load_plugin() {
3232
)
3333
);
3434

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

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

4848
// Start up the WP testing environment.
49-
require $_tests_dir . '/includes/bootstrap.php';
49+
require_once $_tests_dir . '/includes/bootstrap.php';
5050

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

56-
include WP_CLI_ROOT . '/php/utils.php';
57-
include WP_CLI_ROOT . '/php/dispatcher.php';
58-
include WP_CLI_ROOT . '/php/class-wp-cli.php';
59-
include WP_CLI_ROOT . '/php/class-wp-cli-command.php';
56+
include_once WP_CLI_ROOT . '/php/utils.php';
57+
include_once WP_CLI_ROOT . '/php/dispatcher.php';
58+
include_once WP_CLI_ROOT . '/php/class-wp-cli.php';
59+
include_once WP_CLI_ROOT . '/php/class-wp-cli-command.php';
6060

6161
\WP_CLI\Utils\load_dependencies();
6262

6363
// WP_CLI wasn't defined during plugin bootup, so bootstrap our cli classes manually
64-
require dirname( dirname( __FILE__ ) ) . '/includes/wp-cli.php';
64+
require_once dirname( dirname( __FILE__ ) ) . '/includes/wp-cli.php';
6565
Cron_Control\CLI\prepare_environment();

__tests__/unit-tests/test-cli-orchestrate-sites.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
use WP_Site;
77

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

1414
parent::setUp();
1515
}
1616

17-
function tearDown(): void {
18-
parent::tearDown();
19-
}
20-
21-
function test_list_sites_removes_inactive_subsites() {
17+
public function test_list_sites_removes_inactive_subsites() {
2218
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
2319

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

32-
function test_list_sites_2_hosts() {
28+
public function test_list_sites_2_hosts() {
3329
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
3430

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

44-
function test_list_sites_7_hosts() {
40+
public function test_list_sites_7_hosts() {
4541
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
4642

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

56-
function mock_hosts_list( $number_of_hosts ) {
52+
public function mock_hosts_list( $number_of_hosts ) {
5753
// Always have the "current" host.
5854
$heartbeats = [ gethostname() => time() ];
5955

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

69-
function mock_get_sites( $site_data, $query_class ) {
65+
public function mock_get_sites( $site_data, $query_class ) {
7066
if ( $query_class->query_vars['count'] ) {
7167
return 7;
7268
}

__tests__/unit-tests/test-event.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use WP_Error;
88

99
class Event_Tests extends \WP_UnitTestCase {
10-
function setUp(): void {
10+
public function setUp(): void {
1111
parent::setUp();
1212
Utils::clear_cron_table();
1313
}
1414

15-
function tearDown(): void {
15+
public function tearDown(): void {
1616
Utils::clear_cron_table();
1717
parent::tearDown();
1818
}
1919

20-
function test_run() {
20+
public function test_run() {
2121
$called = 0;
2222
add_action( 'test_run_event_action', function () use ( &$called ) {
2323
$called++;
@@ -30,7 +30,7 @@ function test_run() {
3030
$this->assertEquals( 1, $called, 'event callback was triggered once' );
3131
}
3232

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

50-
function test_reschedule() {
50+
public function test_reschedule() {
5151
// Try to reschedule a non-recurring event.
5252
$event = new Event();
5353
$event->set_action( 'test_reschedule' );
@@ -70,10 +70,10 @@ function test_reschedule() {
7070
$result = $event->reschedule();
7171
$this->assertTrue( $result, 'event was successfully rescheduled' );
7272
$this->assertEquals( Events_Store::STATUS_PENDING, $event->get_status() );
73-
$this->assertEquals( time() + HOUR_IN_SECONDS, $event->get_timestamp() );
73+
$this->assertEqualsWithDelta( time() + HOUR_IN_SECONDS, $event->get_timestamp(), 1 );
7474
}
7575

76-
function test_exists() {
76+
public function test_exists() {
7777
$event = new Event();
7878
$event->set_action( 'test_exists' );
7979
$event->set_timestamp( time() );
@@ -83,15 +83,15 @@ function test_exists() {
8383
$this->assertTrue( $event->exists() );
8484
}
8585

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

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

94-
function test_get_wp_event_format() {
94+
public function test_get_wp_event_format() {
9595
$event = new Event();
9696
$event->set_action( 'test_get_wp_event_format' );
9797
$event->set_timestamp( 123 );
@@ -117,7 +117,7 @@ function test_get_wp_event_format() {
117117
], $event->get_wp_event_format() );
118118
}
119119

120-
function test_get() {
120+
public function test_get() {
121121
$test_event = new Event();
122122
$test_event->set_action( 'test_get_action' );
123123
$test_event->set_timestamp( 1637447875 );
@@ -132,7 +132,7 @@ function test_get() {
132132
$this->assertNull( $event, 'could not find event by ID' );
133133
}
134134

135-
function test_find() {
135+
public function test_find() {
136136
$test_event = new Event();
137137
$test_event->set_action( 'test_find_action' );
138138
$test_event->set_timestamp( 1637447876 );
@@ -147,7 +147,7 @@ function test_find() {
147147
$this->assertNull( $event, 'could not find event by args' );
148148
}
149149

150-
function test_validate_props() {
150+
public function test_validate_props() {
151151
// Invalid status.
152152
$this->run_event_save_test( [
153153
'creation' => [
@@ -218,7 +218,7 @@ function test_validate_props() {
218218
}
219219

220220
// Run through various flows of event saving.
221-
function test_event_save() {
221+
public function test_event_save() {
222222
// Create event w/ bare information to test the defaults.
223223
// Then update the timestamp.
224224
$this->run_event_save_test( [

__tests__/unit-tests/test-events-store.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
use Automattic\WP\Cron_Control\Event;
77

88
class Events_Store_Tests extends \WP_UnitTestCase {
9-
function setUp(): void {
9+
public function setUp(): void {
1010
parent::setUp();
1111
Utils::clear_cron_table();
1212
}
1313

14-
function tearDown(): void {
14+
public function tearDown(): void {
1515
Utils::clear_cron_table();
1616
parent::tearDown();
1717
}
1818

19-
function test_table_exists() {
19+
public function test_table_exists() {
2020
global $wpdb;
2121

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

28-
function test_event_creation() {
28+
public function test_event_creation() {
2929
$store = Events_Store::instance();
3030

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

46-
function test_event_updates() {
46+
public function test_event_updates() {
4747
$store = Events_Store::instance();
4848

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

66-
function test_get_raw_event() {
66+
public function test_get_raw_event() {
6767
$store = Events_Store::instance();
6868

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

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

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

210-
$event_one = Utils::create_test_event( [ 'timestamp' => 5, 'action' => 'test_query_raw_events_orderby' ] );
211-
$event_two = Utils::create_test_event( [ 'timestamp' => 2, 'action' => 'test_query_raw_events_orderby' ] );
212-
$event_three = Utils::create_test_event( [ 'timestamp' => 3, 'action' => 'test_query_raw_events_orderby' ] );
213-
$event_four = Utils::create_test_event( [ 'timestamp' => 1, 'action' => 'test_query_raw_events_orderby' ] );
210+
$event_one = Utils::create_test_event( [ 'timestamp' => 5, 'action' => 'test_query_raw_events_orderby' ] );
211+
Utils::create_test_event( [ 'timestamp' => 2, 'action' => 'test_query_raw_events_orderby' ] );
212+
Utils::create_test_event( [ 'timestamp' => 3, 'action' => 'test_query_raw_events_orderby' ] );
213+
$event_four = Utils::create_test_event( [ 'timestamp' => 1, 'action' => 'test_query_raw_events_orderby' ] );
214214

215215
// Default orderby should be timestamp ASC
216216
$result = $store->_query_events_raw();

__tests__/unit-tests/test-events.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
use Automattic\WP\Cron_Control\Event;
77

88
class Events_Tests extends \WP_UnitTestCase {
9-
function setUp(): void {
9+
public function setUp(): void {
1010
parent::setUp();
1111
Utils::clear_cron_table();
1212
}
1313

14-
function tearDown(): void {
14+
public function tearDown(): void {
1515
Utils::clear_cron_table();
1616
parent::tearDown();
1717
}
1818

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

36-
function test_format_events_for_wp() {
36+
public function test_format_events_for_wp() {
3737
$events = $this->create_test_events();
3838

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

76-
function test_flatten_wp_events_array() {
76+
public function test_flatten_wp_events_array() {
7777
// Setup an events array the way WP gives it to us.
7878
$events = $this->create_test_events();
7979
$formatted = Events::format_events_for_wp( array_values( $events ) );
@@ -122,7 +122,7 @@ private function create_test_events() {
122122
return $events;
123123
}
124124

125-
function test_get_events() {
125+
public function test_get_events() {
126126
$events = Events::instance();
127127

128128
$test_events = $this->register_active_events_for_listing();

__tests__/unit-tests/test-internal-events.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
use Automattic\WP\Cron_Control;
66

77
class Internal_Events_Tests extends \WP_UnitTestCase {
8-
function setUp(): void {
8+
public function setUp(): void {
99
parent::setUp();
1010
Utils::clear_cron_table();
1111
}
1212

13-
function tearDown(): void {
13+
public function tearDown(): void {
1414
Utils::clear_cron_table();
1515
parent::tearDown();
1616
}
1717

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

@@ -28,7 +28,7 @@ function test_internal_events_are_scheduled() {
2828
}
2929
}
3030

31-
function test_migrate_legacy_cron_events() {
31+
public function test_migrate_legacy_cron_events() {
3232
global $wpdb;
3333

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

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

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

111-
function test_force_publish_missed_schedules() {
111+
public function test_force_publish_missed_schedules() {
112112
// Define the filter callback to override post status.
113113
$future_insert_filter = function ( $data ) {
114114
if ( 'publish' === $data['post_status'] ) {

__tests__/unit-tests/test-rest-api.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use WP_CRON_CONTROL_SECRET;
99

1010
class REST_API_Tests extends \WP_UnitTestCase {
11+
/** @var WP_REST_Server|null */
12+
private $server = null;
13+
1114
public function setUp(): void {
1215
parent::setUp();
1316

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

22-
function tearDown(): void {
25+
public function tearDown(): void {
2326
global $wp_rest_server;
2427
$wp_rest_server = null;
28+
$this->server = null;
2529

2630
Utils::clear_cron_table();
2731
parent::tearDown();

0 commit comments

Comments
 (0)