Best Atoum code snippet using directory.testClass
scheduled_task_test.php
Source:scheduled_task_test.php
1<?php2// This file is part of Moodle - http://moodle.org/3//4// Moodle is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.8//9// Moodle is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.13//14// You should have received a copy of the GNU General Public License15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.16/**17 * This file contains the unittests for scheduled tasks.18 *19 * @package core20 * @category phpunit21 * @copyright 2013 Damyon Wiese22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later23 */24defined('MOODLE_INTERNAL') || die();25require_once(__DIR__ . '/fixtures/task_fixtures.php');26/**27 * Test class for scheduled task.28 *29 * @package core30 * @category task31 * @copyright 2013 Damyon Wiese32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later33 */34class core_scheduled_task_testcase extends advanced_testcase {35 /**36 * Test the cron scheduling method37 */38 public function test_eval_cron_field() {39 $testclass = new \core\task\scheduled_test_task();40 $this->assertEquals(20, count($testclass->eval_cron_field('*/3', 0, 59)));41 $this->assertEquals(31, count($testclass->eval_cron_field('1,*/2', 0, 59)));42 $this->assertEquals(15, count($testclass->eval_cron_field('1-10,5-15', 0, 59)));43 $this->assertEquals(13, count($testclass->eval_cron_field('1-10,5-15/2', 0, 59)));44 $this->assertEquals(3, count($testclass->eval_cron_field('1,2,3,1,2,3', 0, 59)));45 $this->assertEquals(1, count($testclass->eval_cron_field('-1,10,80', 0, 59)));46 }47 public function test_get_next_scheduled_time() {48 // Test job run at 1 am.49 $testclass = new \core\task\scheduled_test_task();50 // All fields default to '*'.51 $testclass->set_hour('1');52 $testclass->set_minute('0');53 // Next valid time should be 1am of the next day.54 $nexttime = $testclass->get_next_scheduled_time();55 $oneam = mktime(1, 0, 0);56 // Make it 1 am tomorrow if the time is after 1am.57 if ($oneam < time()) {58 $oneam += 86400;59 }60 $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.');61 // Disabled flag does not affect next time.62 $testclass->set_disabled(true);63 $nexttime = $testclass->get_next_scheduled_time();64 $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.');65 // Now test for job run every 10 minutes.66 $testclass = new \core\task\scheduled_test_task();67 // All fields default to '*'.68 $testclass->set_minute('*/10');69 // Next valid time should be next 10 minute boundary.70 $nexttime = $testclass->get_next_scheduled_time();71 $minutes = ((intval(date('i') / 10))+1) * 10;72 $nexttenminutes = mktime(date('H'), $minutes, 0);73 $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');74 // Disabled flag does not affect next time.75 $testclass->set_disabled(true);76 $nexttime = $testclass->get_next_scheduled_time();77 $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');78 // Test hourly job executed on Sundays only.79 $testclass = new \core\task\scheduled_test_task();80 $testclass->set_minute('0');81 $testclass->set_day_of_week('7');82 $nexttime = $testclass->get_next_scheduled_time();83 $this->assertEquals(7, date('N', $nexttime));84 $this->assertEquals(0, date('i', $nexttime));85 // Test monthly job86 $testclass = new \core\task\scheduled_test_task();87 $testclass->set_minute('32');88 $testclass->set_hour('0');89 $testclass->set_day('1');90 $nexttime = $testclass->get_next_scheduled_time();91 $this->assertEquals(32, date('i', $nexttime));92 $this->assertEquals(0, date('G', $nexttime));93 $this->assertEquals(1, date('j', $nexttime));94 }95 public function test_timezones() {96 global $CFG, $USER;97 // The timezones used in this test are chosen because they do not use DST - that would break the test.98 $currenttimezonephp = date_default_timezone_get();99 $currenttimezonecfg = null;100 if (!empty($CFG->timezone)) {101 $currenttimezonecfg = $CFG->timezone;102 }103 $userstimezone = null;104 if (!empty($USER->timezone)) {105 $userstimezone = $USER->timezone;106 }107 // We are testing a difference between $CFG->timezone and the php.ini timezone.108 // GMT+8.109 date_default_timezone_set('Australia/Perth');110 // GMT-04:30.111 $CFG->timezone = 'America/Caracas';112 $testclass = new \core\task\scheduled_test_task();113 // Scheduled tasks should always use servertime - so this is 03:30 GMT.114 $testclass->set_hour('1');115 $testclass->set_minute('0');116 // Next valid time should be 1am of the next day.117 $nexttime = $testclass->get_next_scheduled_time();118 // GMT+05:45.119 $USER->timezone = 'Asia/Kathmandu';120 $userdate = userdate($nexttime);121 // Should be displayed in user timezone.122 // I used http://www.timeanddate.com/worldclock/fixedtime.html?msg=Moodle+Test&iso=20140314T01&p1=58123 // to verify this time.124 $this->assertContains('11:15 AM', core_text::strtoupper($userdate));125 $CFG->timezone = $currenttimezonecfg;126 date_default_timezone_set($currenttimezonephp);127 }128 public function test_reset_scheduled_tasks_for_component() {129 global $DB;130 $this->resetAfterTest(true);131 // Remember the defaults.132 $defaulttasks = \core\task\manager::load_scheduled_tasks_for_component('moodle');133 $initcount = count($defaulttasks);134 // Customise a task.135 $firsttask = reset($defaulttasks);136 $firsttask->set_minute('1');137 $firsttask->set_hour('2');138 $firsttask->set_month('3');139 $firsttask->set_day_of_week('4');140 $firsttask->set_day('5');141 $firsttask->set_customised('1');142 \core\task\manager::configure_scheduled_task($firsttask);143 $firsttaskrecord = \core\task\manager::record_from_scheduled_task($firsttask);144 // We reset this field, because we do not want to compare it.145 $firsttaskrecord->nextruntime = '0';146 // Delete a task to simulate the fact that its new.147 $secondtask = next($defaulttasks);148 $DB->delete_records('task_scheduled', array('classname' => '\\' . trim(get_class($secondtask), '\\')));149 $this->assertFalse(\core\task\manager::get_scheduled_task(get_class($secondtask)));150 // Edit a task to simulate a change in its definition (as if it was not customised).151 $thirdtask = next($defaulttasks);152 $thirdtask->set_minute('1');153 $thirdtask->set_hour('2');154 $thirdtask->set_month('3');155 $thirdtask->set_day_of_week('4');156 $thirdtask->set_day('5');157 $thirdtaskbefore = \core\task\manager::get_scheduled_task(get_class($thirdtask));158 $thirdtaskbefore->set_next_run_time(null); // Ignore this value when comparing.159 \core\task\manager::configure_scheduled_task($thirdtask);160 $thirdtask = \core\task\manager::get_scheduled_task(get_class($thirdtask));161 $thirdtask->set_next_run_time(null); // Ignore this value when comparing.162 $this->assertNotEquals($thirdtaskbefore, $thirdtask);163 // Now call reset on all the tasks.164 \core\task\manager::reset_scheduled_tasks_for_component('moodle');165 // Load the tasks again.166 $defaulttasks = \core\task\manager::load_scheduled_tasks_for_component('moodle');167 $finalcount = count($defaulttasks);168 // Compare the first task.169 $newfirsttask = reset($defaulttasks);170 $newfirsttaskrecord = \core\task\manager::record_from_scheduled_task($newfirsttask);171 // We reset this field, because we do not want to compare it.172 $newfirsttaskrecord->nextruntime = '0';173 // Assert a customised task was not altered by reset.174 $this->assertEquals($firsttaskrecord, $newfirsttaskrecord);175 // Assert that the second task was added back.176 $secondtaskafter = \core\task\manager::get_scheduled_task(get_class($secondtask));177 $secondtaskafter->set_next_run_time(null); // Do not compare the nextruntime.178 $secondtask->set_next_run_time(null);179 $this->assertEquals($secondtask, $secondtaskafter);180 // Assert that the third task edits were overridden.181 $thirdtaskafter = \core\task\manager::get_scheduled_task(get_class($thirdtask));182 $thirdtaskafter->set_next_run_time(null);183 $this->assertEquals($thirdtaskbefore, $thirdtaskafter);184 // Assert we have the same number of tasks.185 $this->assertEquals($initcount, $finalcount);186 }187 public function test_get_next_scheduled_task() {188 global $DB;189 $this->resetAfterTest(true);190 // Delete all existing scheduled tasks.191 $DB->delete_records('task_scheduled');192 // Add a scheduled task.193 // A task that runs once per hour.194 $record = new stdClass();195 $record->blocking = true;196 $record->minute = '0';197 $record->hour = '0';198 $record->dayofweek = '*';199 $record->day = '*';200 $record->month = '*';201 $record->component = 'test_scheduled_task';202 $record->classname = '\core\task\scheduled_test_task';203 $DB->insert_record('task_scheduled', $record);204 // And another one to test failures.205 $record->classname = '\core\task\scheduled_test2_task';206 $DB->insert_record('task_scheduled', $record);207 // And disabled test.208 $record->classname = '\core\task\scheduled_test3_task';209 $record->disabled = 1;210 $DB->insert_record('task_scheduled', $record);211 $now = time();212 // Should get handed the first task.213 $task = \core\task\manager::get_next_scheduled_task($now);214 $this->assertInstanceOf('\core\task\scheduled_test_task', $task);215 $task->execute();216 \core\task\manager::scheduled_task_complete($task);217 // Should get handed the second task.218 $task = \core\task\manager::get_next_scheduled_task($now);219 $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);220 $task->execute();221 \core\task\manager::scheduled_task_failed($task);222 // Should not get any task.223 $task = \core\task\manager::get_next_scheduled_task($now);224 $this->assertNull($task);225 // Should get the second task (retry after delay).226 $task = \core\task\manager::get_next_scheduled_task($now + 120);227 $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);228 $task->execute();229 \core\task\manager::scheduled_task_complete($task);230 // Should not get any task.231 $task = \core\task\manager::get_next_scheduled_task($now);232 $this->assertNull($task);233 }234 public function test_get_broken_scheduled_task() {235 global $DB;236 $this->resetAfterTest(true);237 // Delete all existing scheduled tasks.238 $DB->delete_records('task_scheduled');239 // Add a scheduled task.240 // A broken task that runs all the time.241 $record = new stdClass();242 $record->blocking = true;243 $record->minute = '*';244 $record->hour = '*';245 $record->dayofweek = '*';246 $record->day = '*';247 $record->month = '*';248 $record->component = 'test_scheduled_task';249 $record->classname = '\core\task\scheduled_test_task_broken';250 $DB->insert_record('task_scheduled', $record);251 $now = time();252 // Should not get any task.253 $task = \core\task\manager::get_next_scheduled_task($now);254 $this->assertDebuggingCalled();255 $this->assertNull($task);256 }257 /**258 * Test that the file_temp_cleanup_task removes directories and259 * files as expected.260 */261 public function test_file_temp_cleanup_task() {262 global $CFG;263 // Create directories.264 $dir = $CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR . 'courses';265 mkdir($dir, 0777, true);266 // Create files to be checked and then deleted.267 $file01 = $dir . DIRECTORY_SEPARATOR . 'sections.xml';268 file_put_contents($file01, 'test data 001');269 $file02 = $dir . DIRECTORY_SEPARATOR . 'modules.xml';270 file_put_contents($file02, 'test data 002');271 // Change the time modified for the first file, to a time that will be deleted by the task (greater than seven days).272 touch($file01, time() - (8 * 24 * 3600));273 $task = \core\task\manager::get_scheduled_task('\\core\\task\\file_temp_cleanup_task');274 $this->assertInstanceOf('\core\task\file_temp_cleanup_task', $task);275 $task->execute();276 // Scan the directory. Only modules.xml should be left.277 $filesarray = scandir($dir);278 $this->assertEquals('modules.xml', $filesarray[2]);279 $this->assertEquals(3, count($filesarray));280 // Change the time modified on modules.xml.281 touch($file02, time() - (8 * 24 * 3600));282 // Change the time modified on the courses directory.283 touch($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR .284 'courses', time() - (8 * 24 * 3600));285 // Run the scheduled task to remove the file and directory.286 $task->execute();287 $filesarray = scandir($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01');288 // There should only be two items in the array, '.' and '..'.289 $this->assertEquals(2, count($filesarray));290 // Change the time modified on all of the files and directories.291 $dir = new \RecursiveDirectoryIterator($CFG->tempdir);292 // Show all child nodes prior to their parent.293 $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);294 for ($iter->rewind(); $iter->valid(); $iter->next()) {295 if ($iter->isDir() && !$iter->isDot()) {296 $node = $iter->getRealPath();297 touch($node, time() - (8 * 24 * 3600));298 }299 }300 // Run the scheduled task again to remove all of the files and directories.301 $task->execute();302 $filesarray = scandir($CFG->tempdir);303 // All of the files and directories should be deleted.304 // There should only be two items in the array, '.' and '..'.305 $this->assertEquals(2, count($filesarray));306 }307}...
testClass
Using AI Code Generation
1require_once("directory.php");2$dir = new directory();3$dir->testClass();4require_once("directory.php");5$dir = new directory();6$dir->testClass();7{8 public function testClass()9 {10 echo "testClass";11 }12}
testClass
Using AI Code Generation
1require_once('directory.php');2$dir = new Directory();3$dir->testClass();4{5 public function testClass()6 {7 echo 'testClass';8 }9}
testClass
Using AI Code Generation
1require_once "directory.php";2$dir = new directory;3$dir->testClass();4class Person {5 public $name;6 public $age;7 function set_name($name) {8 $this->name = $name;9 }10 function get_name() {11 return $this->name;12 }13}14class Employee extends Person {15 protected $salary;16 public function set_salary($salary) {17 $this->salary = $salary;18 }19 public function get_salary() {20 return $this->salary;21 }22}23$emp = new Employee;24$emp->set_name('John');25$emp->set_salary(1000);26echo $emp->get_name() . " earns Rs " . $emp->get_salary() . " per month.";27class Fruit {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with testClass on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!