Merge branch 'MDL-49178_m29v2' of https://github.com/sbourget/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 17 Feb 2015 20:14:50 +0000 (21:14 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 17 Feb 2015 20:14:50 +0000 (21:14 +0100)
mod/lesson/tests/generator/lib.php
mod/lesson/tests/generator_test.php

index 4ad1949..0bad1af 100644 (file)
@@ -114,4 +114,315 @@ class mod_lesson_generator extends testing_module_generator {
         $page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
         return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
     }
+
+    /**
+     * Create True/false question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_truefalse($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 TF question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 2,  // LESSON_PAGE_TRUEFALSE.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'The answer is TRUE '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+
+        // First Answer (TRUE).
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => 'TRUE answer for '.$this->pagecount,
+                'format' => FORMAT_HTML
+            );
+        }
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+
+        // Second Answer (FALSE).
+        if (!isset($record['answer_editor'][1])) {
+            $record['answer_editor'][1] = array(
+                'text' => 'FALSE answer for '.$this->pagecount,
+                'format' => FORMAT_HTML
+            );
+        }
+        if (!isset($record['jumpto'][1])) {
+            $record['jumpto'][1] = LESSON_THISPAGE;
+        }
+
+        $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);
+    }
+
+    /**
+     * Create multichoice question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_multichoice($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 multichoice question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 3,  // LESSON_PAGE_MULTICHOICE.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'Pick the correct answer '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+
+        // First Answer (correct).
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => 'correct answer for '.$this->pagecount,
+                'format' => FORMAT_HTML
+            );
+        }
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+
+        // Second Answer (incorrect).
+        if (!isset($record['answer_editor'][1])) {
+            $record['answer_editor'][1] = array(
+                'text' => 'correct answer for '.$this->pagecount,
+                'format' => FORMAT_HTML
+            );
+        }
+        if (!isset($record['jumpto'][1])) {
+            $record['jumpto'][1] = LESSON_THISPAGE;
+        }
+
+        $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);
+    }
+
+    /**
+     * Create essay question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_essay($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 Essay question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 10, // LESSON_PAGE_ESSAY.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'Write an Essay '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+
+        // Essays have an answer of NULL.
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => null,
+                'format' => FORMAT_MOODLE
+            );
+        }
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+
+        $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);
+    }
+
+    /**
+     * Create matching question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_matching($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 Matching question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 5,  // LESSON_PAGE_MATCHING.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'Match the values '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+        // Feedback for correct result.
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => '',
+                'format' => FORMAT_HTML
+            );
+        }
+        // Feedback for wrong result.
+        if (!isset($record['answer_editor'][1])) {
+            $record['answer_editor'][1] = array(
+                'text' => '',
+                'format' => FORMAT_HTML
+            );
+        }
+        // First answer value.
+        if (!isset($record['answer_editor'][2])) {
+            $record['answer_editor'][2] = array(
+                'text' => 'Match value 1',
+                'format' => FORMAT_HTML
+            );
+        }
+        // First response value.
+        if (!isset($record['response_editor'][2])) {
+            $record['response_editor'][2] = 'Match answer 1';
+        }
+        // Second Matching value.
+        if (!isset($record['answer_editor'][3])) {
+            $record['answer_editor'][3] = array(
+                'text' => 'Match value 2',
+                'format' => FORMAT_HTML
+            );
+        }
+        // Second Matching answer.
+        if (!isset($record['response_editor'][3])) {
+            $record['response_editor'][3] = 'Match answer 2';
+        }
+
+        // Jump Values.
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+        if (!isset($record['jumpto'][1])) {
+            $record['jumpto'][1] = LESSON_THISPAGE;
+        }
+
+        // Mark the correct values.
+        if (!isset($record['score'][0])) {
+            $record['score'][0] = 1;
+        }
+        $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);
+    }
+
+    /**
+     * Create shortanswer question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_shortanswer($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 Shortanswer question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 1,  // LESSON_PAGE_SHORTANSWER.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'Fill in the blank '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+
+        // First Answer (correct).
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => 'answer'.$this->pagecount,
+                'format' => FORMAT_MOODLE
+            );
+        }
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+
+        $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);
+    }
+
+    /**
+     * Create shortanswer question pages.
+     * @param object $lesson
+     * @param array $record
+     * @return int
+     */
+    public function create_question_numeric($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 numerical question '.$this->pagecount,
+            'timecreated' => $now,
+            'qtype' => 8,  // LESSON_PAGE_NUMERICAL.
+            'pageid' => 0, // By default insert in the beginning.
+        );
+        if (!isset($record['contents_editor'])) {
+            $record['contents_editor'] = array(
+                'text' => 'Numerical question '.$this->pagecount,
+                'format' => FORMAT_HTML,
+                'itemid' => 0
+            );
+        }
+
+        // First Answer (correct).
+        if (!isset($record['answer_editor'][0])) {
+            $record['answer_editor'][0] = array(
+                'text' => $this->pagecount,
+                'format' => FORMAT_MOODLE
+            );
+        }
+        if (!isset($record['jumpto'][0])) {
+            $record['jumpto'][0] = LESSON_NEXTPAGE;
+        }
+
+        $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);
+    }
 }
