Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

(no description)

File Size: 5593 lines (244 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 1 file
 lib/tests/fixtures/component_class_callback_example.php

Defines 2 classes

moodlelib_test:: (52 methods):
  define_local_decimal_separator()
  test_cleanremoteaddr()
  test_address_in_subnet()
  test_fix_utf8()
  test_optional_param()
  test_optional_param_array()
  test_required_param()
  test_required_param_array()
  test_clean_param()
  test_clean_param_array()
  test_clean_param_raw()
  test_clean_param_trim()
  test_clean_param_clean()
  test_clean_param_alpha()
  test_clean_param_alphanum()
  test_clean_param_alphaext()
  test_clean_param_sequence()
  test_clean_param_component()
  test_clean_param_localisedfloat()
  test_is_valid_plugin_name()
  test_clean_param_plugin()
  test_clean_param_area()
  test_clean_param_text()
  test_clean_param_url()
  test_clean_param_localurl()
  test_clean_param_file()
  test_clean_param_path()
  test_clean_param_safepath()
  test_clean_param_username()
  test_clean_param_stringid()
  test_clean_param_timezone()
  test_clean_param_null_argument()
  test_validate_param()
  test_shorten_text_no_tags_already_short_enough()
  test_shorten_text_with_tags_already_short_enough()
  test_shorten_text_no_tags_needs_shortening()
  test_shorten_text_with_tags_needs_shortening()
  test_shorten_text_with_tags_and_html_comment()
  test_shorten_text_with_entities()
  test_shorten_text_known_tricky_case()
  test_shorten_text_no_spaces()
  test_shorten_text_utf8_european()
  test_shorten_text_utf8_oriental()
  test_shorten_text_multilang()
  shorten_filename_provider()
  test_shorten_filename()
  shorten_filenames_provider()
  test_shorten_filenames()
  test_usergetdate()
  test_mark_user_preferences_changed()
  test_check_user_preferences_loaded()
  test_set_user_preference()

not:: (108 methods):
  test_set_user_preference_for_current_user()
  test_unset_user_preference_for_current_user()
  test_get_extra_user_fields_essentials()
  environment_for_get_extra_user_fields_tests()
  test_get_extra_user_fields_no_access()
  test_get_extra_user_fields_course_only_access()
  test_get_extra_user_fields_course_prevented_access()
  test_get_extra_user_fields_anywhere_access()
  test_get_extra_user_fields_schismatic_access()
  test_get_extra_user_fields_hard_to_prevent_access()
  test_get_extra_user_fields_sql()
  test_some_moodle_special_dst()
  test_userdate()
  test_dst_changes()
  test_make_timestamp()
  test_get_string()
  test_lang_string_var_export()
  test_get_string_limitation()
  test_format_float()
  test_unformat_float()
  test_delete_user()
  test_delete_user_long_username()
  test_delete_user_long_email()
  test_convert_to_array()
  test_date_format_string()
  test_get_config()
  test_get_max_upload_sizes()
  test_password_is_legacy_hash()
  test_validate_internal_user_password()
  test_hash_internal_user_password()
  test_update_internal_user_password()
  test_update_internal_user_password_no_cache()
  test_update_internal_user_password_update_no_cache()
  test_fullname()
  test_get_all_user_name_fields()
  test_order_in_string()
  test_complete_user_login()
  test_require_logout()
  generate_email_messageid_provider()
  test_generate_email_messageid()
  test_send_email_with_custom_header()
  diverted_emails_provider()
  test_email_should_be_diverted()
  test_email_to_user()
  email_to_user_attachment_provider()
  test_email_to_user_attachment()
  test_email_to_user_attachment_missing()
  test_setnew_password_and_mail()
  generate_confirmation_link_provider()
  test_generate_confirmation_link()
  test_generate_confirmation_link_with_custom_admin()
  test_remove_course_contents()
  test_username_load_fields_from_object()
  test_count_words()
  count_words_testcases()
  test_count_letters()
  count_letters_testcases()
  test_getremoteaddr()
  test_random_bytes_emulate()
  test_random_string()
  test_complex_random_string()
  data_private_ips()
  test_ip_is_public_private_ips()
  data_public_ips()
  test_ip_is_public_public_ips()
  test_can_send_from_real_email_address()
  data_can_send_from_real_email_address()
  test_generate_email_processing_address()
  test_email_is_not_allowed_for_allowemailaddresses()
  data_email_is_not_allowed_for_allowemailaddresses()
  test_email_is_not_allowed_for_denyemailaddresses()
  data_email_is_not_allowed_for_denyemailaddresses()
  test_unserialize_array()
  test_unserialize_object()
  test_component_class_callback_not_found()
  test_component_class_callback_method_not_found()
  test_component_class_callback_found_returns_null()
  test_component_class_callback_found_returns_value()
  test_component_class_callback_found_accepts_multiple()
  component_class_callback_default_provider()
  component_class_callback_data_provider()
  component_class_callback_multiple_params_provider()
  test_get_callable_name()
  callable_names_provider()
  user_data_provider()
  test_get_complete_user_data()
  test_send_password_change_info()
  test_get_time_interval_string()
  get_time_interval_string_provider()
  test_rename_to_unused_name_file()
  test_rename_to_unused_name_dir()
  test_rename_to_unused_name_failure()
  display_size_provider()
  test_display_size()
  display_size_fixed_provider()
  test_display_size_fixed()
  display_size_dp_provider()
  test_display_size_dp()
  test_get_list_of_plugins()
  get_list_of_plugins_provider()
  test_get_home_page()
  get_home_page_provider()
  test_get_default_home_page()
  test_get_performance_info_locks()
  test_get_performance_info_session_wait()
  test_html_is_blank()
  is_proxybypass_provider()
  test_is_proxybypass()


Class: moodlelib_test  - X-Ref

Unit tests for (some of) ../moodlelib.php.

define_local_decimal_separator(string $decsep = 'X')   X-Ref
Define a local decimal separator.

It is not possible to directly change the result of get_string in
a unit test. Instead, we create a language pack for language 'xx' in
dataroot and make langconfig.php with the string we need to change.
The default example separator used here is 'X'; on PHP 5.3 and before this
must be a single byte character due to PHP bug/limitation in
number_format, so you can't use UTF-8 characters.

param: string $decsep Separator character. Defaults to `'X'`.

test_cleanremoteaddr()   X-Ref
No description

test_address_in_subnet()   X-Ref
No description

test_fix_utf8()   X-Ref
No description

test_optional_param()   X-Ref
No description

test_optional_param_array()   X-Ref
No description

test_required_param()   X-Ref
No description

test_required_param_array()   X-Ref
No description

test_clean_param()   X-Ref
No description

test_clean_param_array()   X-Ref
No description

test_clean_param_raw()   X-Ref
No description

test_clean_param_trim()   X-Ref
No description

test_clean_param_clean()   X-Ref
No description

test_clean_param_alpha()   X-Ref
No description

test_clean_param_alphanum()   X-Ref
No description

test_clean_param_alphaext()   X-Ref
No description

test_clean_param_sequence()   X-Ref
No description

test_clean_param_component()   X-Ref
No description

test_clean_param_localisedfloat()   X-Ref
No description

test_is_valid_plugin_name()   X-Ref
No description

test_clean_param_plugin()   X-Ref
No description

test_clean_param_area()   X-Ref
No description

test_clean_param_text()   X-Ref
No description

test_clean_param_url()   X-Ref
No description

test_clean_param_localurl()   X-Ref
No description

test_clean_param_file()   X-Ref
No description

test_clean_param_path()   X-Ref
No description

test_clean_param_safepath()   X-Ref
No description

test_clean_param_username()   X-Ref
No description

test_clean_param_stringid()   X-Ref
No description

test_clean_param_timezone()   X-Ref
No description

test_clean_param_null_argument()   X-Ref
No description

test_validate_param()   X-Ref
No description

test_shorten_text_no_tags_already_short_enough()   X-Ref
No description

test_shorten_text_with_tags_already_short_enough()   X-Ref
No description

test_shorten_text_no_tags_needs_shortening()   X-Ref
No description

test_shorten_text_with_tags_needs_shortening()   X-Ref
No description

test_shorten_text_with_tags_and_html_comment()   X-Ref
No description

test_shorten_text_with_entities()   X-Ref
No description

test_shorten_text_known_tricky_case()   X-Ref
No description

test_shorten_text_no_spaces()   X-Ref
No description

test_shorten_text_utf8_european()   X-Ref
No description

test_shorten_text_utf8_oriental()   X-Ref
No description

test_shorten_text_multilang()   X-Ref
No description

shorten_filename_provider()   X-Ref
Provider for long filenames and its expected result, with and without hash.

return: array of ($filename, $length, $expected, $includehash)

test_shorten_filename($filename, $length, $expected, $includehash)   X-Ref
Test the {@link shorten_filename()} method.

param: string $filename
param: int $length
param: string $expected
param: boolean $includehash

shorten_filenames_provider()   X-Ref
Provider for long filenames and its expected result, with and without hash.

return: array of ($filename, $length, $expected, $includehash)

test_shorten_filenames($filenames, $length, $expected, $includehash)   X-Ref
Test the {@link shorten_filenames()} method.

param: string $filenames
param: int $length
param: string $expected
param: boolean $includehash

test_usergetdate()   X-Ref
No description

test_mark_user_preferences_changed()   X-Ref
No description

test_check_user_preferences_loaded()   X-Ref
No description

test_set_user_preference()   X-Ref
No description

Class: not  - X-Ref

test_set_user_preference_for_current_user()   X-Ref
No description

test_unset_user_preference_for_current_user()   X-Ref
No description

test_get_extra_user_fields_essentials()   X-Ref
Test essential features implementation of {@link get_extra_user_fields()} as the admin user with all capabilities.


environment_for_get_extra_user_fields_tests()   X-Ref
Prepare environment for couple of tests related to permission checks in {@link get_extra_user_fields()}.

return: stdClass

test_get_extra_user_fields_no_access()   X-Ref
No identity fields shown to student user (no permission to view identity fields).


test_get_extra_user_fields_course_only_access()   X-Ref
Teacher can see students' identity fields only within the course.


test_get_extra_user_fields_course_prevented_access()   X-Ref
Teacher can be prevented from seeing students' identity fields even within the course.


test_get_extra_user_fields_anywhere_access()   X-Ref
Manager can see students' identity fields anywhere.


test_get_extra_user_fields_schismatic_access()   X-Ref
Manager can be prevented from seeing hidden fields outside the course.


test_get_extra_user_fields_hard_to_prevent_access()   X-Ref
Two capabilities must be currently set to prevent manager from seeing hidden fields.


test_get_extra_user_fields_sql()   X-Ref
Tests get_extra_user_fields_sql.


test_some_moodle_special_dst()   X-Ref
Test some critical TZ/DST.

This method tests some special TZ/DST combinations that were fixed
by MDL-38999. The tests are done by comparing the results of the
output using Moodle TZ/DST support and PHP native one.

Note: If you don't trust PHP TZ/DST support, can verify the
harcoded expectations below with:
http://www.tools4noobs.com/online_tools/unix_timestamp_to_datetime/

test_userdate()   X-Ref
No description

test_dst_changes()   X-Ref
Make sure the DST changes happen at the right time in Moodle.


test_make_timestamp()   X-Ref
No description

test_get_string()   X-Ref
Test get_string and most importantly the implementation of the lang_string
object.


test_lang_string_var_export()   X-Ref
No description

test_get_string_limitation()   X-Ref
No description

test_format_float()   X-Ref
Test localised float formatting.


test_unformat_float()   X-Ref
Test localised float unformatting.


test_delete_user()   X-Ref
Test deleting of users.


test_delete_user_long_username()   X-Ref
Test deletion of user with long username


test_delete_user_long_email()   X-Ref
Test deletion of user with long email address


test_convert_to_array()   X-Ref
Test function convert_to_array()


test_date_format_string()   X-Ref
Test the function date_format_string().


test_get_config()   X-Ref
No description

test_get_max_upload_sizes()   X-Ref
No description

test_password_is_legacy_hash()   X-Ref
Test function password_is_legacy_hash().


test_validate_internal_user_password()   X-Ref
Test function validate_internal_user_password().


test_hash_internal_user_password()   X-Ref
Test function hash_internal_user_password().


test_update_internal_user_password()   X-Ref
Test function update_internal_user_password().


test_update_internal_user_password_no_cache()   X-Ref
Testing that if the password is not cached, that it does not update
the user table and fire event.


test_update_internal_user_password_update_no_cache()   X-Ref
Test if the user has a password hash, but now their auth method
says not to cache it.  Then it should update.


test_fullname()   X-Ref
No description

test_get_all_user_name_fields()   X-Ref
Tests the get_all_user_name_fields() deprecated function.


test_order_in_string()   X-Ref
No description

test_complete_user_login()   X-Ref
No description

test_require_logout()   X-Ref
Test require_logout.


generate_email_messageid_provider()   X-Ref
A data provider for testing email messageid


test_generate_email_messageid($wwwroot, $msgids)   X-Ref
Test email message id generation

param: string $wwwroot The wwwroot
param: array $msgids An array of msgid local parts and the final result

test_send_email_with_custom_header()   X-Ref
Test email with custom headers


diverted_emails_provider()   X-Ref
A data provider for testing email diversion


test_email_should_be_diverted($divertallemailsto, $divertallemailsexcept, $addresses, $expected)   X-Ref
Test email diversion

param: string $divertallemailsto An optional email address
param: string $divertallemailsexcept An optional exclusion list
param: array $addresses An array of test addresses
param: boolean $expected Expected result

test_email_to_user()   X-Ref
No description

email_to_user_attachment_provider()   X-Ref
Data provider for {@see test_email_to_user_attachment}

return: array

test_email_to_user_attachment(?string $filedir)   X-Ref
Test sending attachments with email_to_user

param: string|null $filedir

test_email_to_user_attachment_missing()   X-Ref
Test sending an attachment that doesn't exist to email_to_user


test_setnew_password_and_mail()   X-Ref
Test setnew_password_and_mail.


generate_confirmation_link_provider()   X-Ref
Data provider for test_generate_confirmation_link

return: Array of confirmation urls and expected resultant confirmation links

test_generate_confirmation_link($username, $confirmationurl, $expected)   X-Ref
Test generate_confirmation_link

param: string $username The name of the user
param: string $confirmationurl The url the user should go to to confirm
param: string $expected The expected url of the confirmation link

test_generate_confirmation_link_with_custom_admin()   X-Ref
Test generate_confirmation_link with custom admin link


test_remove_course_contents()   X-Ref
Test remove_course_content deletes course contents
TODO Add asserts to verify other data related to course is deleted as well.


test_username_load_fields_from_object()   X-Ref
Test function username_load_fields_from_object().


test_count_words(int $expectedcount, string $string, $format = null)   X-Ref
Test function {@see count_words()}.

param: int $expectedcount number of words in $string.
param: string $string the test string to count the words of.
param: int|null $format

count_words_testcases()   X-Ref
Data provider for {@see test_count_words}.

return: array of test cases.

test_count_letters(int $expectedcount, string $string, $format = null)   X-Ref
Test function {@see count_letters()}.

param: int $expectedcount number of characters in $string.
param: string $string the test string to count the letters of.
param: int|null $format

count_letters_testcases()   X-Ref
Data provider for {@see count_letters_testcases}.

return: array of test cases.

test_getremoteaddr()   X-Ref
Tests the getremoteaddr() function.


test_random_bytes_emulate()   X-Ref
No description

test_random_string()   X-Ref
Test function for creation of random strings.


test_complex_random_string()   X-Ref
Test function for creation of complex random strings.


data_private_ips()   X-Ref
Data provider for private ips.


test_ip_is_public_private_ips($ip)   X-Ref
Checks ip_is_public returns false for private ips.

param: string $ip the ipaddress to test

data_public_ips()   X-Ref
Data provider for public ips.


test_ip_is_public_public_ips($ip)   X-Ref
Checks ip_is_public returns true for public ips.

param: string $ip the ipaddress to test

test_can_send_from_real_email_address($email, $display, $samecourse, $config, $result)   X-Ref
Test the function can_send_from_real_email_address

param: string $email Email address for the from user.
param: int $display The user's email display preference.
param: bool $samecourse Are the users in the same course?
param: string $config The CFG->allowedemaildomains config values
param: bool $result The expected result.

data_can_send_from_real_email_address()   X-Ref
Data provider for test_can_send_from_real_email_address.

return: array Returns an array of test data for the above function.

test_generate_email_processing_address()   X-Ref
Test that generate_email_processing_address() returns valid email address.


test_email_is_not_allowed_for_allowemailaddresses($email, $config, $result)   X-Ref
Test allowemailaddresses setting.

param: string $email Email address for the from user.
param: string $config The CFG->allowemailaddresses config values
param: false/string $result The expected result.

data_email_is_not_allowed_for_allowemailaddresses()   X-Ref
Data provider for data_email_is_not_allowed_for_allowemailaddresses.

return: array Returns an array of test data for the above function.

test_email_is_not_allowed_for_denyemailaddresses($email, $config, $result)   X-Ref
Test denyemailaddresses setting.

param: string $email Email address for the from user.
param: string $config The CFG->denyemailaddresses config values
param: false/string $result The expected result.

data_email_is_not_allowed_for_denyemailaddresses()   X-Ref
Data provider for test_email_is_not_allowed_for_denyemailaddresses.

return: array Returns an array of test data for the above function.

test_unserialize_array()   X-Ref
Test safe method unserialize_array().


test_unserialize_object()   X-Ref
Test method for safely unserializing a serialized object of type stdClass


test_component_class_callback_not_found($default)   X-Ref
Test that the component_class_callback returns the correct default value when the class was not found.

param: $default

test_component_class_callback_method_not_found($default)   X-Ref
Test that the component_class_callback returns the correct default value when the class was not found.

param: $default

test_component_class_callback_found_returns_null($default)   X-Ref
Test that the component_class_callback returns the default when the method returned null.

param: $default

test_component_class_callback_found_returns_value($value)   X-Ref
Test that the component_class_callback returns the expected value and not the default when there was a value.

param: $default

test_component_class_callback_found_accepts_multiple($params, $count)   X-Ref
Test that the component_class_callback handles multiple params correctly.

param: $default

component_class_callback_default_provider()   X-Ref
Data provider with list of default values for user in component_class_callback tests.

return: array

component_class_callback_data_provider()   X-Ref
Data provider with list of default values for user in component_class_callback tests.

return: array

component_class_callback_multiple_params_provider()   X-Ref
Data provider with list of default values for user in component_class_callback tests.

return: array

test_get_callable_name($callable, $expectedname)   X-Ref
Test that {@link get_callable_name()} describes the callable as expected.

param: callable $callable
param: string $expectedname

callable_names_provider()   X-Ref
Provides a set of callables and their human readable names.

return: array of (string)case => [(mixed)callable, (string|bool)expected description]

user_data_provider()   X-Ref
No description

test_get_complete_user_data($field, $value, $success, $allowaccountssameemail = 0, $expectedexception = '')   X-Ref
Test for get_complete_user_data().

param: string $field The field to use for the query.
param: string|boolean $value The field value. When fetching by ID, set true to fetch valid user ID, false otherwise.
param: boolean $success Whether we expect for the fetch to succeed or return false.
param: int $allowaccountssameemail Value for $CFG->allowaccountssameemail.
param: string $expectedexception The exception to be expected.

test_send_password_change_info()   X-Ref
Test for send_password_change_().


test_get_time_interval_string(int $time1, int $time2, ?string $format, string $expected)   X-Ref
Test the get_time_interval_string for a range of inputs.

param: int $time1 the time1 param.
param: int $time2 the time2 param.
param: string|null $format the format param.
param: string $expected the expected string.

get_time_interval_string_provider()   X-Ref
Data provider for the test_get_time_interval_string() method.


test_rename_to_unused_name_file()   X-Ref
Tests the rename_to_unused_name function with a file.


test_rename_to_unused_name_dir()   X-Ref
Tests the rename_to_unused_name function with a directory.


test_rename_to_unused_name_failure()   X-Ref
Tests the rename_to_unused_name function with error cases.


display_size_provider()   X-Ref
Provider for display_size

return: array of ($size, $expected)

test_display_size($size, $expected)   X-Ref
Test display_size

param: int $size the size in bytes
param: string $expected the expected string.

display_size_fixed_provider()   X-Ref
Provider for display_size using fixed units.

return: array of ($size, $units, $expected)

test_display_size_fixed(int $size, string $units, string $expected)   X-Ref
Test display_size using fixed units.

param: int $size Size in bytes
param: string $units Fixed units
param: string $expected Expected string.

display_size_dp_provider()   X-Ref
Provider for display_size using specified decimal places.

return: array of ($size, $decimalplaces, $units, $expected)

test_display_size_dp(int $size, int $places, string $units, string $expected)   X-Ref
Test display_size using specified decimal places.

param: int $size Size in bytes
param: int $places Number of decimal places
param: string $units Fixed units
param: string $expected Expected string.

test_get_list_of_plugins(array $expectedlist, array $content, string $dir, string $exclude)   X-Ref
Test that the get_list_of_plugins function includes/excludes directories as appropriate.

param: array $expectedlist The expected list of folders
param: array $content The list of file content to set up in the virtual file root
param: string $dir The base dir to look at in the virtual file root
param: string $exclude Any additional folder to exclude

get_list_of_plugins_provider()   X-Ref
Data provider for get_list_of_plugins checks.

return: array

test_get_home_page(string $user, int $expected, ?int $defaulthomepage = null, ?int $enabledashboard = null,?int $userpreference = null)   X-Ref
Test get_home_page() method.

param: string $user Whether the user is logged, guest or not logged.
param: int $expected Expected value after calling the get_home_page method.
param: int $defaulthomepage The $CFG->defaulthomepage setting value.
param: int $enabledashboard Whether the dashboard should be enabled or not.
param: int $userpreference User preference for the home page setting.

get_home_page_provider()   X-Ref
Data provider for get_home_page checks.

return: array

test_get_default_home_page()   X-Ref
Test get_default_home_page() method.


test_get_performance_info_locks()   X-Ref
Tests the get_performance_info function with regard to locks.


test_get_performance_info_session_wait()   X-Ref
Tests the get_performance_info function with regard to session wait time.


test_html_is_blank()   X-Ref
Test the html_is_blank() function.


is_proxybypass_provider()   X-Ref
Provider for is_proxybypass

return: array of test cases.

test_is_proxybypass(string $url, string $proxybypass, bool $expected)   X-Ref
Check if $url matches anything in proxybypass list

Test function {@see is_proxybypass()}.
param: string $url url to check
param: string $proxybypass
param: bool $expected Expected value.