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
 * Unit tests for the coursecompleted condition.
19
 *
20
 * @package   availability_coursecompleted
21
 * @copyright 2017 iplusacademy (www.iplusacademy.org)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace availability_coursecompleted;
27
 
28
 
29
use \availability_coursecompleted\condition;
30
use \availability_coursecompleted\frontend;
31
use \completion_info;
32
use core_availability\tree;
33
use core_availability\info_module;
34
use core_availability\mock_info;
35
use core_availability\mock_condition;
36
use core_completion;
37
 
38
/**
39
 * Unit tests for the coursecompleted condition.
40
 *
41
 * @package   availability_coursecompleted
42
 * @copyright 2017 iplusacademy (www.iplusacademy.org)
43
 * @author    Renaat Debleu <info@eWallah.net>
44
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 * @coversDefaultClass \availability_coursecompleted
46
 */
47
class condition_test extends \advanced_testcase {
48
 
49
    /**
50
     * Tests constructing and using coursecompleted condition as part of tree.
51
     * @covers \availability_coursecompleted\condition
52
     */
53
    public function test_in_tree() {
54
        global $CFG, $USER;
55
        require_once($CFG->dirroot . '/completion/criteria/completion_criteria.php');
56
        require_once($CFG->dirroot . '/completion/criteria/completion_criteria_activity.php');
57
        require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
58
        $this->resetAfterTest();
59
        $this->setAdminUser();
60
 
61
        // Create course with coursecompleted turned on.
62
        set_config('enableavailability', true);
63
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
64
        $userid = $this->getDataGenerator()->create_user()->id;
65
        $this->getDataGenerator()->enrol_user($userid, $course->id);
66
        $info = new mock_info($course, $userid);
67
 
68
        $structure1 = (object)['op' => '|', 'show' => true, 'c' => [(object)['type' => 'coursecompleted', 'id' => '1']]];
69
        $structure2 = (object)['op' => '|', 'show' => true, 'c' => [(object)['type' => 'coursecompleted', 'id' => '0']]];
70
        $tree1 = new tree($structure1);
71
        $tree2 = new tree($structure2);
72
 
73
        $this->assertFalse($tree1->check_available(false, $info, true, $USER->id)->is_available());
74
        $this->assertTrue($tree2->check_available(false, $info, true, $USER->id)->is_available());
75
        $this->assertFalse($tree1->check_available(false, $info, true, $userid)->is_available());
76
        $this->assertTrue($tree2->check_available(false, $info, true, $userid)->is_available());
77
 
78
        $this->setuser($userid);
79
        $this->assertFalse($tree1->check_available(false, $info, true, $USER->id)->is_available());
80
        $this->assertTrue($tree2->check_available(false, $info, true, $USER->id)->is_available());
81
        $this->assertFalse($tree1->check_available(false, $info, true, $userid)->is_available());
82
        $this->assertTrue($tree2->check_available(false, $info, true, $userid)->is_available());
83
 
84
        // Change course completed.
85
        $this->setAdminUser();
86
        $ccompletion = new \completion_completion(['course' => $course->id, 'userid' => $userid]);
87
        $ccompletion->mark_complete();
88
 
89
        $this->assertTrue($tree1->check_available(false, $info, true, $userid)->is_available());
90
        $this->assertFalse($tree2->check_available(false, $info, true, $userid)->is_available());
91
        $this->setuser($userid);
92
        $this->assertTrue($tree1->check_available(false, $info, true, $userid)->is_available());
93
        $this->assertFalse($tree2->check_available(false, $info, true, $userid)->is_available());
94
    }
95
 
96
    /**
97
     * Tests the constructor including error conditions.
98
     * @covers \availability_coursecompleted\condition
99
     */
100
    public function test_constructor() {
101
        // This works with no parameters.
102
        $structure = (object)[];
103
        try {
104
            $completed = new condition($structure);
105
            $this->fail();
106
        } catch (\exception $e) {
107
            $this->assertEquals('', $e->getMessage());
108
        }
109
        $this->assertNotEmpty($completed);
110
 
111
        // This works with '1'.
112
        $structure->id = '1';
113
        try {
114
            $completed = new condition($structure);
115
            $this->fail();
116
        } catch (\exception $e) {
117
            $this->assertEquals('', $e->getMessage());
118
        }
119
        $this->assertNotEmpty($completed);
120
 
121
        // This works with '0'.
122
        $structure->id = '0';
123
        try {
124
            $completed = new condition($structure);
125
            $this->fail();
126
        } catch (\exception $e) {
127
            $this->assertEquals('', $e->getMessage());
128
        }
129
        $this->assertNotEmpty($completed);
130
 
131
        // This fails with null.
132
        $structure->id = null;
133
        try {
134
            $completed = new condition($structure);
135
        } catch (\coding_exception $e) {
136
            $this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
137
        }
138
 
139
        // Invalid ->id.
140
        $structure->id = false;
141
        try {
142
            $completed = new condition($structure);
143
        } catch (\coding_exception $e) {
144
            $this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
145
        }
146
 
147
        // Invalid string. Should be checked 'longer string'.
148
        $structure->id = 1;
149
        try {
150
            $completed = new condition($structure);
151
        } catch (\coding_exception $e) {
152
            $this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
153
        }
154
    }
155
 
156
    /**
157
     * Tests the save() function.
158
     * @covers \availability_coursecompleted\condition
159
     */
160
    public function test_save() {
161
        $structure = (object)['id' => '1'];
162
        $cond = new condition($structure);
163
        $structure->type = 'coursecompleted';
164
        $this->assertEquals($structure, $cond->save());
165
    }
166
 
167
    /**
168
     * Tests the get_description and get_standalone_description functions.
169
     * @covers \availability_coursecompleted\condition
170
     * @covers \availability_coursecompleted\frontend
171
     */
172
    public function test_get_description() {
173
        $this->resetAfterTest();
174
        $this->setAdminUser();
175
        set_config('enableavailability', true);
176
        $userid = $this->getDataGenerator()->create_user()->id;
177
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
178
        $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['completion' => 1]);
179
 
180
        $modinfo = get_fast_modinfo($course);
181
        $sections = $modinfo->get_section_info_all();
182
 
183
        $frontend = new frontend();
184
        $name = 'availability_coursecompleted\frontend';
185
        $this->assertFalse(\phpunit_util::call_internal_method($frontend, 'allow_add', [$course], $name));
186
 
187
        $data = (object) ['id' => $course->id, 'criteria_activity' => [$assign->cmid => 1]];
188
        $criterion = new \completion_criteria_activity();
189
        $criterion->update_config($data);
190
        $this->assertTrue(\phpunit_util::call_internal_method($frontend, 'allow_add', [$course], $name));
191
        $this->assertTrue(\phpunit_util::call_internal_method($frontend, 'allow_add', [$course, null, $sections[0]], $name));
192
        $this->assertTrue(\phpunit_util::call_internal_method($frontend, 'allow_add', [$course, null, $sections[1]], $name));
193
 
194
        $info = new mock_info();
195
        $nau = 'Not available unless: ';
196
        $completed = new condition((object)['type' => 'coursecompleted', 'id' => '1']);
197
        $information = $completed->get_description(true, false, $info);
198
        $this->assertEquals($information, get_string('getdescription', 'availability_coursecompleted'));
199
        $information = $completed->get_description(true, true, $info);
200
        $this->assertEquals($information, get_string('getdescriptionnot', 'availability_coursecompleted'));
201
        $information = $completed->get_standalone_description(true, false, $info);
202
        $this->assertEquals($information, $nau . get_string('getdescription', 'availability_coursecompleted'));
203
        $information = $completed->get_standalone_description(true, true, $info);
204
        $this->assertEquals($information, $nau . get_string('getdescriptionnot', 'availability_coursecompleted'));
205
        $this->setuser($userid);
206
        $information = $completed->get_description(true, false, $info);
207
        $this->assertEquals($information, get_string('getdescription', 'availability_coursecompleted'));
208
        $information = $completed->get_description(true, true, $info);
209
        $this->assertEquals($information, get_string('getdescriptionnot', 'availability_coursecompleted'));
210
        $information = $completed->get_standalone_description(true, false, $info);
211
        $this->assertEquals($information, $nau . get_string('getdescription', 'availability_coursecompleted'));
212
        $information = $completed->get_standalone_description(true, true, $info);
213
        $this->assertEquals($information, $nau . get_string('getdescriptionnot', 'availability_coursecompleted'));
214
    }
