Merge branch 'MDL-39054' of git://github.com/jacks92/moodle
[moodle.git] / cohort / tests / cohortlib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once("$CFG->dirroot/cohort/lib.php");
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  */
40 class cohort_testcase extends advanced_testcase {
42     public function test_cohort_add_cohort() {
43         global $DB;
45         $this->resetAfterTest();
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;
54         $id = cohort_add_cohort($cohort);
55         $this->assertNotEmpty($id);
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);
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);
75             $this->fail('Exception expected when trying to add cohort without name');
76         } catch (Exception $e) {
77             $this->assertInstanceOf('coding_exception', $e);
78         }
79     }
81     public function test_cohort_update_cohort() {
82         global $DB;
84         $this->resetAfterTest();
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));
98         $cohort->name = 'test cohort 2';
99         cohort_update_cohort($cohort);
101         $newcohort = $DB->get_record('cohort', array('id'=>$id));
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     }
113     public function test_cohort_delete_cohort() {
114         global $DB;
116         $this->resetAfterTest();
118         $cohort = $this->getDataGenerator()->create_cohort();
120         cohort_delete_cohort($cohort);
122         $this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id)));
123     }
125     public function test_cohort_delete_category() {
126         global $DB;
128         $this->resetAfterTest();
130         $category = $this->getDataGenerator()->create_category();
132         $cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category->id)->id));
134         cohort_delete_category($category);
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     }
141     public function test_cohort_add_member() {
142         global $DB;
144         $this->resetAfterTest();
146         $cohort = $this->getDataGenerator()->create_cohort();
147         $user = $this->getDataGenerator()->create_user();
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     }
154     public function test_cohort_remove_member() {
155         global $DB;
157         $this->resetAfterTest();
159         $cohort = $this->getDataGenerator()->create_cohort();
160         $user = $this->getDataGenerator()->create_user();
162         cohort_add_member($cohort->id, $user->id);
163         $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
165         cohort_remove_member($cohort->id, $user->id);
166         $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
167     }
169     public function test_cohort_is_member() {
170         global $DB;
172         $this->resetAfterTest();
174         $cohort = $this->getDataGenerator()->create_cohort();
175         $user = $this->getDataGenerator()->create_user();
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     }
182     public function test_cohort_get_visible_list() {
183         global $DB;
185         $this->resetAfterTest();
187         $category1 = $this->getDataGenerator()->create_category();
188         $category2 = $this->getDataGenerator()->create_category();
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();
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));
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();
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'));
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);
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);
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]);
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]);
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]);
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]);
249         $list = cohort_get_visible_list($course3);
250         $this->assertEquals(0, count($list));
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     }
260     public function test_cohort_get_cohorts() {
261         global $DB;
263         $this->resetAfterTest();
265         $category1 = $this->getDataGenerator()->create_category();
266         $category2 = $this->getDataGenerator()->create_category();
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'));
270         $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us'));
271         $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
273         $result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id);
274         $this->assertEquals(0, $result['totalcohorts']);
275         $this->assertEquals(0, count($result['cohorts']));
276         $this->assertEquals(0, $result['allcohorts']);
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']);
281         $this->assertEquals(3, $result['allcohorts']);
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']);
286         $this->assertEquals(3, $result['allcohorts']);
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']);
291         $this->assertEquals(3, $result['allcohorts']);
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']);
296         $this->assertEquals(3, $result['allcohorts']);
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']);
301         $this->assertEquals(3, $result['allcohorts']);
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']);
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']);
312     }