MDL-75275 mod_data: Add user support to entry behat generator

The 'user' parameter has been added to the 'mod_data > entries'
generator, to let define the author of the entry.
When it's not defined, admin is used as the author.
This commit is contained in:
Sara Arjona
2022-08-19 08:38:32 +02:00
parent 17ee072693
commit fd1b874449
2 changed files with 20 additions and 3 deletions
@@ -35,7 +35,7 @@ class behat_mod_data_generator extends behat_generator_base {
'singular' => 'entry',
'datagenerator' => 'entry',
'required' => ['database'],
'switchids' => ['database' => 'databaseid'],
'switchids' => ['database' => 'databaseid', 'user' => 'userid'],
],
'fields' => [
'singular' => 'field',
@@ -81,6 +81,11 @@ class behat_mod_data_generator extends behat_generator_base {
$database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
unset($data['databaseid']);
$userid = 0;
if (array_key_exists('userid', $data)) {
$userid = $data['userid'];
unset($data['userid']);
}
$data = array_reduce(array_keys($data), function ($fields, $fieldname) use ($data, $database) {
global $DB;
@@ -92,7 +97,7 @@ class behat_mod_data_generator extends behat_generator_base {
return $fields;
}, []);
$this->get_data_generator()->create_entry($database, $data);
$this->get_data_generator()->create_entry($database, $data, 0, [], null, $userid);
}
/**
+13 -1
View File
@@ -208,11 +208,19 @@ class mod_data_generator extends testing_module_generator {
* @param int $groupid
* @param array $tags
* @param array $options
* @param int $userid if defined, it will be the author of the entry
* @return int id of the generated record in table {data_records}
*/
public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null) {
public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null, int $userid = 0) {
global $DB, $USER, $CFG;
// Set current user if defined.
if (!empty($userid)) {
$currentuser = $USER;
$user = \core_user::get_user($userid);
$this->set_user($user);
}
$this->databaserecordcount++;
$recordid = data_add_record($data, $groupid);
@@ -362,6 +370,10 @@ class mod_data_generator extends testing_module_generator {
context_module::instance($cm->id), $tags);
}
if (isset($currentuser)) {
$this->set_user($currentuser);
}
return $recordid;
}