|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Google\Site_Kit\Tests\Core\Email_Reporting\Monitor_TaskTest |
| 4 | + * |
| 5 | + * @package Google\Site_Kit\Tests\Core\Email_Reporting |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Google\Site_Kit\Tests\Core\Email_Reporting; |
| 9 | + |
| 10 | +use Google\Site_Kit\Core\Email_Reporting\Email_Reporting_Scheduler; |
| 11 | +use Google\Site_Kit\Core\Email_Reporting\Email_Reporting_Settings; |
| 12 | +use Google\Site_Kit\Core\Email_Reporting\Monitor_Task; |
| 13 | +use Google\Site_Kit\Core\User\Email_Reporting_Settings as User_Email_Reporting_Settings; |
| 14 | +use Google\Site_Kit\Tests\TestCase; |
| 15 | + |
| 16 | +class Monitor_TaskTest extends TestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \PHPUnit_Framework_MockObject_MockObject|Email_Reporting_Scheduler |
| 20 | + */ |
| 21 | + private $scheduler; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \PHPUnit_Framework_MockObject_MockObject|Email_Reporting_Settings |
| 25 | + */ |
| 26 | + private $settings; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Monitor_Task |
| 30 | + */ |
| 31 | + private $task; |
| 32 | + |
| 33 | + public function set_up() { |
| 34 | + parent::set_up(); |
| 35 | + |
| 36 | + $this->scheduler = $this->getMockBuilder( Email_Reporting_Scheduler::class ) |
| 37 | + ->disableOriginalConstructor() |
| 38 | + ->setMethods( array( 'schedule_initiator_once' ) ) |
| 39 | + ->getMock(); |
| 40 | + |
| 41 | + $this->settings = $this->getMockBuilder( Email_Reporting_Settings::class ) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->setMethods( array( 'is_email_reporting_enabled' ) ) |
| 44 | + ->getMock(); |
| 45 | + |
| 46 | + $this->task = new Monitor_Task( $this->scheduler, $this->settings ); |
| 47 | + |
| 48 | + $this->clear_scheduled_initiators(); |
| 49 | + } |
| 50 | + |
| 51 | + public function tear_down() { |
| 52 | + $this->clear_scheduled_initiators(); |
| 53 | + parent::tear_down(); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_handle_monitor_action_bails_when_disabled() { |
| 57 | + $this->settings->expects( $this->once() ) |
| 58 | + ->method( 'is_email_reporting_enabled' ) |
| 59 | + ->willReturn( false ); |
| 60 | + |
| 61 | + $this->scheduler->expects( $this->never() ) |
| 62 | + ->method( 'schedule_initiator_once' ); |
| 63 | + |
| 64 | + $this->task->handle_monitor_action(); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_handle_monitor_action_restores_missing_frequencies() { |
| 68 | + $this->settings->expects( $this->once() ) |
| 69 | + ->method( 'is_email_reporting_enabled' ) |
| 70 | + ->willReturn( true ); |
| 71 | + |
| 72 | + wp_schedule_single_event( time() + HOUR_IN_SECONDS, Email_Reporting_Scheduler::ACTION_INITIATOR, array( User_Email_Reporting_Settings::FREQUENCY_WEEKLY ) ); |
| 73 | + wp_schedule_single_event( time() + HOUR_IN_SECONDS, Email_Reporting_Scheduler::ACTION_INITIATOR, array( User_Email_Reporting_Settings::FREQUENCY_MONTHLY ) ); |
| 74 | + |
| 75 | + $this->scheduler->expects( $this->once() ) |
| 76 | + ->method( 'schedule_initiator_once' ) |
| 77 | + ->with( User_Email_Reporting_Settings::FREQUENCY_QUARTERLY ); |
| 78 | + |
| 79 | + $this->task->handle_monitor_action(); |
| 80 | + } |
| 81 | + |
| 82 | + private function clear_scheduled_initiators() { |
| 83 | + wp_unschedule_hook( Email_Reporting_Scheduler::ACTION_INITIATOR ); |
| 84 | + } |
| 85 | +} |
0 commit comments