Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  /**
  18   * Contains unit tests for core_completion/activity_custom_completion.
  19   *
  20   * @package   mod_data
  21   * @copyright Simey Lameze <simey@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace mod_data;
  28  
  29  use advanced_testcase;
  30  use cm_info;
  31  use coding_exception;
  32  use mod_data\completion\custom_completion;
  33  use moodle_exception;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  global $CFG;
  38  require_once($CFG->libdir . '/completionlib.php');
  39  
  40  /**
  41   * Class for unit testing mod_data/activity_custom_completion.
  42   *
  43   * @package   mod_data
  44   * @copyright Simey Lameze <simey@moodle.com>
  45   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class custom_completion_test extends advanced_testcase {
  48  
  49      /**
  50       * Data provider for get_state().
  51       *
  52       * @return array[]
  53       */
  54      public function get_state_provider(): array {
  55          return [
  56              'Undefined rule' => [
  57                  'somenonexistentrule', COMPLETION_DISABLED, 0, null, coding_exception::class
  58              ],
  59              'Rule not available' => [
  60                  'completionentries', COMPLETION_DISABLED, 0, null, moodle_exception::class
  61              ],
  62              'Rule available, user has not created entries' => [
  63                  'completionentries', COMPLETION_ENABLED, 0, COMPLETION_INCOMPLETE, null
  64              ],
  65              'Rule available, user has created entries' => [
  66                  'completionentries', COMPLETION_ENABLED, 2, COMPLETION_COMPLETE, null
  67              ],
  68          ];
  69      }
  70  
  71      /**
  72       * Test for get_state().
  73       *
  74       * @dataProvider get_state_provider
  75       * @param string $rule The custom completion rule.
  76       * @param int $available Whether this rule is available.
  77       * @param int $entries The number of entries.
  78       * @param int|null $status Expected status.
  79       * @param string|null $exception Expected exception.
  80       */
  81      public function test_get_state(string $rule, int $available, int $entries, ?int $status, ?string $exception) {
  82          global $DB;
  83  
  84          if (!is_null($exception)) {
  85              $this->expectException($exception);
  86          }
  87  
  88          // Custom completion rule data for cm_info::customdata.
  89          $customdataval = [
  90              'customcompletionrules' => [
  91                  $rule => $available
  92              ]
  93          ];
  94  
  95          // Build a mock cm_info instance.
  96          $mockcminfo = $this->getMockBuilder(cm_info::class)
  97              ->disableOriginalConstructor()
  98              ->onlyMethods(['__get'])
  99              ->getMock();
 100  
 101          // Mock the return of the magic getter method when fetching the cm_info object's customdata and instance values.
 102          $mockcminfo->expects($this->any())
 103              ->method('__get')
 104              ->will($this->returnValueMap([
 105                  ['customdata', $customdataval],
 106                  ['instance', 1],
 107              ]));
 108  
 109          // Mock the DB calls.
 110          $DB = $this->createMock(get_class($DB));
 111          $DB->expects($this->atMost(1))
 112              ->method('count_records')
 113              ->willReturn($entries);
 114  
 115          $customcompletion = new custom_completion($mockcminfo, 2);
 116          $this->assertEquals($status, $customcompletion->get_state($rule));
 117      }
 118  
 119      /**
 120       * Test for get_defined_custom_rules().
 121       */
 122      public function test_get_defined_custom_rules() {
 123          $rules = custom_completion::get_defined_custom_rules();
 124          $this->assertCount(1, $rules);
 125          $this->assertEquals('completionentries', reset($rules));
 126      }
 127  
 128      /**
 129       * Test for get_defined_custom_rule_descriptions().
 130       */
 131      public function test_get_custom_rule_descriptions() {
 132          // Get defined custom rules.
 133          $rules = custom_completion::get_defined_custom_rules();
 134          // Build a mock cm_info instance.
 135          $mockcminfo = $this->getMockBuilder(cm_info::class)
 136              ->disableOriginalConstructor()
 137              ->onlyMethods(['__get'])
 138              ->getMock();
 139  
 140          // Instantiate a custom_completion object using the mocked cm_info.
 141          $customcompletion = new custom_completion($mockcminfo, 1);
 142  
 143          // Get custom rule descriptions.
 144          $ruledescriptions = $customcompletion->get_custom_rule_descriptions();
 145  
 146          // Confirm that defined rules and rule descriptions are consistent with each other.
 147          $this->assertEquals(count($rules), count($ruledescriptions));
 148          foreach ($rules as $rule) {
 149              $this->assertArrayHasKey($rule, $ruledescriptions);
 150          }
 151      }
 152  
 153      /**
 154       * Test for is_defined().
 155       */
 156      public function test_is_defined() {
 157          // Build a mock cm_info instance.
 158          $mockcminfo = $this->getMockBuilder(cm_info::class)
 159              ->disableOriginalConstructor()
 160              ->getMock();
 161  
 162          $customcompletion = new custom_completion($mockcminfo, 1);
 163  
 164          // Rule is defined.
 165          $this->assertTrue($customcompletion->is_defined('completionentries'));
 166  
 167          // Undefined rule.
 168          $this->assertFalse($customcompletion->is_defined('somerandomrule'));
 169      }
 170  
 171      /**
 172       * Data provider for test_get_available_custom_rules().
 173       *
 174       * @return array[]
 175       */
 176      public function get_available_custom_rules_provider(): array {
 177          return [
 178              'Completion entries available' => [
 179                  COMPLETION_ENABLED, ['completionentries']
 180              ],
 181              'Completion entries not available' => [
 182                  COMPLETION_DISABLED, []
 183              ],
 184          ];
 185      }
 186  
 187      /**
 188       * Test for get_available_custom_rules().
 189       *
 190       * @dataProvider get_available_custom_rules_provider
 191       * @param int $status
 192       * @param array $expected
 193       */
 194      public function test_get_available_custom_rules(int $status, array $expected) {
 195          $customdataval = [
 196              'customcompletionrules' => [
 197                  'completionentries' => $status
 198              ]
 199          ];
 200  
 201          // Build a mock cm_info instance.
 202          $mockcminfo = $this->getMockBuilder(cm_info::class)
 203              ->disableOriginalConstructor()
 204              ->onlyMethods(['__get'])
 205              ->getMock();
 206  
 207          // Mock the return of magic getter for the customdata attribute.
 208          $mockcminfo->expects($this->any())
 209              ->method('__get')
 210              ->with('customdata')
 211              ->willReturn($customdataval);
 212  
 213          $customcompletion = new custom_completion($mockcminfo, 1);
 214          $this->assertEquals($expected, $customcompletion->get_available_custom_rules());
 215      }
 216  }