Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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_forum\backup;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
  23  require_once($CFG->dirroot . '/rating/lib.php');
  24  
  25  /**
  26   * Restore date tests.
  27   *
  28   * @package    mod_forum
  29   * @copyright  2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class restore_date_test extends \restore_date_testcase {
  33  
  34      /**
  35       * Test restore dates.
  36       */
  37      public function test_restore_dates() {
  38          global $DB, $USER;
  39  
  40          $gg = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  41          $record = ['assesstimefinish' => 100, 'assesstimestart' => 100, 'ratingtime' => 1, 'assessed' => 2, 'scale' => 1];
  42          list($course, $forum) = $this->create_course_and_module('forum', $record);
  43  
  44          // Forum Discussions/posts/ratings.
  45          $timestamp = 996699;
  46          $diff = $this->get_diff();
  47          $record = new \stdClass();
  48          $record->course = $course->id;
  49          $record->userid = $USER->id;
  50          $record->forum = $forum->id;
  51          $record->timestart = $record->timeend = $record->timemodified = $timestamp;
  52          $discussion = $gg->create_discussion($record);
  53  
  54          $record = new \stdClass();
  55          $record->discussion = $discussion->id;
  56          $record->parent = $discussion->firstpost;
  57          $record->userid = $USER->id;
  58          $record->created = $record->modified = $timestamp;
  59          $post = $gg->create_post($record);
  60  
  61          // Time modified is changed internally.
  62          $DB->set_field('forum_discussions', 'timemodified', $timestamp);
  63  
  64          // Ratings.
  65          $ratingoptions = new \stdClass;
  66          $ratingoptions->context = \context_module::instance($forum->cmid);
  67          $ratingoptions->ratingarea = 'post';
  68          $ratingoptions->component = 'mod_forum';
  69          $ratingoptions->itemid  = $post->id;
  70          $ratingoptions->scaleid = 2;
  71          $ratingoptions->userid  = $USER->id;
  72          $rating = new \rating($ratingoptions);
  73          $rating->update_rating(2);
  74          $rating = $DB->get_record('rating', ['itemid' => $post->id]);
  75  
  76          // Do backup and restore.
  77          $newcourseid = $this->backup_and_restore($course);
  78          $newforum = $DB->get_record('forum', ['course' => $newcourseid]);
  79  
  80          $this->assertFieldsNotRolledForward($forum, $newforum, ['timemodified']);
  81          $props = ['assesstimefinish', 'assesstimestart'];
  82          $this->assertFieldsRolledForward($forum, $newforum, $props);
  83  
  84          $newdiscussion = $DB->get_record('forum_discussions', ['forum' => $newforum->id]);
  85          $newposts = $DB->get_records('forum_posts', ['discussion' => $newdiscussion->id]);
  86          $newcm = $DB->get_record('course_modules', ['course' => $newcourseid, 'instance' => $newforum->id]);
  87  
  88          // Forum discussion time checks.
  89          $this->assertEquals($timestamp + $diff, $newdiscussion->timestart);
  90          $this->assertEquals($timestamp + $diff, $newdiscussion->timeend);
  91          $this->assertEquals($timestamp, $newdiscussion->timemodified);
  92  
  93          // Posts test.
  94          foreach ($newposts as $post) {
  95              $this->assertEquals($timestamp, $post->created);
  96              $this->assertEquals($timestamp, $post->modified);
  97          }
  98  
  99          // Rating test.
 100          $newrating = $DB->get_record('rating', ['contextid' => \context_module::instance($newcm->id)->id]);
 101          $this->assertEquals($rating->timecreated, $newrating->timecreated);
 102          $this->assertEquals($rating->timemodified, $newrating->timemodified);
 103      }
 104  }