MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / mod / survey / tests / custom_completion_test.php
blob08b12b7cb0497ea19a0d42b8a5d9da9512b45358
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 /**
18 * Contains unit tests for core_completion/activity_custom_completion.
20 * @package mod_survey
21 * @copyright Simey Lameze <simey@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 declare(strict_types=1);
27 namespace mod_survey;
29 use advanced_testcase;
30 use cm_info;
31 use coding_exception;
32 use mod_survey\completion\custom_completion;
33 use moodle_exception;
35 defined('MOODLE_INTERNAL') || die();
37 global $CFG;
38 require_once($CFG->libdir . '/completionlib.php');
40 /**
41 * Class for unit testing mod_survey/activity_custom_completion.
43 * @package mod_survey
44 * @copyright Simey Lameze <simey@moodle.com>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class custom_completion_test extends advanced_testcase {
49 /**
50 * Data provider for get_state().
52 * @return array[]
54 public function get_state_provider(): array {
55 return [
56 'Undefined rule' => [
57 'somenonexistentrule', COMPLETION_DISABLED, false, null, coding_exception::class
59 'Rule not available' => [
60 'completionsubmit', COMPLETION_DISABLED, false, null, moodle_exception::class
62 'Rule available, user has not submitted' => [
63 'completionsubmit', COMPLETION_ENABLED, false, COMPLETION_INCOMPLETE, null
65 'Rule available, user has submitted' => [
66 'completionsubmit', COMPLETION_ENABLED, true, COMPLETION_COMPLETE, null
71 /**
72 * Test for get_state().
74 * @dataProvider get_state_provider
75 * @param string $rule The custom completion rule.
76 * @param int $available Whether this rule is available.
77 * @param bool $submitted
78 * @param int|null $status Expected status.
79 * @param string|null $exception Expected exception.
81 public function test_get_state(string $rule, int $available, ?bool $submitted, ?int $status, ?string $exception) {
82 global $DB;
84 if (!is_null($exception)) {
85 $this->expectException($exception);
88 // Custom completion rule data for cm_info::customdata.
89 $customdataval = [
90 'customcompletionrules' => [
91 $rule => $available
95 // Build a mock cm_info instance.
96 $mockcminfo = $this->getMockBuilder(cm_info::class)
97 ->disableOriginalConstructor()
98 ->onlyMethods(['__get'])
99 ->getMock();
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 ]));
109 // Mock the DB calls.
110 $DB = $this->createMock(get_class($DB));
111 $DB->expects($this->atMost(1))
112 ->method('record_exists')
113 ->willReturn($submitted);
115 $customcompletion = new custom_completion($mockcminfo, 2);
116 $this->assertEquals($status, $customcompletion->get_state($rule));
120 * Test for get_defined_custom_rules().
122 public function test_get_defined_custom_rules() {
123 $rules = custom_completion::get_defined_custom_rules();
124 $this->assertCount(1, $rules);
125 $this->assertEquals('completionsubmit', reset($rules));
129 * Test for get_defined_custom_rule_descriptions().
131 public function test_get_custom_rule_descriptions() {
132 // Get defined custom rules.
133 $rules = custom_completion::get_defined_custom_rules();
135 // Build a mock cm_info instance.
136 $mockcminfo = $this->getMockBuilder(cm_info::class)
137 ->disableOriginalConstructor()
138 ->onlyMethods(['__get'])
139 ->getMock();
141 // Instantiate a custom_completion object using the mocked cm_info.
142 $customcompletion = new custom_completion($mockcminfo, 1);
144 // Get custom rule descriptions.
145 $ruledescriptions = $customcompletion->get_custom_rule_descriptions();
147 // Confirm that defined rules and rule descriptions are consistent with each other.
148 $this->assertEquals(count($rules), count($ruledescriptions));
149 foreach ($rules as $rule) {
150 $this->assertArrayHasKey($rule, $ruledescriptions);
155 * Test for is_defined().
157 public function test_is_defined() {
158 // Build a mock cm_info instance.
159 $mockcminfo = $this->getMockBuilder(cm_info::class)
160 ->disableOriginalConstructor()
161 ->getMock();
163 $customcompletion = new custom_completion($mockcminfo, 1);
165 // Rule is defined.
166 $this->assertTrue($customcompletion->is_defined('completionsubmit'));
168 // Undefined rule.
169 $this->assertFalse($customcompletion->is_defined('somerandomrule'));
173 * Data provider for test_get_available_custom_rules().
175 * @return array[]
177 public function get_available_custom_rules_provider(): array {
178 return [
179 'Completion submit available' => [
180 COMPLETION_ENABLED, ['completionsubmit']
182 'Completion submit not available' => [
183 COMPLETION_DISABLED, []
189 * Test for get_available_custom_rules().
191 * @dataProvider get_available_custom_rules_provider
192 * @param int $status
193 * @param array $expected
195 public function test_get_available_custom_rules(int $status, array $expected) {
196 $customdataval = [
197 'customcompletionrules' => [
198 'completionsubmit' => $status
202 // Build a mock cm_info instance.
203 $mockcminfo = $this->getMockBuilder(cm_info::class)
204 ->disableOriginalConstructor()
205 ->onlyMethods(['__get'])
206 ->getMock();
208 // Mock the return of magic getter for the customdata attribute.
209 $mockcminfo->expects($this->any())
210 ->method('__get')
211 ->with('customdata')
212 ->willReturn($customdataval);
214 $customcompletion = new custom_completion($mockcminfo, 1);
215 $this->assertEquals($expected, $customcompletion->get_available_custom_rules());