public function test_get_next_scheduled_time() { // Test job run at 1 am. $testclass = new \core\task\scheduled_test_task(); // All fields default to '*'. $testclass->set_hour('1'); $testclass->set_minute('0'); // Next valid time should be 1am of the next day. $nexttime = $testclass->get_next_scheduled_time(); $oneam = mktime(1, 0, 0); // Make it 1 am tomorrow if the time is after 1am. if ($oneam < time()) { $oneam += 86400; } $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.'); // Disabled flag does not affect next time. $testclass->set_disabled(true); $nexttime = $testclass->get_next_scheduled_time(); $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.'); // Now test for job run every 10 minutes. $testclass = new \core\task\scheduled_test_task(); // All fields default to '*'. $testclass->set_minute('*/10'); // Next valid time should be next 10 minute boundary. $nexttime = $testclass->get_next_scheduled_time(); $minutes = (intval(date('i') / 10) + 1) * 10; $nexttenminutes = mktime(date('H'), $minutes, 0); $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.'); // Disabled flag does not affect next time. $testclass->set_disabled(true); $nexttime = $testclass->get_next_scheduled_time(); $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.'); // Test hourly job executed on Sundays only. $testclass = new \core\task\scheduled_test_task(); $testclass->set_minute('0'); $testclass->set_day_of_week('7'); $nexttime = $testclass->get_next_scheduled_time(); $this->assertEquals(7, date('N', $nexttime)); $this->assertEquals(0, date('i', $nexttime)); // Test monthly job $testclass = new \core\task\scheduled_test_task(); $testclass->set_minute('32'); $testclass->set_hour('0'); $testclass->set_day('1'); $nexttime = $testclass->get_next_scheduled_time(); $this->assertEquals(32, date('i', $nexttime)); $this->assertEquals(0, date('G', $nexttime)); $this->assertEquals(1, date('j', $nexttime)); }
/** * Tests the use of 'R' syntax in time fields of tasks to get * tasks be configured with a non-uniform time. */ public function test_random_time_specification() { // Testing non-deterministic things in a unit test is not really // wise, so we just test the values have changed within allowed bounds. $testclass = new \core\task\scheduled_test_task(); // The test task defaults to '*'. $this->assertInternalType('string', $testclass->get_minute()); $this->assertInternalType('string', $testclass->get_hour()); // Set a random value. $testclass->set_minute('R'); $testclass->set_hour('R'); $testclass->set_day_of_week('R'); // Verify the minute has changed within allowed bounds. $minute = $testclass->get_minute(); $this->assertInternalType('int', $minute); $this->assertGreaterThanOrEqual(0, $minute); $this->assertLessThanOrEqual(59, $minute); // Verify the hour has changed within allowed bounds. $hour = $testclass->get_hour(); $this->assertInternalType('int', $hour); $this->assertGreaterThanOrEqual(0, $hour); $this->assertLessThanOrEqual(23, $hour); // Verify the dayofweek has changed within allowed bounds. $dayofweek = $testclass->get_day_of_week(); $this->assertInternalType('int', $dayofweek); $this->assertGreaterThanOrEqual(0, $dayofweek); $this->assertLessThanOrEqual(6, $dayofweek); }