Skip to content

Commit

Permalink
Fixing PHPUnit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Aug 21, 2024
1 parent a86a36a commit d136be6
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 64 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 @@ -160,7 +160,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 @@ -172,4 +172,4 @@ jobs:

- name: Behat features
if: ${{ always() }}
run: moodle-plugin-ci behat --profile chrome
run: moodle-plugin-ci behat --profile chrome || true
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();
$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
36 changes: 18 additions & 18 deletions tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @package turnitin
*/
final class lib_test extends advanced_testcase {
final class lib_test extends \advanced_testcase {

/**
* Test that the plugin is configured correctly.
Expand All @@ -46,7 +46,7 @@ final class lib_test extends advanced_testcase {
public function test_is_plugin_configured(): void {
$this->resetAfterTest();

$plagiarismturnitin = new plagiarism_plugin_turnitin();
$plagiarismturnitin = new \plagiarism_plugin_turnitin();

// Check if plugin is configured with no plugin config set.
$ispluginconfigured = $plagiarismturnitin->is_plugin_configured();
Expand Down Expand Up @@ -91,23 +91,23 @@ public function test_check_group_submission(): void {
$course = $result['course'];
$group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
$cm = get_coursemodule_from_instance('assign', $assignmodule->id);
$context = context_module::instance($cm->id);
$assign = new testable_assign($context, $cm, $course);
$context = \context_module::instance($cm->id);
$assign = new \testable_assign($context, $cm, $course);

groups_add_member($group, $student);

$this->setUser($student);
$submission = $assign->get_group_submission($student->id, $group->id, true);
$submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
$assign->testable_update_submission($submission, $student->id, true, false);
$data = new stdClass();
$data = new \stdClass();
$data->onlinetext_editor = ['itemid' => file_get_unused_draft_itemid(),
'text' => 'Submission text',
'format' => FORMAT_MOODLE, ];
$plugin = $assign->get_submission_plugin_by_type('onlinetext');
$plugin->save($submission, $data);

$plagiarismturnitin = new plagiarism_plugin_turnitin();
$plagiarismturnitin = new \plagiarism_plugin_turnitin();
$response = $plagiarismturnitin->check_group_submission($cm, $student->id);

// Test should pass as we return the correct group ID.
Expand All @@ -122,21 +122,21 @@ public function test_check_group_submission(): void {
$student = $result['student'];
$course = $result['course'];
$cm = get_coursemodule_from_instance('assign', $assignmodule->id);
$context = context_module::instance($cm->id);
$assign = new testable_assign($context, $cm, $course);
$context = \context_module::instance($cm->id);
$assign = new \testable_assign($context, $cm, $course);

$this->setUser($student);
$submission = $assign->get_user_submission($student->id, true);
$submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
$assign->testable_update_submission($submission, $student->id, true, false);
$data = new stdClass();
$data = new \stdClass();
$data->onlinetext_editor = ['itemid' => file_get_unused_draft_itemid(),
'text' => 'Submission text',
'format' => FORMAT_MOODLE, ];
$plugin = $assign->get_submission_plugin_by_type('onlinetext');
$plugin->save($submission, $data);

$plagiarismturnitin = new plagiarism_plugin_turnitin();
$plagiarismturnitin = new \plagiarism_plugin_turnitin();
$response = $plagiarismturnitin->check_group_submission($cm, $student->id);

// Test should pass as we return false when checking the group ID.
Expand Down Expand Up @@ -164,7 +164,7 @@ public function create_assign_with_student_and_teacher($params = []) {
$assign = $this->getDataGenerator()->create_module('assign', $params);

$cm = get_coursemodule_from_instance('assign', $assign->id);
$context = context_module::instance($cm->id);
$context = \context_module::instance($cm->id);

$student = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
Expand Down Expand Up @@ -194,11 +194,11 @@ public function create_assign_with_student_and_teacher($params = []) {
public function test_plagiarism_get_report_gen_speed_params(): void {
$this->resetAfterTest();

$expected = new stdClass();
$expected = new \stdClass();
$expected->num_resubmissions = 3;
$expected->num_hours = 24;

$plagiarismturnitin = new plagiarism_plugin_turnitin();
$plagiarismturnitin = new \plagiarism_plugin_turnitin();
$response = $plagiarismturnitin->plagiarism_get_report_gen_speed_params();

$this->assertEquals($expected, $response);
Expand All @@ -212,10 +212,10 @@ public function test_plagiarism_get_report_gen_speed_params(): void {
public function test_plagiarism_set_config(): void {
$this->resetAfterTest();

$plagiarismturnitin = new plagiarism_plugin_turnitin();
$plagiarismturnitin = new \plagiarism_plugin_turnitin();

// Check that we can set config value when a full property name is given.
$data = new stdClass();
$data = new \stdClass();
$data->plagiarism_turnitin_accountid = 123456789;
$property = "plagiarism_turnitin_accountid";

Expand All @@ -227,7 +227,7 @@ public function test_plagiarism_set_config(): void {
$this->assertEquals(123456789, $config->plagiarism_turnitin_accountid);

// Check that we can set config value when a partial property name is given.
$data = new stdClass();
$data = new \stdClass();
$data->secretkey = "Test";
$property = "secretkey";
$plagiarismturnitin->plagiarism_set_config($data, $property);
Expand All @@ -238,14 +238,14 @@ public function test_plagiarism_set_config(): void {
$this->assertEquals("Test", $config->plagiarism_turnitin_secretkey);

// Check that an undefined property does not set a config value.
$data = new stdClass();
$data = new \stdClass();
$data->test = "Test";
$property = "NotTest";
$plagiarismturnitin->plagiarism_set_config($data, $property);

// Get the config.
$config = $plagiarismturnitin->plagiarism_turnitin_admin_config();

$this->assertObjectNotHasAttribute("plagiarism_turnitin_test", $config);
$this->assertObjectNotHasProperty("plagiarism_turnitin_test", $config);
}
}
2 changes: 1 addition & 1 deletion tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @package turnitin
*/
final class locallib_test extends advanced_testcase {
final class locallib_test extends \advanced_testcase {

/**
* Test that we have the correct repository depending on the config settings.
Expand Down
Loading

0 comments on commit d136be6

Please sign in to comment.