Skip to content

Commit

Permalink
CTP-4147 Fix calendar PHPUnit test
Browse files Browse the repository at this point in the history
Rework mod_coursework_generator->create_instance() and
mod_coursework\test_helpers\factory_mixin->create_a_coursework() so
that the former returns a stdClass with a populated cmid property for
use with core Moodle PHPUnit tests, and the latter returns a
mod_coursework\models\coursework instance where cmid is null.  PHPUnit
tests which depend on the latter but previously called
create_instance() have been updated to call create_a_coursework().

This fixes
core_calendar\container_test::test_delete_module_delete_events which
previously failed with "Failed asserting that an object is empty".
  • Loading branch information
leonstr committed Dec 13, 2024
1 parent 0698138 commit 0bf72db
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 113 deletions.
8 changes: 5 additions & 3 deletions classes/test_helpers/factory_mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ protected function get_coursework_generator() {
/**
* Makes a coursework and saves it as $this->coursework
*
* @throws coding_exception
* @param array $params
* @return coursework
*/
protected function create_a_coursework() {
protected function create_a_coursework(array $params = []) {
$generator = $this->get_coursework_generator();
$this->coursework = $generator->create_instance(['course' => $this->get_course()->id]);
$params['course'] = $this->get_course()->id;
$module = $generator->create_instance($params);
$this->coursework = coursework::find($module->id);
return $this->coursework;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/behat/behat_mod_coursework.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ public function there_is_a_coursework() {

$coursework = new stdClass();
$coursework->course = $this->course;
$this->coursework = $generator->create_instance($coursework);
$this->coursework = coursework::find($generator->create_instance($coursework)->id);
}

/**
Expand Down
18 changes: 7 additions & 11 deletions tests/classes/allocation/auto_allocator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,16 @@ public function setUp(): void {
$this->setAdminUser();

$generator = $this->getDataGenerator();
/**
* @var mod_coursework_generator $coursework_generator
*/
$courseworkgenerator = $generator->get_plugin_generator('mod_coursework');

$this->course = $generator->create_course();

$coursework = new stdClass();
$coursework->course = $this->course;
$coursework->moderationenabled = 1;
$coursework->allocationenabled = 1;
$coursework->assessorallocationstrategy = 'equal';
$coursework->moderatorallocationstrategy = 'equal';
$this->coursework = $courseworkgenerator->create_instance($coursework);
$params = [
'moderationenabled' => 1,
'allocationenabled' => 1,
'assessorallocationstrategy' => 'equal',
'moderatorallocationstrategy' => 'equal',
];
$this->coursework = $this->create_a_coursework($params);

$this->create_a_student();
$this->create_a_teacher();
Expand Down
9 changes: 1 addition & 8 deletions tests/classes/allocation/form/table_processor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ public function setUp(): void {
$this->resetAfterTest();
$this->setAdminUser();

$generator = $this->getDataGenerator();
$courseworkgenerator = $generator->get_plugin_generator('mod_coursework');

$this->course = $generator->create_course();
$coursework = new stdClass();
$coursework->course = $this->course;
$coursework->numberofmarkers = 2;
$this->coursework = $courseworkgenerator->create_instance($coursework);
$this->coursework = $this->create_a_coursework(['numberofmarkers' => 2]);

$this->create_a_student();
$this->create_a_teacher();
Expand Down
62 changes: 27 additions & 35 deletions tests/classes/export/csv_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ public function test_one_stage(): void {
$dateformat = '%a, %d %b %Y, %H:%M';
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');

$coursework = $generator->create_instance(
[
'course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 1,
'deadline' => time() + 86400,
'extensionsenabled' => 1,
]
);
$params = [
'grade' => 100,
'numberofmarkers' => 1,
'deadline' => time() + 86400,
'extensionsenabled' => 1,
];
$coursework = $this->create_a_coursework($params);
$submission = new stdClass();
$submission->userid = $this->student->id;
$submission->allocatableid = $this->student->id;
Expand Down Expand Up @@ -152,14 +150,12 @@ public function test_two_stages(): void {
$dateformat = '%a, %d %b %Y, %H:%M';
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');

$coursework = $generator->create_instance(
[
'course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 2,
'deadline' => strtotime('-1 day', $timenow), // This means that the submissions will be late.
]
);
$params = [
'grade' => 100,
'numberofmarkers' => 2,
'deadline' => strtotime('-1 day', $timenow), // This means that the submissions will be late.
];
$coursework = $this->create_a_coursework($params);

$submission = new stdClass();
$submission->userid = $this->student->id;
Expand Down Expand Up @@ -251,15 +247,13 @@ public function test_student_not_in_sample(): void {
$dateformat = '%a, %d %b %Y, %H:%M';
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');

$coursework = $generator->create_instance(
[
'course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 2,
'samplingenabled' => 1,
'deadline' => time() + 86400,
]
);
$params = [
'grade' => 100,
'numberofmarkers' => 2,
'samplingenabled' => 1,
'deadline' => time() + 86400,
];
$coursework = $this->create_a_coursework($params);
$submission = new stdClass();
$submission->userid = $this->student->id;
$submission = $generator->create_submission($submission, $coursework);
Expand Down Expand Up @@ -330,15 +324,13 @@ public function test_two_students_one_in_sample(): void {
$dateformat = '%a, %d %b %Y, %H:%M';
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');

$coursework = $generator->create_instance(
[
'course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 2,
'samplingenabled' => 1,
'deadline' => time() + 86400,
]
);
$params = [
'grade' => 100,
'numberofmarkers' => 2,
'samplingenabled' => 1,
'deadline' => time() + 86400,
];
$coursework = $this->create_a_coursework($params);
$student1 = $this->student;
$assessor1 = $this->teacher;
$assessor2 = $this->otherteacher;
Expand Down
27 changes: 13 additions & 14 deletions tests/classes/export/grading_sheet_download_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public function setUp(): void {
public function test_one_stage_no_allocations(): void {

$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 1,
'deadline' => time() + 86400]);
$params = [
'grade' => 100,
'numberofmarkers' => 1,
'deadline' => time() + 86400,
];
$coursework = $this->create_a_coursework($params);
$submission = new stdClass();
$submission->userid = $this->student->id;
$submission->allocatableid = $this->student->id;
Expand Down Expand Up @@ -101,16 +103,13 @@ public function test_one_stage_no_allocations(): void {
*/
public function test_two_stages_with_allocations(): void {
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');

$coursework = $generator->create_instance(
[
'course' => $this->course->id,
'grade' => 100,
'numberofmarkers' => 2,
'allocationenabled' => 1,
'deadline' => time() + 86400,
]
);
$params = [
'grade' => 100,
'numberofmarkers' => 2,
'allocationenabled' => 1,
'deadline' => time() + 86400,
];
$coursework = $this->create_a_coursework($params);

// 2 assessors
$assessor1 = $this->teacher;
Expand Down
58 changes: 25 additions & 33 deletions tests/classes/models/coursework_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ final class coursework_test extends advanced_testcase {
public function setUp(): void {
$this->resetAfterTest();
$this->course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$this->setAdminUser();
$this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]);
$this->coursework = $this->create_a_coursework(['grade' => 0]);
}

/**
Expand Down Expand Up @@ -91,8 +90,7 @@ public function test_get_allocation_manager(): void {
$this->assertInstanceOf('\mod_coursework\allocation\manager', $allocationmanager);

// Now make a new coursework with a duff class name.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]);
$this->coursework = $this->create_a_coursework(['grade' => 0]);
$this->coursework->assessorallocationstrategy = 'duffclass';
$this->coursework->save();

Expand All @@ -101,67 +99,61 @@ public function test_get_allocation_manager(): void {
}

public function test_group_decorator_is_added(): void {
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0,
'use_groups' => true]);
$params = [
'grade' => 0,
'use_groups' => true,
];
$coursework = $this->create_a_coursework($params);
$this->assertInstanceOf('\mod_coursework\decorators\coursework_groups_decorator', coursework::find($coursework->id));
}

public function test_group_decorator_is_not_added(): void {
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0]);
$coursework = $this->create_a_coursework(['grade' => 0]);
$this->assertInstanceOf('\mod_coursework\models\coursework',
coursework::find($coursework->id));
}

public function test_get_user_group_no_grouping(): void {

$generator = $this->get_coursework_generator();

$this->create_a_student();
$this->create_a_group();
$this->add_student_to_the_group();

$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0,
'use_groups' => true]);

$params = [
'grade' => 0,
'use_groups' => true,
];
$coursework = $this->create_a_coursework($params);
$this->assertEquals($this->group->id, $coursework->get_student_group($this->student)->id);

}

public function test_get_user_group_with_grouping(): void {

$generator = $this->get_coursework_generator();

$this->create_a_student();
$this->create_a_group();
$this->create_a_grouping_and_add_the_group_to_it();
$this->add_student_to_the_group();

$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0,
'use_groups' => true,
'grouping_id' => $this->grouping->id]);

$params = [
'grade' => 0,
'use_groups' => true,
'grouping_id' => $this->grouping->id,
];
$coursework = $this->create_a_coursework($params);
$this->assertEquals($this->group->id, $coursework->get_student_group($this->student)->id);
}

public function test_get_user_group_with_wrong_grouping(): void {
$generator = $this->get_coursework_generator();

$this->create_a_student();
$this->create_a_group();
$this->create_a_grouping_and_add_the_group_to_it();
$this->add_student_to_the_group();

$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0,
'use_groups' => true,
'grouping_id' => 543]);

$params = [
'grade' => 0,
'use_groups' => true,
'grouping_id' => 543,
];
$coursework = $this->create_a_coursework($params);
$this->assertFalse($coursework->get_student_group($this->student));
}

Expand Down
6 changes: 2 additions & 4 deletions tests/classes/models/submission_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function setUp(): void {
$this->resetAfterTest();

$this->course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$this->setAdminUser();
$this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]);
$this->coursework = $this->create_a_coursework(['grade' => 0]);
$this->redirectMessages();
$this->preventResetByRollback();

Expand Down Expand Up @@ -101,8 +100,7 @@ public function test_save_courseworkid(): void {

public function test_group_decorator_is_not_added(): void {
$generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework');
$coursework = $generator->create_instance(['course' => $this->course->id,
'grade' => 0]);
$coursework = $this->create_a_coursework(['grade' => 0]);

$submission = new stdClass();
$submission->userid = 2;
Expand Down
3 changes: 1 addition & 2 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public function create_instance($record = null, array $options = null) {
$record->moderatorallocationstrategy = 'none';
}

return coursework::find(parent::create_instance($record, $options)->id);

return parent::create_instance($record, $options);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace mod_coursework;

use mod_coursework\models\coursework;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand Down Expand Up @@ -83,7 +85,7 @@ public function test_create_instance(): void {
$this->assertEquals($course->id, $cm->course);

$context = \context_module::instance($cm->id);
$this->assertEquals($coursework->get_coursemodule_id(), $context->instanceid);
$this->assertEquals(coursework::find($coursework)->get_coursemodule_id(), $context->instanceid);

// Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
$gitem = $DB->get_record('grade_items',
Expand Down Expand Up @@ -183,7 +185,7 @@ public function test_create_submission(): void {
$data->userid = $user->id;

// Should fail because we have no assessorid and we have no logged ourselves in.
$submission = $generator->create_submission($data, $coursework);
$submission = $generator->create_submission($data, coursework::find($coursework));
$submission = $DB->get_record('coursework_submissions', ['id' => $submission->id]);

$this->assertNotEmpty($submission);
Expand Down

0 comments on commit 0bf72db

Please sign in to comment.