Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 mod_feedback\backup;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
  23  
  24  /**
  25   * Restore date tests.
  26   *
  27   * @package    mod_feedback
  28   * @copyright  2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class restore_date_test extends \restore_date_testcase {
  32  
  33      public function test_restore_dates() {
  34          global $DB, $USER;
  35  
  36          $time = 10000;
  37          list($course, $feedback) = $this->create_course_and_module('feedback', ['timeopen' => $time, 'timeclose' => $time]);
  38  
  39          // Create response.
  40          $response = new \stdClass();
  41          $response->feedback = $feedback->id;
  42          $response->userid = $USER->id;
  43          $response->anonymous_response = FEEDBACK_ANONYMOUS_NO;
  44          $response->timemodified = $time;
  45          $completedid = $DB->insert_record('feedback_completed', $response);
  46          $response = $DB->get_record('feedback_completed', array('id' => $completedid), '*', MUST_EXIST);
  47  
  48          // Do backup and restore.
  49          $newcourseid = $this->backup_and_restore($course);
  50          $newfeedback = $DB->get_record('feedback', ['course' => $newcourseid]);
  51          $newresponse = $DB->get_record('feedback_completed', ['feedback' => $newfeedback->id]);
  52  
  53          $this->assertFieldsNotRolledForward($feedback, $newfeedback, ['timemodified']);
  54          $props = ['timeopen', 'timeclose'];
  55          $this->assertFieldsRolledForward($feedback, $newfeedback, $props);
  56          $this->assertEquals($response->timemodified, $newresponse->timemodified);
  57      }
  58  
  59      /**
  60       * Test that dependency for items is restored correctly.
  61       */
  62      public function test_restore_item_dependency() {
  63          global $DB;
  64          // Create a course and a feedback activity.
  65          $course = $this->getDataGenerator()->create_course();
  66          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
  67          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
  68  
  69          // Create a couple of items which depend on each other.
  70          $item1 = $feedbackgenerator->create_item_numeric($feedback);
  71          $item2 = $feedbackgenerator->create_item_numeric($feedback, array('dependitem' => $item1->id));
  72          $DB->set_field('feedback_item', 'dependitem', $item2->id, ['id' => $item1->id]);
  73  
  74          // Create one more item with fake/broken dependitem.
  75          $item3 = $feedbackgenerator->create_item_numeric($feedback, array('dependitem' => 123456));
  76  
  77          // Backup and restore the course.
  78          $restoredcourseid = $this->backup_and_restore($course);
  79          $restoredfeedback = $DB->get_record('feedback', ['course' => $restoredcourseid]);
  80  
  81          // Restored item1 and item2 are expected to be dependent the same way as the original ones.
  82          $restoreditem1 = $DB->get_record('feedback_item', ['feedback' => $restoredfeedback->id, 'name' => $item1->name]);
  83          $restoreditem2 = $DB->get_record('feedback_item', ['feedback' => $restoredfeedback->id, 'name' => $item2->name]);
  84          $this->assertEquals($restoreditem2->id, $restoreditem1->dependitem);
  85          $this->assertEquals($restoreditem1->id, $restoreditem2->dependitem);
  86  
  87          // Restored item3 is expected to have an empty dependitem.
  88          $restoreditem3 = $DB->get_record('feedback_item', ['feedback' => $restoredfeedback->id, 'name' => $item3->name]);
  89          $this->assertEquals(0, $restoreditem3->dependitem);
  90      }
  91  }