MDL-40060 mod_book: Data generator for module and chapters
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* mod_book data generator.
|
||||
*
|
||||
* @package mod_book
|
||||
* @category test
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* mod_book data generator class.
|
||||
*
|
||||
* @package mod_book
|
||||
* @category test
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_book_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* @var int keep track of how many chapters have been created.
|
||||
*/
|
||||
protected $chaptercount = 0;
|
||||
|
||||
/**
|
||||
* To be called from data reset code only,
|
||||
* do not use in tests.
|
||||
* @return void
|
||||
*/
|
||||
public function reset() {
|
||||
$this->chaptercount = 0;
|
||||
parent::reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new book module instance
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/book/locallib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'book') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test book ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->numbering)) {
|
||||
$record->numbering = BOOK_NUM_NUMBERS;
|
||||
}
|
||||
if (!isset($record->customtitles)) {
|
||||
$record->customtitles = 0;
|
||||
}
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = book_add_instance($record, null);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
}
|
||||
|
||||
public function create_chapter($record = null, array $options = null) {
|
||||
global $DB;
|
||||
|
||||
$record = (object) (array) $record;
|
||||
$options = (array) $options;
|
||||
$this->chaptercount++;
|
||||
|
||||
if (empty($record->bookid)) {
|
||||
throw new coding_exception('Chapter generator requires $record->bookid');
|
||||
}
|
||||
|
||||
if (empty($record->title)) {
|
||||
$record->title = "Chapter {$this->chaptercount}";
|
||||
}
|
||||
if (empty($record->pagenum)) {
|
||||
$record->pagenum = 1;
|
||||
}
|
||||
if (!isset($record->subchapter)) {
|
||||
$record->subchapter = 0;
|
||||
}
|
||||
if (!isset($record->hidden)) {
|
||||
$record->hidden = 0;
|
||||
}
|
||||
if (!isset($record->importsrc)) {
|
||||
$record->importsrc = '';
|
||||
}
|
||||
if (!isset($record->content)) {
|
||||
$record->content = "Chapter {$this->chaptercount} content";
|
||||
}
|
||||
if (!isset($record->contentformat)) {
|
||||
$record->contentformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->timecreated)) {
|
||||
$record->timecreated = time();
|
||||
}
|
||||
if (!isset($record->timemodified)) {
|
||||
$record->timemodified = time();
|
||||
}
|
||||
|
||||
// Make room for new page.
|
||||
$sql = "UPDATE {book_chapters}
|
||||
SET pagenum = pagenum + 1
|
||||
WHERE bookid = ? AND pagenum >= ?";
|
||||
$DB->execute($sql, array($record->bookid, $record->pagenum));
|
||||
$record->id = $DB->insert_record('book_chapters', $record);
|
||||
|
||||
$sql = "UPDATE {book}
|
||||
SET revision = revision + 1
|
||||
WHERE id = ?";
|
||||
$DB->execute($sql, array($record->bookid));
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Genarator tests.
|
||||
*
|
||||
* @package mod_book
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
/**
|
||||
* Genarator tests class.
|
||||
*
|
||||
* @package mod_book
|
||||
* @copyright 2013 Frédéric Massart
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_book_generator_testcase extends advanced_testcase {
|
||||
|
||||
public function test_create_instance() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
$this->assertFalse($DB->record_exists('book', array('course' => $course->id)));
|
||||
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
|
||||
$this->assertEquals(1, $DB->count_records('book', array('course' => $course->id)));
|
||||
$this->assertTrue($DB->record_exists('book', array('course' => $course->id, 'id' => $book->id)));
|
||||
|
||||
$params = array('course' => $course->id, 'name' => 'One more book');
|
||||
$book = $this->getDataGenerator()->create_module('book', $params);
|
||||
$this->assertEquals(2, $DB->count_records('book', array('course' => $course->id)));
|
||||
$this->assertEquals('One more book', $DB->get_field_select('book', 'name', 'id = :id', array('id' => $book->id)));
|
||||
}
|
||||
|
||||
public function test_create_chapter() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
|
||||
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
|
||||
|
||||
$this->assertFalse($DB->record_exists('book_chapters', array('bookid' => $book->id)));
|
||||
$bookgenerator->create_chapter(array('bookid' => $book->id));
|
||||
$this->assertTrue($DB->record_exists('book_chapters', array('bookid' => $book->id)));
|
||||
|
||||
$chapter = $bookgenerator->create_chapter(array('bookid' => $book->id, 'content' => 'Yay!', 'title' => 'Oops'));
|
||||
$this->assertEquals(2, $DB->count_records('book_chapters', array('bookid' => $book->id)));
|
||||
$this->assertEquals('Oops', $DB->get_field_select('book_chapters', 'title', 'id = :id', array('id' => $chapter->id)));
|
||||
$this->assertEquals('Yay!', $DB->get_field_select('book_chapters', 'content', 'id = :id', array('id' => $chapter->id)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user