MDL-81717 h5p: Improve robustness content type fetching
[moodle.git] / completion / tests / activity_custom_completion_test.php
blob1a237d96424ba4c0a9ee746620174eb6716d3f76
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.
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/>.
17 declare(strict_types = 1);
19 namespace core_completion;
21 use advanced_testcase;
22 use coding_exception;
23 use moodle_exception;
24 use PHPUnit\Framework\MockObject\MockObject;
26 /**
27 * Class for unit testing core_completion/activity_custom_completion.
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
33 class activity_custom_completion_test extends advanced_testcase {
35 /**
36 * Fetches a mocked activity_custom_completion instance.
38 * @param string[] $methods List of methods to mock.
39 * @return activity_custom_completion|MockObject
41 protected function setup_mock(array $methods) {
42 return $this->getMockBuilder(activity_custom_completion::class)
43 ->disableOriginalConstructor()
44 ->onlyMethods($methods)
45 ->getMockForAbstractClass();
48 /**
49 * Data provider for test_get_overall_completion_state().
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],
59 COMPLETION_INCOMPLETE
61 'First complete, second incomplete' => [
62 ['completionsubmit', 'completioncreate'],
63 [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE],
65 COMPLETION_INCOMPLETE
67 'All complete' => [
68 ['completionsubmit', 'completioncreate'],
69 [COMPLETION_COMPLETE, COMPLETION_COMPLETE],
71 COMPLETION_COMPLETE
73 'No rules' => [
74 [],
75 [],
77 COMPLETION_COMPLETE
82 /**
83 * Test for \core_completion\activity_custom_completion::get_overall_completion_state().
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
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 ]);
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);
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]]
110 ->willReturn($rulestates[0], $rulestates[1]);
111 } else {
112 $stub->expects($this->never())
113 ->method('get_state');
116 $this->assertEquals($state, $stub->get_overall_completion_state());
120 * Data provider for test_validate_rule().
122 * @return array[]
124 public function validate_rule_provider() {
125 return [
126 'Not defined' => [
127 false, true, coding_exception::class
129 'Not available' => [
130 true, false, moodle_exception::class
132 'Defined and available' => [
133 true, true, null
139 * Test for validate_rule()
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.
146 public function test_validate_rule(bool $defined, bool $available, ?string $expectedexception) {
147 $stub = $this->setup_mock([
148 'is_defined',
149 'is_available'
152 // Mock activity_custom_completion's is_defined() method.
153 $stub->expects($this->any())
154 ->method('is_defined')
155 ->willReturn($defined);
157 // Mock activity_custom_completion's is_available() method.
158 $stub->expects($this->any())
159 ->method('is_available')
160 ->willReturn($available);
162 if ($expectedexception) {
163 $this->expectException($expectedexception);
165 $stub->validate_rule('customcompletionrule');
169 * Test for is_available().
171 public function test_is_available() {
172 $stub = $this->setup_mock([
173 'get_available_custom_rules',
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']);
181 // Rule is available.
182 $this->assertTrue($stub->is_available('rule1'));
184 // Rule is not available.
185 $this->assertFalse($stub->is_available('rule'));