Skip to content

Commit

Permalink
MDL-81612 assign: test generation of extensions
Browse files Browse the repository at this point in the history
Also, some other improvements.

* In Behat you can now use either idnumber of name to identify an
  assignment you are generating things for.
* create_submission generator methods now wants the cmid in $data['cmid']
  not the confusingly named 'assignid'. The old name is accepted, but
  generates a debugging warning. (That will fail the test, but in a way
  that explains how to fix it.)
  • Loading branch information
timhunt committed Sep 19, 2024
1 parent d3749a8 commit 7bde044
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mod/assign/tests/downloader_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function test_load_filelist(
}
$datagenerator->create_submission([
'userid' => $user->id,
'assignid' => $cm->id,
'cmid' => $cm->id,
'file' => implode(',', $files),
]);
}
Expand Down
14 changes: 10 additions & 4 deletions mod/assign/tests/generator/behat_mod_assign_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ protected function get_creatable_entities(): array {
'required' => ['assign', 'user'],
'switchids' => ['assign' => 'assignid', 'user' => 'userid'],
],
'extensions' => [
'singular' => 'extension',
'datagenerator' => 'extension',
'required' => ['assign', 'user', 'extensionduedate'],
'switchids' => ['assign' => 'cmid', 'user' => 'userid'],
],
];
}

/**
* Get the assignment CMID using an activity idnumber.
* Get the assignment cmid using an activity name or idnumber.
*
* @param string $idnumber
* @param string $identifier activity name or idnumber
* @return int The cmid
*/
protected function get_assign_id(string $idnumber): int {
return $this->get_activity_id($idnumber);
protected function get_assign_id(string $identifier): int {
return $this->get_cm_by_activity_name('assign', $identifier)->id;
}
}
36 changes: 32 additions & 4 deletions mod/assign/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,22 @@ public function create_instance($record = null, array $options = null) {
/**
* Create an assignment submission.
*
* @param array $data
* @param array $data with keys userid, cmid and
* then data for each assignsubmission plugin used.
* For backwards compatibility, you can pass cmid as 'assignid' but that generates a warning.
*/
public function create_submission(array $data): void {
global $USER;

if (array_key_exists('assignid', $data)) {
debugging(
'The cmid passed to create_submission should have array key cmid, not assignid.',
DEBUG_DEVELOPER,
);
$data['cmid'] = $data['assignid'];
unset($data['assignid']);
}

$currentuser = $USER;
$user = \core_user::get_user($data['userid']);
$this->set_user($user);
Expand All @@ -90,7 +101,7 @@ public function create_submission(array $data): void {
'userid' => $user->id,
];

[$course, $cm] = get_course_and_cm_from_cmid($data['assignid'], 'assign');
[$course, $cm] = get_course_and_cm_from_cmid($data['cmid'], 'assign');
$context = context_module::instance($cm->id);
$assign = new assign($context, $cm, $course);

Expand All @@ -102,11 +113,28 @@ public function create_submission(array $data): void {
}
}

$assign->save_submission((object) $submission, $notices);
$assign->save_submission($submission, $notices);

$this->set_user($currentuser);
}

/**
* Create an assignment extension.
*
* @param array $data must have keys cmid, userid, extensionduedate.
*/
public function create_extension(array $data): void {
$user = \core_user::get_user($data['userid'], '*', MUST_EXIST);

[$course, $cm] = get_course_and_cm_from_cmid($data['cmid'], 'assign');
$context = context_module::instance($cm->id);
$assign = new assign($context, $cm, $course);

if (!$assign->save_user_extension($user->id, $data['extensionduedate'] ?: null)) {
throw new \core\exception\coding_exception('The requested extension could not be created.');
}
}

/**
* Gets the grouping id from it's idnumber.
*
Expand All @@ -119,7 +147,7 @@ protected function get_grouping_id(string $idnumber): int {

// Do not fetch grouping ID for empty grouping idnumber.
if (empty($idnumber)) {
return null;
throw new \core\exception\coding_exception('idnumber cannot be empty');
}

if (!$id = $DB->get_field('groupings', 'id', ['idnumber' => $idnumber])) {
Expand Down

0 comments on commit 7bde044

Please sign in to comment.