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   * Asyncronhous helper tests.
  19   *
  20   * @package    core_backup
  21   * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
  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 . '/backup/util/includes/backup_includes.php');
  29  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  30  
  31  /**
  32   * Asyncronhous helper tests.
  33   *
  34   * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_backup_async_helper_testcase extends \core_privacy\tests\provider_testcase {
  38  
  39      /**
  40       * Tests sending message for asynchronous backup.
  41       */
  42      public function test_send_message() {
  43          global $DB, $USER;
  44          $this->preventResetByRollback();
  45          $this->resetAfterTest(true);
  46          $this->setAdminUser();
  47  
  48          set_config('backup_async_message_users', '1', 'backup');
  49          set_config('backup_async_message_subject', 'Moodle {operation} completed sucessfully', 'backup');
  50          set_config('backup_async_message',
  51                  'Dear {user_firstname} {user_lastname}, <br/> Your {operation} (ID: {backupid}) has completed successfully!',
  52                  'backup');
  53          set_config('allowedemaildomains', 'example.com');
  54  
  55          $generator = $this->getDataGenerator();
  56          $course = $generator->create_course();  // Create a course with some availability data set.
  57          $user2 = $generator->create_user(array('firstname' => 'test', 'lastname' => 'human', 'maildisplay' => 1));
  58          $generator->enrol_user($user2->id, $course->id, 'editingteacher');
  59  
  60          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
  61          set_user_preference('message_provider_moodle_asyncbackupnotification', 'email', $user2);
  62  
  63          // Make the backup controller for an async backup.
  64          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
  65                  backup::INTERACTIVE_YES, backup::MODE_ASYNC, $user2->id);
  66          $bc->finish_ui();
  67          $backupid = $bc->get_backupid();
  68          $bc->destroy();
  69  
  70          $sink = $this->redirectEmails();
  71  
  72          // Send message.
  73          $asynchelper = new async_helper('backup', $backupid);
  74          $messageid = $asynchelper->send_message();
  75  
  76          $emails = $sink->get_messages();
  77          $this->assertCount(1, $emails);
  78          $email = reset($emails);
  79  
  80          $this->assertSame($USER->email, $email->from);
  81          $this->assertSame($user2->email, $email->to);
  82          $this->assertSame('Moodle backup completed sucessfully', $email->subject);
  83          $this->assertNotEmpty($email->header);
  84          $this->assertNotEmpty($email->body);
  85          $this->assertRegExp("/$backupid/", $email->body);
  86          $this->assertThat($email->body, $this->logicalNot($this->stringContains('{')));
  87          $this->assertGreaterThan(0, $messageid);
  88          $sink->clear();
  89      }
  90  
  91      /**
  92       * Tests getting the asynchronous backup table items.
  93       */
  94      public function test_get_async_backups() {
  95          global $DB, $CFG, $USER, $PAGE;
  96  
  97          $this->resetAfterTest(true);
  98          $this->setAdminUser();
  99          $CFG->enableavailability = true;
 100          $CFG->enablecompletion = true;
 101  
 102          // Create a course with some availability data set.
 103          $generator = $this->getDataGenerator();
 104          $course = $generator->create_course(
 105              array('format' => 'topics', 'numsections' => 3,
 106                  'enablecompletion' => COMPLETION_ENABLED),
 107              array('createsections' => true));
 108          $forum = $generator->create_module('forum', array(
 109              'course' => $course->id));
 110          $forum2 = $generator->create_module('forum', array(
 111              'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
 112  
 113          // We need a grade, easiest is to add an assignment.
 114          $assignrow = $generator->create_module('assign', array(
 115              'course' => $course->id));
 116          $assign = new assign(context_module::instance($assignrow->cmid), false, false);
 117          $item = $assign->get_grade_item();
 118  
 119          // Make a test grouping as well.
 120          $grouping = $generator->create_grouping(array('courseid' => $course->id,
 121              'name' => 'Grouping!'));
 122  
 123          $availability = '{"op":"|","show":false,"c":[' .
 124              '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
 125              '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
 126              '{"type":"grouping","id":' . $grouping->id . '}' .
 127              ']}';
 128          $DB->set_field('course_modules', 'availability', $availability, array(
 129              'id' => $forum->cmid));
 130          $DB->set_field('course_sections', 'availability', $availability, array(
 131              'course' => $course->id, 'section' => 1));
 132  
 133          // Make the backup controller for an async backup.
 134          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
 135              backup::INTERACTIVE_YES, backup::MODE_ASYNC, $USER->id);
 136          $bc->finish_ui();
 137          $bc->destroy();
 138          unset($bc);
 139  
 140          $coursecontext = context_course::instance($course->id);
 141          $renderer = $PAGE->get_renderer('core', 'backup');
 142  
 143          $result = \async_helper::get_async_backups($renderer, $coursecontext->instanceid);
 144  
 145          $this->assertEquals(1, count($result));
 146          $this->assertEquals('backup.mbz', $result[0][0]);
 147      }
 148  
 149      /**
 150       * Tests getting the backup record.
 151       */
 152      public function test_get_backup_record() {
 153          global $USER;
 154  
 155          $this->resetAfterTest();
 156          $this->setAdminUser();
 157          $generator = $this->getDataGenerator();
 158          $course = $generator->create_course();
 159  
 160          // Create the initial backupcontoller.
 161          $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE,
 162              \backup::INTERACTIVE_NO, \backup::MODE_COPY, $USER->id, \backup::RELEASESESSION_YES);
 163          $backupid = $bc->get_backupid();
 164          $bc->destroy();
 165          $copyrec = \async_helper::get_backup_record($backupid);
 166  
 167          $this->assertEquals($backupid, $copyrec->backupid);
 168  
 169      }
 170  
 171      /**
 172       * Tests is async pending conditions.
 173       */
 174      public function test_is_async_pending() {
 175          global $USER;
 176  
 177          $this->resetAfterTest();
 178          $this->setAdminUser();
 179          $generator = $this->getDataGenerator();
 180          $course = $generator->create_course();
 181  
 182          set_config('enableasyncbackup', '0');
 183          $ispending = async_helper::is_async_pending($course->id, 'course', 'backup');
 184  
 185          // Should be false as there are no backups and async backup is false.
 186          $this->assertFalse($ispending);
 187  
 188          // Create the initial backupcontoller.
 189          $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE,
 190              \backup::INTERACTIVE_NO, \backup::MODE_ASYNC, $USER->id, \backup::RELEASESESSION_YES);
 191          $bc->destroy();
 192          $ispending = async_helper::is_async_pending($course->id, 'course', 'backup');
 193  
 194          // Should be false as there as async backup is false.
 195          $this->assertFalse($ispending);
 196  
 197          set_config('enableasyncbackup', '1');
 198          // Should be true as there as async backup is true and there is a pending backup.
 199          $this->assertFalse($ispending);
 200      }
 201  
 202      /**
 203       * Tests is async pending conditions for course copies.
 204       */
 205      public function test_is_async_pending_copy() {
 206          global $USER;
 207  
 208          $this->resetAfterTest();
 209          $this->setAdminUser();
 210          $generator = $this->getDataGenerator();
 211          $course = $generator->create_course();
 212  
 213          set_config('enableasyncbackup', '0');
 214          $ispending = async_helper::is_async_pending($course->id, 'course', 'backup');
 215  
 216          // Should be false as there are no copies and async backup is false.
 217          $this->assertFalse($ispending);
 218  
 219          // Create the initial backupcontoller.
 220          $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE,
 221              \backup::INTERACTIVE_NO, \backup::MODE_COPY, $USER->id, \backup::RELEASESESSION_YES);
 222          $bc->destroy();
 223          $ispending = async_helper::is_async_pending($course->id, 'course', 'backup');
 224  
 225          // Should be True as this a copy operation.
 226          $this->assertTrue($ispending);
 227  
 228          set_config('enableasyncbackup', '1');
 229          $ispending = async_helper::is_async_pending($course->id, 'course', 'backup');
 230  
 231          // Should be true as there as async backup is true and there is a pending copy.
 232          $this->assertTrue($ispending);
 233      }
 234  
 235  }