From 50c734e8a552f9eba4fe8113ea9ffce180a60004 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 9 Feb 2016 15:00:53 +0800 Subject: [PATCH] MDL-52397 mod_assign: Unit tests for feedback plugins. Testing that the new method is_feedback_modified() works as it should with the standard plugins. --- .../feedback/comments/tests/comments_test.php | 92 +++++++++ .../feedback/editpdf/tests/editpdf_test.php | 120 +++++++++++ mod/assign/feedback/file/tests/file_test.php | 189 ++++++++++++++++++ mod/assign/tests/locallib_test.php | 4 +- 4 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 mod/assign/feedback/comments/tests/comments_test.php create mode 100644 mod/assign/feedback/file/tests/file_test.php diff --git a/mod/assign/feedback/comments/tests/comments_test.php b/mod/assign/feedback/comments/tests/comments_test.php new file mode 100644 index 00000000000..8050ca42c1e --- /dev/null +++ b/mod/assign/feedback/comments/tests/comments_test.php @@ -0,0 +1,92 @@ +. + +/** + * Unit tests for assignfeedback_comments + * + * @package assignfeedback_comments + * @copyright 2016 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/assign/tests/base_test.php'); + +/** + * Unit tests for assignfeedback_comments + * + * @copyright 2016 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class assignfeedback_comments_testcase extends mod_assign_base_testcase { + + /** + * Create an assign object and submit an online text submission. + */ + protected function create_assign_and_submit_text() { + $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1, + 'assignfeedback_comments_enabled' => 1)); + + $user = $this->students[0]; + $this->setUser($user); + + // Create an online text submission. + $submission = $assign->get_user_submission($user->id, true); + + $data = new stdClass(); + $data->onlinetext_editor = array( + 'text' => '

This is some text.

', + 'format' => 1, + 'itemid' => file_get_unused_draft_itemid()); + $plugin = $assign->get_submission_plugin_by_type('onlinetext'); + $plugin->save($submission, $data); + + return $assign; + } + + /** + * Test the is_feedback_modified() method for the comments feedback. + */ + public function test_is_feedback_modified() { + $assign = $this->create_assign_and_submit_text(); + + $this->setUser($this->teachers[0]); + + // Create formdata. + $data = new stdClass(); + $data->assignfeedbackcomments_editor = array( + 'text' => '

first comment for this test

', + 'format' => 1 + ); + $grade = $assign->get_user_grade($this->students[0]->id, true); + + // This is the first time that we are submitting feedback, so it is modified. + $plugin = $assign->get_feedback_plugin_by_type('comments'); + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + // Save the feedback. + $plugin->save($grade, $data); + // Try again with the same data. + $this->assertFalse($plugin->is_feedback_modified($grade, $data)); + // Change the data. + $data->assignfeedbackcomments_editor = array( + 'text' => '

Altered comment for this test

', + 'format' => 1 + ); + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + } +} diff --git a/mod/assign/feedback/editpdf/tests/editpdf_test.php b/mod/assign/feedback/editpdf/tests/editpdf_test.php index 6afe93ffa8b..3a0ad840d2b 100644 --- a/mod/assign/feedback/editpdf/tests/editpdf_test.php +++ b/mod/assign/feedback/editpdf/tests/editpdf_test.php @@ -300,4 +300,124 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase { $this->assertEmpty($file3); } + + /** + * Test that modifying the annotated pdf form return true when modified + * and false when not modified. + */ + public function test_is_feedback_modified() { + global $DB; + $assign = $this->create_assign_and_submit_pdf(); + $this->setUser($this->teachers[0]); + + $grade = $assign->get_user_grade($this->students[0]->id, true); + + $notempty = page_editor::has_annotations_or_comments($grade->id, false); + $this->assertFalse($notempty); + + $comment = new comment(); + + $comment->rawtext = 'Comment text'; + $comment->width = 100; + $comment->x = 100; + $comment->y = 100; + $comment->colour = 'red'; + + page_editor::set_comments($grade->id, 0, array($comment)); + + $annotations = array(); + + $annotation = new annotation(); + $annotation->path = ''; + $annotation->x = 100; + $annotation->y = 100; + $annotation->endx = 200; + $annotation->endy = 200; + $annotation->type = 'line'; + $annotation->colour = 'red'; + array_push($annotations, $annotation); + + page_editor::set_annotations($grade->id, 0, $annotations); + + $plugin = $assign->get_feedback_plugin_by_type('editpdf'); + $data = new stdClass(); + $data->editpdf_source_userid = $this->students[0]->id; + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + $annotation = new annotation(); + $annotation->gradeid = $grade->id; + $annotation->pageno = 0; + $annotation->path = ''; + $annotation->x = 100; + $annotation->y = 100; + $annotation->endx = 200; + $annotation->endy = 200; + $annotation->type = 'rectangle'; + $annotation->colour = 'yellow'; + + page_editor::add_annotation($annotation); + + // Add a comment as well. + $comment = new comment(); + $comment->gradeid = $grade->id; + $comment->pageno = 0; + $comment->rawtext = 'Second Comment text'; + $comment->width = 100; + $comment->x = 100; + $comment->y = 100; + $comment->colour = 'red'; + page_editor::add_comment($comment); + + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // We should have two annotations. + $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false)); + // And two comments. + $this->assertCount(2, page_editor::get_comments($grade->id, 0, false)); + + // Add one annotation and delete another. + $annotation = new annotation(); + $annotation->gradeid = $grade->id; + $annotation->pageno = 0; + $annotation->path = '100,100:105,105:110,100'; + $annotation->x = 100; + $annotation->y = 100; + $annotation->endx = 110; + $annotation->endy = 105; + $annotation->type = 'pen'; + $annotation->colour = 'black'; + page_editor::add_annotation($annotation); + + $annotations = page_editor::get_annotations($grade->id, 0, true); + page_editor::remove_annotation($annotations[1]->id); + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // We should have two annotations. + $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false)); + // And two comments. + $this->assertCount(2, page_editor::get_comments($grade->id, 0, false)); + + // Add a comment and then remove it. Should not be considered as modified. + $comment = new comment(); + $comment->gradeid = $grade->id; + $comment->pageno = 0; + $comment->rawtext = 'Third Comment text'; + $comment->width = 400; + $comment->x = 57; + $comment->y = 205; + $comment->colour = 'black'; + $comment->id = page_editor::add_comment($comment); + + // We should now have three comments. + $this->assertCount(3, page_editor::get_comments($grade->id, 0, true)); + // Now delete the newest record. + page_editor::remove_comment($comment->id); + // Back to two comments. + $this->assertCount(2, page_editor::get_comments($grade->id, 0, true)); + // No modification. + $this->assertFalse($plugin->is_feedback_modified($grade, $data)); + } } diff --git a/mod/assign/feedback/file/tests/file_test.php b/mod/assign/feedback/file/tests/file_test.php new file mode 100644 index 00000000000..6063dc1dad9 --- /dev/null +++ b/mod/assign/feedback/file/tests/file_test.php @@ -0,0 +1,189 @@ +. + +/** + * Unit tests for assignfeedback_file + * + * @package assignfeedback_file + * @copyright 2016 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/assign/tests/base_test.php'); + +/** + * Unit tests for assignfeedback_file + * + * @copyright 2016 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class assignfeedback_file_testcase extends mod_assign_base_testcase { + + /** + * Create an assign object and submit an online text submission. + */ + protected function create_assign_and_submit_text() { + $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1, + 'assignfeedback_comments_enabled' => 1)); + + $user = $this->students[0]; + $this->setUser($user); + + // Create an online text submission. + $submission = $assign->get_user_submission($user->id, true); + + $data = new stdClass(); + $data->onlinetext_editor = array( + 'text' => '

