MDL-52397 mod_assign: Unit tests for feedback plugins.
authorAdrian Greeve <adrian@moodle.com>
Tue, 9 Feb 2016 07:00:53 +0000 (15:00 +0800)
committerAdrian Greeve <adrian@moodle.com>
Wed, 24 Feb 2016 07:09:27 +0000 (15:09 +0800)
Testing that the new method is_feedback_modified() works
as it should with the standard plugins.

mod/assign/feedback/comments/tests/comments_test.php [new file with mode: 0644]
mod/assign/feedback/editpdf/tests/editpdf_test.php
mod/assign/feedback/file/tests/file_test.php [new file with mode: 0644]
mod/assign/tests/locallib_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 (file)
index 0000000..8050ca4
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Unit tests for assignfeedback_comments
+ *
+ * @package    assignfeedback_comments
+ * @copyright  2016 Adrian Greeve <adrian@moodle.com>
+ * @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 <adrian@moodle.com>
+ * @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' => '<p>This is some text.</p>',
+                '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' => '<p>first comment for this test</p>',
+                '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' => '<p>Altered comment for this test</p>',
+                'format' => 1
+            );
+        $this->assertTrue($plugin->is_feedback_modified($grade, $data));
+    }
+}
index 6afe93f..3a0ad84 100644 (file)
@@ -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 (file)
index 0000000..6063dc1
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Unit tests for assignfeedback_file
+ *
+ * @package    assignfeedback_file
+ * @copyright  2016 Adrian Greeve <adrian@moodle.com>
+ * @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 <adrian@moodle.com>
+ * @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' => '<p>This is some text.</p>',
+                '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));
+    }
+}
index 547ac86..f80ec78 100644 (file)
@@ -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);