From 7abaa052b49a0e6027ee71ecc12d3b4e4e0e15fe Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 1 Oct 2013 13:43:31 +0800 Subject: [PATCH] MDL-40060 mod_book: Data generator for module and chapters --- mod/book/tests/generator/lib.php | 147 ++++++++++++++++++++++++++++++ mod/book/tests/generator_test.php | 71 +++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 mod/book/tests/generator/lib.php create mode 100644 mod/book/tests/generator_test.php diff --git a/mod/book/tests/generator/lib.php b/mod/book/tests/generator/lib.php new file mode 100644 index 00000000000..f0361a3e021 --- /dev/null +++ b/mod/book/tests/generator/lib.php @@ -0,0 +1,147 @@ +. + +/** + * 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; + } + +} diff --git a/mod/book/tests/generator_test.php b/mod/book/tests/generator_test.php new file mode 100644 index 00000000000..75ab59b76ea --- /dev/null +++ b/mod/book/tests/generator_test.php @@ -0,0 +1,71 @@ +. + +/** + * 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))); + } + +}