This is some text.

', + 'format' => 1, + 'itemid' => file_get_unused_draft_itemid()); + $plugin = $assign->get_submission_plugin_by_type('onlinetext'); + $plugin->save($submission, $data); + + return $assign; + } + + /** + * Test the is_feedback_modified() method for the file feedback. + */ + public function test_is_feedback_modified() { + $assign = $this->create_assign_and_submit_text(); + + $this->setUser($this->teachers[0]); + + $fs = get_file_storage(); + $context = context_user::instance($this->teachers[0]->id); + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy = array( + 'contextid' => $context->id, + 'component' => 'user', + 'filearea' => 'draft', + 'itemid' => $draftitemid, + 'filepath' => '/', + 'filename' => 'feedback1.txt' + ); + + $file = $fs->create_file_from_string($dummy, 'This is the first feedback file'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $grade = $assign->get_user_grade($this->students[0]->id, true); + + // This is the first time that we are submitting feedback, so it is modified. + $plugin = $assign->get_feedback_plugin_by_type('file'); + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + // Save the feedback. + $plugin->save($grade, $data); + // Try again with the same data. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + + $file = $fs->create_file_from_string($dummy, 'This is the first feedback file'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertFalse($plugin->is_feedback_modified($grade, $data)); + + // Same name for the file but different content. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + + $file = $fs->create_file_from_string($dummy, 'This is different feedback'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // Add another file. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + + $file = $fs->create_file_from_string($dummy, 'This is different feedback'); + $dummy['filename'] = 'feedback2.txt'; + $file = $fs->create_file_from_string($dummy, 'A second feedback file'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // Deleting a file. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + + $file = $fs->create_file_from_string($dummy, 'This is different feedback'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // The file was moved to a folder. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + $dummy['filepath'] = '/testdir/'; + + $file = $fs->create_file_from_string($dummy, 'This is different feedback'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertTrue($plugin->is_feedback_modified($grade, $data)); + $plugin->save($grade, $data); + + // No modification to the file in the folder. + $draftitemid = file_get_unused_draft_itemid(); + file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1); + + $dummy['itemid'] = $draftitemid; + $dummy['filepath'] = '/testdir/'; + + $file = $fs->create_file_from_string($dummy, 'This is different feedback'); + + // Create formdata. + $data = new stdClass(); + $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid; + + $this->assertFalse($plugin->is_feedback_modified($grade, $data)); + } +} diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index 547ac86a876..f80ec785694 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -781,7 +781,7 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase { // Wait 1 second so the submission and grade do not have the same timemodified. sleep(1); // Simulate adding a grade. - $this->setUser($this->teachers[0]); + $this->setUser($this->editingteachers[0]); $data = new stdClass(); $data->grade = '50.0'; $assign1->testable_apply_grade_to_user($data, $this->extrastudents[3]->id, 0); @@ -898,7 +898,7 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase { $plugin->save($submission, $data); // Simulate adding a grade. - $this->setUser($this->teachers[0]); + $this->setUser($this->editingteachers[0]); $data = new stdClass(); $data->grade = '50.0'; $assign->testable_apply_grade_to_user($data, $this->extrastudents[3]->id, 0);