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 |
* Basic 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, frontend};
|
|
|
30 |
use completion_info;
|
|
|
31 |
use core_availability\{tree, info_module, mock_info, mock_condition};
|
|
|
32 |
use core_completion;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Bare tests for the coursecompleted condition.
|
|
|
36 |
*
|
|
|
37 |
* @package availability_coursecompleted
|
|
|
38 |
* @copyright 2017 iplusacademy (www.iplusacademy.org)
|
|
|
39 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
* @coversDefaultClass \availability_coursecompleted
|
|
|
42 |
*/
|
|
|
43 |
final class basic_test extends \basic_testcase {
|
|
|
44 |
/**
|
|
|
45 |
* Tests the constructor including error conditions.
|
|
|
46 |
* @covers \availability_coursecompleted\condition
|
|
|
47 |
*/
|
|
|
48 |
public function test_constructor(): void {
|
|
|
49 |
// This works with no parameters.
|
|
|
50 |
$structure = (object)[];
|
|
|
51 |
try {
|
|
|
52 |
$completed = new condition($structure);
|
|
|
53 |
$this->fail();
|
|
|
54 |
} catch (\exception $e) {
|
|
|
55 |
$this->assertEquals('', $e->getMessage());
|
|
|
56 |
}
|
|
|
57 |
$this->assertNotEmpty($completed);
|
|
|
58 |
|
|
|
59 |
// This works with '1'.
|
|
|
60 |
$structure->id = '1';
|
|
|
61 |
try {
|
|
|
62 |
$completed = new condition($structure);
|
|
|
63 |
$this->fail();
|
|
|
64 |
} catch (\exception $e) {
|
|
|
65 |
$this->assertEquals('', $e->getMessage());
|
|
|
66 |
}
|
|
|
67 |
$this->assertNotEmpty($completed);
|
|
|
68 |
|
|
|
69 |
// This works with '0'.
|
|
|
70 |
$structure->id = '0';
|
|
|
71 |
try {
|
|
|
72 |
$completed = new condition($structure);
|
|
|
73 |
$this->fail();
|
|
|
74 |
} catch (\exception $e) {
|
|
|
75 |
$this->assertEquals('', $e->getMessage());
|
|
|
76 |
}
|
|
|
77 |
$this->assertNotEmpty($completed);
|
|
|
78 |
|
|
|
79 |
// This fails with null.
|
|
|
80 |
$structure->id = null;
|
|
|
81 |
try {
|
|
|
82 |
$completed = new condition($structure);
|
|
|
83 |
} catch (\coding_exception $e) {
|
|
|
84 |
$this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Invalid ->id.
|
|
|
88 |
$structure->id = false;
|
|
|
89 |
try {
|
|
|
90 |
$completed = new condition($structure);
|
|
|
91 |
} catch (\coding_exception $e) {
|
|
|
92 |
$this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Invalid string. Should be checked 'longer string'.
|
|
|
96 |
$structure->id = 1;
|
|
|
97 |
try {
|
|
|
98 |
$completed = new condition($structure);
|
|
|
99 |
} catch (\coding_exception $e) {
|
|
|
100 |
$this->assertStringContainsString('Invalid value for course completed condition', $e->getMessage());
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Tests the save() function.
|
|
|
106 |
* @covers \availability_coursecompleted\condition
|
|
|
107 |
*/
|
|
|
108 |
public function test_save(): void {
|
|
|
109 |
$structure = (object)['id' => '1'];
|
|
|
110 |
$cond = new condition($structure);
|
|
|
111 |
$structure->type = 'coursecompleted';
|
|
|
112 |
$this->assertEquals($structure, $cond->save());
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Tests json.
|
|
|
117 |
* @covers \availability_coursecompleted\condition
|
|
|
118 |
*/
|
|
|
119 |
public function test_json(): void {
|
|
|
120 |
$this->assertEqualsCanonicalizing((object)['type' => 'coursecompleted', 'id' => '3'], condition::get_json('3'));
|
|
|
121 |
$this->assertEqualsCanonicalizing((object)['type' => 'coursecompleted', 'id' => '0'], condition::get_json('0'));
|
|
|
122 |
}
|
|
|
123 |
}
|