Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Class: phpunit_util  - X-Ref

Collection of utility methods.

initialise_cfg()   X-Ref
Load global $CFG;

return: void

reset_all_data($detectchanges = false)   X-Ref
Reset contents of all database tables to initial values, reset caches, etc.

Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!

param: bool $detectchanges
return: void

reset_database()   X-Ref
Reset all database tables to default values.

return: bool true if reset done, false if skipped

bootstrap_init()   X-Ref
Called during bootstrap only!

return: void

bootstrap_moodle_info()   X-Ref
Print some Moodle related info to console.

return: void

get_global_backup($name)   X-Ref
Returns original state of global variable.

param: string $name
return: mixed

testing_ready_problem()   X-Ref
Is this site initialised to run unit tests?

return: int array errorcode=>message, 0 means ok

drop_site($displayprogress = false)   X-Ref
Drop all test site data.

Note: To be used from CLI scripts only.

param: bool $displayprogress if true, this method will echo progress information.
return: void may terminate execution with exit code

install_site()   X-Ref
Perform a fresh test site installation

Note: To be used from CLI scripts only.

return: void may terminate execution with exit code

build_config_file()   X-Ref
Builds dirroot/phpunit.xml file using defaults from /phpunit.xml.dist

return: bool true means main config file created, false means only dataroot file created

build_component_config_files()   X-Ref
Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist

return: void, stops if can not write files

debugging_triggered($message, $level, $from)   X-Ref
To be called from debugging() only.

param: string $message
param: int $level
param: string $from

reset_debugging()   X-Ref
Resets the list of debugging messages.


get_debugging_messages()   X-Ref
Returns all debugging messages triggered during test.

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

display_debugging_messages($return = false)   X-Ref
Prints out any debug messages accumulated during test execution.

param: bool $return true to return the messages or false to print them directly. Default false.
return: bool|string false if no debug messages, true if debug triggered or string of messages

start_message_redirection()   X-Ref
Start message redirection.

Note: Do not call directly from tests,
use $sink = $this->redirectMessages() instead.

return: phpunit_message_sink

stop_message_redirection()   X-Ref
End message redirection.

Note: Do not call directly from tests,
use $sink->close() instead.

is_redirecting_messages()   X-Ref
Are messages redirected to some sink?

Note: to be called from messagelib.php only!

return: bool

message_sent($message)   X-Ref
To be called from messagelib.php only!

param: stdClass $message record from messages table
return: bool true means send message, false means message "sent" to sink.

start_phpmailer_redirection()   X-Ref
Start phpmailer redirection.

Note: Do not call directly from tests,
use $sink = $this->redirectEmails() instead.

return: phpunit_phpmailer_sink

stop_phpmailer_redirection()   X-Ref
End phpmailer redirection.

Note: Do not call directly from tests,
use $sink->close() instead.

is_redirecting_phpmailer()   X-Ref
Are messages for phpmailer redirected to some sink?

Note: to be called from moodle_phpmailer.php only!

return: bool

phpmailer_sent($message)   X-Ref
To be called from messagelib.php only!

param: stdClass $message record from messages table
return: bool true means send message, false means message "sent" to sink.

start_event_redirection()   X-Ref
Start event redirection.

return: phpunit_event_sink

stop_event_redirection()   X-Ref
End event redirection.


is_redirecting_events()   X-Ref
Are events redirected to some sink?

Note: to be called from \core\event\base only!

return: bool

event_triggered(\core\event\base $event)   X-Ref
To be called from \core\event\base only!

param: \core\event\base $event record from event_read table
return: bool true means send event, false means event "sent" to sink.

get_locale_name()   X-Ref
Gets the name of the locale for testing environment (Australian English)
depending on platform environment.

return: string the locale name.

run_all_adhoc_tasks()   X-Ref
Executes all adhoc tasks in the queue. Useful for testing asynchronous behaviour.

return: void

call_internal_method($object, $methodname, array $params, $classname)   X-Ref
Helper function to call a protected/private method of an object using reflection.

Example 1. Calling a protected object method:
$result = call_internal_method($myobject, 'method_name', [$param1, $param2], '\my\namespace\myobjectclassname');

Example 2. Calling a protected static method:
$result = call_internal_method(null, 'method_name', [$param1, $param2], '\my\namespace\myclassname');

param: object|null $object the object on which to call the method, or null if calling a static method.
param: string $methodname the name of the protected/private method.
param: array $params the array of function params to pass to the method.
param: string $classname the fully namespaced name of the class the object was created from (base in the case of mocks),
return: mixed the respective return value of the method.

pad(string $string, int $level)   X-Ref
Pad the supplied string with $level levels of indentation.

param: string  $string The string to pad
param: int     $level The number of levels of indentation to pad
return: string

get_coverage_config(array $includelists, array $excludelists)   X-Ref
Get the coverage config for the supplied includelist and excludelist configuration.

param: array[] $includelists The list of files/folders in the includelist.
param: array[] $excludelists The list of files/folders in the excludelist.
return: string

get_coverage_info(string $fulldir)   X-Ref
Get the phpunit_coverage_info for the specified plugin or subsystem directory.

param: string  $fulldir The directory to find the coverage info file in.
return: phpunit_coverage_info

is_in_isolated_process()   X-Ref
Whether the current process is an isolated test process.

return: bool