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  declare(strict_types = 1);
  18  
  19  namespace core_completion;
  20  
  21  use advanced_testcase;
  22  use coding_exception;
  23  use moodle_exception;
  24  use PHPUnit\Framework\MockObject\MockObject;
  25  
  26  /**
  27   * Class for unit testing core_completion/activity_custom_completion.
  28   *
  29   * @package   core_completion
  30   * @copyright 2021 Jun Pataleta <jun@moodle.com>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class activity_custom_completion_test extends advanced_testcase {
  34  
  35      /**
  36       * Fetches a mocked activity_custom_completion instance.
  37       *
  38       * @param string[] $methods List of methods to mock.
  39       * @return activity_custom_completion|MockObject
  40       */
  41      protected function setup_mock(array $methods) {
  42          return $this->getMockBuilder(activity_custom_completion::class)
  43              ->disableOriginalConstructor()
  44              ->onlyMethods($methods)
  45              ->getMockForAbstractClass();
  46      }
  47  
  48      /**
  49       * Data provider for test_get_overall_completion_state().
  50       */
  51      public function overall_completion_state_provider(): array {
  52          global $CFG;
  53          require_once($CFG->libdir . '/completionlib.php');
  54          return [
  55              'First incomplete, second complete' => [
  56                  ['completionsubmit', 'completioncreate'],
  57                  [COMPLETION_INCOMPLETE, COMPLETION_COMPLETE],
  58                  1,
  59                  COMPLETION_INCOMPLETE
  60              ],
  61              'First complete, second incomplete' => [
  62                  ['completionsubmit', 'completioncreate'],
  63                  [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE],
  64                  2,
  65                  COMPLETION_INCOMPLETE
  66              ],
  67              'All complete' => [
  68                  ['completionsubmit', 'completioncreate'],
  69                  [COMPLETION_COMPLETE, COMPLETION_COMPLETE],
  70                  2,
  71                  COMPLETION_COMPLETE
  72              ],
  73              'No rules' => [
  74                  [],
  75                  [],
  76                  0,
  77                  COMPLETION_COMPLETE
  78              ],
  79          ];
  80      }
  81  
  82      /**
  83       * Test for \core_completion\activity_custom_completion::get_overall_completion_state().
  84       *
  85       * @dataProvider overall_completion_state_provider
  86       * @param string[] $rules The custom completion rules.
  87       * @param int[] $rulestates The completion states of these custom completion rules.
  88       * @param int $invokecount Expected invoke count of get_state().
  89       * @param int $state The expected overall completion state
  90       */
  91      public function test_get_overall_completion_state(array $rules, array $rulestates, int $invokecount, int $state) {
  92          $stub = $this->setup_mock([
  93              'get_available_custom_rules',
  94              'get_state',
  95          ]);
  96  
  97          // Mock activity_custom_completion's get_available_custom_rules() method.
  98          $stub->expects($this->once())
  99              ->method('get_available_custom_rules')
 100              ->willReturn($rules);
 101  
 102          // Mock activity_custom_completion's get_state() method.
 103          if ($invokecount > 0) {
 104              $stub->expects($this->exactly($invokecount))
 105                  ->method('get_state')
 106                  ->withConsecutive(
 107                      [$rules[0]],
 108                      [$rules[1]]
 109                  )
 110                  ->willReturn($rulestates[0], $rulestates[1]);
 111          } else {
 112              $stub->expects($this->never())
 113                  ->method('get_state');
 114          }
 115  
 116          $this->assertEquals($state, $stub->get_overall_completion_state());
 117      }
 118  
 119      /**
 120       * Data provider for test_validate_rule().
 121       *
 122       * @return array[]
 123       */
 124      public function validate_rule_provider() {
 125          return [
 126              'Not defined' => [
 127                  false, true, coding_exception::class
 128              ],
 129              'Not available' => [
 130                  true, false, moodle_exception::class
 131              ],
 132              'Defined and available' => [
 133                  true, true, null
 134              ],
 135          ];
 136      }
 137  
 138      /**
 139       * Test for validate_rule()
 140       *
 141       * @dataProvider validate_rule_provider
 142       * @param bool $defined is_defined()'s mocked return value.
 143       * @param bool $available is_available()'s mocked return value.
 144       * @param string|null $expectedexception Expected expectation class name.
 145       */
 146      public function test_validate_rule(bool $defined, bool $available, ?string $expectedexception) {
 147          $stub = $this->setup_mock([
 148              'is_defined',
 149              'is_available'
 150          ]);
 151  
 152          // Mock activity_custom_completion's is_defined() method.
 153          $stub->expects($this->any())
 154              ->method('is_defined')
 155              ->willReturn($defined);
 156  
 157          // Mock activity_custom_completion's is_available() method.
 158          $stub->expects($this->any())
 159              ->method('is_available')
 160              ->willReturn($available);
 161  
 162          if ($expectedexception) {
 163              $this->expectException($expectedexception);
 164          }
 165          $stub->validate_rule('customcompletionrule');
 166      }
 167  
 168      /**
 169       * Test for is_available().
 170       */
 171      public function test_is_available() {
 172          $stub = $this->setup_mock([
 173              'get_available_custom_rules',
 174          ]);
 175  
 176          // Mock activity_custom_completion's get_available_custom_rules() method.
 177          $stub->expects($this->any())
 178              ->method('get_available_custom_rules')
 179              ->willReturn(['rule1', 'rule2']);
 180  
 181          // Rule is available.
 182          $this->assertTrue($stub->is_available('rule1'));
 183  
 184          // Rule is not available.
 185          $this->assertFalse($stub->is_available('rule'));
 186      }
 187  }