MDL-55609 assignfeedback_comments: Remove shared setUp
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 25 May 2018 03:58:35 +0000 (11:58 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Thu, 31 May 2018 23:30:32 +0000 (07:30 +0800)
15 files changed:
mod/assign/feedback/comments/tests/comments_test.php
mod/assign/feedback/editpdf/tests/editpdf_test.php
mod/assign/feedback/file/tests/file_test.php
mod/assign/submission/comments/tests/events_test.php
mod/assign/submission/file/tests/events_test.php
mod/assign/submission/file/tests/locallib_test.php
mod/assign/submission/onlinetext/tests/events_test.php
mod/assign/submission/onlinetext/tests/locallib_test.php
mod/assign/tests/base_test.php
mod/assign/tests/externallib_test.php
mod/assign/tests/fixtures/testable_assign.php [new file with mode: 0644]
mod/assign/tests/generator.php
mod/assign/tests/locallib_test.php
mod/assign/tests/portfolio_caller_test.php
mod/assign/tests/restore_date_test.php

index 8050ca4..8ba047d 100644 (file)
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 /**
  * Unit tests for assignfeedback_comments
@@ -33,60 +33,54 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  * @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 {
+class assignfeedback_comments_testcase extends advanced_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;
-    }
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test the is_feedback_modified() method for the comments feedback.
      */
     public function test_is_feedback_modified() {
-        $assign = $this->create_assign_and_submit_text();
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 
-        $this->setUser($this->teachers[0]);
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => 1,
+                'assignfeedback_comments_enabled' => 1,
+            ]);
+
+        // Create an online text submission.
+        $this->add_submission($student, $assign);
+
+        $this->setUser($teacher);
 
         // Create formdata.
-        $data = new stdClass();
-        $data->assignfeedbackcomments_editor = array(
+        $grade = $assign->get_user_grade($student->id, true);
+        $data = (object) [
+            'assignfeedbackcomments_editor' => [
                 'text' => '<p>first comment for this test</p>',
-                'format' => 1
-            );
-        $grade = $assign->get_user_grade($this->students[0]->id, true);
+                'format' => 1,
+            ]
+        ];
 
         // 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(
+        $data->assignfeedbackcomments_editor = [
                 'text' => '<p>Altered comment for this test</p>',
-                'format' => 1
-            );
+                'format' => 1,
+            ];
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
     }
 }
index 0182e80..db5d8d1 100644 (file)
@@ -33,7 +33,7 @@ use \assignfeedback_editpdf\comment;
 use \assignfeedback_editpdf\annotation;
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 /**
  * Unit tests for assignfeedback_editpdf\comments_quick_list
@@ -41,31 +41,29 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  * @copyright  2013 Damyon Wiese
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
+class assignfeedback_editpdf_testcase extends advanced_testcase {
 
-    protected function setUp() {
+    // Use the generator helper.
+    use mod_assign_test_generator;
+
+    /**
+     * Ensure that GS is available.
+     */
+    protected function require_ghostscript() {
         // Skip this test if ghostscript is not supported.
         $result = pdf::test_gs_path(false);
         if ($result->status !== assignfeedback_editpdf\pdf::GSPATH_OK) {
             $this->markTestSkipped('Ghostscript not setup');
-            return;
         }
-        parent::setUp();
     }
 
-    protected function create_assign_and_submit_pdf() {
+    protected function add_file_submission($student, $assign) {
         global $CFG;
-        $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1,
-                                               'assignsubmission_file_enabled' => 1,
-                                               'assignsubmission_file_maxfiles' => 1,
-                                               'assignfeedback_editpdf_enabled' => 1,
-                                               'assignsubmission_file_maxsizebytes' => 1000000));
 
-        $user = $this->students[0];
-        $this->setUser($user);
+        $this->setUser($student);
 
         // Create a file submission with the test pdf.
-        $submission = $assign->get_user_submission($user->id, true);
+        $submission = $assign->get_user_submission($student->id, true);
 
         $fs = get_file_storage();
         $pdfsubmission = (object) array(
@@ -77,27 +75,24 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
             'filename' => 'submission.pdf'
         );
         $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
-        $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
+        $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
 
         $data = new stdClass();
         $plugin = $assign->get_submission_plugin_by_type('file');
         $plugin->save($submission, $data);
-
-        return $assign;
     }
 
     public function test_comments_quick_list() {
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 
-        $this->setUser($this->teachers[0]);
+        $this->setUser($teacher);
 
-        $comments = comments_quick_list::get_comments();
-
-        $this->assertEmpty($comments);
+        $this->assertEmpty(comments_quick_list::get_comments());
 
         $comment = comments_quick_list::add_comment('test', 45, 'red');
-
         $comments = comments_quick_list::get_comments();
-
         $this->assertEquals(count($comments), 1);
         $first = reset($comments);
         $this->assertEquals($comment, $first);
@@ -105,26 +100,36 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         $commentbyid = comments_quick_list::get_comment($comment->id);
         $this->assertEquals($comment, $commentbyid);
 
-        $result = comments_quick_list::remove_comment($comment->id);
-
-        $this->assertTrue($result);
+        $this->assertTrue(comments_quick_list::remove_comment($comment->id));
 
         $comments = comments_quick_list::get_comments();
         $this->assertEmpty($comments);
     }
 
     public function test_page_editor() {
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => 1,
+                'assignsubmission_file_enabled' => 1,
+                'assignsubmission_file_maxfiles' => 1,
+                'assignfeedback_editpdf_enabled' => 1,
+                'assignsubmission_file_maxsizebytes' => 1000000,
+            ]);
+
+        // Add the standard submission.
+        $this->add_file_submission($student, $assign);
 
