Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Contains unit tests for core_completion/activity_custom_completion.
19
 *
20
 * @package   mod_questionnaire
21
 * @copyright 2022 The Open University
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_questionnaire;
28
 
29
use advanced_testcase;
30
use cm_info;
31
use coding_exception;
32
use mod_questionnaire\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
require_once($CFG->dirroot . '/mod/questionnaire/classes/question/question.php');
40
 
41
/**
42
 * Class for unit testing mod_questionnaire/custom_completion.
43
 *
44
 * @package   mod_questionnaire
45
 * @copyright 2022 The Open University
46
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 */
48
class custom_completion_test extends advanced_testcase {
49
 
50
    /**
51
     * Data provider for get_state().
52
     *
53
     * @return array[]
54
     */
55
    public function get_state_provider(): array {
56
        return [
57
            'Undefined rule' => [
58
                'somenonexistentrule', COMPLETION_DISABLED, false, null, coding_exception::class
59
            ],
60
            'Rule not available' => [
61
                'completionsubmit', COMPLETION_DISABLED, false, null, moodle_exception::class
62
            ],
63
            'Rule available, user has not submitted' => [
64
                'completionsubmit', COMPLETION_ENABLED, false, COMPLETION_INCOMPLETE, null
65
            ],
66
            'Rule available, user has submitted' => [
67
                'completionsubmit', COMPLETION_ENABLED, true, COMPLETION_COMPLETE, null
68
            ],
69
        ];
70
    }
71
 
72
    /**
73
     * Test for get_state().
74
     *
75
     * @dataProvider get_state_provider
76
     * @param string $rule The custom completion rule.
77
     * @param int $available Whether this rule is available.
78
     * @param bool $submitted
79
     * @param int|null $status Expected status.
80
     * @param string|null $exception Expected exception.
81
     * @throws coding_exception
82
     */
83
    public function test_get_state(string $rule, int $available, ?bool $submitted, ?int $status, ?string $exception) {
84
        if (!is_null($exception)) {
85
            $this->expectException($exception);
86
        }
87
 
88
        $this->resetAfterTest();
89
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_questionnaire');
90
 
91
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
92
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
93
        $questionnaire = $generator->create_instance(['course' => $course->id, 'completion' => COMPLETION_TRACKING_AUTOMATIC,
94
            $rule => $available]);
95
 
96
        $questiondata['type_id'] = 1;
97
        $questiondata['surveyid'] = $questionnaire->sid;
98
        $questiondata['name'] = 'Q1';
99
        $questiondata['content'] = 'Test content';
100
        $question = $generator->create_question($questionnaire, $questiondata);
101
 
102
        // For case user done completion.
103
        if ($status !== COMPLETION_INCOMPLETE) {
104
            $response = $generator->create_question_response($questionnaire, $question, 'y', (int)$student->id);
105
        }
106
 
107
        $this->setUser($student);
108
        $cm = get_coursemodule_from_instance('questionnaire', $questionnaire->id);
109
        $cm = cm_info::create($cm);
110
 
111
        $customcompletion = new custom_completion($cm, (int)$student->id);
112
        $this->assertEquals($status, $customcompletion->get_state($rule));
113
    }
114
 
115
    /**
116
     * Test for get_defined_custom_rules().
117
     */
118
    public function test_get_defined_custom_rules() {
119
        $rules = custom_completion::get_defined_custom_rules();
120
        $this->assertCount(1, $rules);
121
        $this->assertEquals('completionsubmit', reset($rules));
122
    }
123
 
124
    /**
125
     * Test for get_defined_custom_rule_descriptions().
126
     */
127
    public function test_get_custom_rule_descriptions() {
128
        // Get defined custom rules.
129
        $rules = custom_completion::get_defined_custom_rules();
130
 
131
        // Build a mock cm_info instance.
132
        $mockcminfo = $this->getMockBuilder(cm_info::class)
133
            ->disableOriginalConstructor()
134
            ->onlyMethods(['__get'])
135
            ->getMock();
136
 
137
        // Instantiate a custom_completion object using the mocked cm_info.
138
        $customcompletion = new custom_completion($mockcminfo, 1);
139
 
140
        // Get custom rule descriptions.
141
        $ruledescriptions = $customcompletion->get_custom_rule_descriptions();
142
 
143
        // Confirm that defined rules and rule descriptions are consistent with each other.
144
        $this->assertEquals(count($rules), count($ruledescriptions));
145
        foreach ($rules as $rule) {
146
            $this->assertArrayHasKey($rule, $ruledescriptions);
147
        }
148
    }
149
 
150
    /**
151
     * Test for is_defined().
152
     */
153
    public function test_is_defined() {
154
        // Build a mock cm_info instance.
155
        $mockcminfo = $this->getMockBuilder(cm_info::class)
156
            ->disableOriginalConstructor()
157
            ->getMock();
158
 
159
        $customcompletion = new custom_completion($mockcminfo, 1);
160
 
161
        // Rule is defined.
162
        $this->assertTrue($customcompletion->is_defined('completionsubmit'));
163
 
164
        // Undefined rule.
165
        $this->assertFalse($customcompletion->is_defined('somerandomrule'));
166
    }
167
 
168
    /**
169
     * Data provider for test_get_available_custom_rules().
170
     *
171
     * @return array[]
172
     */
173
    public function get_available_custom_rules_provider(): array {
174
        return [
175
            'Completion submit available' => [
176
                COMPLETION_ENABLED, ['completionsubmit']
177
            ],
178
            'Completion submit not available' => [
179
                COMPLETION_DISABLED, []
180
            ],
181
        ];
182
    }
183
 
184
    /**
185
     * Test for get_available_custom_rules().
186
     *
187
     * @dataProvider get_available_custom_rules_provider
188
     * @param int $status
189
     * @param array $expected
190
     */
191
    public function test_get_available_custom_rules(int $status, array $expected) {
192
        $customdataval = [
193
            'customcompletionrules' => [
194
                'completionsubmit' => $status
195
            ]
196
        ];
197
 
198
        // Build a mock cm_info instance.
199
        $mockcminfo = $this->getMockBuilder(cm_info::class)
200
            ->disableOriginalConstructor()
201
            ->onlyMethods(['__get'])
202
            ->getMock();
203
 
204
        // Mock the return of magic getter for the customdata attribute.
205
        $mockcminfo->expects($this->any())
206
            ->method('__get')
207
            ->with('customdata')
208
            ->willReturn($customdataval);
209
 
210
        $customcompletion = new custom_completion($mockcminfo, 1);
211
        $this->assertEquals($expected, $customcompletion->get_available_custom_rules());
212
    }
213
}