Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 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  namespace core\moodlenet;
  18  
  19  use core\context\user;
  20  
  21  /**
  22   * Unit tests for {@see activity_packager}.
  23   *
  24   * @coversDefaultClass \core\moodlenet\activity_packager
  25   * @package core
  26   * @copyright 2023 Michael Hawkins <michaelh@moodle.com>
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class activity_packager_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test fetching and overriding a backup task setting.
  33       *
  34       * @covers ::override_task_setting
  35       * @covers ::get_all_task_settings
  36       */
  37      public function test_override_task_setting(): void {
  38          global $USER;
  39          $this->resetAfterTest();
  40          $this->setAdminUser();
  41  
  42          $generator = $this->getDataGenerator();
  43          $course = $generator->create_course();
  44          $assigndata = [
  45              'course' => $course->id,
  46              'name' => 'Extremely interesting assignment',
  47              'intro' => 'A great assignment to share',
  48          ];
  49          $assign = $generator->create_module('assign', $assigndata);
  50          $cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid);
  51          $packager = new activity_packager($cminfo, $USER->id);
  52  
  53          // Fetch all backup task settings.
  54          $rc = new \ReflectionClass(activity_packager::class);
  55          $rcmgetall = $rc->getMethod('get_all_task_settings');
  56          $rcmgetall->setAccessible(true);
  57          $tasksettings = $rcmgetall->invoke($packager);
  58  
  59          // Fetch the default settings and grab an example value (setting_root_users).
  60          $rootsettings = $tasksettings[\backup_root_task::class];
  61          $testsettingname = 'setting_root_users';
  62  
  63          $oldvalue = 99;
  64          foreach ($rootsettings as $setting) {
  65              $name = $setting->get_ui_name();
  66              if ($name == $testsettingname) {
  67                  $oldvalue = $setting->get_value();
  68                  break;
  69              }
  70          }
  71  
  72          // Check we found the setting value (either 0 or 1 are valid).
  73          $this->assertNotEquals(99, $oldvalue);
  74          $this->assertLessThanOrEqual(1, $oldvalue);
  75  
  76          // Override the setting_root_users value, then re-fetch the settings to check the change is reflected.
  77          $overridevalue = ($oldvalue == 1) ? 0 : 1;
  78          $rcmoverridesetting = $rc->getMethod('override_task_setting');
  79          $rcmoverridesetting->setAccessible(true);
  80          $rcmoverridesetting->invoke($packager, $tasksettings, $testsettingname, $overridevalue);
  81          $tasksettings = $rcmgetall->invoke($packager);
  82          $rootsettings = $tasksettings[\backup_root_task::class];
  83  
  84          $newvalue = 99;
  85          foreach ($rootsettings as $setting) {
  86              $name = $setting->get_ui_name();
  87              if ($name == $testsettingname) {
  88                  $newvalue = $setting->get_value();
  89                  break;
  90              }
  91          }
  92  
  93          $this->assertEquals($overridevalue, $newvalue);
  94      }
  95  
  96      /**
  97       * Test overriding a backup task setting.
  98       *
  99       * @covers ::get_package
 100       * @covers ::package
 101       */
 102      public function test_get_package(): void {
 103          global $USER;
 104          $this->resetAfterTest();
 105          $this->setAdminUser();
 106  
 107          $currenttime = time();
 108          $generator = $this->getDataGenerator();
 109          $course = $generator->create_course();
 110  
 111          $assigndata = [
 112              'course' => $course->id,
 113              'name' => 'Extremely interesting assignment',
 114              'intro' => 'A great assignment to share',
 115          ];
 116          $assign = $generator->create_module('assign', $assigndata);
 117          $cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid);
 118          $packager = new activity_packager($cminfo, $USER->id);
 119          $package = $packager->get_package();
 120  
 121          // Confirm the expected stored_file object is returned.
 122          $this->assertInstanceOf(\stored_file::class, $package);
 123  
 124          // Check some known values in the returned stored_file object to confirm they match the file we have packaged.
 125          $this->assertNotEmpty($package->get_contenthash());
 126          $this->assertEquals(user::instance($USER->id)->id, $package->get_contextid());
 127          $this->assertEquals('user', $package->get_component());
 128          $this->assertEquals('draft', $package->get_filearea());
 129          $this->assertEquals('assign_backup.mbz', $package->get_filename());
 130          $this->assertGreaterThan(0, $package->get_filesize());
 131          $timecreated = $package->get_timecreated();
 132          $this->assertGreaterThanOrEqual($currenttime, $timecreated);
 133          $this->assertEquals($timecreated, $package->get_timemodified());
 134      }
 135  }