diff --git a/mod/lesson/tests/generator/lib.php b/mod/lesson/tests/generator/lib.php new file mode 100644 index 00000000000..4ad19499b08 --- /dev/null +++ b/mod/lesson/tests/generator/lib.php @@ -0,0 +1,117 @@ +. + +/** + * mod_lesson data generator. + * + * @package mod_lesson + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_lesson data generator class. + * + * @package mod_lesson + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_lesson_generator extends testing_module_generator { + + /** + * @var int keep track of how many pages have been created. + */ + protected $pagecount = 0; + + /** + * To be called from data reset code only, + * do not use in tests. + * @return void + */ + public function reset() { + $this->pagecount = 0; + parent::reset(); + } + + public function create_instance($record = null, array $options = null) { + global $CFG; + + // Add default values for lesson. + $record = (array)$record + array( + 'progressbar' => 0, + 'ongoing' => 0, + 'displayleft' => 0, + 'displayleftif' => 0, + 'slideshow' => 0, + 'maxanswers' => $CFG->lesson_maxanswers, + 'feedback' => 0, + 'activitylink' => 0, + 'available' => 0, + 'deadline' => 0, + 'usepassword' => 0, + 'password' => '', + 'dependency' => 0, + 'timespent' => 0, + 'completed' => 0, + 'gradebetterthan' => 0, + 'modattempts' => 0, + 'review' => 0, + 'maxattempts' => 1, + 'nextpagedefault' => $CFG->lesson_defaultnextpage, + 'maxpages' => 0, + 'practice' => 0, + 'custom' => 1, + 'retake' => 0, + 'usemaxgrade' => 0, + 'minquestions' => 0, + 'grade' => 100, + ); + if (!isset($record['mediafile'])) { + require_once($CFG->libdir.'/filelib.php'); + $record['mediafile'] = file_get_unused_draft_itemid(); + } + + return parent::create_instance($record, (array)$options); + } + + public function create_content($lesson, $record = array()) { + global $DB, $CFG; + require_once($CFG->dirroot.'/mod/lesson/locallib.php'); + $now = time(); + $this->pagecount++; + $record = (array)$record + array( + 'lessonid' => $lesson->id, + 'title' => 'Lesson page '.$this->pagecount, + 'timecreated' => $now, + 'qtype' => 20, // LESSON_PAGE_BRANCHTABLE + 'pageid' => 0, // By default insert in the beginning. + ); + if (!isset($record['contents_editor'])) { + $record['contents_editor'] = array( + 'text' => 'Contents of lesson page '.$this->pagecount, + 'format' => FORMAT_MOODLE, + 'itemid' => 0, + ); + } + $context = context_module::instance($lesson->cmid); + $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes); + return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST); + } +} diff --git a/mod/lesson/tests/generator_test.php b/mod/lesson/tests/generator_test.php new file mode 100644 index 00000000000..41725a8f6c8 --- /dev/null +++ b/mod/lesson/tests/generator_test.php @@ -0,0 +1,73 @@ +. + +/** + * mod_lesson generator tests + * + * @package mod_lesson + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Genarator tests class for mod_lesson. + * + * @package mod_lesson + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_lesson_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('lesson', array('course' => $course->id))); + $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course)); + $records = $DB->get_records('lesson', array('course' => $course->id), 'id'); + $this->assertEquals(1, count($records)); + $this->assertTrue(array_key_exists($lesson->id, $records)); + + $params = array('course' => $course->id, 'name' => 'Another lesson'); + $lesson = $this->getDataGenerator()->create_module('lesson', $params); + $records = $DB->get_records('lesson', array('course' => $course->id), 'id'); + $this->assertEquals(2, count($records)); + $this->assertEquals('Another lesson', $records[$lesson->id]->name); + } + + public function test_create_content() { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course)); + $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + + $page1 = $lessongenerator->create_content($lesson); + $page2 = $lessongenerator->create_content($lesson, array('title' => 'Custom title')); + $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id'); + $this->assertEquals(2, count($records)); + $this->assertEquals($page1->id, $records[$page1->id]->id); + $this->assertEquals($page2->id, $records[$page2->id]->id); + $this->assertEquals('Custom title', $records[$page2->id]->title); + } +}