Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  29  
  30  /**
  31   * Unit tests for mod/assign/submission/file/locallib.php
  32   *
  33   * @copyright  2016 Cameron Ball
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class assignsubmission_file_locallib_testcase extends advanced_testcase {
  37  
  38      // Use the generator helper.
  39      use mod_assign_test_generator;
  40  
  41      /**
  42       * Test submission_is_empty
  43       *
  44       * @dataProvider submission_is_empty_testcases
  45       * @param string $data The file submission data
  46       * @param bool $expected The expected return value
  47       */
  48      public function test_submission_is_empty($data, $expected) {
  49          $this->resetAfterTest();
  50  
  51          $course = $this->getDataGenerator()->create_course();
  52          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  53          $assign = $this->create_instance($course, [
  54                  'assignsubmission_file_enabled' => 1,
  55                  'assignsubmission_file_maxfiles' => 12,
  56                  'assignsubmission_file_maxsizebytes' => 10,
  57              ]);
  58  
  59          $this->setUser($student->id);
  60  
  61          $itemid = file_get_unused_draft_itemid();
  62          $submission = (object)['files_filemanager' => $itemid];
  63          $plugin = $assign->get_submission_plugin_by_type('file');
  64  
  65          if ($data) {
  66              $data += ['contextid' => context_user::instance($student->id)->id, 'itemid' => $itemid];
  67              $fs = get_file_storage();
  68              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
  69          }
  70  
  71          $result = $plugin->submission_is_empty($submission);
  72          $this->assertTrue($result === $expected);
  73      }
  74  
  75      /**
  76       * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
  77       */
  78      public function test_submission_is_empty_directory_only() {
  79          $this->resetAfterTest();
  80          $course = $this->getDataGenerator()->create_course();
  81          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  82          $assign = $this->create_instance($course, [
  83                  'assignsubmission_file_enabled' => 1,
  84                  'assignsubmission_file_maxfiles' => 12,
  85                  'assignsubmission_file_maxsizebytes' => 10,
  86              ]);
  87          $this->setUser($student->id);
  88          $itemid = file_get_unused_draft_itemid();
  89          $submission = (object)['files_filemanager' => $itemid];
  90          $plugin = $assign->get_submission_plugin_by_type('file');
  91          $fs = get_file_storage();
  92          $fs->create_directory(
  93                  context_user::instance($student->id)->id,
  94                  'user',
  95                  'draft',
  96                  $itemid,
  97                  '/subdirectory/'
  98          );
  99  
 100          $this->assertTrue($plugin->submission_is_empty($submission));
 101      }
 102  
 103      /**
 104       * Test new_submission_empty
 105       *
 106       * @dataProvider submission_is_empty_testcases
 107       * @param string $data The file submission data
 108       * @param bool $expected The expected return value
 109       */
 110      public function test_new_submission_empty($data, $expected) {
 111          $this->resetAfterTest();
 112  
 113          $course = $this->getDataGenerator()->create_course();
 114          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 115          $assign = $this->create_instance($course, [
 116                  'assignsubmission_file_enabled' => 1,
 117                  'assignsubmission_file_maxfiles' => 12,
 118                  'assignsubmission_file_maxsizebytes' => 10,
 119              ]);
 120  
 121          $this->setUser($student);
 122  
 123          $itemid = file_get_unused_draft_itemid();
 124          $submission = (object) ['files_filemanager' => $itemid];
 125  
 126          if ($data) {
 127              $data += ['contextid' => context_user::instance($student->id)->id, 'itemid' => $itemid];
 128              $fs = get_file_storage();
 129              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
 130          }
 131  
 132          $result = $assign->new_submission_empty($submission);
 133          $this->assertTrue($result === $expected);
 134      }
 135  
 136      /**
 137       * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
 138       */
 139      public function test_new_submission_empty_directory_only() {
 140          $this->resetAfterTest();
 141          $course = $this->getDataGenerator()->create_course();
 142          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 143          $assign = $this->create_instance($course, [
 144                  'assignsubmission_file_enabled' => 1,
 145                  'assignsubmission_file_maxfiles' => 12,
 146                  'assignsubmission_file_maxsizebytes' => 10,
 147              ]);
 148          $this->setUser($student->id);
 149          $itemid = file_get_unused_draft_itemid();
 150          $submission = (object)['files_filemanager' => $itemid];
 151          $plugin = $assign->get_submission_plugin_by_type('file');
 152          $fs = get_file_storage();
 153          $fs->create_directory(
 154                  context_user::instance($student->id)->id,
 155                  'user',
 156                  'draft',
 157                  $itemid,
 158                  '/subdirectory/'
 159          );
 160  
 161          $this->assertTrue($assign->new_submission_empty($submission));
 162      }
 163  
 164      /**
 165       * Dataprovider for the test_submission_is_empty testcase
 166       *
 167       * @return array of testcases
 168       */
 169      public function submission_is_empty_testcases() {
 170          return [
 171              'With file' => [
 172                  [
 173                      'component' => 'user',
 174                      'filearea' => 'draft',
 175                      'filepath' => '/',
 176                      'filename' => 'not_a_virus.exe'
 177                  ],
 178                  false
 179              ],
 180              'With file in directory' => [
 181                  [
 182                      'component' => 'user',
 183                      'filearea' => 'draft',
 184                      'filepath' => '/subdir/',
 185                      'filename' => 'not_a_virus.exe'
 186                  ],
 187                  false
 188              ],
 189              'Without file' => [null, true]
 190          ];
 191      }
 192  }