-        $assign = $this->create_assign_and_submit_pdf();
-        $this->setUser($this->teachers[0]);
+        $this->setUser($teacher);
 
-        $grade = $assign->get_user_grade($this->students[0]->id, true);
+        $grade = $assign->get_user_grade($student->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;
@@ -132,7 +137,6 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         $comment->colour = 'red';
 
         $comment2 = new comment();
-
         $comment2->rawtext = 'Comment text 2';
         $comment2->width = 100;
         $comment2->x = 200;
@@ -142,7 +146,6 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         page_editor::set_comments($grade->id, 0, array($comment, $comment2));
 
         $annotation = new annotation();
-
         $annotation->path = '';
         $annotation->x = 100;
         $annotation->y = 100;
@@ -152,7 +155,6 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         $annotation->colour = 'red';
 
         $annotation2 = new annotation();
-
         $annotation2->path = '';
         $annotation2->x = 100;
         $annotation2->y = 100;
@@ -163,24 +165,19 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
 
         page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
 
-        $notempty = page_editor::has_annotations_or_comments($grade->id, false);
         // Still empty because all edits are still drafts.
-        $this->assertFalse($notempty);
+        $this->assertFalse(page_editor::has_annotations_or_comments($grade->id, false));
 
         $comments = page_editor::get_comments($grade->id, 0, false);
-
         $this->assertEmpty($comments);
 
         $comments = page_editor::get_comments($grade->id, 0, true);
-
         $this->assertEquals(count($comments), 2);
 
         $annotations = page_editor::get_annotations($grade->id, 0, false);
-
         $this->assertEmpty($annotations);
 
         $annotations = page_editor::get_annotations($grade->id, 0, true);
-
         $this->assertEquals(count($annotations), 2);
 
         $comment = reset($comments);
@@ -190,32 +187,44 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         page_editor::remove_annotation($annotation->id);
 
         $comments = page_editor::get_comments($grade->id, 0, true);
-
         $this->assertEquals(count($comments), 1);
 
         $annotations = page_editor::get_annotations($grade->id, 0, true);
-
         $this->assertEquals(count($annotations), 1);
 
+        // Release the drafts.
         page_editor::release_drafts($grade->id);
 
         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
-
         $this->assertTrue($notempty);
 
+        // Unrelease the drafts.
         page_editor::unrelease_drafts($grade->id);
 
         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
-
         $this->assertFalse($notempty);
     }
 
     public function test_document_services() {
-
-        $assign = $this->create_assign_and_submit_pdf();
-        $this->setUser($this->teachers[0]);
-
-        $grade = $assign->get_user_grade($this->students[0]->id, true);
+        $this->require_ghostscript();
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => 1,
+                'assignsubmission_file_enabled' => 1,
+                'assignsubmission_file_maxfiles' => 1,
+                'assignfeedback_editpdf_enabled' => 1,
+                'assignsubmission_file_maxsizebytes' => 1000000,
+            ]);
+
+        // Add the standard submission.
+        $this->add_file_submission($student, $assign);
+
+        $this->setUser($teacher);
+
+        $grade = $assign->get_user_grade($student->id, true);
 
         $contextid = $assign->get_context()->id;
         $component = 'assignfeedback_editpdf';
@@ -240,7 +249,7 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
         $this->assertEquals($combinedpdf->get_contenthash(), document_services::BLANK_PDF_HASH);
 
         // Generate page images and verify that the combined pdf has been replaced.
-        document_services::get_page_images_for_attempt($assign, $this->students[0]->id, -1);
+        document_services::get_page_images_for_attempt($assign, $student->id, -1);
         $combinedpdf = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
         $this->assertNotEquals($combinedpdf->get_contenthash(), document_services::BLANK_PDF_HASH);
 
@@ -334,11 +343,23 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
      * 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);
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => 1,
+                'assignsubmission_file_enabled' => 1,
+                'assignsubmission_file_maxfiles' => 1,
+                'assignfeedback_editpdf_enabled' => 1,
+                'assignsubmission_file_maxsizebytes' => 1000000,
+            ]);
+
+        // Add the standard submission.
+        $this->add_file_submission($student, $assign);
+
+        $this->setUser($teacher);
+        $grade = $assign->get_user_grade($student->id, true);
 
         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
         $this->assertFalse($notempty);
@@ -369,7 +390,7 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
 
         $plugin = $assign->get_feedback_plugin_by_type('editpdf');
         $data = new stdClass();
-        $data->editpdf_source_userid = $this->students[0]->id;
+        $data->editpdf_source_userid = $student->id;
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
         $plugin->save($grade, $data);
 
index 6063dc1..6376b60 100644 (file)
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 /**
  * Unit tests for assignfeedback_file
@@ -33,42 +33,32 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  * @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 {
+class assignfeedback_file_testcase extends advanced_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;
-    }
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test the is_feedback_modified() method for the file feedback.
      */
     public function test_is_feedback_modified() {
-        $assign = $this->create_assign_and_submit_text();
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => 1,
+                'assignfeedback_comments_enabled' => 1,
+            ]);
+
+        // Create an online text submission.
+        $this->add_submission($student, $assign);
 
-        $this->setUser($this->teachers[0]);
+        $this->setUser($teacher);
 
         $fs = get_file_storage();
-        $context = context_user::instance($this->teachers[0]->id);
+        $context = context_user::instance($teacher->id);
         $draftitemid = file_get_unused_draft_itemid();
         file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 
