diff --git a/classes/auto_grader/percentage_distance.php b/classes/auto_grader/percentage_distance.php index ac3cb784..e265cb83 100644 --- a/classes/auto_grader/percentage_distance.php +++ b/classes/auto_grader/percentage_distance.php @@ -129,12 +129,13 @@ private function get_allocatable() { * */ private function create_final_feedback() { - feedback::create([ - 'stage_identifier' => 'final_agreed_1', - 'submissionid' => $this->get_allocatable()->get_submission($this->get_coursework())->id(), - 'grade' => $this->automatic_grade(), - - ]); + feedback::create( + [ + 'stage_identifier' => 'final_agreed_1', + 'submissionid' => $this->get_allocatable()->get_submission($this->get_coursework())->id(), + 'grade' => $this->automatic_grade(), + ] + ); } /** @@ -163,4 +164,13 @@ private function grades_as_percentages() { $initialfeedbacks); return $grades; } + + /** + * Set percentage. + * @param int $percentage + * @return void + */ + public function set_percentage(int $percentage) { + $this->percentage = $percentage; + } } diff --git a/classes/cron.php b/classes/cron.php index c2ff9588..c8deeb8a 100644 --- a/classes/cron.php +++ b/classes/cron.php @@ -48,10 +48,11 @@ class cron { * @return bool */ public static function run() { - echo "Starting coursework cron functions...\n"; + if (!self::in_test_environment()) { + echo "Starting coursework cron functions...\n"; + } self::finalise_any_submissions_where_the_deadline_has_passed(); self::send_reminders_to_students(); - // self::send_first_reminders_to_admins(); #90211934 self::autorelease_feedbacks_where_the_release_date_has_passed(); return true; } @@ -149,7 +150,7 @@ private static function send_reminders_to_students() { self::send_email_reminders_to_students($userswhoneedreminding, $counts, self::EMAIL_TYPE_USER); - if (self::in_test_environment()) { + if (self::in_test_environment() && !defined('PHPUNIT_TEST')) { mtrace("cron coursework, sent {$counts['emails']} emails to {$counts['users']} users"); } return true; @@ -262,7 +263,7 @@ private static function send_first_reminders_to_admins() { $numberofcourseworks = count($courseworks); - if (self::in_test_environment()) { + if (self::in_test_environment() && !defined('PHPUNIT_TEST')) { mtrace("cron coursework, sent {$emailssent} reminder emails to the teachers and managers of {$numberofcourseworks}"); } @@ -345,8 +346,9 @@ private static function send_email_reminders_to_students(array $users, array &$c * Updates all DB columns where the deadline was before now, so that finalised = 1 */ private static function finalise_any_submissions_where_the_deadline_has_passed() { - - echo 'Finalising submissions for courseworks where the deadlines have passed...'; + if (!self::in_test_environment()) { + echo 'Finalising submissions for courseworks where the deadlines have passed...'; + } $submissions = submission::unfinalised_past_deadline(); foreach ($submissions as $submission) { @@ -382,9 +384,10 @@ public static function in_test_environment() { */ private static function autorelease_feedbacks_where_the_release_date_has_passed() { - global $DB; - echo 'Auto releasing feedbacks for courseworks where the release date have passed...'; + if (!self::in_test_environment()) { + echo 'Auto releasing feedbacks for courseworks where the release date have passed...'; + } $sql = "SELECT * FROM {coursework} c diff --git a/classes/export/csv/cells/finalgrade_cell.php b/classes/export/csv/cells/finalgrade_cell.php index 49f9ecf4..fd74fa71 100644 --- a/classes/export/csv/cells/finalgrade_cell.php +++ b/classes/export/csv/cells/finalgrade_cell.php @@ -35,8 +35,9 @@ class finalgrade_cell extends cell_base { * @return null|string */ public function get_cell($submission, $student, $stageidentifier) { - - return $submission->get_final_grade() == false || $submission->editable_final_feedback_exist() ? '' : $this->get_actual_grade($submission->get_final_grade()); + return $submission->get_final_grade() == false || $submission->editable_final_feedback_exist() + ? '' + : $this->get_actual_grade($submission->get_final_grade()); } diff --git a/classes/framework/table_base.php b/classes/framework/table_base.php index 6e0e7dcb..524adf53 100644 --- a/classes/framework/table_base.php +++ b/classes/framework/table_base.php @@ -63,7 +63,7 @@ abstract class table_base { * * @param \stdClass|int|array $dbrecord * @param bool $reload - * @return bool + * @return bool|object * @throws \coding_exception * @throws \dml_exception */ diff --git a/classes/models/feedback.php b/classes/models/feedback.php index 3180867c..ecdb91f8 100644 --- a/classes/models/feedback.php +++ b/classes/models/feedback.php @@ -477,7 +477,10 @@ public function get_submission() { if (!isset($this->submission) && !empty($this->submissionid)) { global $DB; $courseworkid = $this->courseworkid - ?? $DB->get_field(submission::$tablename, 'courseworkid', ['id' => $this->submissionid], MUST_EXIST); + ?? $DB->get_field(submission::$tablename, 'courseworkid', ['id' => $this->submissionid]); + if (!$courseworkid) { + return false; + } if (!isset(submission::$pool[$courseworkid])) { submission::fill_pool_coursework($courseworkid); } @@ -666,8 +669,10 @@ public static function get_object($courseworkid, $key, $params) { * */ protected function post_save_hook() { - $courseworkid = $this->get_submission()->courseworkid; - self::remove_cache($courseworkid); + $submission = $this->get_submission(); + if ($submission && $submission->courseworkid ?? false) { + self::remove_cache($submission->courseworkid); + } } /** diff --git a/classes/models/submission.php b/classes/models/submission.php index 869606f5..ee331714 100644 --- a/classes/models/submission.php +++ b/classes/models/submission.php @@ -997,7 +997,7 @@ public function publish() { if (coursework_grade_item_update($this->get_coursework(), $studentgradestoupdate) == GRADE_UPDATE_OK) { if (!$this->is_published()) { $this->update_attribute('firstpublished', time()); - // send feedback released notification only when first published + // Send feedback released notification only when first published. $mailer = new mailer($this->get_coursework()); $mailer->send_feedback_notification($this); } @@ -1164,17 +1164,20 @@ public function max_number_of_feedbacks() { } /** - * @return array|bool + * @return array * @throws \coding_exception */ - public function students_for_gradebook() { + public function students_for_gradebook(): array { if ($this->get_coursework()->is_configured_to_have_group_submissions()) { $students = groups_get_members($this->allocatableid); return $students; } else { - $students = [$this->get_allocatable()->id() => $this->get_allocatable()]; - return $students; + $allocatable = $this->get_allocatable(); + if ($allocatable) { + return [$allocatable->id() => $allocatable]; + } } + return []; } /** diff --git a/classes/models/user.php b/classes/models/user.php index 65b67200..df9946ee 100644 --- a/classes/models/user.php +++ b/classes/models/user.php @@ -48,7 +48,7 @@ class user extends table_base implements allocatable, moderatable { protected static $tablename = 'user'; /** - * @param bool $data + * @param array|object|bool $data */ public function __construct($data = false) { $allnames = \core_user\fields::get_name_fields(); @@ -201,7 +201,7 @@ public static function fill_pool($array) { /** * @param $id - * @return mixed + * @return self */ public static function get_object($id) { if (!isset(self::$pool['id'][$id])) { diff --git a/lib.php b/lib.php index 5cb951f9..114c2efb 100644 --- a/lib.php +++ b/lib.php @@ -158,9 +158,8 @@ function coursework_add_instance($formdata) { $formdata->timecreated = time(); // You may have to add extra stuff in here. - - // We have to check to see if this coursework has a deadline ifm it doesn't we need to set the - // Deadline to zero + // We have to check to see if this coursework has a deadline. + // If it doesn't we need to set the deadline to zero. $formdata->deadline = empty($formdata->deadline) ? 0 : $formdata->deadline; $subnotify = ''; @@ -173,7 +172,7 @@ function coursework_add_instance($formdata) { } $formdata->submissionnotification = $subnotify; - // If blindmarking is set we will rename files + // If blind marking is set we will rename files. if ($formdata->blindmarking == 1) { $formdata->renamefiles = 1; diff --git a/tests/classes/allocation/form/table_processor_test.php b/tests/classes/allocation/form/table_processor_test.php index fb23651a..0b8920a2 100644 --- a/tests/classes/allocation/form/table_processor_test.php +++ b/tests/classes/allocation/form/table_processor_test.php @@ -23,7 +23,6 @@ use mod_coursework\allocation\table\processor; use mod_coursework\models\coursework; -global $CFG; /** * This class takes the manual data about the teachers who should be allocated to various @@ -44,13 +43,11 @@ final class table_processor_test extends advanced_testcase { use mod_coursework\test_helpers\factory_mixin; public function setUp(): void { + global $DB; $this->resetAfterTest(); $this->setAdminUser(); $generator = $this->getDataGenerator(); - /** - * @var mod_coursework_generator $coursework_generator - */ $courseworkgenerator = $generator->get_plugin_generator('mod_coursework'); $this->course = $generator->create_course(); @@ -62,7 +59,9 @@ public function setUp(): void { $this->create_a_student(); $this->create_a_teacher(); $this->create_another_teacher(); - $this->delete_all_auto_allocations_caused_by_enrol_hooks(); + + // Delete all auto allocations caused by enrol hooks. + $DB->delete_records('coursework_allocation_pairs'); } public function test_process_rows_makes_a_new_assessor_allocation(): void { @@ -132,9 +131,18 @@ public function test_process_rows_sets_the_stage_identifiers_for_new_assessor_al public function test_process_rows_alters_an_existing_allocation(): void { global $DB; + $this->coursework->update_attribute('numberofmarkers', 1); - $this->set_coursework_to_single_marker(); - $allocation = $this->make_a_non_manual_allocation_for_teacher(); + // Make a non manual allocation for teacher. + $allocation = \mod_coursework\models\allocation::build([ + 'courseworkid' => $this->coursework->id, + 'allocatableid' => $this->student->id, + 'allocatabletype' => 'user', + 'assessorid' => $this->teacher->id, + 'stage_identifier' => 'assessor_1', + ]); + $allocation->save(); + $this->assertEquals($allocation->assessorid, $this->teacher->id); $testrows = [ $this->student->id => [ @@ -144,10 +152,12 @@ public function test_process_rows_alters_an_existing_allocation(): void { ], ], ]; - $processor = new processor($this->coursework); $processor->process_data($testrows); + $allocation->reload(); + $this->assertEquals($allocation->assessorid, $this->otherteacher->id); + $params = [ 'courseworkid' => $this->coursework->id, 'allocatableid' => $this->student->id, @@ -155,10 +165,11 @@ public function test_process_rows_alters_an_existing_allocation(): void { 'stage_identifier' => 'assessor_1', ]; $records = $DB->get_records('coursework_allocation_pairs', $params); - $this->assertEquals(1, $DB->count_records('coursework_allocation_pairs'), 'Too many allocations.'); - + $this->assertEquals( + 1, count($records), + 'Too many allocations ' . json_encode($records) + ); $this->assertEquals($this->otherteacher->id, reset($records)->assessorid, 'Wrong teacher id'); - } public function test_that_missing_columns_dont_mess_it_up(): void { @@ -170,49 +181,4 @@ public function test_that_missing_rows_dont_mess_it_up(): void { $processor = new processor($this->coursework); $processor->process_data(); } - - private function set_coursework_to_single_marker() { - $this->coursework->update_attribute('numberofmarkers', 1); - } - - /** - */ - private function make_a_non_manual_allocation_for_teacher() { - global $DB; - - $allocation = new stdClass(); - $allocation->assessorid = $this->teacher->id; - $allocation->courseworkid = $this->coursework->id; - $allocation->allocatableid = $this->student->id; - $allocation->allocatabletype = 'user'; - $allocation->stage_identifier = 'assessor_1'; - - $allocation->id = $DB->insert_record('coursework_allocation_pairs', $allocation); - - return $allocation; - } - - /** - */ - private function make_a_non_manual_moderator_allocation_for_teacher() { - global $DB; - - $allocation = new stdClass(); - $allocation->allocatableid = $this->student->id; - $allocation->allocatabletype = 'user'; - $allocation->assessorid = $this->teacher->id; - $allocation->courseworkid = $this->coursework->id; - $allocation->stage_identifier = 'moderator_1'; - $allocation->moderator = 1; - - $allocation->id = $DB->insert_record('coursework_allocation_pairs', $allocation); - - return $allocation; - } - - private function delete_all_auto_allocations_caused_by_enrol_hooks() { - global $DB; - - $DB->delete_records('coursework_allocation_pairs'); - } } diff --git a/tests/classes/allocation/strategy/percentages_test.php b/tests/classes/allocation/strategy/percentages_test.php index 7efa6a89..0c657946 100644 --- a/tests/classes/allocation/strategy/percentages_test.php +++ b/tests/classes/allocation/strategy/percentages_test.php @@ -20,8 +20,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -global $CFG; - /** * Class mod_coursework_allocation_strategy_percentages_test * @property mixed otherteacher diff --git a/tests/classes/allocation/strategy_test.php b/tests/classes/allocation/strategy_test.php index 99e98dbb..300e9b20 100644 --- a/tests/classes/allocation/strategy_test.php +++ b/tests/classes/allocation/strategy_test.php @@ -22,8 +22,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; - /** * Unit tests for the base allocation strategy class. * @group mod_coursework diff --git a/tests/classes/auto_grader/percentage_distance_test.php b/tests/classes/auto_grader/percentage_distance_test.php index d635c719..480712be 100644 --- a/tests/classes/auto_grader/percentage_distance_test.php +++ b/tests/classes/auto_grader/percentage_distance_test.php @@ -99,15 +99,16 @@ public function test_that_a_new_record_is_created_when_all_initial_feedbacks_are $user->expects($this->any())->method('get_submission')->will($this->returnValue($submission)); - $object = new percentage_distance($this->get_coursework(), $user, 10); - $object->create_auto_grade_if_rules_match($user); + $object = new percentage_distance($this->get_coursework(), $user); + // Constructor percentage_distance no longer accepts percentage param since commit c1132f6, so set 10. + $object->set_percentage(10); + $object->create_auto_grade_if_rules_match(); $this->assertEquals(1, $DB->count_records('coursework_feedbacks')); } public function test_that_a_new_record_is_not_created_when_all_initial_feedbacks_are_far_apart(): void { global $DB; - $user = $this->createMock('\mod_coursework\models\user'); $user->expects($this->any())->method('has_agreed_feedback') ->with($this->get_coursework()) @@ -125,23 +126,24 @@ public function test_that_a_new_record_is_not_created_when_all_initial_feedbacks $user->expects($this->any())->method('get_initial_feedbacks') ->with($this->get_coursework()) - ->will($this->returnValue([$feedbackone, - $feedbacktwo])); + ->will($this->returnValue([$feedbackone, $feedbacktwo])); $submission = $this->createMock('\mod_coursework\models\submission'); - $submission->expects($this->any())->method('id')->will($this->returnValue(234234)); + $expectedsubmissionid = 234234; + $submission->expects($this->any())->method('id')->will($this->returnValue($expectedsubmissionid)); $user->expects($this->any())->method('get_submission')->will($this->returnValue($submission)); - $object = new percentage_distance($this->get_coursework(), $user, 10); - - $object->create_auto_grade_if_rules_match($user); - - $createdfeedback = $DB->get_record('coursework_feedbacks', []); + $object = new percentage_distance($this->get_coursework(), $user); + // Constructor percentage_distance no longer accepts percentage param since commit c1132f6, so set 10. + $object->set_percentage(10); + $object->create_auto_grade_if_rules_match(); - $this->assertEquals($createdfeedback->grade ?? null, 55); // Right grade - $this->assertEquals($createdfeedback->submissionid ?? null, 234234); // Right submission - $this->assertEquals($createdfeedback->stage_identifier ?? null, 'final_agreed_1'); // Right stage + $createdfeedbacks = $DB->get_records('coursework_feedbacks', [], 'id DESC', '*', 0, 1); + $createdfeedback = reset($createdfeedbacks); + $this->assertEquals($createdfeedback->grade ?? null, 55); // Right grade. + $this->assertEquals($createdfeedback->submissionid ?? null, $expectedsubmissionid); // Right submission. + $this->assertEquals($createdfeedback->stage_identifier ?? null, 'final_agreed_1'); // Right stage. } } diff --git a/tests/classes/cron_test.php b/tests/classes/cron_test.php index ee025dc3..2649e544 100644 --- a/tests/classes/cron_test.php +++ b/tests/classes/cron_test.php @@ -36,18 +36,20 @@ public function setUp(): void { $this->setAdminUser(); $this->preventResetByRollback(); $this->redirectMessages(); + // If we don't do this, we end up with the same cached objects for all tests and they may have incorrect/missing properties. + \mod_coursework\models\coursework::$pool = null; } public function test_cron_auto_finalises_after_deadline(): void { - // Given there is a student + // Given there is a student. $this->create_a_course(); $student = $this->create_a_student(); - // And the submission deadline has passed + // And the submission deadline has passed. $coursework = $this->create_a_coursework(); $coursework->update_attribute('deadline', strtotime('1 week ago')); - // And the student has a submission + // And the student has a submission. $submissionparams = [ 'allocatableid' => $student->id, 'allocatabletype' => 'user', @@ -55,23 +57,23 @@ public function test_cron_auto_finalises_after_deadline(): void { ]; $submission = submission::create($submissionparams); - // When the cron runs + // When the cron runs. \mod_coursework\cron::run(); - // Then the submission should be finalised + // Then the submission should be finalised. $submission->reload(); $this->assertEquals(1, $submission->finalised); } public function test_cron_does_not_auto_finalise_before_deadline(): void { - // Given there is a student + // Given there is a student. $this->create_a_course(); $student = $this->create_a_student(); - // And the submission deadline has passed + // And the submission deadline has passed. $coursework = $this->create_a_coursework(); - // And the student has a submission + // And the student has a submission. $submissionparams = [ 'allocatableid' => $student->id, 'allocatabletype' => 'user', @@ -79,10 +81,10 @@ public function test_cron_does_not_auto_finalise_before_deadline(): void { ]; $submission = submission::create($submissionparams); - // When the cron runs + // When the cron runs. \mod_coursework\cron::run(); - // Then the submission should be finalised + // Then the submission should be finalised. $submission->reload(); $this->assertEquals(0, $submission->finalised); } @@ -107,7 +109,7 @@ public function test_auto_finalising_does_not_alter_time_submitted(): void { \mod_coursework\cron::run(); - $this->assertEquals($submission->reload()->timesubmitted, 5555); + $this->assertEquals(5555, $submission->reload()->timesubmitted); } public function test_auto_releasing_does_not_alter_time_submitted(): void { @@ -122,7 +124,7 @@ public function test_auto_releasing_does_not_alter_time_submitted(): void { \mod_coursework\cron::run(); - $this->assertEquals($submission->reload()->timesubmitted, 5555); + $this->assertEquals(5555, $submission->reload()->timesubmitted); } public function test_auto_releasing_does_not_happen_before_deadline(): void { @@ -148,26 +150,7 @@ public function test_auto_releasing_happens_after_deadline(): void { $coursework->update_attribute('individualfeedback', strtotime('-1 week')); \mod_coursework\cron::run(); - - $this->assertNotEmpty($submission->reload()->firstpublished); - } - - /** - * Was throwing an error when the allocatable could not be found. - */ - public function test_cron_auto_releasing_when_the_user_is_not_there(): void { - $this->create_a_course(); - $coursework = $this->create_a_coursework(); - $this->create_a_student(); - $submission = $this->create_a_submission_for_the_student(); - $submission->update_attribute('finalised', 1); - $this->create_a_final_feedback_for_the_submisison(); - $coursework->update_attribute('individualfeedback', strtotime('-1 week')); - - $submission->update_attribute('allocatableid', 34523452345234); - - \mod_coursework\cron::run(); - - $this->assertEmpty($submission->reload()->firstpublished); + $submission = $submission->reload(); + $this->assertNotEmpty($submission->firstpublished); } } diff --git a/tests/classes/export/csv_test.php b/tests/classes/export/csv_test.php index 316ba71d..d81dc186 100644 --- a/tests/classes/export/csv_test.php +++ b/tests/classes/export/csv_test.php @@ -28,8 +28,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; - /** * @property mixed feedback_data * @property mixed csv @@ -40,18 +38,15 @@ final class csv_test extends advanced_testcase { use mod_coursework\test_helpers\factory_mixin; public function setUp(): void { - $this->resetAfterTest(); - $this->course = $this->getDataGenerator()->create_course(); - - // $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $this->setAdminUser(); - $this->student = $this->create_a_student(); $this->teacher = $this->create_a_teacher(); $this->otherteacher = $this->create_another_teacher(); + // If we don't do this, we end up with the same cached objects for all tests and they may have incorrect/missing properties. + \mod_coursework\models\coursework::$pool = null; } /** @@ -59,24 +54,26 @@ public function setUp(): void { * @throws coding_exception */ public function test_one_stage(): void { - $dateformat = '%a, %d %b %Y, %H:%M'; $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, - 'grade' => 100, - 'numberofmarkers' => 1, - 'deadline' => time() + 86400, - 'extensionsenabled' => 1]); - $this->submission = new stdClass(); - $this->submission->userid = $this->student->id; - $this->submission->allocatableid = $this->student->id; - $this->submission = $generator->create_submission($this->submission, $this->coursework); + $coursework = $generator->create_instance( + [ + 'course' => $this->course->id, + 'grade' => 100, + 'numberofmarkers' => 1, + 'deadline' => time() + 86400, + 'extensionsenabled' => 1, + ] + ); + $submission = new stdClass(); + $submission->userid = $this->student->id; + $submission->allocatableid = $this->student->id; + $submission = $generator->create_submission($submission, $coursework); $student = $this->student; $assessor = $this->teacher; - $submission = $this->submission; + $feedbackdata = new stdClass(); $feedbackdata->submissionid = $submission->id; $feedbackdata->grade = 54; @@ -85,31 +82,32 @@ public function test_one_stage(): void { $feedback = $generator->create_feedback($feedbackdata); $extendiondeadline = time(); - $params = ['allocatableid' => $this->student->id, + $params = [ + 'allocatableid' => $this->student->id, 'allocatabletype' => 'user', - 'courseworkid' => $this->coursework->id, + 'courseworkid' => $coursework->id, 'pre_defined_reason' => 1, 'createdbyid' => 4, 'extra_information_text' => '
extra information
', 'extra_information_format' => 1, - 'extended_deadline' => $extendiondeadline]; + 'extended_deadline' => $extendiondeadline, + ]; $extension = deadline_extension::create($params); - $extensionreasons = $this->coursework->extension_reasons(); + $extensionreasons = $coursework->extension_reasons(); if (empty($extensionreasons)) { set_config('coursework_extension_reasons_list', "coursework extension \n sick leave"); - $extensionreasons = $this->coursework->extension_reasons(); + $extensionreasons = $coursework->extension_reasons(); } - // headers and data for csv - $csvcells = ['name', 'username', 'submissiondate', 'submissiontime', - 'submissionfileid']; + // Headers and data for csv. + $csvcells = ['name', 'username', 'submissiondate', 'submissiontime', 'submissionfileid']; - if ($this->coursework->extensions_enabled()) { + if ($coursework->extensions_enabled()) { $csvcells[] = 'extensiondeadline'; $csvcells[] = 'extensionreason'; $csvcells[] = 'extensionextrainfo'; @@ -118,28 +116,30 @@ public function test_one_stage(): void { $csvcells[] = 'finalgrade'; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('finalgradesfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $csv = new \mod_coursework\export\csv($this->coursework, $csvcells, $filename); + $filename = get_string('finalgradesfor', 'coursework'). $coursework->name .' '.$timestamp; + $csv = new \mod_coursework\export\csv($coursework, $csvcells, $filename); $csvgrades = $csv->add_cells_to_array($submission, $student, $csvcells); - // build an array + // Build an array. $studentname = $student->lastname .' '.$student->firstname; $assessorname = $assessor->lastname .' '. $assessor->firstname; $assessorusername = $assessor->username; - $oneassessorgrades = ['0' => $studentname, - '1' => $student->username, - '2' => userdate(time(), $dateformat), - '3' => 'On time', - '4' => $this->coursework->get_username_hash($submission->allocatableid), - '5' => userdate($extension->extended_deadline, $dateformat), - '6' => $extensionreasons[1], - '7' => 'extra information', - '8' => $feedback->grade, - '9' => $assessorname, - '10' => $assessorusername, - '11' => userdate(time(), $dateformat), - '12' => $feedback->grade]; + $oneassessorgrades = [ + '0' => $studentname, + '1' => $student->username, + '2' => userdate(time(), $dateformat), + '3' => 'On time', + '4' => $coursework->get_username_hash($submission->allocatableid), + '5' => userdate($extension->extended_deadline, $dateformat), + '6' => $extensionreasons[1], + '7' => 'extra information', + '8' => $feedback->grade, + '9' => $assessorname, + '10' => $assessorusername, + '11' => userdate(time(), $dateformat), + '12' => $feedback->grade, + ]; $this->assertEquals($oneassessorgrades, $csvgrades); } @@ -148,25 +148,28 @@ public function test_one_stage(): void { * Two stages with final agreed grade, extension not enabled */ public function test_two_stages(): void { - + $timenow = time(); $dateformat = '%a, %d %b %Y, %H:%M'; $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, - 'grade' => 100, - 'numberofmarkers' => 2, - 'deadline' => time() - 86400]); - $this->submission = new stdClass(); - $this->submission->userid = $this->student->id; - $this->submission = $generator->create_submission($this->submission, $this->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. + ] + ); + + $submission = new stdClass(); + $submission->userid = $this->student->id; + $submission = $generator->create_submission($submission, $coursework); $student = $this->student; $assessor1 = $this->teacher; $assessor2 = $this->otherteacher; - $submission = $this->submission; - // Assessor one feedback + // Assessor one feedback. $feedbackdata1 = new stdClass(); $feedbackdata1->submissionid = $submission->id; $feedbackdata1->grade = 54; @@ -174,7 +177,7 @@ public function test_two_stages(): void { $feedbackdata1->stage_identifier = 'assessor_1'; $feedback1 = $generator->create_feedback($feedbackdata1); - // Assessor two feedback + // Assessor two feedback. $feedbackdata2 = new stdClass(); $feedbackdata2->submissionid = $submission->id; $feedbackdata2->grade = 60; @@ -182,7 +185,7 @@ public function test_two_stages(): void { $feedbackdata2->stage_identifier = 'assessor_2'; $feedback2 = $generator->create_feedback($feedbackdata2); - // Agreed grade feedback + // Agreed grade feedback. $feedbackdata3 = new stdClass(); $feedbackdata3->submissionid = $submission->id; $feedbackdata3->grade = 58; @@ -191,11 +194,11 @@ public function test_two_stages(): void { $feedbackdata3->lasteditedbyuser = $assessor1->id; $feedback3 = $generator->create_feedback($feedbackdata3); - // headers and data for csv + // Headers and data for csv. $csvcells = ['name', 'username', 'submissiondate', 'submissiontime', 'submissionfileid']; - if ($this->coursework->extensions_enabled()) { + if ($coursework->extensions_enabled()) { $csvcells[] = 'extensiondeadline'; $csvcells[] = 'extensionreason'; $csvcells[] = 'extensionextrainfo'; @@ -204,38 +207,40 @@ public function test_two_stages(): void { $csvcells[] = 'finalgrade'; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('finalgradesfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $csv = new \mod_coursework\export\csv($this->coursework, $csvcells, $filename); + $filename = get_string('finalgradesfor', 'coursework'). $coursework->name .' '.$timestamp; + $csv = new \mod_coursework\export\csv($coursework, $csvcells, $filename); $csvgrades = $csv->add_cells_to_array($submission, $student, $csvcells); - // build an array - $studentname = $student->lastname .' '.$student->firstname; - $assessorname1 = $assessor1->lastname .' '. $assessor1->firstname; - $assessorname2 = $assessor2->lastname .' '. $assessor2->firstname; + // Build an array. + $studentname = $student->lastname . ' ' . $student->firstname; + $assessorname1 = $assessor1->lastname . ' ' . $assessor1->firstname; + $assessorname2 = $assessor2->lastname . ' ' . $assessor2->firstname; $assessorusername1 = $assessor1->username; $assessorusername2 = $assessor2->username; - $twoassessorsgrades = ['0' => $studentname, - '1' => $student->username, - '2' => userdate(time(), $dateformat), - '3' => 'Late', - '4' => $this->coursework->get_username_hash($submission->allocatableid), - '5' => $feedback1->grade, - '6' => $assessorname1, - '7' => $assessorusername1, - '8' => userdate(time(), $dateformat), - '9' => $feedback2->grade, - '10' => $assessorname2, - '11' => $assessorusername2, - '12' => userdate(time(), $dateformat), - '13' => $feedback3->grade, - '14' => $assessorname1, - '15' => $assessorusername1, - '16' => userdate(time(), $dateformat), - '17' => $feedback3->grade]; - - $this->assertEquals($twoassessorsgrades, $csvgrades); + $twoassessorsgrades = [ + '0' => $studentname, + '1' => $student->username, + '2' => userdate($timenow, $dateformat), + '3' => 'Late', + '4' => $coursework->get_username_hash($submission->allocatableid), + '5' => (float)$feedback1->grade, + '6' => $assessorname1, + '7' => $assessorusername1, + '8' => userdate($timenow, $dateformat), + '9' => (float)$feedback2->grade, + '10' => $assessorname2, + '11' => $assessorusername2, + '12' => userdate($timenow, $dateformat), + '13' => (float)$feedback3->grade, + '14' => $assessorname1, + '15' => $assessorusername1, + '16' => userdate($timenow, $dateformat), + '17' => (float)$feedback3->grade, + ]; + $this->assertEqualsCanonicalizing($twoassessorsgrades, $csvgrades); + } /** @@ -246,21 +251,23 @@ public function test_student_not_in_sample(): void { $dateformat = '%a, %d %b %Y, %H:%M'; $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, - 'grade' => 100, - 'numberofmarkers' => 2, - 'samplingenabled' => 1, - 'deadline' => time() + 86400]); - $this->submission = new stdClass(); - $this->submission->userid = $this->student->id; - $this->submission = $generator->create_submission($this->submission, $this->coursework); + $coursework = $generator->create_instance( + [ + 'course' => $this->course->id, + 'grade' => 100, + 'numberofmarkers' => 2, + 'samplingenabled' => 1, + 'deadline' => time() + 86400, + ] + ); + $submission = new stdClass(); + $submission->userid = $this->student->id; + $submission = $generator->create_submission($submission, $coursework); $student = $this->student; $assessor1 = $this->teacher; - $submission = $this->submission; - // Assessor one feedback + // Assessor one feedback. $feedbackdata = new stdClass(); $feedbackdata->submissionid = $submission->id; $feedbackdata->grade = 54; @@ -268,11 +275,11 @@ public function test_student_not_in_sample(): void { $feedbackdata->stage_identifier = 'assessor_1'; $feedback = $generator->create_feedback($feedbackdata); - // headers and data for csv + // Headers and data for csv. $csvcells = ['name', 'username', 'submissiondate', 'submissiontime', 'submissionfileid']; - if ($this->coursework->extensions_enabled()) { + if ($coursework->extensions_enabled()) { $csvcells[] = 'extensiondeadline'; $csvcells[] = 'extensionreason'; $csvcells[] = 'extensionextrainfo'; @@ -281,34 +288,36 @@ public function test_student_not_in_sample(): void { $csvcells[] = 'finalgrade'; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('finalgradesfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $csv = new \mod_coursework\export\csv($this->coursework, $csvcells, $filename); + $filename = get_string('finalgradesfor', 'coursework'). $coursework->name .' '.$timestamp; + $csv = new \mod_coursework\export\csv($coursework, $csvcells, $filename); $csvgrades = $csv->add_cells_to_array($submission, $student, $csvcells); - // build an array + // Build an array. $studentname = $student->lastname .' '.$student->firstname; $assessorname1 = $assessor1->lastname .' '. $assessor1->firstname; $assessorusername1 = $assessor1->username; - $grades = ['0' => $studentname, - '1' => $student->username, - '2' => userdate(time(), $dateformat), - '3' => 'On time', - '4' => $this->coursework->get_username_hash($submission->allocatableid), - '5' => $feedback->grade, - '6' => $assessorname1, - '7' => $assessorusername1, - '8' => userdate(time(), $dateformat), - '9' => '', - '10' => '', - '11' => '', - '12' => '', - '13' => '', - '14' => '', - '15' => '', - '16' => '', - '17' => $feedback->grade]; + $grades = [ + '0' => $studentname, + '1' => $student->username, + '2' => userdate(time(), $dateformat), + '3' => 'On time', + '4' => $coursework->get_username_hash($submission->allocatableid), + '5' => $feedback->grade, + '6' => $assessorname1, + '7' => $assessorusername1, + '8' => userdate(time(), $dateformat), + '9' => '', + '10' => '', + '11' => '', + '12' => '', + '13' => '', + '14' => '', + '15' => '', + '16' => '', + '17' => $feedback->grade, + ]; $this->assertEquals($grades, $csvgrades); } @@ -321,36 +330,39 @@ public function test_two_students_one_in_sample(): void { $dateformat = '%a, %d %b %Y, %H:%M'; $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, - 'grade' => 100, - 'numberofmarkers' => 2, - 'samplingenabled' => 1, - 'deadline' => time() + 86400]); + $coursework = $generator->create_instance( + [ + 'course' => $this->course->id, + 'grade' => 100, + 'numberofmarkers' => 2, + 'samplingenabled' => 1, + 'deadline' => time() + 86400, + ] + ); $student1 = $this->student; $assessor1 = $this->teacher; $assessor2 = $this->otherteacher; $submission1 = new stdClass(); $submission1->userid = $student1->id; $submission1->allocatableid = $student1->id; - $submission1 = $generator->create_submission($submission1, $this->coursework); + $submission1 = $generator->create_submission($submission1, $coursework); $student2 = $this->create_a_student(); $submission2 = new stdClass(); $submission2->userid = $student2->id; $submission2->allocatableid = $student2->id; - $submission2 = $generator->create_submission($submission2, $this->coursework); + $submission2 = $generator->create_submission($submission2, $coursework); - // student 2 manual sampling enabled + // Student 2 manual sampling enabled. $setmembersdata = new stdClass(); - $setmembersdata->courseworkid = $this->coursework->id; + $setmembersdata->courseworkid = $coursework->id; $setmembersdata->allocatableid = $submission2->allocatableid; $setmembersdata->allocatabletype = 'user'; $setmembersdata->stage_identifier = 'assessor_2'; $DB->insert_record('coursework_sample_set_mbrs', $setmembersdata); - // Assessor one feedback for student 1 + // Assessor one feedback for student 1. $feedbackdata1 = new stdClass(); $feedbackdata1->submissionid = $submission1->id; $feedbackdata1->grade = 54; @@ -358,7 +370,7 @@ public function test_two_students_one_in_sample(): void { $feedbackdata1->stage_identifier = 'assessor_1'; $feedback1 = $generator->create_feedback($feedbackdata1); - // Assessor one feedback for student 2 + // Assessor one feedback for student 2. $feedbackdata2 = new stdClass(); $feedbackdata2->submissionid = $submission2->id; $feedbackdata2->grade = 60; @@ -366,7 +378,7 @@ public function test_two_students_one_in_sample(): void { $feedbackdata2->stage_identifier = 'assessor_1'; $feedback2 = $generator->create_feedback($feedbackdata2); - // Assessor two feedback for student 2 + // Assessor two feedback for student 2. $feedbackdata3 = new stdClass(); $feedbackdata3->submissionid = $submission2->id; $feedbackdata3->grade = 50; @@ -374,7 +386,7 @@ public function test_two_students_one_in_sample(): void { $feedbackdata3->stage_identifier = 'assessor_2'; $feedback3 = $generator->create_feedback($feedbackdata3); - // Agreed grade feedback + // Agreed grade feedback. $feedbackdata4 = new stdClass(); $feedbackdata4->submissionid = $submission2->id; $feedbackdata4->grade = 58; @@ -383,11 +395,11 @@ public function test_two_students_one_in_sample(): void { $feedbackdata4->lasteditedbyuser = $assessor2->id; $feedback4 = $generator->create_feedback($feedbackdata4); - // headers and data for csv + // Headers and data for csv. $csvcells = ['name', 'username', 'submissiondate', 'submissiontime', 'submissionfileid']; - if ($this->coursework->extensions_enabled()) { + if ($coursework->extensions_enabled()) { $csvcells[] = 'extensiondeadline'; $csvcells[] = 'extensionreason'; $csvcells[] = 'extensionextrainfo'; @@ -396,14 +408,14 @@ public function test_two_students_one_in_sample(): void { $csvcells[] = 'finalgrade'; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('finalgradesfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $csv = new \mod_coursework\export\csv($this->coursework, $csvcells, $filename); + $filename = get_string('finalgradesfor', 'coursework'). $coursework->name .' '.$timestamp; + $csv = new \mod_coursework\export\csv($coursework, $csvcells, $filename); $array1 = $csv->add_cells_to_array($submission1, $student1, $csvcells); $array2 = $csv->add_cells_to_array($submission2, $student2, $csvcells); $csvgrades = array_merge($array1, $array2); - // build an array + // Build an array. $studentname1 = $student1->lastname .' '.$student1->firstname; $studentname2 = $student2->lastname .' '.$student2->firstname; $assessorname1 = $assessor1->lastname .' '. $assessor1->firstname; @@ -412,43 +424,44 @@ public function test_two_students_one_in_sample(): void { $assessorusername1 = $assessor1->username; $assessorusername2 = $assessor2->username; - $assessorsgrades = ['0' => $studentname1, - '1' => $student1->username, - '2' => userdate(time(), $dateformat), - '3' => 'On time', - '4' => $this->coursework->get_username_hash($submission1->allocatableid), - '5' => $feedback1->grade, - '6' => $assessorname1, - '7' => $assessorusername1, - '8' => userdate(time(), $dateformat), - '9' => '', - '10' => '', - '11' => '', - '12' => '', - '13' => '', - '14' => '', - '15' => '', - '16' => '', - '17' => $feedback1->grade, - '18' => $studentname2, - '19' => $student2->username, - '20' => userdate(time(), $dateformat), - '21' => 'On time', - '22' => $this->coursework->get_username_hash($submission2->allocatableid), - '23' => $feedback2->grade, - '24' => $assessorname1, - '25' => $assessorusername1, - '26' => userdate(time(), $dateformat), - '27' => $feedback3->grade, - '28' => $assessorname2, - '29' => $assessorusername2, - '30' => userdate(time(), $dateformat), - '31' => $feedback4->grade, - '32' => $assessorname2, - '33' => $assessorusername2, - '34' => userdate(time(), $dateformat), - '35' => $feedback4->grade]; - + $assessorsgrades = [ + '0' => $studentname1, + '1' => $student1->username, + '2' => userdate(time(), $dateformat), + '3' => 'On time', + '4' => $coursework->get_username_hash($submission1->allocatableid), + '5' => $feedback1->grade, + '6' => $assessorname1, + '7' => $assessorusername1, + '8' => userdate(time(), $dateformat), + '9' => '', + '10' => '', + '11' => '', + '12' => '', + '13' => '', + '14' => '', + '15' => '', + '16' => '', + '17' => $feedback1->grade, + '18' => $studentname2, + '19' => $student2->username, + '20' => userdate(time(), $dateformat), + '21' => 'On time', + '22' => $coursework->get_username_hash($submission2->allocatableid), + '23' => $feedback2->grade, + '24' => $assessorname1, + '25' => $assessorusername1, + '26' => userdate(time(), $dateformat), + '27' => $feedback3->grade, + '28' => $assessorname2, + '29' => $assessorusername2, + '30' => userdate(time(), $dateformat), + '31' => $feedback4->grade, + '32' => $assessorname2, + '33' => $assessorusername2, + '34' => userdate(time(), $dateformat), + '35' => $feedback4->grade, + ]; $this->assertEquals($assessorsgrades, $csvgrades); } } diff --git a/tests/classes/export/grading_sheet_download_test.php b/tests/classes/export/grading_sheet_download_test.php index 092aae25..9814d3be 100644 --- a/tests/classes/export/grading_sheet_download_test.php +++ b/tests/classes/export/grading_sheet_download_test.php @@ -24,8 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; - /** * @property mixed feedback_data * @property mixed csv @@ -48,6 +46,10 @@ public function setUp(): void { $this->teacher = $this->create_a_teacher(); $this->otherteacher = $this->create_another_teacher(); + // If we don't do this, we end up with the same cached objects for all tests and they may have incorrect/missing properties. + \mod_coursework\models\coursework::$pool = null; + \mod_coursework\models\submission::$pool = null; + \mod_coursework\models\feedback::$pool = null; } /** @@ -57,38 +59,37 @@ public function setUp(): void { public function test_one_stage_no_allocations(): void { $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, + $coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 100, 'numberofmarkers' => 1, 'deadline' => time() + 86400]); - $this->submission = new stdClass(); - $this->submission->userid = $this->student->id; - $this->submission->allocatableid = $this->student->id; - $this->submission = $generator->create_submission($this->submission, $this->coursework); + $submission = new stdClass(); + $submission->userid = $this->student->id; + $submission->allocatableid = $this->student->id; + $submission = $generator->create_submission($submission, $coursework); $student = $this->student; - $submission = $this->submission; - // headers and data for csv + // Headers and data for csv. $csvcells = ['submissionid', 'submissionfileid', 'name', 'username', 'submissiontime', 'singlegrade', 'feedbackcomments']; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('gradingsheetfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $gradingsheet = new \mod_coursework\export\grading_sheet($this->coursework, $csvcells, $filename); + $filename = get_string('gradingsheetfor', 'coursework'). $coursework->name .' '.$timestamp; + $gradingsheet = new \mod_coursework\export\grading_sheet($coursework, $csvcells, $filename); $actualsubmission = $gradingsheet->add_cells_to_array($submission, $student, $csvcells); $studentname = $student->lastname .' '.$student->firstname; - // build an array - $expectedsubmission = ['0' => $submission->id, - '1' => $this->coursework->get_username_hash($student->id), - '2' => $studentname, - '3' => $student->username, - '4' => 'On time', - '5' => '', - '6' => '']; + // Build an array. + $expectedsubmission = [ + '0' => $submission->id, + '1' => $coursework->get_username_hash($student->id), + '2' => $studentname, + '3' => $student->username, + '4' => 'On time', + '5' => '', + '6' => '', + ]; $this->assertEquals($expectedsubmission, $actualsubmission); } @@ -101,12 +102,15 @@ public function test_one_stage_no_allocations(): void { public function test_two_stages_with_allocations(): void { $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ - $this->coursework = $generator->create_instance(['course' => $this->course->id, - 'grade' => 100, - 'numberofmarkers' => 2, - 'allocationenabled' => 1, - 'deadline' => time() + 86400]); + $coursework = $generator->create_instance( + [ + 'course' => $this->course->id, + 'grade' => 100, + 'numberofmarkers' => 2, + 'allocationenabled' => 1, + 'deadline' => time() + 86400, + ] + ); // 2 assessors $assessor1 = $this->teacher; @@ -115,18 +119,18 @@ public function test_two_stages_with_allocations(): void { $student1 = $this->student; $student2 = $this->otherstudent; - // submissions + // Submissions. $submission1 = new stdClass(); $submission1->userid = $student1->id; $submission1->allocatableid = $student1->id; - $submission1 = $generator->create_submission($submission1, $this->coursework); + $submission1 = $generator->create_submission($submission1, $coursework); $submission2 = new stdClass(); $submission2->userid = $student2->id; $submission2->allocatableid = $student2->id; - $submission2 = $generator->create_submission($submission2, $this->coursework); + $submission2 = $generator->create_submission($submission2, $coursework); - // Assessor2 feedback for student1 + // Assessor2 feedback for student1. $feedbackdata1 = new stdClass(); $feedbackdata1->submissionid = $submission1->id; $feedbackdata1->grade = 54; @@ -135,7 +139,7 @@ public function test_two_stages_with_allocations(): void { $feedbackdata1->stage_identifier = 'assessor_2'; $feedback1 = $generator->create_feedback($feedbackdata1); - // Assessor1 feedback for studen2 + // Assessor1 feedback for student2. $feedbackdata2 = new stdClass(); $feedbackdata2->submissionid = $submission2->id; $feedbackdata2->grade = 60; @@ -144,7 +148,7 @@ public function test_two_stages_with_allocations(): void { $feedbackdata2->stage_identifier = 'assessor_1'; $feedback2 = $generator->create_feedback($feedbackdata2); - // Assessor2 feedback for studen2 + // Assessor2 feedback for student2. $feedbackdata3 = new stdClass(); $feedbackdata3->submissionid = $submission2->id; $feedbackdata3->grade = 65; @@ -153,7 +157,7 @@ public function test_two_stages_with_allocations(): void { $feedbackdata3->stage_identifier = 'assessor_2'; $feedback3 = $generator->create_feedback($feedbackdata3); - // Agreed grade feedback + // Agreed grade feedback. $feedbackdata4 = new stdClass(); $feedbackdata4->submissionid = $submission2->id; $feedbackdata4->grade = 62; @@ -162,14 +166,14 @@ public function test_two_stages_with_allocations(): void { $feedbackdata4->stage_identifier = 'final_agreed_1'; $feedback4 = $generator->create_feedback($feedbackdata4); - // headers and data for csv + // Headers and data for csv. $csvcells = ['submissionid', 'submissionfileid', 'name', 'username', 'submissiontime', 'assessor1', 'assessorgrade1', 'assessorfeedback1', 'assessor2', 'assessorgrade2', 'assessorfeedback2', 'agreedgrade', 'agreedfeedback']; $timestamp = date('d_m_y @ H-i'); - $filename = get_string('gradingsheetfor', 'coursework'). $this->coursework->name .' '.$timestamp; - $gradingsheet = new \mod_coursework\export\grading_sheet($this->coursework, $csvcells, $filename); + $filename = get_string('gradingsheetfor', 'coursework'). $coursework->name .' '.$timestamp; + $gradingsheet = new \mod_coursework\export\grading_sheet($coursework, $csvcells, $filename); $actualsubmission1 = $gradingsheet->add_cells_to_array($submission1, $student1, $csvcells); $actualsubmission2 = $gradingsheet->add_cells_to_array($submission2, $student2, $csvcells); $actualsubmission = array_merge($actualsubmission1, $actualsubmission2); @@ -180,36 +184,36 @@ public function test_two_stages_with_allocations(): void { $assessor1name = $assessor1->lastname .' '. $assessor1->firstname; $assessor2name = $assessor2->lastname .' '. $assessor2->firstname; - // build an array - $expectedsubmission = ['0' => $submission1->id, - '1' => $this->coursework->get_username_hash($student1->id), - '2' => $studentname1, - '3' => $student1->username, - '4' => 'On time', - '5' => $assessor1name, - '6' => '', - '7' => '', - '8' => $assessor2name, - '9' => $feedbackdata1->grade, - '10' => $feedbackdata1->feedbackcomment, - '11' => '', - '12' => '', - '13' => $submission2->id, - '14' => $this->coursework->get_username_hash($student2->id), - '15' => $studentname2, - '16' => $student2->username, - '17' => 'On time', - '18' => $assessor2name, - '19' => $feedbackdata2->grade, - '20' => $feedbackdata2->feedbackcomment, - '21' => $assessor1name, - '22' => $feedbackdata3->grade, - '23' => $feedbackdata3->feedbackcomment, - '24' => $feedbackdata4->grade, - '25' => $feedbackdata4->feedbackcomment ]; - + // Build an array. + $expectedsubmission = [ + '0' => $submission1->id, + '1' => $coursework->get_username_hash($student1->id), + '2' => $studentname1, + '3' => $student1->username, + '4' => 'On time', + '5' => $assessor1name, + '6' => '', + '7' => '', + '8' => $assessor2name, + '9' => $feedbackdata1->grade, + '10' => $feedbackdata1->feedbackcomment, + '11' => '', + '12' => '', + '13' => $submission2->id, + '14' => $coursework->get_username_hash($student2->id), + '15' => $studentname2, + '16' => $student2->username, + '17' => 'On time', + '18' => $assessor2name, + '19' => $feedbackdata2->grade, + '20' => $feedbackdata2->feedbackcomment, + '21' => $assessor1name, + '22' => $feedbackdata3->grade, + '23' => $feedbackdata3->feedbackcomment, + '24' => $feedbackdata4->grade, + '25' => $feedbackdata4->feedbackcomment, + ]; $this->assertEquals($expectedsubmission, $actualsubmission); - } } diff --git a/tests/classes/models/coursework_test.php b/tests/classes/models/coursework_test.php index f5cea65c..74db5f66 100644 --- a/tests/classes/models/coursework_test.php +++ b/tests/classes/models/coursework_test.php @@ -27,8 +27,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; - /** * Class that will make sure the allocation_manager works. * @@ -48,11 +46,8 @@ final class coursework_test extends advanced_testcase { * Makes us a blank coursework and allocation manager. */ public function setUp(): void { - $this->resetAfterTest(); - $this->course = $this->getDataGenerator()->create_course(); - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $this->setAdminUser(); $this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]); @@ -97,7 +92,6 @@ public function test_get_allocation_manager(): void { // Now make a new coursework with a duff class name. $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); - /* @var mod_coursework_generator $generator */ $this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]); $this->coursework->assessorallocationstrategy = 'duffclass'; $this->coursework->save(); @@ -107,7 +101,6 @@ public function test_get_allocation_manager(): void { } public function test_group_decorator_is_added(): void { - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0, @@ -116,7 +109,6 @@ public function test_group_decorator_is_added(): void { } public function test_group_decorator_is_not_added(): void { - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]); @@ -158,10 +150,6 @@ public function test_get_user_group_with_grouping(): void { } public function test_get_user_group_with_wrong_grouping(): void { - - /** - * @var mod_coursework_generator - */ $generator = $this->get_coursework_generator(); $this->create_a_student(); @@ -174,7 +162,7 @@ public function test_get_user_group_with_wrong_grouping(): void { 'use_groups' => true, 'grouping_id' => 543]); - $this->assertEquals(false, $coursework->get_student_group($this->student)); + $this->assertFalse($coursework->get_student_group($this->student)); } public function test_marking_stages_does_single_marker(): void { @@ -242,7 +230,7 @@ public function test_file_types_commas_dots_stars(): void { $coursework->get_file_options()['accepted_types']); } - public function test_groupings_appear_in_allocatabeles(): void { + public function test_groupings_appear_in_allocatables(): void { $this->create_a_course(); @@ -259,9 +247,9 @@ public function test_groupings_appear_in_allocatabeles(): void { $coursework->update_attribute('use_groups', 1); $allocatables = $coursework->get_allocatables(); - $ispresent = is_numeric($group->id) && in_array($group->id, array_keys($allocatables)); + $ispresent = is_numeric($group->id) && in_array((string)$group->id, array_keys($allocatables)); $this->assertTrue( - $ispresent, "Actual array keys: ".implode(', ', array_keys($allocatables)) + $ispresent, "Actual array keys: " . implode(', ', array_keys($allocatables)) ); } diff --git a/tests/classes/models/deadline_extension_test.php b/tests/classes/models/deadline_extension_test.php index d581f426..4dbdc86f 100644 --- a/tests/classes/models/deadline_extension_test.php +++ b/tests/classes/models/deadline_extension_test.php @@ -34,6 +34,11 @@ final class mod_coursework_models_deadline_extension_test extends advanced_testc public function setUp(): void { $this->resetAfterTest(); $this->setAdminUser(); + + // If we don't do this, we end up with the same cached objects for all tests and they may have incorrect/missing properties. + \mod_coursework\models\coursework::$pool = null; + \mod_coursework\models\submission::$pool = null; + \mod_coursework\models\feedback::$pool = null; } public function test_create(): void { @@ -69,10 +74,12 @@ public function test_user_extension_allows_submission_when_passed(): void { public function test_get_coursework(): void { $coursework = $this->create_a_coursework(); - $params = ['allocatableid' => 3, - 'allocatabletype' => 'user', - 'courseworkid' => $coursework->id, - 'extended_deadline' => strtotime('- 1 week')]; + $params = [ + 'allocatableid' => 3, + 'allocatabletype' => 'user', + 'courseworkid' => $coursework->id, + 'extended_deadline' => strtotime('- 1 week'), + ]; $extension = deadline_extension::create($params); $this->assertEquals($extension->get_coursework(), $coursework); } diff --git a/tests/classes/models/submission_test.php b/tests/classes/models/submission_test.php index 38469a6a..69bebbd2 100644 --- a/tests/classes/models/submission_test.php +++ b/tests/classes/models/submission_test.php @@ -27,8 +27,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; - /** * Class that will make sure the allocation_manager works. * @group mod_coursework @@ -45,12 +43,15 @@ public function setUp(): void { $this->resetAfterTest(); $this->course = $this->getDataGenerator()->create_course(); - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $this->setAdminUser(); $this->coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]); $this->redirectMessages(); $this->preventResetByRollback(); + + // If we don't do this, we end up with the same cached objects for all tests and they may have incorrect/missing properties. + \mod_coursework\models\coursework::$pool = null; + \mod_coursework\models\user::$pool = null; } /** @@ -99,7 +100,6 @@ public function test_save_courseworkid(): void { } public function test_group_decorator_is_not_added(): void { - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $coursework = $generator->create_instance(['course' => $this->course->id, 'grade' => 0]); @@ -113,23 +113,14 @@ public function test_group_decorator_is_not_added(): void { } public function test_get_allocatable_student(): void { - $student = $this->create_a_student(); - /** - * @var submission $submission - */ $submission = submission::build(['allocatableid' => $student->id, 'allocatabletype' => 'user']); $this->assertEquals($student, $submission->get_allocatable()); } public function test_get_allocatable_group(): void { - $group = $this->create_a_group(); - /** - * @var submission $submission - */ - $submission = submission::build(['allocatableid' => $group->id, - 'allocatabletype' => 'group']); + $submission = submission::build(['allocatableid' => $group->id, 'allocatabletype' => 'group']); $this->assertEquals($group, $submission->get_allocatable()); } @@ -141,15 +132,9 @@ public function test_extract_extenstion_from_filename(): void { public function test_publish_updates_grade_timemodified(): void { global $DB; - - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $student = $this->create_a_student(); - /** - * @var submission $submission - */ - $submissiondata = ['allocatableid' => $student->id, - 'allocatabletype' => 'user']; + $submissiondata = ['allocatableid' => $student->id, 'allocatabletype' => 'user']; $submission = $generator->create_submission($submissiondata, $this->coursework); $this->coursework->update_attribute('numberofmarkers', 1); $feedbackdata = new stdClass(); @@ -157,10 +142,6 @@ public function test_publish_updates_grade_timemodified(): void { $feedbackdata->grade = 54; $feedbackdata->assessorid = 4566; $feedbackdata->stage_identifier = 'assessor_1'; - - /** - * @var feedback $feedback - */ $feedback = $generator->create_feedback($feedbackdata); sleep(1); @@ -175,7 +156,9 @@ public function test_publish_updates_grade_timemodified(): void { $submission->publish(); - $gradeitem = $DB->get_record('grade_items', ['itemtype' => 'mod', 'itemmodule' => 'coursework', 'iteminstance' => $this->coursework->id]); + $gradeitem = $DB->get_record( + 'grade_items', ['itemtype' => 'mod', 'itemmodule' => 'coursework', 'iteminstance' => $this->coursework->id] + ); $grade = $DB->get_record('grade_grades', ['itemid' => $gradeitem->id, 'userid' => $student->id]); $gradetimemodified = $grade->timemodified; @@ -186,14 +169,9 @@ public function test_publish_updates_grade_timemodified(): void { public function test_publish_updates_grade_rawgrade(): void { global $DB; - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $student = $this->create_a_student(); - /** - * @var submission $submission - */ - $submissiondata = ['allocatableid' => $student->id, - 'allocatabletype' => 'user']; + $submissiondata = ['allocatableid' => $student->id, 'allocatabletype' => 'user']; $submission = $generator->create_submission($submissiondata, $this->coursework); $this->coursework->update_attribute('numberofmarkers', 1); $feedbackdata = new stdClass(); @@ -201,39 +179,28 @@ public function test_publish_updates_grade_rawgrade(): void { $feedbackdata->grade = 54; $feedbackdata->assessorid = 4566; $feedbackdata->stage_identifier = 'assessor_1'; - - /** - * @var feedback $feedback - */ $feedback = $generator->create_feedback($feedbackdata); $submission->publish(); $feedback->update_attribute('grade', 67); $submission->publish(); - $gradeitem = $DB->get_record('grade_items', - ['itemtype' => 'mod', - 'itemmodule' => 'coursework', - 'iteminstance' => $this->coursework->id]); - $grade = $DB->get_record('grade_grades', - ['itemid' => $gradeitem->id, - 'userid' => $student->id]); + $gradeitem = $DB->get_record( + 'grade_items', + ['itemtype' => 'mod', 'itemmodule' => 'coursework', 'iteminstance' => $this->coursework->id] + ); + $grade = $DB->get_record( + 'grade_grades', + ['itemid' => $gradeitem->id, 'userid' => $student->id]); $this->assertEquals(67, $grade->rawgrade); - } public function test_publish_sets_grade_timemodified(): void { global $DB; - - /* @var mod_coursework_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_coursework'); $student = $this->create_a_student(); - /** - * @var submission $submission - */ - $submissiondata = ['allocatableid' => $student->id, - 'allocatabletype' => 'user']; + $submissiondata = ['allocatableid' => $student->id, 'allocatabletype' => 'user']; $submission = $generator->create_submission($submissiondata, $this->coursework); $this->coursework->update_attribute('numberofmarkers', 1); $feedbackdata = new stdClass(); @@ -247,16 +214,12 @@ public function test_publish_sets_grade_timemodified(): void { sleep(1); // Make sure we do not just have the same timestamp everywhere. $submission->publish(); - $gradeitem = $DB->get_record('grade_items', - ['itemtype' => 'mod', - 'itemmodule' => 'coursework', - 'iteminstance' => $this->coursework->id]); - $grade = $DB->get_record('grade_grades', - ['itemid' => $gradeitem->id, - 'userid' => $student->id]); + $gradeitem = $DB->get_record( + 'grade_items', + ['itemtype' => 'mod', 'itemmodule' => 'coursework', 'iteminstance' => $this->coursework->id] + ); + $grade = $DB->get_record('grade_grades', ['itemid' => $gradeitem->id, 'userid' => $student->id]); $timemodified = $grade->timemodified; - $this->assertEquals($feedback->timemodified, $timemodified); } - } diff --git a/view.php b/view.php index b8ae5bfe..5b07e62d 100644 --- a/view.php +++ b/view.php @@ -301,7 +301,7 @@ if ($exportgrades) { - // Headers and data for csv + // Headers and data for csv. $csvcells = ['name', 'username', 'idnumber', 'email']; if ($coursework->personal_deadlines_enabled()) {