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