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 core_cohort\customfield;
  18  
  19  use advanced_testcase;
  20  use context_system;
  21  use context_coursecat;
  22  use moodle_url;
  23  use core_customfield\field_controller;
  24  
  25  /**
  26   * Unit tests for cohort custom field handler.
  27   *
  28   * @package    core_cohort
  29   * @covers     \core_cohort\customfield\cohort_handler
  30   * @copyright  2022 Dmitrii Metelkin <dmitriim@catalys-au.net>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class cohort_handler_test extends advanced_testcase {
  34      /**
  35       * Test custom field handler.
  36       * @var \core_customfield\handler
  37       */
  38      protected $handler;
  39  
  40      /**
  41       * Setup.
  42       */
  43      public function setUp(): void {
  44          $this->handler = cohort_handler::create();
  45      }
  46  
  47      /**
  48       * Create Cohort custom field for testing.
  49       *
  50       * @return field_controller
  51       */
  52      protected function create_cohort_custom_field(): field_controller {
  53          $fieldcategory = self::getDataGenerator()->create_custom_field_category([
  54              'component' => 'core_cohort',
  55              'area' => 'cohort',
  56          ]);
  57  
  58          return self::getDataGenerator()->create_custom_field([
  59              'shortname' => 'testfield1',
  60              'type' => 'text',
  61              'categoryid' => $fieldcategory->get('id'),
  62          ]);
  63      }
  64  
  65      /**
  66       * Test configuration context.
  67       */
  68      public function test_get_configuration_context() {
  69          $this->assertInstanceOf(context_system::class, $this->handler->get_configuration_context());
  70      }
  71  
  72      /**
  73       * Test getting config URL.
  74       */
  75      public function test_get_configuration_url() {
  76          $this->assertInstanceOf(moodle_url::class, $this->handler->get_configuration_url());
  77          $this->assertEquals('/cohort/customfield.php', $this->handler->get_configuration_url()->out_as_local_url());
  78      }
  79  
  80      /**
  81       * Test can configure check.
  82       */
  83      public function test_can_configure() {
  84          $this->resetAfterTest();
  85  
  86          $user = self::getDataGenerator()->create_user();
  87          self::setUser($user);
  88  
  89          $this->assertFalse($this->handler->can_configure());
  90  
  91          $roleid = self::getDataGenerator()->create_role();
  92          assign_capability('moodle/cohort:configurecustomfields', CAP_ALLOW, $roleid, context_system::instance()->id, true);
  93          role_assign($roleid, $user->id, context_system::instance()->id);
  94  
  95          $this->assertTrue($this->handler->can_configure());
  96      }
  97  
  98      /**
  99       * Test getting instance context.
 100       */
 101      public function test_get_instance_context() {
 102          $this->resetAfterTest();
 103  
 104          $category = self::getDataGenerator()->create_category();
 105          $catcontext = context_coursecat::instance($category->id);
 106          $systemcontext = context_system::instance();
 107          $cohortsystem = self::getDataGenerator()->create_cohort();
 108          $cohortcategory = self::getDataGenerator()->create_cohort(['contextid' => $catcontext->id]);
 109  
 110          $this->assertInstanceOf(context_system::class, $this->handler->get_instance_context($cohortsystem->id));
 111          $this->assertSame($systemcontext, $this->handler->get_instance_context($cohortsystem->id));
 112  
 113          $this->assertInstanceOf(context_coursecat::class, $this->handler->get_instance_context($cohortcategory->id));
 114          $this->assertSame($catcontext, $this->handler->get_instance_context($cohortcategory->id));
 115      }
 116  
 117      /**
 118       * Test can edit functionality.
 119       */
 120      public function test_can_edit() {
 121          $this->resetAfterTest();
 122  
 123          $roleid = self::getDataGenerator()->create_role();
 124          assign_capability('moodle/cohort:manage', CAP_ALLOW, $roleid, context_system::instance()->id, true);
 125  
 126          $field = $this->create_cohort_custom_field();
 127  
 128          $user = self::getDataGenerator()->create_user();
 129          self::setUser($user);
 130  
 131          $this->assertFalse($this->handler->can_edit($field, 0));
 132  
 133          role_assign($roleid, $user->id, context_system::instance()->id);
 134          $this->assertTrue($this->handler->can_edit($field, 0));
 135      }
 136  
 137      /**
 138       * Test can view functionality.
 139       */
 140      public function test_can_view() {
 141          $this->resetAfterTest();
 142  
 143          $manageroleid = self::getDataGenerator()->create_role();
 144          assign_capability('moodle/cohort:manage', CAP_ALLOW, $manageroleid, context_system::instance()->id, true);
 145  
 146          $viewroleid = self::getDataGenerator()->create_role();
 147          assign_capability('moodle/cohort:view', CAP_ALLOW, $viewroleid, context_system::instance()->id, true);
 148  
 149          $viewandmanageroleid = self::getDataGenerator()->create_role();
 150          assign_capability('moodle/cohort:manage', CAP_ALLOW, $viewandmanageroleid, context_system::instance()->id, true);
 151          assign_capability('moodle/cohort:view', CAP_ALLOW, $viewandmanageroleid, context_system::instance()->id, true);
 152  
 153          $field = $this->create_cohort_custom_field();
 154          $cohort = self::getDataGenerator()->create_cohort();
 155  
 156          $user1 = self::getDataGenerator()->create_user();
 157          $user2 = self::getDataGenerator()->create_user();
 158          $user3 = self::getDataGenerator()->create_user();
 159  
 160          self::setUser($user1);
 161          $this->assertFalse($this->handler->can_view($field, $cohort->id));
 162  
 163          self::setUser($user2);
 164          $this->assertFalse($this->handler->can_view($field, $cohort->id));
 165  
 166          self::setUser($user3);
 167          $this->assertFalse($this->handler->can_view($field, $cohort->id));
 168  
 169          role_assign($manageroleid, $user1->id, context_system::instance()->id);
 170          role_assign($viewroleid, $user2->id, context_system::instance()->id);
 171          role_assign($viewandmanageroleid, $user3->id, context_system::instance()->id);
 172  
 173          self::setUser($user1);
 174          $this->assertTrue($this->handler->can_view($field, $cohort->id));
 175  
 176          self::setUser($user2);
 177          $this->assertTrue($this->handler->can_view($field, $cohort->id));
 178  
 179          self::setUser($user3);
 180          $this->assertTrue($this->handler->can_view($field, $cohort->id));
 181      }
 182  }