Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
            'First complete, second failed' => [
68
                ['completionsubmit', 'completioncreate'],
69
                [COMPLETION_COMPLETE, COMPLETION_COMPLETE_FAIL],
70
                2,
71
                COMPLETION_COMPLETE_FAIL,
72
            ],
73
            'First complete, second incomplete, third failed' => [
74
                ['completionsubmit', 'completioncreate'],
75
                [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE, COMPLETION_COMPLETE_FAIL],
76
                2,
77
                COMPLETION_INCOMPLETE,
78
            ],
79
            'All complete' => [
80
                ['completionsubmit', 'completioncreate'],
81
                [COMPLETION_COMPLETE, COMPLETION_COMPLETE],
82
                2,
83
                COMPLETION_COMPLETE,
84
            ],
85
            'No rules' => [
86
                [],
87
                [],
88
                0,
89
                COMPLETION_COMPLETE,
90
            ],
91
        ];
92
    }
93
 
94
    /**
95
     * Test for \core_completion\activity_custom_completion::get_overall_completion_state().
96
     *
97
     * @dataProvider overall_completion_state_provider
98
     * @param string[] $rules The custom completion rules.
99
     * @param int[] $rulestates The completion states of these custom completion rules.
100
     * @param int $invokecount Expected invoke count of get_state().
101
     * @param int $state The expected overall completion state
102
     */
11 efrain 103
    public function test_get_overall_completion_state(array $rules, array $rulestates, int $invokecount, int $state): void {
1 efrain 104
        $stub = $this->setup_mock([
105
            'get_available_custom_rules',
106
            'get_state',
107
        ]);
108
 
109
        // Mock activity_custom_completion's get_available_custom_rules() method.
110
        $stub->expects($this->once())
111
            ->method('get_available_custom_rules')
112
            ->willReturn($rules);
113
 
114
        // Mock activity_custom_completion's get_state() method.
115
        if ($invokecount > 0) {
116
            $stub->expects($this->exactly($invokecount))
117
                ->method('get_state')
118
                ->withConsecutive(
119
                    [$rules[0]],
120
                    [$rules[1]]
121
                )
122
                ->willReturn($rulestates[0], $rulestates[1]);
123
        } else {
124
            $stub->expects($this->never())
125
                ->method('get_state');
126
        }
127
 
128
        $this->assertEquals($state, $stub->get_overall_completion_state());
129
    }
130
 
131
    /**
132
     * Data provider for test_validate_rule().
133
     *
134
     * @return array[]
135
     */
136
    public function validate_rule_provider() {
137
        return [
138
            'Not defined' => [
139
                false, true, coding_exception::class
140
            ],
141
            'Not available' => [
142
                true, false, moodle_exception::class
143
            ],
144
            'Defined and available' => [
145
                true, true, null
146
            ],
147
        ];
148
    }
149
 
150
    /**
151
     * Test for validate_rule()
152
     *
153
     * @dataProvider validate_rule_provider
154
     * @param bool $defined is_defined()'s mocked return value.
155
     * @param bool $available is_available()'s mocked return value.
156
     * @param string|null $expectedexception Expected expectation class name.
157
     */
11 efrain 158
    public function test_validate_rule(bool $defined, bool $available, ?string $expectedexception): void {
1 efrain 159
        $stub = $this->setup_mock([
160
            'is_defined',
161
            'is_available'
162
        ]);
163
 
164
        // Mock activity_custom_completion's is_defined() method.
165
        $stub->expects($this->any())
166
            ->method('is_defined')
167
            ->willReturn($defined);
168
 
169
        // Mock activity_custom_completion's is_available() method.
170
        $stub->expects($this->any())
171
            ->method('is_available')
172
            ->willReturn($available);
173
 
174
        if ($expectedexception) {
175
            $this->expectException($expectedexception);
176
        }
177
        $stub->validate_rule('customcompletionrule');
178
    }
179
 
180
    /**
181
     * Test for is_available().
182
     */
11 efrain 183
    public function test_is_available(): void {
1 efrain 184
        $stub = $this->setup_mock([
185
            'get_available_custom_rules',
186
        ]);
187
 
188
        // Mock activity_custom_completion's get_available_custom_rules() method.
189
        $stub->expects($this->any())
190
            ->method('get_available_custom_rules')
191
            ->willReturn(['rule1', 'rule2']);
192
 
193
        // Rule is available.
194
        $this->assertTrue($stub->is_available('rule1'));
195
 
196
        // Rule is not available.
197
        $this->assertFalse($stub->is_available('rule'));
198
    }
199
}