Merge branch 'MDL-39054' of git://github.com/jacks92/moodle
[moodle.git] / cohort / tests / cohortlib_test.php
CommitLineData
4729332b
PS
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 * Cohort library tests.
19 *
20 * @package core_cohort
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once("$CFG->dirroot/cohort/lib.php");
30
31
32/**
33 * Cohort library tests.
34 *
35 * @package core_cohort
36 * @category phpunit
37 * @copyright 2012 Petr Skoda {@link http://skodak.org}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class cohort_testcase extends advanced_testcase {
41
42 public function test_cohort_add_cohort() {
43 global $DB;
44
45 $this->resetAfterTest();
46
47 $cohort = new stdClass();
48 $cohort->contextid = context_system::instance()->id;
49 $cohort->name = 'test cohort';
50 $cohort->idnumber = 'testid';
51 $cohort->description = 'test cohort desc';
52 $cohort->descriptionformat = FORMAT_HTML;
53
54 $id = cohort_add_cohort($cohort);
55 $this->assertNotEmpty($id);
56
57 $newcohort = $DB->get_record('cohort', array('id'=>$id));
58 $this->assertEquals($cohort->contextid, $newcohort->contextid);
59 $this->assertSame($cohort->name, $newcohort->name);
60 $this->assertSame($cohort->description, $newcohort->description);
61 $this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat);
62 $this->assertNotEmpty($newcohort->timecreated);
63 $this->assertSame($newcohort->component, '');
64 $this->assertSame($newcohort->timecreated, $newcohort->timemodified);
65
66 try {
67 $cohort = new stdClass();
68 $cohort->contextid = context_system::instance()->id;
69 $cohort->name = null;
70 $cohort->idnumber = 'testid';
71 $cohort->description = 'test cohort desc';
72 $cohort->descriptionformat = FORMAT_HTML;
73 cohort_add_cohort($cohort);
74
75 $this->fail('Exception expected when trying to add cohort without name');
76 } catch (Exception $e) {
77 $this->assertInstanceOf('coding_exception', $e);
78 }
79 }
80
81 public function test_cohort_update_cohort() {
82 global $DB;
83
84 $this->resetAfterTest();
85
86 $cohort = new stdClass();
87 $cohort->contextid = context_system::instance()->id;
88 $cohort->name = 'test cohort';
89 $cohort->idnumber = 'testid';
90 $cohort->description = 'test cohort desc';
91 $cohort->descriptionformat = FORMAT_HTML;
92 $id = cohort_add_cohort($cohort);
93 $this->assertNotEmpty($id);
94 $DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id));
95 $DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id));
96 $cohort = $DB->get_record('cohort', array('id'=>$id));
97
98 $cohort->name = 'test cohort 2';
99 cohort_update_cohort($cohort);
100
101 $newcohort = $DB->get_record('cohort', array('id'=>$id));
102
103 $this->assertSame($cohort->contextid, $newcohort->contextid);
104 $this->assertSame($cohort->name, $newcohort->name);
105 $this->assertSame($cohort->description, $newcohort->description);
106 $this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat);
107 $this->assertSame($cohort->timecreated, $newcohort->timecreated);
108 $this->assertSame($cohort->component, $newcohort->component);
109 $this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified);
110 $this->assertLessThanOrEqual(time(), $newcohort->timemodified);
111 }
112
113 public function test_cohort_delete_cohort() {
114 global $DB;
115
116 $this->resetAfterTest();
117
118 $cohort = $this->getDataGenerator()->create_cohort();
119
120 cohort_delete_cohort($cohort);
121
122 $this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id)));
123 }
124
125 public function test_cohort_delete_category() {
126 global $DB;
127
128 $this->resetAfterTest();
129
130 $category = $this->getDataGenerator()->create_category();
131
132 $cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category->id)->id));
133
134 cohort_delete_category($category);
135
136 $this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id)));
137 $newcohort = $DB->get_record('cohort', array('id'=>$cohort->id));
138 $this->assertEquals(context_system::instance()->id, $newcohort->contextid);
139 }
140
141 public function test_cohort_add_member() {
142 global $DB;
143
144 $this->resetAfterTest();
145
146 $cohort = $this->getDataGenerator()->create_cohort();
147 $user = $this->getDataGenerator()->create_user();
148
149 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
150 cohort_add_member($cohort->id, $user->id);
151 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
152 }
153
154 public function test_cohort_remove_member() {
155 global $DB;
156
157 $this->resetAfterTest();
158
159 $cohort = $this->getDataGenerator()->create_cohort();
160 $user = $this->getDataGenerator()->create_user();
161
162 cohort_add_member($cohort->id, $user->id);
163 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
164
165 cohort_remove_member($cohort->id, $user->id);
166 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
167 }
168
169 public function test_cohort_is_member() {
170 global $DB;
171
172 $this->resetAfterTest();
173
174 $cohort = $this->getDataGenerator()->create_cohort();
175 $user = $this->getDataGenerator()->create_user();
176
177 $this->assertFalse(cohort_is_member($cohort->id, $user->id));
178 cohort_add_member($cohort->id, $user->id);
179 $this->assertTrue(cohort_is_member($cohort->id, $user->id));
180 }
a09ae434
PS
181
182 public function test_cohort_get_visible_list() {
183 global $DB;
184
185 $this->resetAfterTest();
186
187 $category1 = $this->getDataGenerator()->create_category();
188 $category2 = $this->getDataGenerator()->create_category();
189
190 $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
191 $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
192 $course3 = $this->getDataGenerator()->create_course();
193
194 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id));
195 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category2->id)->id));
196 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
197 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
198
199 $user1 = $this->getDataGenerator()->create_user();
200 $user2 = $this->getDataGenerator()->create_user();
201 $user3 = $this->getDataGenerator()->create_user();
202 $user4 = $this->getDataGenerator()->create_user();
203 $user5 = $this->getDataGenerator()->create_user();
204
205 $manualenrol = enrol_get_plugin('manual');
206 $enrol1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'));
207 $enrol2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'));
208
209 $manualenrol->enrol_user($enrol1, $user1->id);
210 $manualenrol->enrol_user($enrol1, $user3->id);
211 $manualenrol->enrol_user($enrol1, $user4->id);
212 $manualenrol->enrol_user($enrol2, $user2->id);
213
214 cohort_add_member($cohort1->id, $user1->id);
215 cohort_add_member($cohort3->id, $user1->id);
216 cohort_add_member($cohort1->id, $user3->id);
217 cohort_add_member($cohort2->id, $user2->id);
218
219 $list = cohort_get_visible_list($course1);
220 $this->assertEquals(2, count($list));
221 $this->assertNotEmpty($list[$cohort1->id]);
222 $this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
223 $this->assertNotEmpty($list[$cohort3->id]);
224 $this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
225
226 $list = cohort_get_visible_list($course1, false);
227 $this->assertEquals(3, count($list));
228 $this->assertNotEmpty($list[$cohort1->id]);
229 $this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
230 $this->assertNotEmpty($list[$cohort3->id]);
231 $this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
232 $this->assertNotEmpty($list[$cohort4->id]);
233 $this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
234
235 $list = cohort_get_visible_list($course2);
236 $this->assertEquals(1, count($list));
237 $this->assertNotEmpty($list[$cohort2->id]);
238 $this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
239
240 $list = cohort_get_visible_list($course2, false);
241 $this->assertEquals(3, count($list));
242 $this->assertNotEmpty($list[$cohort2->id]);
243 $this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
244 $this->assertNotEmpty($list[$cohort3->id]);
245 $this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
246 $this->assertNotEmpty($list[$cohort4->id]);
247 $this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
248
249 $list = cohort_get_visible_list($course3);
250 $this->assertEquals(0, count($list));
251
252 $list = cohort_get_visible_list($course3, false);
253 $this->assertEquals(2, count($list));
254 $this->assertNotEmpty($list[$cohort3->id]);
255 $this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
256 $this->assertNotEmpty($list[$cohort4->id]);
257 $this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
258 }
aad6e655
PS
259
260 public function test_cohort_get_cohorts() {
261 global $DB;
262
263 $this->resetAfterTest();
264
265 $category1 = $this->getDataGenerator()->create_category();
266 $category2 = $this->getDataGenerator()->create_category();
267
268 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>''));
269 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr'));
960931d9 270 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us'));
aad6e655
PS
271 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
272
273 $result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id);
274 $this->assertEquals(0, $result['totalcohorts']);
275 $this->assertEquals(0, count($result['cohorts']));
9a2f6770 276 $this->assertEquals(0, $result['allcohorts']);
aad6e655
PS
277
278 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id);
279 $this->assertEquals(3, $result['totalcohorts']);
280 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3), $result['cohorts']);
9a2f6770 281 $this->assertEquals(3, $result['allcohorts']);
aad6e655
PS
282
283 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh');
284 $this->assertEquals(1, $result['totalcohorts']);
285 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']);
9a2f6770 286 $this->assertEquals(3, $result['allcohorts']);
aad6e655
PS
287
288 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'brrr');
289 $this->assertEquals(1, $result['totalcohorts']);
290 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
9a2f6770 291 $this->assertEquals(3, $result['allcohorts']);
aad6e655
PS
292
293 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'grrr');
294 $this->assertEquals(1, $result['totalcohorts']);
295 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
9a2f6770 296 $this->assertEquals(3, $result['allcohorts']);
aad6e655
PS
297
298 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy');
299 $this->assertEquals(3, $result['totalcohorts']);
300 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
9a2f6770 301 $this->assertEquals(3, $result['allcohorts']);
960931d9
PS
302
303 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'po_us');
304 $this->assertEquals(1, $result['totalcohorts']);
305 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']);
306 $this->assertEquals(3, $result['allcohorts']);
307
308 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'pokus');
309 $this->assertEquals(0, $result['totalcohorts']);
310 $this->assertEquals(array(), $result['cohorts']);
311 $this->assertEquals(3, $result['allcohorts']);
aad6e655 312 }
4729332b 313}