215
 
216
    /**
217
     * Tests a page before and after completion.
218
     * @covers \availability_coursecompleted\condition
219
     * @covers \availability_coursecompleted\frontend
220
     */
221
    public function test_page() {
222
        global $PAGE;
223
        $this->resetAfterTest();
224
        $this->setAdminUser();
225
 
226
        // Create course with coursecompleted turned on.
227
        set_config('enableavailability', true);
228
        $generator = $this->getDataGenerator();
229
        $course = $generator->create_course(['enablecompletion' => true]);
230
        $user = $generator->create_user();
231
        $generator->enrol_user($user->id, $course->id);
232
        $page = $generator->get_plugin_generator('mod_page')->create_instance(['course' => $course]);
233
        $modinfo = get_fast_modinfo($course);
234
        $cm = $modinfo->get_cm($page->cmid);
235
        $PAGE->set_url('/course/modedit.php', ['update' => $page->cmid]);
236
        frontend::include_all_javascript($course, $cm);
237
        $info = new info_module($cm);
238
        $cond = new condition((object)['type' => 'coursecompleted', 'id' => '1']);
239
        $this->assertFalse($cond->is_available(false, $info, true, $user->id));
240
        $this->assertFalse($cond->is_available(false, $info, false, $user->id));
241
        $this->assertTrue($cond->is_available(true, $info, false, $user->id));
242
        $this->assertTrue($cond->is_available(true, $info, true, $user->id));
243
        $ccompletion = new \completion_completion(['course' => $course->id, 'userid' => $user->id]);
244
        $ccompletion->mark_complete();
245
        rebuild_course_cache($course->id, true);
246
        $this->assertFalse($cond->is_available(true, $info, true, $user->id));
247
        $this->assertFalse($cond->is_available(true, $info, false, $user->id));
248
        $this->assertTrue($cond->is_available(false, $info, false, $user->id));
249
        $this->assertTrue($cond->is_available(false, $info, true, $user->id));
250
        // No id.
251
        $cond = new condition((object)['type' => 'coursecompleted']);
252
        $this->assertFalse($cond->is_available(false, $info, false, $user->id));
253
        $this->assertFalse($cond->is_available_for_all());
254
        $this->assertFalse($cond->update_dependency_id(null, 1, 2));
255
        $this->assertEquals($cond->__toString(), '{coursecompleted:False}');
256
        $this->assertEquals($cond->get_standalone_description(true, true, $info),
257
            'Not available unless: You completed this course.');
258
    }
259
 
260
    /**
261
     * Tests using course completion condition in front end.
262
     * @covers \availability_coursecompleted\condition
263
     */
264
    public function test_other() {
265
        $this->assertEqualsCanonicalizing((object)['type' => 'coursecompleted', 'id' => '3'], condition::get_json('3'));
266
        $this->assertEqualsCanonicalizing((object)['type' => 'coursecompleted', 'id' => '0'], condition::get_json('0'));
267
    }
268
 
269
    /**
270
     * Test behat funcs
271
     * @covers \behat_availability_coursecompleted
272
     */
273
    public function test_behat() {
274
        global $CFG;
275
        require_once($CFG->dirroot . '/availability/condition/coursecompleted/tests/behat/behat_availability_coursecompleted.php');
276
        $this->resetAfterTest();
277
        set_config('enableavailability', true);
278
        $user = $this->getDataGenerator()->create_user();
279
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
280
        $class = new \behat_availability_coursecompleted();
281
        $class->i_mark_course_completed_for_user($course->fullname, $user->username);
282
        $this->expectExceptionMessage("A user with username 'otheruser' does not exist");
283
        $class->i_mark_course_completed_for_user($course->fullname, 'otheruser');
284
    }
285
}