mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
182 lines
6.5 KiB
PHP
182 lines
6.5 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Cohort library tests.
|
|
*
|
|
* @package core_cohort
|
|
* @category phpunit
|
|
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
global $CFG;
|
|
require_once("$CFG->dirroot/cohort/lib.php");
|
|
|
|
|
|
/**
|
|
* Cohort library tests.
|
|
*
|
|
* @package core_cohort
|
|
* @category phpunit
|
|
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class cohort_testcase extends advanced_testcase {
|
|
|
|
public function test_cohort_add_cohort() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = new stdClass();
|
|
$cohort->contextid = context_system::instance()->id;
|
|
$cohort->name = 'test cohort';
|
|
$cohort->idnumber = 'testid';
|
|
$cohort->description = 'test cohort desc';
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
|
|
|
$id = cohort_add_cohort($cohort);
|
|
$this->assertNotEmpty($id);
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$id));
|
|
$this->assertEquals($cohort->contextid, $newcohort->contextid);
|
|
$this->assertSame($cohort->name, $newcohort->name);
|
|
$this->assertSame($cohort->description, $newcohort->description);
|
|
$this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat);
|
|
$this->assertNotEmpty($newcohort->timecreated);
|
|
$this->assertSame($newcohort->component, '');
|
|
$this->assertSame($newcohort->timecreated, $newcohort->timemodified);
|
|
|
|
try {
|
|
$cohort = new stdClass();
|
|
$cohort->contextid = context_system::instance()->id;
|
|
$cohort->name = null;
|
|
$cohort->idnumber = 'testid';
|
|
$cohort->description = 'test cohort desc';
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
|
cohort_add_cohort($cohort);
|
|
|
|
$this->fail('Exception expected when trying to add cohort without name');
|
|
} catch (Exception $e) {
|
|
$this->assertInstanceOf('coding_exception', $e);
|
|
}
|
|
}
|
|
|
|
public function test_cohort_update_cohort() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = new stdClass();
|
|
$cohort->contextid = context_system::instance()->id;
|
|
$cohort->name = 'test cohort';
|
|
$cohort->idnumber = 'testid';
|
|
$cohort->description = 'test cohort desc';
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
|
$id = cohort_add_cohort($cohort);
|
|
$this->assertNotEmpty($id);
|
|
$DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id));
|
|
$DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id));
|
|
$cohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
$cohort->name = 'test cohort 2';
|
|
cohort_update_cohort($cohort);
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
$this->assertSame($cohort->contextid, $newcohort->contextid);
|
|
$this->assertSame($cohort->name, $newcohort->name);
|
|
$this->assertSame($cohort->description, $newcohort->description);
|
|
$this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat);
|
|
$this->assertSame($cohort->timecreated, $newcohort->timecreated);
|
|
$this->assertSame($cohort->component, $newcohort->component);
|
|
$this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified);
|
|
$this->assertLessThanOrEqual(time(), $newcohort->timemodified);
|
|
}
|
|
|
|
public function test_cohort_delete_cohort() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
cohort_delete_cohort($cohort);
|
|
|
|
$this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id)));
|
|
}
|
|
|
|
public function test_cohort_delete_category() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$category = $this->getDataGenerator()->create_category();
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category->id)->id));
|
|
|
|
cohort_delete_category($category);
|
|
|
|
$this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id)));
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$cohort->id));
|
|
$this->assertEquals(context_system::instance()->id, $newcohort->contextid);
|
|
}
|
|
|
|
public function test_cohort_add_member() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
cohort_add_member($cohort->id, $user->id);
|
|
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
}
|
|
|
|
public function test_cohort_remove_member() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
|
|
cohort_remove_member($cohort->id, $user->id);
|
|
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
}
|
|
|
|
public function test_cohort_is_member() {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
$this->assertFalse(cohort_is_member($cohort->id, $user->id));
|
|
cohort_add_member($cohort->id, $user->id);
|
|
$this->assertTrue(cohort_is_member($cohort->id, $user->id));
|
|
}
|
|
}
|