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]

Data generator.

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

Defines 2 classes


Class: testing_data_generator  - X-Ref

Data generator class for unit tests and other tools that need to create fake test sites.

Class: aptent  - X-Ref

reset()   X-Ref
To be called from data reset code only,
do not use in tests.

return: void

get_plugin_generator($component)   X-Ref
Return generator for given plugin or component.

param: string $component the component name, e.g. 'mod_forum' or 'core_question'.
return: component_generator_base or rather an instance of the appropriate subclass.

create_user($record=null, array $options=null)   X-Ref
Create a test user

param: array|stdClass $record
param: array $options
return: stdClass user record

create_category($record=null, array $options=null)   X-Ref
Create a test course category

param: array|stdClass $record
param: array $options
return: core_course_category course category record

create_cohort($record=null, array $options=null)   X-Ref
Create test cohort.

param: array|stdClass $record
param: array $options
return: stdClass cohort record

create_course($record=null, array $options=null)   X-Ref
Create a test course

param: array|stdClass $record
param: array $options with keys:
return: stdClass course record

create_course_section($record = null, array $options = null)   X-Ref
Create course section if does not exist yet

param: array|stdClass $record must contain 'course' and 'section' attributes
param: array|null $options
return: stdClass

create_block($blockname, $record=null, array $options=array()   X-Ref
Create a test block.

The $record passed in becomes the basis for the new row added to the
block_instances table. You only need to supply the values of interest.
Any missing values have sensible defaults filled in, and ->blockname will be set based on $blockname.

The $options array provides additional data, not directly related to what
will be inserted in the block_instance table, which may affect the block
that is created. The meanings of any data passed here depends on the particular
type of block being created.

param: string $blockname the type of block to create. E.g. 'html'.
param: array|stdClass $record forms the basis for the entry to be inserted in the block_instances table.
param: array $options further, block-specific options to control how the block is created.
return: stdClass new block_instance record.

create_module($modulename, $record=null, array $options=null)   X-Ref
Create a test activity module.

The $record should contain the same data that you would call from
->get_data() when the mod_[type]_mod_form is submitted, except that you
only need to supply values of interest. The only required value is
'course'. Any missing values will have a sensible default supplied.

The $options array provides additional data, not directly related to what
would come back from the module edit settings form, which may affect the activity
that is created. The meanings of any data passed here depends on the particular
type of activity being created.

param: string $modulename the type of activity to create. E.g. 'forum' or 'quiz'.
param: array|stdClass $record data, as if from the module edit settings form.
param: array $options additional data that may affect how the module is created.
return: stdClass activity record new new record that was just inserted in the table

create_group($record)   X-Ref
Create a test group for the specified course

$record should be either an array or a stdClass containing infomation about the group to create.
At the very least it needs to contain courseid.
Default values are added for name, description, and descriptionformat if they are not present.

This function calls groups_create_group() to create the group within the database.
param: array|stdClass $record
return: stdClass group record

create_group_member($record)   X-Ref
Create a test group member

param: array|stdClass $record
return: boolean

create_grouping($record)   X-Ref
Create a test grouping for the specified course

$record should be either an array or a stdClass containing infomation about the grouping to create.
At the very least it needs to contain courseid.
Default values are added for name, description, and descriptionformat if they are not present.

This function calls groups_create_grouping() to create the grouping within the database.
param: array|stdClass $record
return: stdClass grouping record

create_grouping_group($record)   X-Ref
Create a test grouping group

param: array|stdClass $record
return: boolean

create_repository($type, $record=null, array $options = null)   X-Ref
Create an instance of a repository.

param: string type of repository to create an instance for.
param: array|stdClass $record data to use to up set the instance.
param: array $options options
return: stdClass repository instance record

create_repository_type($type, $record=null, array $options = null)   X-Ref
Create an instance of a repository.

param: string type of repository to create an instance for.
param: array|stdClass $record data to use to up set the instance.
param: array $options options
return: repository_type object

create_scale($record=null, array $options=null)   X-Ref
Create a test scale

param: array|stdClass $record
param: array $options
return: stdClass block instance record

create_role($record=null)   X-Ref
Creates a new role in the system.

You can fill $record with the role 'name',
'shortname', 'description' and 'archetype'.

If an archetype is specified it's capabilities,
context where the role can be assigned and
all other properties are copied from the archetype;
if no archetype is specified it will create an
empty role.

param: array|stdClass $record
return: int The new role id

create_role_capability(int $roleid, array $rolecapabilities, context $context = null)   X-Ref
Set role capabilities for the specified role.

param: int $roleid The Role to set capabilities for
param: array $rolecapabilities The list of capability =>permission to set for this role
param: null|context $context The context to apply this capability to

create_tag($record = null)   X-Ref
Create a tag.

param: array|stdClass $record
return: stdClass the tag record

combine_defaults_and_record(array $defaults, $record)   X-Ref
Helper method which combines $defaults with the values specified in $record.
If $record is an object, it is converted to an array.
Then, for each key that is in $defaults, but not in $record, the value
from $defaults is copied.

param: array $defaults the default value for each field with
param: array|stdClass $record
return: array updated $record.

enrol_user($userid, $courseid, $roleidorshortname = null, $enrol = 'manual',$timestart = 0, $timeend = 0, $status = null)   X-Ref
Simplified enrolment of user to course using default options.

It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!

param: int $userid
param: int $courseid
param: int|string $roleidorshortname optional role id or role shortname, use only with manual plugin
param: string $enrol name of enrol plugin,
param: int $timestart (optional) 0 means unknown
param: int $timeend (optional) 0 means forever
param: int $status (optional) default to ENROL_USER_ACTIVE for new enrolments
return: bool success

role_assign($role, $userid, $contextid = false)   X-Ref
Assigns the specified role to a user in the context.

param: int|string $role either an int role id or a string role shortname.
param: int $userid
param: int $contextid Defaults to the system context
return: int new/existing id of the assignment

create_grade_category($record = null)   X-Ref
Create a grade_category.

param: array|stdClass $record
return: stdClass the grade category record

create_grade_grade(?array $record = null)   X-Ref
Create a grade_grade.

param: array $record
return: grade_grade the grade record

create_grade_item($record = null)   X-Ref
Create a grade_item.

param: array|stdClass $record
return: stdClass the grade item record

create_grade_outcome($record = null)   X-Ref
Create a grade_outcome.

param: array|stdClass $record
return: stdClass the grade outcome record

create_lti_tool($data = array()   X-Ref
Helper function used to create an LTI tool.

param: array $data
return: stdClass the tool

create_event($data = [])   X-Ref
Helper function used to create an event.

param: array   $data
return: stdClass

create_custom_field_category($data)   X-Ref
Create a new course custom field category with the given name.

param: array $data Array with data['name'] of category
return: \core_customfield\category_controller   The created category

create_custom_field($data)   X-Ref
Create a new custom field

param: array $data Array with 'name', 'shortname' and 'type' of the field
return: \core_customfield\field_controller   The created field

create_custom_profile_field_category(array $data)   X-Ref
Create a new category for custom profile fields.

param: array $data Array with 'name' and optionally 'sortorder'
return: \stdClass New category object

create_custom_profile_field(array $data)   X-Ref
Creates a new custom profile field.

Optional fields are:

categoryid (or use 'category' to specify by name). If you don't specify
either, it will add the field to a 'Testing' category, which will be created for you if
necessary.

sortorder (if you don't specify this, it will pick the next one in the category).

all the other database fields (if you don't specify this, it will pick sensible defaults
based on the data type).

param: array $data Array with 'datatype', 'shortname', and 'name'
return: \stdClass Database object from the user_info_field table

create_and_enrol($course, $role = 'student', $userparams = null, $enrol = 'manual',$timestart = 0, $timeend = 0, $status = null)   X-Ref
Create a new user, and enrol them in the specified course as the supplied role.

param: \stdClass   $course The course to enrol in
param: string      $role The role to give within the course
param: \stdClass   $userparams User parameters
return: \stdClass   The created user

create_user_course_lastaccess(\stdClass $user, \stdClass $course, int $timestamp)   X-Ref
Create a new last access record for a given user in a course.

param: \stdClass   $user The user
param: \stdClass   $course The course the user accessed
param: int         $timestamp The timestamp for when the user last accessed the course
return: \stdClass   The user_lastaccess record

get_default_plugin_generator(string $component, ?string $classname = null)   X-Ref
Gets a default generator for a given component.

param: string $component The component name, e.g. 'mod_forum' or 'core_question'.
param: string $classname The name of the class missing from the generators file.
return: component_generator_base The generator.