How to use testClass method of field class

Best Atoum code snippet using field.testClass

scheduled_task_test.php

Source:scheduled_task_test.php Github

copy

Full Screen

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}...

Full Screen

Full Screen

ChainVirtualFieldProviderTest.php

Source:ChainVirtualFieldProviderTest.php Github

copy

Full Screen

...26 public function testIsVirtualFieldByLowPriorityProvider()27 {28 $this->providers[0]->expects($this->once())29 ->method('isVirtualField')30 ->with('testClass', 'testField')31 ->willReturn(true);32 $this->providers[1]->expects($this->never())33 ->method('isVirtualField');34 $this->assertTrue($this->chainProvider->isVirtualField('testClass', 'testField'));35 }36 public function testIsVirtualFieldByHighPriorityProvider()37 {38 $this->providers[0]->expects($this->once())39 ->method('isVirtualField')40 ->with('testClass', 'testField')41 ->willReturn(false);42 $this->providers[1]->expects($this->once())43 ->method('isVirtualField')44 ->with('testClass', 'testField')45 ->willReturn(true);46 $this->assertTrue($this->chainProvider->isVirtualField('testClass', 'testField'));47 }48 public function testIsVirtualFieldNone()49 {50 $this->providers[0]->expects($this->once())51 ->method('isVirtualField')52 ->with('testClass', 'testField')53 ->willReturn(false);54 $this->providers[1]->expects($this->once())55 ->method('isVirtualField')56 ->with('testClass', 'testField')57 ->willReturn(false);58 $this->assertFalse($this->chainProvider->isVirtualField('testClass', 'testField'));59 }60 public function testIsVirtualFieldWithoutChildProviders()61 {62 $chainProvider = new ChainVirtualFieldProvider([], $this->configProvider);63 $this->assertFalse($chainProvider->isVirtualField('testClass', 'testField'));64 }65 public function testGetVirtualFields()66 {67 $entityClass = 'testClass';68 $this->configProvider->expects($this->once())69 ->method('hasConfig')70 ->with($entityClass)71 ->willReturn(false);72 $this->configProvider->expects($this->never())73 ->method('getConfig');74 $this->providers[0]->expects($this->once())75 ->method('getVirtualFields')76 ->with($entityClass)77 ->willReturn(['testField1', 'testField2']);78 $this->providers[1]->expects($this->once())79 ->method('getVirtualFields')80 ->with($entityClass)81 ->willReturn(['testField1', 'testField3']);82 $this->assertEquals(83 ['testField1', 'testField2', 'testField3'],84 $this->chainProvider->getVirtualFields($entityClass)85 );86 }87 public function testGetVirtualFieldsForNotAccessibleEntity()88 {89 $entityClass = 'testClass';90 $entityConfig = new Config(91 $this->createMock(ConfigIdInterface::class),92 ['is_extend' => true, 'state' => ExtendScope::STATE_NEW]93 );94 $this->configProvider->expects($this->once())95 ->method('hasConfig')96 ->with($entityClass)97 ->willReturn(true);98 $this->configProvider->expects($this->once())99 ->method('getConfig')100 ->with($entityClass)101 ->willReturn($entityConfig);102 $this->providers[0]->expects($this->never())103 ->method('getVirtualFields');104 $this->providers[1]->expects($this->never())105 ->method('getVirtualFields');106 $this->assertSame(107 [],108 $this->chainProvider->getVirtualFields($entityClass)109 );110 }111 public function testGetVirtualFieldsWithoutChildProviders()112 {113 $chainProvider = new ChainVirtualFieldProvider([], $this->configProvider);114 $this->assertSame([], $chainProvider->getVirtualFields('testClass'));115 }116 public function testGetVirtualFieldQuery()117 {118 $fieldsConfig = [119 'testClass0' => [120 'testField0-1' => [121 'select' => ['expr' => 'test.name', 'return_type' => 'string'],122 'join' => ['left' => [['join' => 'entity.test', 'alias' => 'test']]]123 ],124 ],125 'testClass1' => [126 'testField1-1' => [127 'select' => ['expr' => 'test.name', 'return_type' => 'string'],128 'join' => ['left' => [['join' => 'entity.test', 'alias' => 'test']]]129 ]130 ]131 ];132 $this->addQueryMock($fieldsConfig);133 $this->assertEquals(134 $fieldsConfig['testClass0']['testField0-1'],135 $this->chainProvider->getVirtualFieldQuery('testClass0', 'testField0-1')136 );137 $this->assertEquals(138 $fieldsConfig['testClass1']['testField1-1'],139 $this->chainProvider->getVirtualFieldQuery('testClass1', 'testField1-1')140 );141 try {142 $this->chainProvider->getVirtualFieldQuery('testClass1', 'testField1-2');143 $this->fail('Expected exception not thrown');144 } catch (\Exception $e) {145 $this->assertEquals(0, $e->getCode());146 $this->assertEquals(147 'A query for field "testField1-2" in class "testClass1" was not found.',148 $e->getMessage()149 );150 }151 }152 /**153 * Mocks for getVirtualFieldQuery method154 */155 protected function addQueryMock($fieldsConfig)156 {157 $providers = $this->providers;158 foreach ($providers as $idx => $provider) {159 $fields = $fieldsConfig['testClass' . $idx];160 $with = [];161 $willForIsVirtualField = [];162 $willForGetVirtualFieldQuery = [];163 foreach ($fields as $fieldName => $fieldConfig) {164 $with[] = ['testClass' . $idx, $fieldName];165 $willForIsVirtualField[] = new ReturnCallback(function () use ($fieldsConfig, $idx, $fieldName) {166 return isset($fieldsConfig['testClass' . $idx][$fieldName]);167 });168 $willForGetVirtualFieldQuery[] = $fieldsConfig['testClass' . $idx][$fieldName];169 }170 $provider->expects($this->atLeastOnce())171 ->method('isVirtualField')172 ->withConsecutive(...$with)173 ->willReturnOnConsecutiveCalls(...$willForIsVirtualField);174 $provider->expects($this->atLeastOnce())175 ->method('getVirtualFieldQuery')176 ->withConsecutive(...$with)177 ->willReturnOnConsecutiveCalls(...$willForGetVirtualFieldQuery);178 }179 }180 public function testGetVirtualFieldQueryWithoutChildProviders()181 {182 $this->expectException(\RuntimeException::class);183 $this->expectExceptionMessage('A query for field "testField" in class "testClass" was not found.');184 $chainProvider = new ChainVirtualFieldProvider([], $this->configProvider);185 $chainProvider->getVirtualFieldQuery('testClass', 'testField');186 }187}...

