Commit 8ed0c38e authored by Erick Hitter's avatar Erick Hitter

Tests for event addition

Removal of existing Internal Events is covered by the existing tests.
parent 746cc6b7
......@@ -19,6 +19,14 @@ require_once $_tests_dir . '/includes/functions.php';
function _manually_load_plugin() {
define( 'WP_CRON_CONTROL_SECRET', 'testtesttest' );
define( 'CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS', array(
array(
'schedule' => 'hourly',
'action' => 'cron_control_additional_internal_event',
'callback' => '__return_true',
),
) );
require dirname( dirname( __FILE__ ) ) . '/cron-control.php';
// Plugin loads after `wp_install()` is called, so we compensate.
......
......@@ -6,11 +6,14 @@
*/
namespace Automattic\WP\Cron_Control\Tests;
use Automattic\WP\Cron_Control;
use Automattic\WP\Cron_Control\Internal_Events;
use WP_UnitTestCase;
/**
* Internal Events tests
*/
class Internal_Events_Tests extends \WP_UnitTestCase {
class Internal_Events_Tests extends WP_UnitTestCase {
/**
* Prepare test environment
*/
......@@ -19,6 +22,8 @@ class Internal_Events_Tests extends \WP_UnitTestCase {
// make sure the schedule is clear.
_set_cron_array( array() );
Internal_Events::instance()->schedule_internal_events();
}
/**
......@@ -34,17 +39,36 @@ class Internal_Events_Tests extends \WP_UnitTestCase {
/**
* Internal events should be scheduled
*/
function test_events() {
\Automattic\WP\Cron_Control\Internal_Events::instance()->schedule_internal_events();
function test_events_scheduled() {
$events = Cron_Control\collapse_events_array( get_option( 'cron' ) );
$expected = 4; // Number of events created by the Internal_Events::prepare_internal_events() method, which is private.
$expected += count( CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS );
$events = \Automattic\WP\Cron_Control\collapse_events_array( get_option( 'cron' ) );
$this->assertEquals( count( $events ), $expected, 'Incorrect number of Internal Events registered' );
}
// Check that the plugin scheduled the expected number of events.
$this->assertEquals( count( $events ), 4 );
/**
* Test that all scheduled events are from the Internal Events class
*/
function test_events_are_internal() {
$events = Cron_Control\collapse_events_array( get_option( 'cron' ) );
// Confirm that the scheduled jobs came from the Internal Events class.
foreach ( $events as $event ) {
$this->assertTrue( \Automattic\WP\Cron_Control\is_internal_event( $event['action'] ) );
$this->assertTrue( Cron_Control\is_internal_event( $event['action'] ), sprintf( 'Action `%s` is not an Internal Event', $event['action'] ) );
}
}
/**
* Test that additional Internal Events can be added
*/
function test_add_events() {
$additional = CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS;
foreach ( $additional as $added ) {
$next = wp_next_scheduled( $added['action'], array() );
$this->assertInternalType( 'int', $next, sprintf( 'Additional Internal Event `%s` not scheduled', $added['action'] ) );
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment