Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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: 714 lines (25 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

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

param: string $xmlFile
return: PHPUnit\DbUnit\DataSet\FlatXmlDataSet

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

param: string $xmlFile
return: PHPUnit\DbUnit\DataSet\XmlDataSet

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
param: string $enclosure
param: string $escape
return: PHPUnit\DbUnit\DataSet\CsvDataSet

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_ArrayDataSet

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

Note: it is usually better to use data generators

param: PHPUnit\DbUnit\DataSet\IDataSet $dataset
return: void

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.