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