Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Tests for mod/assign/submission/file/locallib.php
  19   *
  20   * @package   assignsubmission_file
  21   * @copyright 2016 Cameron Ball
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace assignsubmission_file;
  25  
  26  use mod_assign_test_generator;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  32  
  33  /**
  34   * Unit tests for mod/assign/submission/file/locallib.php
  35   *
  36   * @copyright  2016 Cameron Ball
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class locallib_test extends \advanced_testcase {
  40  
  41      // Use the generator helper.
  42      use mod_assign_test_generator;
  43  
  44      /**
  45       * Test submission_is_empty
  46       *
  47       * @dataProvider submission_is_empty_testcases
  48       * @param string $data The file submission data
  49       * @param bool $expected The expected return value
  50       */
  51      public function test_submission_is_empty($data, $expected) {
  52          $this->resetAfterTest();
  53  
  54          $course = $this->getDataGenerator()->create_course();
  55          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  56          $assign = $this->create_instance($course, [
  57                  'assignsubmission_file_enabled' => 1,
  58                  'assignsubmission_file_maxfiles' => 12,
  59                  'assignsubmission_file_maxsizebytes' => 10,
  60              ]);
  61  
  62          $this->setUser($student->id);
  63  
  64          $itemid = file_get_unused_draft_itemid();
  65          $submission = (object)['files_filemanager' => $itemid];
  66          $plugin = $assign->get_submission_plugin_by_type('file');
  67  
  68          if ($data) {
  69              $data += ['contextid' => \context_user::instance($student->id)->id, 'itemid' => $itemid];
  70              $fs = get_file_storage();
  71              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
  72          }
  73  
  74          $result = $plugin->submission_is_empty($submission);
  75          $this->assertTrue($result === $expected);
  76      }
  77  
  78      /**
  79       * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
  80       */
  81      public function test_submission_is_empty_directory_only() {
  82          $this->resetAfterTest();
  83          $course = $this->getDataGenerator()->create_course();
  84          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  85          $assign = $this->create_instance($course, [
  86                  'assignsubmission_file_enabled' => 1,
  87                  'assignsubmission_file_maxfiles' => 12,
  88                  'assignsubmission_file_maxsizebytes' => 10,
  89              ]);
  90          $this->setUser($student->id);
  91          $itemid = file_get_unused_draft_itemid();
  92          $submission = (object)['files_filemanager' => $itemid];
  93          $plugin = $assign->get_submission_plugin_by_type('file');
  94          $fs = get_file_storage();
  95          $fs->create_directory(
  96                  \context_user::instance($student->id)->id,
  97                  'user',
  98                  'draft',
  99                  $itemid,
 100                  '/subdirectory/'
 101          );
 102  
 103          $this->assertTrue($plugin->submission_is_empty($submission));
 104      }
 105  
 106      /**
 107       * Test new_submission_empty
 108       *
 109       * @dataProvider submission_is_empty_testcases
 110       * @param string $data The file submission data
 111       * @param bool $expected The expected return value
 112       */
 113      public function test_new_submission_empty($data, $expected) {
 114          $this->resetAfterTest();
 115  
 116          $course = $this->getDataGenerator()->create_course();
 117          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 118          $assign = $this->create_instance($course, [
 119                  'assignsubmission_file_enabled' => 1,
 120                  'assignsubmission_file_maxfiles' => 12,
 121                  'assignsubmission_file_maxsizebytes' => 10,
 122              ]);
 123  
 124          $this->setUser($student);
 125  
 126          $itemid = file_get_unused_draft_itemid();
 127          $submission = (object) ['files_filemanager' => $itemid];
 128  
 129          if ($data) {
 130              $data += ['contextid' => \context_user::instance($student->id)->id, 'itemid' => $itemid];
 131              $fs = get_file_storage();
 132              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
 133          }
 134  
 135          $result = $assign->new_submission_empty($submission);
 136          $this->assertTrue($result === $expected);
 137      }
 138  
 139      /**
 140       * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
 141       */
 142      public function test_new_submission_empty_directory_only() {
 143          $this->resetAfterTest();
 144          $course = $this->getDataGenerator()->create_course();
 145          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 146          $assign = $this->create_instance($course, [
 147                  'assignsubmission_file_enabled' => 1,
 148                  'assignsubmission_file_maxfiles' => 12,
 149                  'assignsubmission_file_maxsizebytes' => 10,
 150              ]);
 151          $this->setUser($student->id);
 152          $itemid = file_get_unused_draft_itemid();
 153          $submission = (object)['files_filemanager' => $itemid];
 154          $plugin = $assign->get_submission_plugin_by_type('file');
 155          $fs = get_file_storage();
 156          $fs->create_directory(
 157                  \context_user::instance($student->id)->id,
 158                  'user',
 159                  'draft',
 160                  $itemid,
 161                  '/subdirectory/'
 162          );
 163  
 164          $this->assertTrue($assign->new_submission_empty($submission));
 165      }
 166  
 167      /**
 168       * Dataprovider for the test_submission_is_empty testcase
 169       *
 170       * @return array of testcases
 171       */
 172      public function submission_is_empty_testcases() {
 173          return [
 174              'With file' => [
 175                  [
 176                      'component' => 'user',
 177                      'filearea' => 'draft',
 178                      'filepath' => '/',
 179                      'filename' => 'not_a_virus.exe'
 180                  ],
 181                  false
 182              ],
 183              'With file in directory' => [
 184                  [
 185                      'component' => 'user',
 186                      'filearea' => 'draft',
 187                      'filepath' => '/subdir/',
 188                      'filename' => 'not_a_virus.exe'
 189                  ],
 190                  false
 191              ],
 192              'Without file' => [null, true]
 193          ];
 194      }
 195  }