Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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   * Unit tests for assignfeedback_file
  19   *
  20   * @package    assignfeedback_file
  21   * @copyright  2016 Adrian Greeve <adrian@moodle.com>
  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 assignfeedback_file
  32   *
  33   * @copyright  2016 Adrian Greeve <adrian@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class assignfeedback_file_testcase extends advanced_testcase {
  37  
  38      // Use the generator helper.
  39      use mod_assign_test_generator;
  40  
  41      /**
  42       * Test the is_feedback_modified() method for the file feedback.
  43       */
  44      public function test_is_feedback_modified() {
  45          $this->resetAfterTest();
  46          $course = $this->getDataGenerator()->create_course();
  47          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  48          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  49  
  50          $assign = $this->create_instance($course, [
  51                  'assignsubmission_onlinetext_enabled' => 1,
  52                  'assignfeedback_comments_enabled' => 1,
  53              ]);
  54  
  55          // Create an online text submission.
  56          $this->add_submission($student, $assign);
  57  
  58          $this->setUser($teacher);
  59  
  60          $fs = get_file_storage();
  61          $context = context_user::instance($teacher->id);
  62          $draftitemid = file_get_unused_draft_itemid();
  63          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
  64  
  65          $dummy = array(
  66              'contextid' => $context->id,
  67              'component' => 'user',
  68              'filearea' => 'draft',
  69              'itemid' => $draftitemid,
  70              'filepath' => '/',
  71              'filename' => 'feedback1.txt'
  72          );
  73  
  74          $file = $fs->create_file_from_string($dummy, 'This is the first feedback file');
  75  
  76          // Create formdata.
  77          $data = new stdClass();
  78          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
  79  
  80          $grade = $assign->get_user_grade($student->id, true);
  81  
  82          // This is the first time that we are submitting feedback, so it is modified.
  83          $plugin = $assign->get_feedback_plugin_by_type('file');
  84          $this->assertTrue($plugin->is_feedback_modified($grade, $data));
  85          // Save the feedback.
  86          $plugin->save($grade, $data);
  87          // Try again with the same data.
  88          $draftitemid = file_get_unused_draft_itemid();
  89          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
  90  
  91          $dummy['itemid'] = $draftitemid;
  92  
  93          $file = $fs->create_file_from_string($dummy, 'This is the first feedback file');
  94  
  95          // Create formdata.
  96          $data = new stdClass();
  97          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
  98  
  99          $this->assertFalse($plugin->is_feedback_modified($grade, $data));
 100  
 101          // Same name for the file but different content.
 102          $draftitemid = file_get_unused_draft_itemid();
 103          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 104  
 105          $dummy['itemid'] = $draftitemid;
 106  
 107          $file = $fs->create_file_from_string($dummy, 'This is different feedback');
 108  
 109          // Create formdata.
 110          $data = new stdClass();
 111          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 112  
 113          $this->assertTrue($plugin->is_feedback_modified($grade, $data));
 114          $plugin->save($grade, $data);
 115  
 116          // Add another file.
 117          $draftitemid = file_get_unused_draft_itemid();
 118          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 119  
 120          $dummy['itemid'] = $draftitemid;
 121  
 122          $file = $fs->create_file_from_string($dummy, 'This is different feedback');
 123          $dummy['filename'] = 'feedback2.txt';
 124          $file = $fs->create_file_from_string($dummy, 'A second feedback file');
 125  
 126          // Create formdata.
 127          $data = new stdClass();
 128          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 129  
 130          $this->assertTrue($plugin->is_feedback_modified($grade, $data));
 131          $plugin->save($grade, $data);
 132  
 133          // Deleting a file.
 134          $draftitemid = file_get_unused_draft_itemid();
 135          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 136  
 137          $dummy['itemid'] = $draftitemid;
 138  
 139          $file = $fs->create_file_from_string($dummy, 'This is different feedback');
 140  
 141          // Create formdata.
 142          $data = new stdClass();
 143          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 144  
 145          $this->assertTrue($plugin->is_feedback_modified($grade, $data));
 146          $plugin->save($grade, $data);
 147  
 148          // The file was moved to a folder.
 149          $draftitemid = file_get_unused_draft_itemid();
 150          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 151  
 152          $dummy['itemid'] = $draftitemid;
 153          $dummy['filepath'] = '/testdir/';
 154  
 155          $file = $fs->create_file_from_string($dummy, 'This is different feedback');
 156  
 157          // Create formdata.
 158          $data = new stdClass();
 159          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 160  
 161          $this->assertTrue($plugin->is_feedback_modified($grade, $data));
 162          $plugin->save($grade, $data);
 163  
 164          // No modification to the file in the folder.
 165          $draftitemid = file_get_unused_draft_itemid();
 166          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 167  
 168          $dummy['itemid'] = $draftitemid;
 169          $dummy['filepath'] = '/testdir/';
 170  
 171          $file = $fs->create_file_from_string($dummy, 'This is different feedback');
 172  
 173          // Create formdata.
 174          $data = new stdClass();
 175          $data->{'files_' . $student->id . '_filemanager'} = $draftitemid;
 176  
 177          $this->assertFalse($plugin->is_feedback_modified($grade, $data));
 178      }
 179  }