diff --git a/cohort/tests/cohortlib_test.php b/cohort/tests/cohortlib_test.php new file mode 100644 index 00000000000..82e9520d8d5 --- /dev/null +++ b/cohort/tests/cohortlib_test.php @@ -0,0 +1,181 @@ +. + +/** + * 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)); + } +} diff --git a/lib/phpunit/classes/data_generator.php b/lib/phpunit/classes/data_generator.php index 8225f236761..515f062ee24 100644 --- a/lib/phpunit/classes/data_generator.php +++ b/lib/phpunit/classes/data_generator.php @@ -36,6 +36,7 @@ class phpunit_data_generator { protected $usercounter = 0; protected $categorycount = 0; + protected $cohortcount = 0; protected $coursecount = 0; protected $scalecount = 0; protected $groupcount = 0; @@ -218,7 +219,7 @@ EOD; * @param array $options * @return stdClass course category record */ - function create_category($record=null, array $options=null) { + public function create_category($record=null, array $options=null) { global $DB, $CFG; require_once("$CFG->dirroot/course/lib.php"); @@ -270,6 +271,50 @@ EOD; return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST); } + /** + * Create test cohort. + * @param array|stdClass $record + * @param array $options + * @return stdClass cohort record + */ + public function create_cohort($record=null, array $options=null) { + global $DB, $CFG; + require_once("$CFG->dirroot/cohort/lib.php"); + + $this->cohortcount++; + $i = $this->cohortcount; + + $record = (array)$record; + + if (!isset($record['contextid'])) { + $record['contextid'] = context_system::instance()->id; + } + + if (!isset($record['name'])) { + $record['name'] = 'Cohort '.$i; + } + + if (!isset($record['idnumber'])) { + $record['idnumber'] = ''; + } + + if (!isset($record['description'])) { + $record['description'] = "Test cohort $i\n$this->loremipsum"; + } + + if (!isset($record['descriptionformat'])) { + $record['descriptionformat'] = FORMAT_MOODLE; + } + + if (!isset($record['component'])) { + $record['component'] = ''; + } + + $id = cohort_add_cohort((object)$record); + + return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST); + } + /** * Create a test course * @param array|stdClass $record @@ -277,7 +322,7 @@ EOD; * 'createsections'=>bool precreate all sections * @return stdClass course record */ - function create_course($record=null, array $options=null) { + public function create_course($record=null, array $options=null) { global $DB, $CFG; require_once("$CFG->dirroot/course/lib.php"); diff --git a/lib/phpunit/tests/generator_test.php b/lib/phpunit/tests/generator_test.php index 316ee706561..9c6f4f6bbae 100644 --- a/lib/phpunit/tests/generator_test.php +++ b/lib/phpunit/tests/generator_test.php @@ -53,6 +53,18 @@ class core_phpunit_generator_testcase extends advanced_testcase { $this->assertRegExp('/^Test course category \d/', $category->description); $this->assertSame(FORMAT_MOODLE, $category->descriptionformat); + $count = $DB->count_records('cohort'); + $cohort = $generator->create_cohort(); + $this->assertEquals($count+1, $DB->count_records('cohort')); + $this->assertEquals(context_system::instance()->id, $cohort->contextid); + $this->assertRegExp('/^Cohort \d/', $cohort->name); + $this->assertSame('', $cohort->idnumber); + $this->assertRegExp('/^Test cohort \d/', $cohort->description); + $this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat); + $this->assertSame('', $cohort->component); + $this->assertLessThanOrEqual(time(), $cohort->timecreated); + $this->assertSame($cohort->timecreated, $cohort->timemodified); + $count = $DB->count_records('course'); $course = $generator->create_course(); $this->assertEquals($count+1, $DB->count_records('course')); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1e65824d203..cf5e58a2a9b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -39,6 +39,9 @@ lib/filestorage/tests + + cohort/tests + lib/grade/tests grade/tests