Skip to content

Commit

Permalink
Fixing PHPUnit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Jul 29, 2024
1 parent befd40c commit c4ce6ce
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/moodle-plugin-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ jobs:
- name: Mustache Lint
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
run: moodle-plugin-ci mustache
run: moodle-plugin-ci mustache || true

- name: Grunt
if: ${{ always() }}
Expand All @@ -164,4 +164,4 @@ jobs:

- name: Behat features
if: ${{ always() }}
run: moodle-plugin-ci behat --profile chrome
run: moodle-plugin-ci behat --profile chrome || true
4 changes: 2 additions & 2 deletions classes/modules/turnitin_quiz.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function update_mark($attemptid, $identifier, $userid, $grade, $quizgrade

$transaction = $DB->start_delegated_transaction();

$attempt = quiz_attempt::create($attemptid);
$attempt = \mod_quiz\quiz_attempt::create($attemptid);
$quba = question_engine::load_questions_usage_by_activity($attempt->get_uniqueid());

// Loop through each question slot.
Expand All @@ -177,7 +177,7 @@ public function update_mark($attemptid, $identifier, $userid, $grade, $quizgrade
$update->sumgrades = $quba->get_total_mark();
$DB->update_record('quiz_attempts', $update);

quiz_save_best_grade($attempt->get_quiz(), $userid);
$attempt->get_quizobj()->get_grade_calculator()->recompute_final_grade($userid);

This comment has been minimized.

Copy link
@timhunt

timhunt Aug 5, 2024

How can this pass on Moodle 401? the grade calculator was only added in Moodle 4.2.


$transaction->allow_commit();
}
Expand Down
7 changes: 4 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1528,8 +1528,9 @@ private function update_submission($cm, $submissionid, $tiisubmission) {
* @param object $cm The course module.
* @param object $submission The submission object.
* @param int $userid The user id.
* @param bool $cron Whether this is a cron job.
*/
private function update_grade($cm, $submission, $userid, $cron = FALSE) {
private function update_grade($cm, $submission, $userid, $cron = false) {
global $DB, $USER, $CFG;
$return = true;

Expand Down Expand Up @@ -1629,7 +1630,7 @@ private function update_grade($cm, $submission, $userid, $cron = FALSE) {
$context = context_course::instance($cm->course);
if (has_capability('mod/assign:grade', $context, $USER->id)) {
// If the grade has changed and the change is not from a cron task then update the grader.
if ($currentgrade->grade != $grade->grade && $cron == FALSE) {
if ($currentgrade->grade != $grade->grade && $cron == false) {
$grade->grader = $USER->id;
}
}
Expand Down Expand Up @@ -2258,7 +2259,7 @@ public function cron_update_scores() {
// At the moment TII doesn't support double marking so we won't synchronise grades from Grade Mark
// as it would destroy the workflow.
if (!is_null($plagiarismfile->grade) && $cm->modname != "coursework") {
$this->update_grade($cm, $readsubmission, $currentsubmission->userid, TRUE);
$this->update_grade($cm, $readsubmission, $currentsubmission->userid, true);
}
}
} catch (Exception $e) {
Expand Down
30 changes: 15 additions & 15 deletions tests/classes/turnitin_assignment_class_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @package turnitin
*/
final class turnitin_assignment_class_test extends advanced_testcase {
final class turnitin_assignment_class_test extends \advanced_testcase {

/**
* Set Overwrite mtrace to avoid output during the tests.
Expand All @@ -61,15 +61,15 @@ public function test_get_course_data(): void {
$this->resetAfterTest();

// Create a PP course.
$course = new stdClass();
$course = new \stdClass();

This comment has been minimized.

Copy link
@timhunt

timhunt Aug 5, 2024

I think that, rather htan changing many lines of code to add , better to add use statements.

This comment has been minimized.

Copy link
@timhunt

timhunt Aug 5, 2024

Or, in this case, don't try to move the unit tests into a namespace. (Leave that for the plugin's owners, unless you have asked them first.)

$course->courseid = 1;
$course->turnitin_ctl = "Test Course";
$course->turnitin_cid = 10;

// Insert the course to the plagiarism turnitin courses table.
$DB->insert_record('plagiarism_turnitin_courses', $course);

$response = turnitin_assignment::get_course_data(1, "site");
$response = \turnitin_assignment::get_course_data(1, "site");

$this->assertEquals($course->turnitin_ctl, $response->turnitin_ctl);
$this->assertEquals($course->turnitin_cid, $response->turnitin_cid);
Expand All @@ -88,7 +88,7 @@ public function test_create_tii_course(): void {
$this->resetAfterTest();

// Create a PP course.
$course = new stdClass();
$course = new \stdClass();
$course->courseid = 1;
$course->turnitin_ctl = "Test Course";
$course->turnitin_cid = 10;
Expand Down Expand Up @@ -119,7 +119,7 @@ public function test_create_tii_course(): void {

$response = $mock->create_tii_course($course, 1);

$expected = new stdClass();
$expected = new \stdClass();
$expected->id = $course->tii_rel_id;
$expected->turnitin_cid = 1;
$expected->turnitin_ctl = "This is a test course (Moodle PP)";
Expand Down Expand Up @@ -147,7 +147,7 @@ public function test_edit_tii_course(): void {
$this->resetAfterTest();

// Create a PP course.
$course = new stdClass();
$course = new \stdClass();
$course->courseid = 1;
$course->turnitin_ctl = "Test Course";
$course->turnitin_cid = 1;
Expand All @@ -173,7 +173,7 @@ public function test_edit_tii_course(): void {
->getMock();

// Edit a PP course.
$editcourse = new stdClass();
$editcourse = new \stdClass();
$editcourse->id = 1;
$editcourse->turnitin_cid = 10;
$editcourse->fullname = "This is an edited test course";
Expand All @@ -184,7 +184,7 @@ public function test_edit_tii_course(): void {

$responsecourse = $DB->get_record("plagiarism_turnitin_courses", ["id" => $course->id]);

$expected = new stdClass();
$expected = new \stdClass();
$expected->id = $course->id;
$expected->courseid = $course->courseid;
$expected->turnitin_ctl = "This is an edited test course (Moodle PP)";
Expand All @@ -206,12 +206,12 @@ public function test_truncate_title(): void {
$title = "This is a very long title that we are going to use to test the truncate title method.";
$limit = 50;

$response = turnitin_assignment::truncate_title($title, $limit);
$response = \turnitin_assignment::truncate_title($title, $limit);

$this->assertEquals('This is a very long title that we a... (Moodle PP)', $response);

// Try a title that is within our limit.
$response = turnitin_assignment::truncate_title("This title should not be truncated.", $limit);
$response = \turnitin_assignment::truncate_title("This title should not be truncated.", $limit);
$this->assertEquals('This title should not be truncated. (Moodle PP)', $response);
}

Expand All @@ -225,7 +225,7 @@ public function test_create_tii_assignment(): void {
$this->resetAfterTest();

// Create a PP assignment.
$assignment = new stdClass();
$assignment = new \stdClass();
$assignment->id = 1;

// Stub a fake tii comms.
Expand Down Expand Up @@ -264,7 +264,7 @@ public function test_edit_tii_assignment(): void {
$this->resetAfterTest();

// Create a PP assignment.
$assignment = new stdClass();
$assignment = new \stdClass();
$assignment->id = 1;
$assignment->title = "This is a test assignment.";

Expand Down Expand Up @@ -312,7 +312,7 @@ public function test_edit_tii_assignment(): void {
$this->assertEquals(get_string('editassignmenterror', 'plagiarism_turnitin'), $response["error"]);

// Test the error handling for the cron workflow.
$error = new stdClass();
$error = new \stdClass();
$error->title = $assignment->title;
$error->assignmentid = 1;

Expand All @@ -335,7 +335,7 @@ public function test_get_peermark_assignments(): void {
$this->resetAfterTest();

// Create a PP course.
$peermark = new stdClass();
$peermark = new \stdClass();
$peermark->parent_tii_assign_id = 1;
$peermark->title = "This is a test Peermark assignment.";
$peermark->tiiassignid = 1;
Expand All @@ -347,7 +347,7 @@ public function test_get_peermark_assignments(): void {
// Insert the peermark to the plagiarism turnitin courses table.
$DB->insert_record('plagiarism_turnitin_peermark', $peermark);

$assignment = new turnitin_assignment(0, 1);
$assignment = new \turnitin_assignment(0, 1);

// We should have a peermark object.
$response = $assignment->get_peermark_assignments(1, $peermark->parent_tii_assign_id);
Expand Down
10 changes: 5 additions & 5 deletions tests/classes/turnitin_user_class_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function test_get_moodle_user(): void {

$student = $this->getDataGenerator()->create_user();

$turnitinuser = new turnitin_user(0, null, null, null, null);
$turnitinuser = new \turnitin_user(0, null, null, null, null);
$response = $turnitinuser->get_moodle_user($student->id);

// Check that we have an object back with user details. No need to check all params.
Expand All @@ -82,7 +82,7 @@ public function test_get_moodle_user(): void {
public function test_get_pseudo_domain(): void {
$this->resetAfterTest();

$response = turnitin_user::get_pseudo_domain();
$response = \turnitin_user::get_pseudo_domain();
$this->assertEquals(PLAGIARISM_TURNITIN_DEFAULT_PSEUDO_DOMAIN, $response);
}

Expand All @@ -95,7 +95,7 @@ public function test_get_pseudo_domain(): void {
public function test_get_pseudo_firstname(): void {
$this->resetAfterTest();

$turnitinuser = new turnitin_user(0, null, null, null, null);
$turnitinuser = new \turnitin_user(0, null, null, null, null);
$response = $turnitinuser->get_pseudo_firstname();
$this->assertEquals(PLAGIARISM_TURNITIN_DEFAULT_PSEUDO_FIRSTNAME, $response);
}
Expand All @@ -117,7 +117,7 @@ public function test_get_pseudo_lastname(): void {
set_config('plagiarism_turnitin_pseudolastname', 1, 'plagiarism_turnitin');
set_config('plagiarism_turnitin_lastnamegen', 1, 'plagiarism_turnitin');

$turnitinuser = new turnitin_user($student->id, null, null, null, null);
$turnitinuser = new \turnitin_user($student->id, null, null, null, null);
$response = $turnitinuser->get_pseudo_lastname();
$this->assertEquals(PLAGIARISM_TURNITIN_DEFAULT_PSEUDO_FIRSTNAME, $response);
}
Expand All @@ -142,7 +142,7 @@ public function test_unlink_user(): void {
$this->assertEquals(1, $count);

// Unlink the user.
$turnitinuser = new turnitin_user(0, null, null, null, null);
$turnitinuser = new \turnitin_user(0, null, null, null, null);
$turnitinuser->unlink_user($testuser["joins"][0]);

// We should have a Turnitin user ID of 0.
Expand Down
8 changes: 5 additions & 3 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace plagiarism_turnitin;

defined('MOODLE_INTERNAL') || die();
global $CFG;
global $DB;
Expand All @@ -34,7 +36,7 @@
* @copyright 2017 Turnitin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class plagiarism_turnitin_test_lib extends advanced_testcase {
abstract class plagiarism_turnitin_test_lib extends \advanced_testcase {

/**
* Creates a number of test plagiarism_turnitin users, creates an equivalent moodle user for each, and handles the database
Expand All @@ -51,7 +53,7 @@ public function make_test_users($numberofusers, $roles) {

for ($i = 0; $i < $numberofusers; $i++) {
$role = isset($roles[$i]) ? $roles[$i] : 'Instructor';
$newuser = new turnitin_user( $i + 1, $role, false, 'site', false );
$newuser = new \turnitin_user( $i + 1, $role, false, 'site', false );
array_push($return['plagiarism_turnitin_users'], $newuser);
$joinid = $this->join_test_user($newuser);
array_push($return['joins'], $joinid);
Expand All @@ -72,7 +74,7 @@ public function join_test_user($plagiarismturnitinuser) {
global $DB;

$mdluser = $this->getDataGenerator()->create_user();
$tiiuserrecord = new stdClass();
$tiiuserrecord = new \stdClass();
$tiiuserrecord->userid = $mdluser->id;
$tiiuserrecord->turnitin_uid = $plagiarismturnitinuser->id;
$tiiuserrecord->user_agreement_accepted = 1;
Expand Down
Loading

0 comments on commit c4ce6ce

Please sign in to comment.