123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- /**
- * Test the cron scheduling functions
- *
- * @group cron
- */
- class Tests_Cron extends WP_UnitTestCase {
- function setUp() {
- parent::setUp();
- // make sure the schedule is clear
- _set_cron_array(array());
- }
- function tearDown() {
- parent::tearDown();
- // make sure the schedule is clear
- _set_cron_array(array());
- }
- function test_wp_get_schedule_empty() {
- // nothing scheduled
- $hook = rand_str();
- $this->assertFalse(wp_get_schedule($hook));
- }
- function test_schedule_event_single() {
- // schedule an event and make sure it's returned by wp_next_scheduled
- $hook = rand_str();
- $timestamp = strtotime('+1 hour');
- wp_schedule_single_event( $timestamp, $hook );
- $this->assertEquals( $timestamp, wp_next_scheduled($hook) );
- // it's a non recurring event
- $this->assertEquals( '', wp_get_schedule($hook) );
- }
- function test_schedule_event_single_args() {
- // schedule an event with arguments and make sure it's returned by wp_next_scheduled
- $hook = rand_str();
- $timestamp = strtotime('+1 hour');
- $args = array(rand_str());
- wp_schedule_single_event( $timestamp, $hook, $args );
- // this returns the timestamp only if we provide matching args
- $this->assertEquals( $timestamp, wp_next_scheduled($hook, $args) );
- // these don't match so return nothing
- $this->assertEquals( false, wp_next_scheduled($hook) );
- $this->assertEquals( false, wp_next_scheduled($hook, array(rand_str())) );
- // it's a non recurring event
- $this->assertEquals( '', wp_get_schedule($hook, $args) );
- }
- function test_schedule_event() {
- // schedule an event and make sure it's returned by wp_next_scheduled
- $hook = rand_str();
- $recur = 'hourly';
- $timestamp = strtotime('+1 hour');
- wp_schedule_event( $timestamp, $recur, $hook );
- // it's scheduled for the right time
- $this->assertEquals( $timestamp, wp_next_scheduled($hook) );
- // it's a recurring event
- $this->assertEquals( $recur, wp_get_schedule($hook) );
- }
- function test_schedule_event_args() {
- // schedule an event and make sure it's returned by wp_next_scheduled
- $hook = rand_str();
- $timestamp = strtotime('+1 hour');
- $recur = 'hourly';
- $args = array(rand_str());
- wp_schedule_event( $timestamp, 'hourly', $hook, $args );
- // this returns the timestamp only if we provide matching args
- $this->assertEquals( $timestamp, wp_next_scheduled($hook, $args) );
- // these don't match so return nothing
- $this->assertEquals( false, wp_next_scheduled($hook) );
- $this->assertEquals( false, wp_next_scheduled($hook, array(rand_str())) );
- $this->assertEquals( $recur, wp_get_schedule($hook, $args) );
- }
- function test_unschedule_event() {
- // schedule an event and make sure it's returned by wp_next_scheduled
- $hook = rand_str();
- $timestamp = strtotime('+1 hour');
- wp_schedule_single_event( $timestamp, $hook );
- $this->assertEquals( $timestamp, wp_next_scheduled($hook) );
- // now unschedule it and make sure it's gone
- wp_unschedule_event( $timestamp, $hook );
- $this->assertEquals( false, wp_next_scheduled($hook) );
- }
- function test_clear_schedule() {
- $hook = rand_str();
- $args = array(rand_str());
- // schedule several events with and without arguments
- wp_schedule_single_event( strtotime('+1 hour'), $hook );
- wp_schedule_single_event( strtotime('+2 hour'), $hook );
- wp_schedule_single_event( strtotime('+3 hour'), $hook, $args );
- wp_schedule_single_event( strtotime('+4 hour'), $hook, $args );
- // make sure they're returned by wp_next_scheduled()
- $this->assertTrue( wp_next_scheduled($hook) > 0 );
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the no args events and make sure it's gone
- wp_clear_scheduled_hook($hook);
- $this->assertFalse( wp_next_scheduled($hook) );
- // the args events should still be there
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the args events and make sure they're gone too
- // note: wp_clear_scheduled_hook() expects args passed directly, rather than as an array
- wp_clear_scheduled_hook($hook, $args);
- $this->assertFalse( wp_next_scheduled($hook, $args) );
- }
- function test_clear_schedule_multiple_args() {
- $hook = rand_str();
- $args = array(rand_str(), rand_str());
- // schedule several events with and without arguments
- wp_schedule_single_event( strtotime('+1 hour'), $hook );
- wp_schedule_single_event( strtotime('+2 hour'), $hook );
- wp_schedule_single_event( strtotime('+3 hour'), $hook, $args );
- wp_schedule_single_event( strtotime('+4 hour'), $hook, $args );
- // make sure they're returned by wp_next_scheduled()
- $this->assertTrue( wp_next_scheduled($hook) > 0 );
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the no args events and make sure it's gone
- wp_clear_scheduled_hook($hook);
- $this->assertFalse( wp_next_scheduled($hook) );
- // the args events should still be there
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the args events and make sure they're gone too
- // note: wp_clear_scheduled_hook() used to expect args passed directly, rather than as an array pre WP 3.0
- wp_clear_scheduled_hook($hook, $args);
- $this->assertFalse( wp_next_scheduled($hook, $args) );
- }
- /**
- * @ticket 10468
- */
- function test_clear_schedule_new_args() {
- $hook = rand_str();
- $args = array(rand_str());
- $multi_hook = rand_str();
- $multi_args = array(rand_str(), rand_str());
- // schedule several events with and without arguments
- wp_schedule_single_event( strtotime('+1 hour'), $hook );
- wp_schedule_single_event( strtotime('+2 hour'), $hook );
- wp_schedule_single_event( strtotime('+3 hour'), $hook, $args );
- wp_schedule_single_event( strtotime('+4 hour'), $hook, $args );
- wp_schedule_single_event( strtotime('+5 hour'), $multi_hook, $multi_args );
- wp_schedule_single_event( strtotime('+6 hour'), $multi_hook, $multi_args );
- // make sure they're returned by wp_next_scheduled()
- $this->assertTrue( wp_next_scheduled($hook) > 0 );
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the no args events and make sure it's gone
- wp_clear_scheduled_hook($hook);
- $this->assertFalse( wp_next_scheduled($hook) );
- // the args events should still be there
- $this->assertTrue( wp_next_scheduled($hook, $args) > 0 );
- // clear the schedule for the args events and make sure they're gone too
- // wp_clear_scheduled_hook() should take args as an array like the other functions.
- wp_clear_scheduled_hook($hook, $args);
- $this->assertFalse( wp_next_scheduled($hook, $args) );
- // clear the schedule for the args events and make sure they're gone too
- // wp_clear_scheduled_hook() should take args as an array like the other functions and does from WP 3.0
- wp_clear_scheduled_hook($multi_hook, $multi_args);
- $this->assertFalse( wp_next_scheduled($multi_hook, $multi_args) );
- }
- /**
- * @ticket 6966
- */
- function test_duplicate_event() {
- // duplicate events close together should be skipped
- $hook = rand_str();
- $args = array(rand_str());
- $ts1 = strtotime('+5 minutes');
- $ts2 = strtotime('+3 minutes');
- // first one works
- wp_schedule_single_event( $ts1, $hook, $args );
- // second one is ignored
- wp_schedule_single_event( $ts2, $hook, $args );
- // the next event should be at +5 minutes, not +3
- $this->assertEquals( $ts1, wp_next_scheduled($hook, $args) );
- }
- /**
- * @ticket 6966
- */
- function test_not_duplicate_event() {
- // duplicate events far apart should work normally
- $hook = rand_str();
- $args = array(rand_str());
- $ts1 = strtotime('+30 minutes');
- $ts2 = strtotime('+3 minutes');
- // first one works
- wp_schedule_single_event( $ts1, $hook, $args );
- // second works too
- wp_schedule_single_event( $ts2, $hook, $args );
- // the next event should be at +5 minutes, not +3
- $this->assertEquals( $ts2, wp_next_scheduled($hook, $args) );
- wp_unschedule_event( $ts2, $hook, $args );
- // following event should be there too
- $this->assertEquals( $ts1, wp_next_scheduled($hook, $args) );
- }
- }
- /*
- * Disable the WP Cron running test for the moment as it kills the whole test suite.
- * TODO - Fix it to work with the new cron implementation in trunk
- *
- class WPTestCronRunning extends _WPEmptyBlog {
- function setUp() {
- parent::setUp();
- // make sure the schedule is clear
- _set_cron_array(array());
- }
- function tearDown() {
- parent::tearDown();
- // make sure the schedule is clear
- _set_cron_array(array());
- }
- function _do_cron() {
- // FIXME: wp-cron.php is difficult to test, could be wrapped in a function
- $_GET['check'] = wp_hash('187425');
- require(ABSPATH.'/wp-cron.php');
- }
- function test_run_schedule_single() {
- // schedule an event, run it, and make sure the hook is called
- $hook = rand_str();
- $args = array(rand_str());
- $timestamp = strtotime('-1 second');
- // register a test action
- $a = new MockAction();
- add_action($hook, array(&$a, 'action'));
- // schedule an event for 1 second ago
- wp_schedule_single_event( $timestamp, $hook, $args );
- $this->assertEquals( $timestamp, wp_next_scheduled($hook, $args) );
- // run cron jobs
- $this->_do_cron();
- // our action should have been called once with the correct args
- $this->assertEquals( 1, $a->get_call_count() );
- $this->assertEquals( array($args), $a->get_args() );
- // it shouldn't appear in the schedule anymore
- $this->assertFalse( wp_next_scheduled($hook, $args) );
- }
- function test_run_schedule_recurring() {
- // schedule a recurring event, run it, and make sure the hook is called
- $hook = rand_str();
- $args = array(rand_str());
- $timestamp = strtotime('-1 second');
- $recur = 'hourly';
- // register a test action
- $a = new MockAction();
- add_action($hook, array(&$a, 'action'));
- // schedule an event for 1 second ago
- wp_schedule_event( $timestamp, $recur, $hook, $args );
- $this->assertEquals( $timestamp, wp_next_scheduled($hook, $args) );
- $this->assertEquals( $recur, wp_get_schedule($hook, $args) );
- // run cron jobs
- $this->_do_cron();
- // our action should have been called once with the correct args
- $this->assertEquals( 1, $a->get_call_count() );
- $this->assertEquals( array($args), $a->get_args() );
- // it should appear in the schedule to run again in an hour's time
- $this->assertEquals( $timestamp + 3600, wp_next_scheduled($hook, $args) );
- }
- }
- */
|