MDL-49025 assign: Test than an empty directory will not submit
authorNeill Magill <neill.magill@nottingham.ac.uk>
Fri, 1 Jun 2018 09:48:58 +0000 (10:48 +0100)
committerNeill Magill <neill.magill@nottingham.ac.uk>
Fri, 13 Jul 2018 13:03:41 +0000 (14:03 +0100)
mod/assign/submission/file/tests/locallib_test.php

index b033946..c055ece 100644 (file)
@@ -72,6 +72,34 @@ class assignsubmission_file_locallib_testcase extends advanced_testcase {
         $this->assertTrue($result === $expected);
     }
 
+    /**
+     * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
+     */
+    public function test_submission_is_empty_directory_only() {
+        $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 = $assign->get_submission_plugin_by_type('file');
+        $fs = get_file_storage();
+        $fs->create_directory(
+                context_user::instance($student->id)->id,
+                'user',
+                'draft',
+                $itemid,
+                '/subdirectory/'
+        );
+
+        $this->assertTrue($plugin->submission_is_empty($submission));
+    }
+
     /**
      * Test new_submission_empty
      *
@@ -105,6 +133,34 @@ class assignsubmission_file_locallib_testcase extends advanced_testcase {
         $this->assertTrue($result === $expected);
     }
 
+    /**
+     * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
+     */
+    public function test_new_submission_empty_directory_only() {
+        $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 = $assign->get_submission_plugin_by_type('file');
+        $fs = get_file_storage();
+        $fs->create_directory(
+                context_user::instance($student->id)->id,
+                'user',
+                'draft',
+                $itemid,
+                '/subdirectory/'
+        );
+
+        $this->assertTrue($assign->new_submission_empty($submission));
+    }
+
     /**
      * Dataprovider for the test_submission_is_empty testcase
      *
@@ -121,6 +177,15 @@ class assignsubmission_file_locallib_testcase extends advanced_testcase {
                 ],
                 false
             ],
+            'With file in directory' => [
+                [
+                    'component' => 'user',
+                    'filearea' => 'draft',
+                    'filepath' => '/subdir/',
+                    'filename' => 'not_a_virus.exe'
+                ],
+                false
+            ],
             'Without file' => [null, true]
         ];
     }