| 685 | |
| 686 | /** |
| 687 | * Recurring events have a max event limiter. Make sure it's honored properly. |
| 688 | * |
| 689 | * @ticket 49693 |
| 690 | */ |
| 691 | function test_duplicate_recurring_event() { |
| 692 | $hook = __FUNCTION__; |
| 693 | $args = array( 'arg1' ); |
| 694 | $schedule = 'hourly'; |
| 695 | |
| 696 | // Add the max number of events for an 'hourly' schedule. |
| 697 | $max_events = DAY_IN_SECONDS / HOUR_IN_SECONDS; // Hourly schedule, max events per day is 24. |
| 698 | for ( $i = 0; $i < $max_events; $i++ ) { |
| 699 | $timestamp_next = strtotime( '+' . ( 60 + $i ) . ' minutes' ); |
| 700 | // Scheduling the same recurring event (hook and args), but with different timestamp should fail. |
| 701 | $this->assertTrue( wp_schedule_event( $timestamp_next, $schedule, $hook, $args ) ); |
| 702 | } |
| 703 | $expected = _get_cron_array(); |
| 704 | |
| 705 | // Scheduling the event $max_events + 1 time should fail. |
| 706 | $this->assertFalse( wp_schedule_event( strtotime( '+120 minutes' ), $schedule, $hook, $args ) ); |
| 707 | |
| 708 | // Check cron option is unchanged. |
| 709 | $this->assertEquals( $expected, _get_cron_array() ); |
| 710 | $this->assertCount( $max_events, _get_cron_array() ); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Recurring events that have the same hook but different args or schedule are allowed. |
| 715 | * |
| 716 | * @ticket 49693 |
| 717 | */ |
| 718 | function test_not_duplicate_recurring_event() { |
| 719 | $hook = __FUNCTION__; |
| 720 | $args_one = array( 'arg1' ); |
| 721 | $args_two = array( 'arg2' ); |
| 722 | $timestamp = strtotime( '+60 minutes' ); |
| 723 | $schedule_one = 'hourly'; |
| 724 | $schedule_two = 'daily'; |
| 725 | |
| 726 | // Schedule recurring event. |
| 727 | $this->assertNotFalse( wp_schedule_event( $timestamp, $schedule_one, $hook, $args_one ) ); |
| 728 | |
| 729 | // Schedule recurring event as above, but with different schedule is allowed. |
| 730 | $this->assertNotFalse( wp_schedule_event( $timestamp, $schedule_two, $hook, $args_one ) ); |
| 731 | |
| 732 | // Schedule recurring event as above, but with different args is allowed. |
| 733 | $this->assertNotFalse( wp_schedule_event( $timestamp, $schedule_one, $hook, $args_two ) ); |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Make sure that rescheduling still works with the recurring event limiter when running CRON. |
| 738 | * |
| 739 | * @ticket 49693 |
| 740 | */ |
| 741 | function test_reschedule_recurring_event() { |
| 742 | $hook = __FUNCTION__; |
| 743 | $ts_one = strtotime( '+30 minutes' ); |
| 744 | |
| 745 | // Pretend CRON is running |
| 746 | add_filter( 'wp_doing_cron', '__return_true' ); |
| 747 | |
| 748 | // Confirm there's no events. |
| 749 | $this->assertEmpty( _get_cron_array() ); |
| 750 | |
| 751 | // Add an event. |
| 752 | $this->assertTrue( wp_schedule_event( $ts_one, 'hourly', $hook ) ); |
| 753 | |
| 754 | // Reschedule it. |
| 755 | $this->assertNotFalse( wp_reschedule_event( $ts_one, 'hourly', $hook ) ); |
| 756 | |
| 757 | // Make sure the original event is still there. |
| 758 | $this->assertNotFalse( wp_get_scheduled_event( $hook, array(), $ts_one ) ); |
| 759 | |
| 760 | // Make sure the rescheduled event is also there. |
| 761 | $this->assertEquals( 2, count( _get_cron_array() ) ); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Make sure that the limiter applies to event rescheduling if we're not running CRON. |
| 766 | * |
| 767 | * @ticket 49693 |
| 768 | */ |
| 769 | function test_not_reschedule_recurring_event() { |
| 770 | $hook = __FUNCTION__; |
| 771 | $schedule = 'hourly'; |
| 772 | |
| 773 | // Add the max number of events for an 'hourly' schedule. |
| 774 | $max_events = DAY_IN_SECONDS / HOUR_IN_SECONDS; // Hourly schedule, max events per day is 24. |
| 775 | for ( $i = 0; $i < $max_events; $i++ ) { |
| 776 | $timestamp_next = strtotime( '+' . ( 60 + $i ) . ' minutes' ); |
| 777 | // Scheduling the same recurring event (hook and args), but with different timestamp should fail. |
| 778 | $this->assertTrue( wp_schedule_event( $timestamp_next, $schedule, $hook ) ); |
| 779 | } |
| 780 | $expected = _get_cron_array(); |
| 781 | |
| 782 | // Rescheduling the event fails because of the limiter. |
| 783 | $this->assertFalse( wp_reschedule_event( strtotime( '+120 minutes' ), $schedule, $hook ) ); |
| 784 | |
| 785 | // Check cron option is unchanged. |
| 786 | $this->assertEquals( $expected, _get_cron_array() ); |
| 787 | $this->assertCount( $max_events, _get_cron_array() ); |
| 788 | } |