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.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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 tool_moodlenet;
  18  
  19  /**
  20   * Unit tests for the import_backup_helper
  21   *
  22   * @package    tool_moodlenet
  23   * @category   test
  24   * @copyright  2020 Adrian Greeve <adrian@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class import_backup_helper_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test that the first available context with the capability to upload backup files is returned.
  31       */
  32      public function test_get_context_for_user() {
  33          global $DB;
  34  
  35          $this->resetAfterTest();
  36  
  37          $user1 = $this->getDataGenerator()->create_user();
  38          $user2 = $this->getDataGenerator()->create_user();
  39          $user3 = $this->getDataGenerator()->create_user();
  40          $user4 = $this->getDataGenerator()->create_user();
  41          $user5 = $this->getDataGenerator()->create_user();
  42  
  43          $course = $this->getDataGenerator()->create_course();
  44          $coursecontext = \context_course::instance($course->id);
  45  
  46          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  47          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
  48          $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'student');
  49  
  50          $category = $this->getDataGenerator()->create_category();
  51          $rolerecord = $DB->get_record('role', ['shortname' => 'manager']);
  52          $categorycontext = \context_coursecat::instance($category->id);
  53          $this->getDataGenerator()->role_assign($rolerecord->id, $user3->id, $categorycontext->id);
  54          $this->getDataGenerator()->role_assign($rolerecord->id, $user5->id, $categorycontext->id);
  55  
  56          $roleid = $this->getDataGenerator()->create_role();
  57          $sitecontext = \context_system::instance();
  58          assign_capability('moodle/restore:uploadfile', CAP_ALLOW, $roleid, $sitecontext->id, true);
  59          accesslib_clear_all_caches_for_unit_testing();
  60          $this->getDataGenerator()->role_assign($roleid, $user4->id, $sitecontext->id);
  61  
  62          $result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user1->id);
  63          $this->assertNull($result);
  64          $result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user2->id);
  65          $this->assertEquals($result, $coursecontext);
  66          $this->assertEquals(CONTEXT_COURSE, $result->contextlevel);
  67          $result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user3->id);
  68          $this->assertEquals($result, $categorycontext);
  69          $this->assertEquals(CONTEXT_COURSECAT, $result->contextlevel);
  70          $result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user4->id);
  71          $this->assertEquals($result, $sitecontext);
  72          $this->assertEquals(CONTEXT_SYSTEM, $result->contextlevel);
  73          $result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user5->id);
  74          $this->assertEquals($result, $categorycontext);
  75          $this->assertEquals(CONTEXT_COURSECAT, $result->contextlevel);
  76      }
  77  
  78  }