@@ -85,9 +75,9 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
-        $grade = $assign->get_user_grade($this->students[0]->id, true);
+        $grade = $assign->get_user_grade($student->id, true);
 
         // This is the first time that we are submitting feedback, so it is modified.
         $plugin = $assign->get_feedback_plugin_by_type('file');
@@ -104,7 +94,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertFalse($plugin->is_feedback_modified($grade, $data));
 
@@ -118,7 +108,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
         $plugin->save($grade, $data);
@@ -135,7 +125,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
         $plugin->save($grade, $data);
@@ -150,7 +140,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
         $plugin->save($grade, $data);
@@ -166,7 +156,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertTrue($plugin->is_feedback_modified($grade, $data));
         $plugin->save($grade, $data);
@@ -182,7 +172,7 @@ class assignfeedback_file_testcase extends mod_assign_base_testcase {
 
         // Create formdata.
         $data = new stdClass();
-        $data->{'files_' . $this->students[0]->id . '_filemanager'} = $draftitemid;
+        $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 
         $this->assertFalse($plugin->is_feedback_modified($grade, $data));
     }
index eacc25c..2ec414a 100644 (file)
@@ -28,7 +28,8 @@ defined('MOODLE_INTERNAL') || die();
 global $CFG;
 require_once($CFG->dirroot . '/mod/assign/lib.php');
 require_once($CFG->dirroot . '/mod/assign/locallib.php');
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
+require_once($CFG->dirroot . '/comment/lib.php');
 
 /**
  * Events tests class.
@@ -38,18 +39,24 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class assignsubmission_comments_events_testcase extends mod_assign_base_testcase {
+class assignsubmission_comments_events_testcase extends advanced_testcase {
+
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test comment_created event.
      */
     public function test_comment_created() {
-        global $CFG;
-        require_once($CFG->dirroot . '/comment/lib.php');
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+
+        $assign = $this->create_instance($course);
 
-        $this->setUser($this->editingteachers[0]);
-        $assign = $this->create_instance();
-        $submission = $assign->get_user_submission($this->students[0]->id, true);
+        $this->setUser($teacher);
+        $submission = $assign->get_user_submission($student->id, true);
 
         $context = $assign->get_context();
         $options = new stdClass();
@@ -69,6 +76,7 @@ class assignsubmission_comments_events_testcase extends mod_assign_base_testcase
         $events = $sink->get_events();
         $this->assertCount(1, $events);
         $event = reset($events);
+        $sink->close();
 
         // Checking that the event contains the expected values.
         $this->assertInstanceOf('\assignsubmission_comments\event\comment_created', $event);
@@ -82,12 +90,15 @@ class assignsubmission_comments_events_testcase extends mod_assign_base_testcase
      * Test comment_deleted event.
      */
     public function test_comment_deleted() {
-        global $CFG;
-        require_once($CFG->dirroot . '/comment/lib.php');
+        $this->resetAfterTest();
+        $course = $this->getDataGenerator()->create_course();
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+
+        $assign = $this->create_instance($course);
 
-        $this->setUser($this->editingteachers[0]);
-        $assign = $this->create_instance();
-        $submission = $assign->get_user_submission($this->students[0]->id, true);
+        $this->setUser($teacher);
+        $submission = $assign->get_user_submission($student->id, true);
 
         $context = $assign->get_context();
         $options = new stdClass();
index f8f4eac..45e19b3 100644 (file)
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 class assignsubmission_file_events_testcase extends advanced_testcase {
 
-    /** @var stdClass $user A user to submit an assignment. */
-    protected $user;
-
-    /** @var stdClass $course New course created to hold the assignment activity. */
-    protected $course;
-
-    /** @var stdClass $cm A context module object. */
-    protected $cm;
-
-    /** @var stdClass $context Context of the assignment activity. */
-    protected $context;
-
-    /** @var stdClass $assign The assignment object. */
-    protected $assign;
-
-    /** @var stdClass $files Files that are being submitted for the assignment. */
-    protected $files;
-
-    /** @var stdClass $submission Submission information. */
-    protected $submission;
-
-    /** @var stdClass $fi File information - First file*/
-    protected $fi;
-
-    /** @var stdClass $fi2 File information - Second file*/
-    protected $fi2;
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
-     * Setup all the various parts of an assignment activity including creating a file submission.
+     * Test that the assessable_uploaded event is fired when a file submission has been made.
      */
-    protected function setUp() {
-        $this->user = $this->getDataGenerator()->create_user();
-        $this->course = $this->getDataGenerator()->create_course();
-        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
-        $params['course'] = $this->course->id;
-        $instance = $generator->create_instance($params);
-        $this->cm = get_coursemodule_from_instance('assign', $instance->id);
-        $this->context = context_module::instance($this->cm->id);
-        $this->assign = new testable_assign($this->context, $this->cm, $this->course);
-
-        $this->setUser($this->user->id);
-        $this->submission = $this->assign->get_user_submission($this->user->id, true);
+    public function test_assessable_uploaded() {
+        $this->resetAfterTest();
+
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+        $cm = $assign->get_course_module();
+
+        $this->setUser($student->id);
+        $submission = $assign->get_user_submission($student->id, true);
 
         $fs = get_file_storage();
         $dummy = (object) array(
-            'contextid' => $this->context->id,
+            'contextid' => $context->id,
             'component' => 'assignsubmission_file',
             'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
-            'itemid' => $this->submission->id,
+            'itemid' => $submission->id,
             'filepath' => '/',
             'filename' => 'myassignmnent.pdf'
         );
-        $this->fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
         $dummy = (object) array(
-            'contextid' => $this->context->id,
+            'contextid' => $context->id,
             'component' => 'assignsubmission_file',
             'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
-            'itemid' => $this->submission->id,
+            'itemid' => $submission->id,
             'filepath' => '/',
             'filename' => 'myassignmnent.png'
         );
-        $this->fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
-        $this->files = $fs->get_area_files($this->context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
-            $this->submission->id, 'id', false);
-
-    }
-
-    /**
-     * Test that the assessable_uploaded event is fired when a file submission has been made.
-     */
-    public function test_assessable_uploaded() {
-        $this->resetAfterTest();
+        $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
+            $submission->id, 'id', false);
 
         $data = new stdClass();
-        $plugin = $this->assign->get_submission_plugin_by_type('file');
+        $plugin = $assign->get_submission_plugin_by_type('file');
         $sink = $this->redirectEvents();
-        $plugin->save($this->submission, $data);
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
         $this->assertCount(2, $events);
         $event = reset($events);
         $this->assertInstanceOf('\assignsubmission_file\event\assessable_uploaded', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->submission->id, $event->objectid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($submission->id, $event->objectid);
         $this->assertCount(2, $event->other['pathnamehashes']);
-        $this->assertEquals($this->fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
-        $this->assertEquals($this->fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
+        $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
+        $this->assertEquals($fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
         $expected = new stdClass();
         $expected->modulename = 'assign';
-        $expected->cmid = $this->cm->id;
-        $expected->itemid = $this->submission->id;
-        $expected->courseid = $this->course->id;
-        $expected->userid = $this->user->id;
-        $expected->file = $this->files;
-        $expected->files = $this->files;
-        $expected->pathnamehashes = array($this->fi->get_pathnamehash(), $this->fi2->get_pathnamehash());
+        $expected->cmid = $cm->id;
+        $expected->itemid = $submission->id;
+        $expected->courseid = $course->id;
+        $expected->userid = $student->id;
+        $expected->file = $files;
+        $expected->files = $files;
+        $expected->pathnamehashes = array($fi->get_pathnamehash(), $fi2->get_pathnamehash());
         $this->assertEventLegacyData($expected, $event);
         $this->assertEventContextNotUsed($event);
     }
@@ -135,22 +102,52 @@ class assignsubmission_file_events_testcase extends advanced_testcase {
     public function test_submission_created() {
         $this->resetAfterTest();
 
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+
+        $this->setUser($student->id);
+        $submission = $assign->get_user_submission($student->id, true);
+
+        $fs = get_file_storage();
+        $dummy = (object) array(
+            'contextid' => $context->id,
+            'component' => 'assignsubmission_file',
+            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
+            'itemid' => $submission->id,
+            'filepath' => '/',
+            'filename' => 'myassignmnent.pdf'
+        );
+        $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $dummy = (object) array(
+            'contextid' => $context->id,
+            'component' => 'assignsubmission_file',
+            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
+            'itemid' => $submission->id,
+            'filepath' => '/',
+            'filename' => 'myassignmnent.png'
+        );
+        $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
+            $submission->id, 'id', false);
+
         $data = new stdClass();
-        $plugin = $this->assign->get_submission_plugin_by_type('file');
+        $plugin = $assign->get_submission_plugin_by_type('file');
         $sink = $this->redirectEvents();
-        $plugin->save($this->submission, $data);
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
         $this->assertCount(2, $events);
         // We want to test the last event fired.
         $event = $events[1];
         $this->assertInstanceOf('\assignsubmission_file\event\submission_created', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->course->id, $event->courseid);
-        $this->assertEquals($this->submission->id, $event->other['submissionid']);
-        $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
-        $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
-        $this->assertEquals($this->submission->userid, $event->relateduserid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($course->id, $event->courseid);
+        $this->assertEquals($submission->id, $event->other['submissionid']);
+        $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
+        $this->assertEquals($submission->status, $event->other['submissionstatus']);
+        $this->assertEquals($submission->userid, $event->relateduserid);
     }
 
     /**
@@ -159,25 +156,55 @@ class assignsubmission_file_events_testcase extends advanced_testcase {
     public function test_submission_updated() {
         $this->resetAfterTest();
 
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+
+        $this->setUser($student->id);
+        $submission = $assign->get_user_submission($student->id, true);
+
+        $fs = get_file_storage();
+        $dummy = (object) array(
+            'contextid' => $context->id,
+            'component' => 'assignsubmission_file',
+            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
+            'itemid' => $submission->id,
+            'filepath' => '/',
+            'filename' => 'myassignmnent.pdf'
+        );
+        $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $dummy = (object) array(
+            'contextid' => $context->id,
+            'component' => 'assignsubmission_file',
+            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
+            'itemid' => $submission->id,
+            'filepath' => '/',
+            'filename' => 'myassignmnent.png'
+        );
+        $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
+        $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
+            $submission->id, 'id', false);
+
         $data = new stdClass();
-        $plugin = $this->assign->get_submission_plugin_by_type('file');
+        $plugin = $assign->get_submission_plugin_by_type('file');
         $sink = $this->redirectEvents();
         // Create a submission.
-        $plugin->save($this->submission, $data);
+        $plugin->save($submission, $data);
         // Update a submission.
-        $plugin->save($this->submission, $data);
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
         $this->assertCount(4, $events);
         // We want to test the last event fired.
         $event = $events[3];
         $this->assertInstanceOf('\assignsubmission_file\event\submission_updated', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->course->id, $event->courseid);
-        $this->assertEquals($this->submission->id, $event->other['submissionid']);
-        $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
-        $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
-        $this->assertEquals($this->submission->userid, $event->relateduserid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($course->id, $event->courseid);
+        $this->assertEquals($submission->id, $event->other['submissionid']);
+        $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
+        $this->assertEquals($submission->status, $event->other['submissionstatus']);
+        $this->assertEquals($submission->userid, $event->relateduserid);
     }
 
 }
index 2291d62..b033946 100644 (file)
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 /**
  * Unit tests for mod/assign/submission/file/locallib.php
@@ -35,40 +35,8 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  */
 class assignsubmission_file_locallib_testcase extends advanced_testcase {
 
-    /** @var stdClass $user A user to submit an assignment. */
-    protected $user;
-
-    /** @var stdClass $course New course created to hold the assignment activity. */
-    protected $course;
-
-    /** @var stdClass $cm A context module object. */
-    protected $cm;
-
-    /** @var stdClass $context Context of the assignment activity. */
-    protected $context;
-
-    /** @var stdClass $assign The assignment object. */
-    protected $assign;
-
-    /**
-     * Setup all the various parts of an assignment activity including creating an onlinetext submission.
-     */
-    protected function setUp() {
-        $this->user = $this->getDataGenerator()->create_user();
-        $this->course = $this->getDataGenerator()->create_course();
-        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
-        $params = [
-            'course' => $this->course->id,
-            'assignsubmission_file_enabled' => 1,
-            'assignsubmission_file_maxfiles' => 12,
-            'assignsubmission_file_maxsizebytes' => 10,
-        ];
-        $instance = $generator->create_instance($params);
-        $this->cm = get_coursemodule_from_instance('assign', $instance->id);
-        $this->context = context_module::instance($this->cm->id);
-        $this->assign = new testable_assign($this->context, $this->cm, $this->course);
-        $this->setUser($this->user->id);
-    }
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test submission_is_empty
@@ -80,12 +48,22 @@ class assignsubmission_file_locallib_testcase extends advanced_testcase {
     public function test_submission_is_empty($data, $expected) {
         $this->resetAfterTest();
 
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_file_enabled' => 1,
+                'assignsubmission_file_maxfiles' => 12,
+                'assignsubmission_file_maxsizebytes' => 10,
+            ]);
+
+        $this->setUser($student->id);
+
         $itemid = file_get_unused_draft_itemid();
         $submission = (object)['files_filemanager' => $itemid];
-        $plugin = $this->assign->get_submission_plugin_by_type('file');
+        $plugin = $assign->get_submission_plugin_by_type('file');
 
         if ($data) {
-            $data += ['contextid' => context_user::instance($this->user->id)->id, 'itemid' => $itemid];
+            $data += ['contextid' => context_user::instance($student->id)->id, 'itemid' => $itemid];
             $fs = get_file_storage();
             $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
         }
@@ -104,16 +82,26 @@ class assignsubmission_file_locallib_testcase extends advanced_testcase {
     public function test_new_submission_empty($data, $expected) {
         $this->resetAfterTest();
 
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_file_enabled' => 1,
+                'assignsubmission_file_maxfiles' => 12,
+                'assignsubmission_file_maxsizebytes' => 10,
+            ]);
+
+        $this->setUser($student);
+
         $itemid = file_get_unused_draft_itemid();
-        $submission = (object)['files_filemanager' => $itemid];
+        $submission = (object) ['files_filemanager' => $itemid];
 
         if ($data) {
-            $data += ['contextid' => context_user::instance($this->user->id)->id, 'itemid' => $itemid];
+            $data += ['contextid' => context_user::instance($student->id)->id, 'itemid' => $itemid];
             $fs = get_file_storage();
             $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
         }
 
-        $result = $this->assign->new_submission_empty($submission);
+        $result = $assign->new_submission_empty($submission);
         $this->assertTrue($result === $expected);
     }
 
index 56cf1ef..7c5c07a 100644 (file)
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
 
-    /** @var stdClass $user A user to submit an assignment. */
-    protected $user;
-
-    /** @var stdClass $course New course created to hold the assignment activity. */
-    protected $course;
-
-    /** @var stdClass $cm A context module object. */
-    protected $cm;
-
-    /** @var stdClass $context Context of the assignment activity. */
-    protected $context;
-
-    /** @var stdClass $assign The assignment object. */
-    protected $assign;
-
-    /** @var stdClass $submission Submission information. */
-    protected $submission;
-
-    /** @var stdClass $data General data for the assignment submission. */
-    protected $data;
-
-    /**
-     * Setup all the various parts of an assignment activity including creating an onlinetext submission.
-     */
-    protected function setUp() {
-        $this->user = $this->getDataGenerator()->create_user();
-        $this->course = $this->getDataGenerator()->create_course();
-        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
-        $params['course'] = $this->course->id;
-        $instance = $generator->create_instance($params);
-        $this->cm = get_coursemodule_from_instance('assign', $instance->id);
-        $this->context = context_module::instance($this->cm->id);
-        $this->assign = new testable_assign($this->context, $this->cm, $this->course);
-
-        $this->setUser($this->user->id);
-        $this->submission = $this->assign->get_user_submission($this->user->id, true);
-        $this->data = new stdClass();
-        $this->data->onlinetext_editor = array(
-            'itemid' => file_get_unused_draft_itemid(),
-            'text' => 'Submission text',
-            'format' => FORMAT_PLAIN
-        );
-    }
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test that the assessable_uploaded event is fired when an online text submission is saved.
@@ -79,25 +38,42 @@ class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
     public function test_assessable_uploaded() {
         $this->resetAfterTest();
 
-        $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+        $cm = $assign->get_course_module();
+
+        $this->setUser($student->id);
+
+        $submission = $assign->get_user_submission($student->id, true);
+        $data = (object) [
+            'onlinetext_editor' => [
+                'itemid' => file_get_unused_draft_itemid(),
+                'text' => 'Submission text',
+                'format' => FORMAT_PLAIN,
+            ],
+        ];
+
         $sink = $this->redirectEvents();
-        $plugin->save($this->submission, $this->data);
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
         $this->assertCount(2, $events);
         $event = reset($events);
         $this->assertInstanceOf('\assignsubmission_onlinetext\event\assessable_uploaded', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->submission->id, $event->objectid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($submission->id, $event->objectid);
         $this->assertEquals(array(), $event->other['pathnamehashes']);
         $this->assertEquals(FORMAT_PLAIN, $event->other['format']);
         $this->assertEquals('Submission text', $event->other['content']);
         $expected = new stdClass();
         $expected->modulename = 'assign';
-        $expected->cmid = $this->cm->id;
-        $expected->itemid = $this->submission->id;
-        $expected->courseid = $this->course->id;
-        $expected->userid = $this->user->id;
+        $expected->cmid = $cm->id;
+        $expected->itemid = $submission->id;
+        $expected->courseid = $course->id;
+        $expected->userid = $student->id;
         $expected->content = 'Submission text';
         $this->assertEventLegacyData($expected, $event);
         $this->assertEventContextNotUsed($event);
@@ -109,20 +85,36 @@ class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
     public function test_submission_created() {
         $this->resetAfterTest();
 
-        $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+
+        $this->setUser($student->id);
+
+        $submission = $assign->get_user_submission($student->id, true);
+        $data = (object) [
+            'onlinetext_editor' => [
+                'itemid' => file_get_unused_draft_itemid(),
+                'text' => 'Submission text',
+                'format' => FORMAT_PLAIN,
+            ],
+        ];
+
         $sink = $this->redirectEvents();
-        $plugin->save($this->submission, $this->data);
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
         $this->assertCount(2, $events);
         $event = $events[1];
         $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_created', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->course->id, $event->courseid);
-        $this->assertEquals($this->submission->id, $event->other['submissionid']);
-        $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
-        $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
-        $this->assertEquals($this->submission->userid, $event->relateduserid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($course->id, $event->courseid);
+        $this->assertEquals($submission->id, $event->other['submissionid']);
+        $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
+        $this->assertEquals($submission->status, $event->other['submissionstatus']);
+        $this->assertEquals($submission->userid, $event->relateduserid);
     }
 
     /**
@@ -132,22 +124,39 @@ class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
     public function test_submission_updated() {
         $this->resetAfterTest();
 
-        $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course);
+        $context = $assign->get_context();
+
+        $this->setUser($student->id);
+
+        $submission = $assign->get_user_submission($student->id, true);
+        $data = (object) [
+            'onlinetext_editor' => [
+                'itemid' => file_get_unused_draft_itemid(),
+                'text' => 'Submission text',
+                'format' => FORMAT_PLAIN,
+            ],
+        ];
+
         $sink = $this->redirectEvents();
-        // Create a submission.
-        $plugin->save($this->submission, $this->data);
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $plugin->save($submission, $data);
+        $sink->clear();
+
         // Update a submission.
-        $plugin->save($this->submission, $this->data);
+        $plugin->save($submission, $data);
         $events = $sink->get_events();
 
-        $this->assertCount(4, $events);
-        $event = $events[3];
+        $this->assertCount(2, $events);
+        $event = $events[1];
         $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_updated', $event);
-        $this->assertEquals($this->context->id, $event->contextid);
-        $this->assertEquals($this->course->id, $event->courseid);
-        $this->assertEquals($this->submission->id, $event->other['submissionid']);
-        $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
-        $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
-        $this->assertEquals($this->submission->userid, $event->relateduserid);
+        $this->assertEquals($context->id, $event->contextid);
+        $this->assertEquals($course->id, $event->courseid);
+        $this->assertEquals($submission->id, $event->other['submissionid']);
+        $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
+        $this->assertEquals($submission->status, $event->other['submissionstatus']);
+        $this->assertEquals($submission->userid, $event->relateduserid);
     }
 }
index c7595c4..2448c39 100644 (file)
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
 
 /**
  * Unit tests for mod/assign/submission/onlinetext/locallib.php
@@ -35,35 +35,8 @@ require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  */
 class assignsubmission_onlinetext_locallib_testcase extends advanced_testcase {
 
-    /** @var stdClass $user A user to submit an assignment. */
-    protected $user;
-
-    /** @var stdClass $course New course created to hold the assignment activity. */
-    protected $course;
-
-    /** @var stdClass $cm A context module object. */
-    protected $cm;
-
-    /** @var stdClass $context Context of the assignment activity. */
-    protected $context;
-
-    /** @var stdClass $assign The assignment object. */
-    protected $assign;
-
-    /**
-     * Setup all the various parts of an assignment activity including creating an onlinetext submission.
-     */
-    protected function setUp() {
-        $this->user = $this->getDataGenerator()->create_user();
-        $this->course = $this->getDataGenerator()->create_course();
-        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
-        $params = ['course' => $this->course->id, 'assignsubmission_onlinetext_enabled' => 1];
-        $instance = $generator->create_instance($params);
-        $this->cm = get_coursemodule_from_instance('assign', $instance->id);
-        $this->context = context_module::instance($this->cm->id);
-        $this->assign = new testable_assign($this->context, $this->cm, $this->course);
-        $this->setUser($this->user->id);
-    }
+    // Use the generator helper.
+    use mod_assign_test_generator;
 
     /**
      * Test submission_is_empty
@@ -75,11 +48,20 @@ class assignsubmission_onlinetext_locallib_testcase extends advanced_testcase {
     public function test_submission_is_empty($submissiontext, $expected) {
         $this->resetAfterTest();
 
-        $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
-        $data = new stdClass();
-        $data->onlinetext_editor = ['text' => $submissiontext];
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => true,
+            ]);
+
+        $this->setUser($student->id);
 
-        $result = $plugin->submission_is_empty($data);
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $result = $plugin->submission_is_empty((object) [
+                'onlinetext_editor' => [
+                    'text' => $submissiontext,
+                ],
+            ]);
         $this->assertTrue($result === $expected);
     }
 
@@ -92,10 +74,21 @@ class assignsubmission_onlinetext_locallib_testcase extends advanced_testcase {
      */
     public function test_new_submission_empty($submissiontext, $expected) {
         $this->resetAfterTest();
-        $data = new stdClass();
-        $data->onlinetext_editor = ['text' => $submissiontext];
 
-        $result = $this->assign->new_submission_empty($data);
+        $course = $this->getDataGenerator()->create_course();
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $assign = $this->create_instance($course, [
+                'assignsubmission_onlinetext_enabled' => true,
+            ]);
+
+        $this->setUser($student->id);
+
+        $result = $assign->new_submission_empty((object) [
+                'onlinetext_editor' => [
+                    'text' => $submissiontext,
+                ],
+            ]);
+
         $this->assertTrue($result === $expected);
     }
 
index bd1b313..d30f989 100644 (file)
@@ -220,3 +220,5 @@ class mod_assign_base_testcase extends advanced_testcase {
     }
 
 }
+
+class_alias('mod_assign_testable_assign', 'testable_assign');
index fb336e6..49a13ac 100644 (file)
@@ -20,6 +20,7 @@ global $CFG;
 
 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
 require_once($CFG->dirroot . '/mod/assign/externallib.php');
+require_once(__DIR__ . '/fixtures/testable_assign.php');
 
 /**
  * External mod assign functions unit tests
@@ -1818,8 +1819,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
      * @return array an array containing all the required data for testing
      */
     private function create_submission_for_testing_status($submitforgrading = false) {
-        global $DB, $CFG;
-        require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+        global $DB;
 
         // Create a course and assignment and users.
         $course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
@@ -1847,7 +1847,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $cm = get_coursemodule_from_instance('assign', $instance->id);
         $context = context_module::instance($cm->id);
 
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         $student1 = self::getDataGenerator()->create_user();
         $student2 = self::getDataGenerator()->create_user();
@@ -2355,8 +2355,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
      * submitted.
      */
     public function test_get_participant_group_submission() {
-        global $DB, $CFG;
-        require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
+        global $DB;
 
         $this->resetAfterTest(true);
 
@@ -2373,7 +2372,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
         $cm = get_coursemodule_from_instance('assign', $assignmodule->id);
         $context = context_module::instance($cm->id);
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         groups_add_member($group, $student);
 
diff --git a/mod/assign/tests/fixtures/testable_assign.php b/mod/assign/tests/fixtures/testable_assign.php
new file mode 100644 (file)
index 0000000..e9adbab
--- /dev/null
@@ -0,0 +1,176 @@
+<?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/>.
+
+/**
+ * The testable assign class.
+ *
+ * @package   mod_assign
+ * @copyright 2014 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/locallib.php');
+
+/**
+ * Test subclass that makes all the protected methods we want to test public.
+ */
+class mod_assign_testable_assign extends assign {
+
+    public function testable_show_intro() {
+        return parent::show_intro();
+    }
+
+    public function testable_delete_grades() {
+        return parent::delete_grades();
+    }
+
+    public function testable_apply_grade_to_user($formdata, $userid, $attemptnumber) {
+        return parent::apply_grade_to_user($formdata, $userid, $attemptnumber);
+    }
+
+    public function testable_format_submission_for_log(stdClass $submission) {
+        return parent::format_submission_for_log($submission);
+    }
+
+    public function testable_get_grading_userid_list() {
+        return parent::get_grading_userid_list();
+    }
+
+    public function testable_is_graded($userid) {
+        return parent::is_graded($userid);
+    }
+
+    public function testable_update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
+        return parent::update_submission($submission, $userid, $updatetime, $teamsubmission);
+    }
+
+    public function testable_process_add_attempt($userid = 0) {
+        return parent::process_add_attempt($userid);
+    }
+
+    public function testable_process_save_quick_grades($postdata) {
+        // Ugly hack to get something into the method.
+        global $_POST;
+        $_POST = $postdata;
+        return parent::process_save_quick_grades();
+    }
+
+    public function testable_process_set_batch_marking_allocation($selectedusers, $markerid) {
+        global $CFG;
+        require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
+
+        // Simulate the form submission.
+        $data = array();
+        $data['id'] = $this->get_course_module()->id;
+        $data['selectedusers'] = $selectedusers;
+        $data['allocatedmarker'] = $markerid;
+        $data['action'] = 'setbatchmarkingallocation';
+        mod_assign_batch_set_allocatedmarker_form::mock_submit($data);
+
+        return parent::process_set_batch_marking_allocation();
+    }
+
+    public function testable_process_set_batch_marking_workflow_state($selectedusers, $state) {
+        global $CFG;
+        require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
+
+        // Simulate the form submission.
+        $data = array();
+        $data['id'] = $this->get_course_module()->id;
+        $data['selectedusers'] = $selectedusers;
+        $data['markingworkflowstate'] = $state;
+        $data['action'] = 'setbatchmarkingworkflowstate';
+        mod_assign_batch_set_marking_workflow_state_form::mock_submit($data);
+
+        return parent::process_set_batch_marking_workflow_state();
+    }
+
+    public function testable_submissions_open($userid = 0) {
+        return parent::submissions_open($userid);
+    }
+
+    public function testable_save_user_extension($userid, $extensionduedate) {
+        return parent::save_user_extension($userid, $extensionduedate);
+    }
+
+    public function testable_get_graders($userid) {
+        // Changed method from protected to public.
+        return parent::get_graders($userid);
+    }
+
+    public function testable_get_notifiable_users($userid) {
+        return parent::get_notifiable_users($userid);
+    }
+
+    public function testable_view_batch_set_workflow_state($selectedusers) {
+        global $PAGE;
+        $PAGE->set_url('/mod/assign/view.php');
+        $mform = $this->testable_grading_batch_operations_form('setmarkingworkflowstate', $selectedusers);
+        return parent::view_batch_set_workflow_state($mform);
+    }
+
+    public function testable_view_batch_markingallocation($selectedusers) {
+        global $PAGE;
+        $PAGE->set_url('/mod/assign/view.php');
+        $mform = $this->testable_grading_batch_operations_form('setmarkingallocation', $selectedusers);
+        return parent::view_batch_markingallocation($mform);
+    }
+
+    public function testable_grading_batch_operations_form($operation, $selectedusers) {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
+
+        // Mock submit the grading operations form.
+        $data = array();
+        $data['id'] = $this->get_course_module()->id;
+        $data['selectedusers'] = $selectedusers;
+        $data['returnaction'] = 'grading';
+        $data['operation'] = $operation;
+        mod_assign_grading_batch_operations_form::mock_submit($data);
+
+        // Set required variables in the form.
+        $formparams = array();
+        $formparams['submissiondrafts'] = 1;
+        $formparams['duedate'] = 1;
+        $formparams['attemptreopenmethod'] = ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
+        $formparams['feedbackplugins'] = array();
+        $formparams['markingworkflow'] = 1;
+        $formparams['markingallocation'] = 1;
+        $formparams['cm'] = $this->get_course_module()->id;
+        $formparams['context'] = $this->get_context();
+        $mform = new mod_assign_grading_batch_operations_form(null, $formparams);
+
+        return $mform;
+    }
+
+    public function testable_update_activity_completion_records($teamsubmission,
+                                                          $requireallteammemberssubmit,
+                                                          $submission,
+                                                          $userid,
+                                                          $complete,
+                                                          $completion) {
+        return parent::update_activity_completion_records($teamsubmission,
+                                                          $requireallteammemberssubmit,
+                                                          $submission,
+                                                          $userid,
+                                                          $complete,
+                                                          $completion);
+    }
+}
index 96ea18c..ba28665 100644 (file)
@@ -27,7 +27,6 @@ defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
 require_once($CFG->dirroot . '/mod/assign/locallib.php');
-require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
 require_once(__DIR__ . '/fixtures/testable_assign.php');
 
 /**
index 4a6dae9..db8958a 100644 (file)
@@ -52,8 +52,8 @@ class mod_assign_locallib_testcase extends advanced_testcase {
         $PAGE->set_url(new moodle_url('/mod/assign/view.php', ['id' => $assign->get_course_module()->id]));
 
         $assign->register_return_link('RETURNACTION', ['param' => 1]);
-        $this->assertEquals($returnaction, $assign->get_return_action());
-        $this->assertEquals($returnparams, $assign->get_return_params());
+        $this->assertEquals('RETURNACTION', $assign->get_return_action());
+        $this->assertEquals(['param' => 1], $assign->get_return_params());
     }
 
     public function test_get_feedback_plugins() {
index f56db90..933b767 100644 (file)
@@ -27,7 +27,6 @@ defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
 require_once($CFG->dirroot . '/mod/assign/locallib.php');
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
 require_once($CFG->dirroot . '/group/lib.php');
 
 /**
@@ -55,7 +54,7 @@ class mod_assign_portfolio_caller_testcase extends advanced_testcase {
         $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
         $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
         $context = context_module::instance($cm->id);
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         $submission = $assign->get_user_submission($user->id, true);
 
@@ -105,7 +104,7 @@ class mod_assign_portfolio_caller_testcase extends advanced_testcase {
         $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
         $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
         $context = context_module::instance($cm->id);
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         $submission = $assign->get_user_submission($user->id, true);
 
@@ -154,7 +153,7 @@ class mod_assign_portfolio_caller_testcase extends advanced_testcase {
         $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
         $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
         $context = context_module::instance($cm->id);
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         $submission = $assign->get_group_submission($user->id, $groupid, true);
 
@@ -211,7 +210,7 @@ class mod_assign_portfolio_caller_testcase extends advanced_testcase {
         $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
         $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
         $context = context_module::instance($cm->id);
-        $assign = new testable_assign($context, $cm, $course);
+        $assign = new mod_assign_testable_assign($context, $cm, $course);
 
         $submission = $assign->get_group_submission($user->id, $groupid,true);
 
index c6c50b9..f3794bd 100644 (file)
@@ -26,7 +26,6 @@ defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
 require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
-require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
 
 /**
  * Restore date tests.
@@ -46,7 +45,7 @@ class mod_assign_restore_date_testcase extends restore_date_testcase {
         $record = ['cutoffdate' => 100, 'allowsubmissionsfromdate' => 100, 'duedate' => 100, 'timemodified' => 100];
         list($course, $assign) = $this->create_course_and_module('assign', $record);
         $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $assign->id]);
-        $assignobj = new testable_assign(context_module::instance($cm->id), $cm, $course);
+        $assignobj = new mod_assign_testable_assign(context_module::instance($cm->id), $cm, $course);
         $submission = $assignobj->get_user_submission($USER->id, true);
         $grade = $assignobj->get_user_grade($USER->id, true);