MDL-32149 courselib tests and support for generation of course sections

This commit is contained in:
Petr Skoda 2012-04-03 10:24:45 +02:00
parent 4b02743182
commit 354b214c9f
4 changed files with 157 additions and 2 deletions

View File

@ -0,0 +1,99 @@
<?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/>.
/**
* Course related unit tests
*
* @package core
* @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();
class courselib_testcase extends advanced_testcase {
public function test_reorder_sections() {
global $DB;
$this->resetAfterTest(true);
$this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
$course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
$oldsections = array();
$sections = array();
foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
$oldsections[$section->section] = $section->id;
$sections[$section->id] = $section->section;
}
ksort($oldsections);
$neworder = reorder_sections($sections, 2, 4);
$neworder = array_keys($neworder);
$this->assertEquals($oldsections[0], $neworder[0]);
$this->assertEquals($oldsections[1], $neworder[1]);
$this->assertEquals($oldsections[2], $neworder[4]);
$this->assertEquals($oldsections[3], $neworder[2]);
$this->assertEquals($oldsections[4], $neworder[3]);
$this->assertEquals($oldsections[5], $neworder[5]);
$this->assertEquals($oldsections[6], $neworder[6]);
$neworder = reorder_sections(1, 2, 4);
$this->assertFalse($neworder);
}
public function test_move_section() {
global $DB;
$this->resetAfterTest(true);
$this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
$course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
$oldsections = array();
foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
$oldsections[$section->section] = $section->id;
}
ksort($oldsections);
move_section_to($course, 2, 4);
$sections = array();
foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
$sections[$section->section] = $section->id;
}
ksort($sections);
$this->assertEquals($oldsections[0], $sections[0]);
$this->assertEquals($oldsections[1], $sections[1]);
$this->assertEquals($oldsections[2], $sections[4]);
$this->assertEquals($oldsections[3], $sections[2]);
$this->assertEquals($oldsections[4], $sections[3]);
$this->assertEquals($oldsections[5], $sections[5]);
$this->assertEquals($oldsections[6], $sections[6]);
}
public function test_get_course_display_name_for_list() {
global $CFG;
$this->resetAfterTest(true);
$course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
$CFG->courselistshortnames = 0;
$this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
$CFG->courselistshortnames = 1;
$this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
}
}

View File

@ -195,7 +195,8 @@ class phpunit_data_generator {
/**
* Create a test course
* @param array|stdClass $record
* @param array $options
* @param array $options with keys:
* 'createsections'=>bool precreate all sections
* @return stdClass course record
*/
function create_course($record=null, array $options=null) {
@ -246,9 +247,62 @@ class phpunit_data_generator {
$course = create_course((object)$record);
context_course::instance($course->id);
if (!empty($options['createsections'])) {
for($i=1; $i<$record['numsections']; $i++) {
self::create_course_section(array('course'=>$course->id, 'section'=>$i));
}
}
return $course;
}
/**
* Create course section if does not exist yet
* @param mixed $record
* @param array|null $options
* @return stdClass
* @throws coding_exception
*/
public function create_course_section($record = null, array $options = null) {
global $DB;
$record = (array)$record;
if (empty($record['course'])) {
throw new coding_exception('course must be present in phpunit_util::create_course_section() $record');
}
if (!isset($record['section'])) {
throw new coding_exception('section must be present in phpunit_util::create_course_section() $record');
}
if (!isset($record['name'])) {
$record['name'] = '';
}
if (!isset($record['summary'])) {
$record['summary'] = '';
}
if (!isset($record['summaryformat'])) {
$record['summaryformat'] = FORMAT_MOODLE;
}
if ($section = $DB->get_record('course_sections', array('course'=>$record['course'], 'section'=>$record['section']))) {
return $section;
}
$section = new stdClass();
$section->course = $record['course'];
$section->section = $record['section'];
$section->name = $record['name'];
$section->summary = $record['summary'];
$section->summaryformat = $record['summaryformat'];
$id = $DB->insert_record('course_sections', $section);
return $DB->get_record('course_sections', array('id'=>$id));
}
/**
* Create a test block
* @param string $blockname

View File

@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

View File

@ -35,6 +35,9 @@
<testsuite name="core_blog">
<directory suffix="_test.php">blog/tests</directory>
</testsuite>
<testsuite name="core_course">
<directory suffix="_test.php">course/tests</directory>
</testsuite>
<!--Plugin suites: use admin/tool/phpunit/cli/util.php to build phpunit.xml from phpunit.xml.dist with up-to-date list of plugins in current install-->
<!--@plugin_suites_start@-->