index 41725a8..c024dbd 100644 (file)
@@ -70,4 +70,154 @@ class mod_lesson_generator_testcase extends advanced_testcase {
         $this->assertEquals($page2->id, $records[$page2->id]->id);
         $this->assertEquals('Custom title', $records[$page2->id]->title);
     }
+
+    /**
+     * This tests the true/false question generator.
+     */
+    public function test_create_question_truefalse() {
+        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_question_truefalse($lesson);
+        $page2 = $lessongenerator->create_question_truefalse($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(2, $p1answers); // True/false only supports 2 answer records.
+        $this->assertCount(2, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
+
+    /**
+     * This tests the multichoice question generator.
+     */
+    public function test_create_question_multichoice() {
+        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_question_multichoice($lesson);
+        $page2 = $lessongenerator->create_question_multichoice($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(2, $p1answers); // Multichoice requires at least 2 records.
+        $this->assertCount(2, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
+
+    /**
+     * This tests the essay question generator.
+     */
+    public function test_create_question_essay() {
+        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_question_essay($lesson);
+        $page2 = $lessongenerator->create_question_essay($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(1, $p1answers); // Essay creates a single (empty) answer record.
+        $this->assertCount(1, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
+
+    /**
+     * This tests the matching question generator.
+     */
+    public function test_create_question_matching() {
+        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_question_matching($lesson);
+        $page2 = $lessongenerator->create_question_matching($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(4, $p1answers); // Matching creates two extra records plus 1 for each answer value.
+        $this->assertCount(4, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
+
+    /**
+     * This tests the numeric question generator.
+     */
+    public function test_create_question_numeric() {
+        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_question_numeric($lesson);
+        $page2 = $lessongenerator->create_question_numeric($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(1, $p1answers); // Numeric only requires 1 answer.
+        $this->assertCount(1, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
+
+    /**
+     * This tests the shortanswer question generator.
+     */
+    public function test_create_question_shortanswer() {
+        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_question_shortanswer($lesson);
+        $page2 = $lessongenerator->create_question_shortanswer($lesson, array('title' => 'Custom title'));
+        $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
+        $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
+        $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
+        $this->assertCount(2, $records);
+        $this->assertCount(1, $p1answers); // Shortanswer only requires 1 answer.
+        $this->assertCount(1, $p2answers);
+        $this->assertEquals($page1->id, $records[$page1->id]->id);
+        $this->assertEquals($page2->id, $records[$page2->id]->id);
+        $this->assertEquals($page2->title, $records[$page2->id]->title);
+    }
 }