Full Screen

Full Screen

testClass

Using AI Code Generation

copy

Full Screen

1$test = new testClass();2$test->testMethod();3$test = new testClass();4$test->testMethod();5$test = new testClass();6$test->testMethod();

Full Screen

Full Screen

testClass

Using AI Code Generation

copy

Full Screen

1include('field.php');2$field = new field();3$field->testClass();4include('field.php');5$field = new field();6$field->testClass();7Related Posts: PHP: Object Oriented Programming (OOP) - Encapsulation8PHP: Object Oriented Programming (OOP) - Inheritance9PHP: Object Oriented Programming (OOP) - Abstraction10PHP: Object Oriented Programming (OOP) - Polymorphism11PHP: Object Oriented Programming (OOP) - Introduction12PHP: Object Oriented Programming (OOP) - Class13PHP: Object Oriented Programming (OOP) - Object14PHP: Object Oriented Programming (OOP) - Method15PHP: Object Oriented Programming (OOP) - Properties16PHP: Object Oriented Programming (OOP) - Constructor17PHP: Object Oriented Programming (OOP) - Destructor18PHP: Object Oriented Programming (OOP) - Static Properties19PHP: Object Oriented Programming (OOP) - Static Methods20PHP: Object Oriented Programming (OOP) - Constants21PHP: Object Oriented Programming (OOP) - Visibility22PHP: Object Oriented Programming (OOP) - Interface23PHP: Object Oriented Programming (OOP) - Abstract Class24PHP: Object Oriented Programming (OOP) - Traits25PHP: Object Oriented Programming (OOP) - Magic Methods26PHP: Object Oriented Programming (OOP) - Late Static Binding27PHP: Object Oriented Programming (OOP) - Type Hinting28PHP: Object Oriented Programming (OOP) - Namespaces29PHP: Object Oriented Programming (OOP) - Exceptions30PHP: Object Oriented Programming (OOP) - Reflection31PHP: Object Oriented Programming (OOP) - Autoloading32PHP: Object Oriented Programming (OOP) - Design Patterns33PHP: Object Oriented Programming (OOP) - Singleton Pattern

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Atoum automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger testClass code on LambdaTest Cloud Grid

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 Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful