Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

Advanced test case.

Copyright: 2012 Petr Skoda {@link http://skodak.org}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 745 lines (27 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: advanced_testcase  - X-Ref

Advanced PHPUnit test case customised for Moodle.

__construct($name = null, array $data = array()   X-Ref
Constructs a test case with the given name.

Note: use setUp() or setUpBeforeClass() in your test cases.

param: string $name
param: array  $data
param: string $dataName

runBare()   X-Ref
Runs the bare test sequence.

return: void

createXMLDataSet($xmlFile)   X-Ref
Creates a new XMLDataSet with the given $xmlFile. (absolute path.)

param: string $xmlFile
return: phpunit_dataset

createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"')   X-Ref
Creates a new CsvDataSet from the given array of csv files. (absolute paths.)

param: array $files array tablename=>cvsfile
param: string $delimiter unused
param: string $enclosure unused
param: string $escape unused
return: phpunit_dataset

createArrayDataSet(array $data)   X-Ref
Creates new ArrayDataSet from given array

param: array $data array of tables, first row in each table is columns
return: phpunit_dataset

loadDataSet(phpunit_dataset $dataset)   X-Ref
Load date into moodle database tables from standard PHPUnit data set.

param: phpunit_dataset $dataset
return: void

dataset_from_files(array $files)   X-Ref
Creates a new dataset from CVS/XML files.

This method accepts an array of full paths to CSV or XML files to be loaded
into the dataset. For CSV files, the name of the table which the file belongs
to needs to be specified. Example:

$fullpaths = [
'/path/to/users.xml',
'course' => '/path/to/courses.csv',
];

param: array $files full paths to CSV or XML files to load.
return: phpunit_dataset

dataset_from_string(string $content, string $type, ?string $table = null)   X-Ref
Creates a new dataset from string (CSV or XML).

param: string $content contents (CSV or XML) to load.
param: string $type format of the content to be loaded (csv or xml).
param: string $table name of the table which the file belongs to (only for CSV files).
return: phpunit_dataset

dataset_from_array(array $data)   X-Ref
Creates a new dataset from PHP array.

param: array $data array of tables, see {@see phpunit_dataset::from_array()} for supported formats.
return: phpunit_dataset

preventResetByRollback()   X-Ref
Call this method from test if you want to make sure that
the resetting of database is done the slow way without transaction
rollback.

This is useful especially when testing stuff that is not compatible with transactions.

return: void

resetAfterTest($reset = true)   X-Ref
Reset everything after current test.

param: bool $reset true means reset state back, false means keep all data for the next test,
return: void

getDebuggingMessages()   X-Ref
Return debugging messages from the current test.

return: array with instances having 'message', 'level' and 'stacktrace' property.

resetDebugging()   X-Ref
Clear all previous debugging messages in current test
and revert to default DEVELOPER_DEBUG level.


assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '')   X-Ref
Assert that exactly debugging was just called once.

Discards the debugging message if successful.

param: null|string $debugmessage null means any
param: null|string $debuglevel null means any
param: string $message

assertDebuggingCalledCount($expectedcount, $debugmessages = array()   X-Ref
Asserts how many times debugging has been called.

param: int $expectedcount The expected number of times
param: array $debugmessages Expected debugging messages, one for each expected message.
param: array $debuglevels Expected debugging levels, one for each expected message.
param: string $message
return: void

assertDebuggingNotCalled($message = '')   X-Ref
Call when no debugging() messages expected.

param: string $message

assertEventLegacyData($expected, \core\event\base $event, $message = '')   X-Ref
Assert that an event legacy data is equal to the expected value.

param: mixed $expected expected data.
param: \core\event\base $event the event object.
param: string $message
return: void

assertEventLegacyLogData($expected, \core\event\base $event, $message = '')   X-Ref
Assert that an event legacy log data is equal to the expected value.

param: mixed $expected expected data.
param: \core\event\base $event the event object.
param: string $message
return: void

assertEventContextNotUsed(\core\event\base $event, $message = '')   X-Ref
Assert that an event is not using event->contxet.
While restoring context might not be valid and it should not be used by event url
or description methods.

param: \core\event\base $event the event object.
param: string $message
return: void

setCurrentTimeStart()   X-Ref
Stores current time as the base for assertTimeCurrent().

Note: this is called automatically before calling individual test methods.
return: int current time

assertTimeCurrent($time, $message = '')   X-Ref
Assert that: start < $time < time()

param: int $time
param: string $message
return: void

redirectMessages()   X-Ref
Starts message redirection.

You can verify if messages were sent or not by inspecting the messages
array in the returned messaging sink instance. The redirection
can be stopped by calling $sink->close();

return: phpunit_message_sink

redirectEmails()   X-Ref
Starts email redirection.

You can verify if email were sent or not by inspecting the email
array in the returned phpmailer sink instance. The redirection
can be stopped by calling $sink->close();

return: phpunit_message_sink

redirectEvents()   X-Ref
Starts event redirection.

You can verify if events were triggered or not by inspecting the events
array in the returned event sink instance. The redirection
can be stopped by calling $sink->close();

return: phpunit_event_sink

resetAllData($detectchanges = false)   X-Ref
Reset all database tables, restore global state and clear caches and optionally purge dataroot dir.

param: bool $detectchanges
return: void

setUser($user = null)   X-Ref
Set current $USER, reset access cache.

param: null|int|stdClass $user user record, null or 0 means non-logged-in, positive integer means userid
return: void

setAdminUser()   X-Ref
Set current $USER to admin account, reset access cache.

return: void

setGuestUser()   X-Ref
Set current $USER to guest account, reset access cache.

return: void

setTimezone($servertimezone = 'Australia/Perth', $defaultphptimezone = 'Australia/Perth')   X-Ref
Change server and default php timezones.

param: string $servertimezone timezone to set in $CFG->timezone (not validated)
param: string $defaultphptimezone timezone to fake default php timezone (must be valid)

getDataGenerator()   X-Ref
Get data generator

return: testing_data_generator

getExternalTestFileUrl($path, $https = false)   X-Ref
Returns UTL of the external test file.

The result depends on the value of following constants:
- TEST_EXTERNAL_FILES_HTTP_URL
- TEST_EXTERNAL_FILES_HTTPS_URL

They should point to standard external test files repository,
it defaults to 'http://download.moodle.org/unittest'.

False value means skip tests that require external files.

param: string $path
param: bool $https true if https required
return: string url

recurseFolders($path, $callback, $fileregexp = '/.*/', $exclude = false, $ignorefolders = array()   X-Ref
Recursively visit all the files in the source tree. Calls the callback
function with the pathname of each file found.

param: string $path the folder to start searching from.
param: string $callback the method of this class to call with the name of each file found.
param: string $fileregexp a regexp used to filter the search (optional).
param: bool $exclude If true, pathnames that match the regexp will be ignored. If false,
param: array $ignorefolders will not go into any of these folders (optional).
return: void

waitForSecond()   X-Ref
Wait for a second to roll over, ensures future calls to time() return a different result.

This is implemented instead of sleep() as we do not need to wait a full second. In some cases
due to calls we may wait more than sleep() would have, on average it will be less.

runAdhocTasks($matchclass = '', $matchuserid = null)   X-Ref
Run adhoc tasks, optionally matching the specified classname.

param: string  $matchclass The name of the class to match on.
param: int     $matchuserid The userid to match.