mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-77581 behat: Create generators for lesson clusters
This commit is contained in:
parent
71dcb7bcc5
commit
cfbfeacae5
@ -87,6 +87,29 @@ class lesson_page_type_cluster extends lesson_page {
|
||||
}
|
||||
return $this->properties->nextpageid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates answers within the database for this cluster page. Usually only ever
|
||||
* called when creating a new page instance.
|
||||
* @param object $properties
|
||||
* @return array
|
||||
*/
|
||||
public function create_answers($properties) {
|
||||
global $DB;
|
||||
|
||||
$newanswer = new stdClass;
|
||||
$newanswer->lessonid = $this->lesson->id;
|
||||
$newanswer->pageid = $this->properties->id;
|
||||
$newanswer->timecreated = $this->properties->timecreated;
|
||||
|
||||
if (isset($properties->jumpto[0])) {
|
||||
$newanswer->jumpto = $properties->jumpto[0];
|
||||
}
|
||||
$newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
|
||||
$answers = [$newanswer->id => new lesson_page_answer($newanswer)];
|
||||
$this->answers = $answers;
|
||||
return $answers;
|
||||
}
|
||||
}
|
||||
|
||||
class lesson_add_page_form_cluster extends lesson_add_page_form_base {
|
||||
|
@ -115,6 +115,29 @@ class lesson_page_type_endofbranch extends lesson_page {
|
||||
public function valid_page_and_view(&$validpages, &$pageviews) {
|
||||
return $this->properties->nextpageid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates answers within the database for this end of cluster page. Usually only ever
|
||||
* called when creating a new page instance.
|
||||
* @param object $properties
|
||||
* @return array
|
||||
*/
|
||||
public function create_answers($properties) {
|
||||
global $DB;
|
||||
|
||||
$newanswer = new stdClass;
|
||||
$newanswer->lessonid = $this->lesson->id;
|
||||
$newanswer->pageid = $this->properties->id;
|
||||
$newanswer->timecreated = $this->properties->timecreated;
|
||||
|
||||
if (isset($properties->jumpto[0])) {
|
||||
$newanswer->jumpto = $properties->jumpto[0];
|
||||
}
|
||||
$newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
|
||||
$answers = [$newanswer->id => new lesson_page_answer($newanswer)];
|
||||
$this->answers = $answers;
|
||||
return $answers;
|
||||
}
|
||||
}
|
||||
|
||||
class lesson_add_page_form_endofbranch extends lesson_add_page_form_base {
|
||||
|
@ -95,6 +95,29 @@ class lesson_page_type_endofcluster extends lesson_page {
|
||||
public function valid_page_and_view(&$validpages, &$pageviews) {
|
||||
return $this->properties->nextpageid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates answers within the database for this end of cluster page. Usually only ever
|
||||
* called when creating a new page instance.
|
||||
* @param object $properties
|
||||
* @return array
|
||||
*/
|
||||
public function create_answers($properties) {
|
||||
global $DB;
|
||||
|
||||
$newanswer = new stdClass;
|
||||
$newanswer->lessonid = $this->lesson->id;
|
||||
$newanswer->pageid = $this->properties->id;
|
||||
$newanswer->timecreated = $this->properties->timecreated;
|
||||
|
||||
if (isset($properties->jumpto[0])) {
|
||||
$newanswer->jumpto = $properties->jumpto[0];
|
||||
}
|
||||
$newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
|
||||
$answers = [$newanswer->id => new lesson_page_answer($newanswer)];
|
||||
$this->answers = $answers;
|
||||
return $answers;
|
||||
}
|
||||
}
|
||||
|
||||
class lesson_add_page_form_endofcluster extends lesson_add_page_form_base {
|
||||
|
@ -58,6 +58,7 @@ class mod_lesson_generator extends testing_module_generator {
|
||||
'Unseen question within a content page' => LESSON_UNSEENBRANCHPAGE,
|
||||
'Random question within a content page' => LESSON_RANDOMPAGE,
|
||||
'Random content page' => LESSON_RANDOMBRANCH,
|
||||
'Unseen question within a cluster' => LESSON_CLUSTERJUMP,
|
||||
];
|
||||
|
||||
/**
|
||||
@ -180,7 +181,16 @@ class mod_lesson_generator extends testing_module_generator {
|
||||
return null;
|
||||
}
|
||||
|
||||
$funcname = $qtype === 'content' ? 'create_content' : "create_question_{$qtype}";
|
||||
switch ($qtype) {
|
||||
case 'content':
|
||||
case 'cluster':
|
||||
case 'endofcluster':
|
||||
case 'endofbranch':
|
||||
$funcname = "create_{$qtype}";
|
||||
break;
|
||||
default:
|
||||
$funcname = "create_question_{$qtype}";
|
||||
}
|
||||
|
||||
if (!method_exists($this, $funcname)) {
|
||||
throw new coding_exception('The page '.$record['title']." has an invalid qtype: $qtype");
|
||||
@ -524,6 +534,96 @@ class mod_lesson_generator extends testing_module_generator {
|
||||
return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a cluster page for testing purposes.
|
||||
*
|
||||
* @param stdClass $lesson instance where to create the page.
|
||||
* @param array $record data for page being generated.
|
||||
* @return stdClass page record.
|
||||
*/
|
||||
public function create_cluster(stdClass $lesson, array $record = []): stdClass {
|
||||
global $DB, $CFG;
|
||||
$now = time();
|
||||
$this->pagecount++;
|
||||
$record = $record + [
|
||||
'lessonid' => $lesson->id,
|
||||
'title' => 'Cluster '.$this->pagecount,
|
||||
'timecreated' => $now,
|
||||
'qtype' => 30, // LESSON_PAGE_CLUSTER.
|
||||
'pageid' => 0, // By default insert in the beginning.
|
||||
];
|
||||
if (!isset($record['contents_editor'])) {
|
||||
$record['contents_editor'] = [
|
||||
'text' => 'Cluster '.$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', ['id' => $page->id], '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a end of cluster page for testing purposes.
|
||||
*
|
||||
* @param stdClass $lesson instance where to create the page.
|
||||
* @param array $record data for page being generated.
|
||||
* @return stdClass page record.
|
||||
*/
|
||||
public function create_endofcluster(stdClass $lesson, array $record = []): stdClass {
|
||||
global $DB, $CFG;
|
||||
$now = time();
|
||||
$this->pagecount++;
|
||||
$record = $record + [
|
||||
'lessonid' => $lesson->id,
|
||||
'title' => 'End of cluster '.$this->pagecount,
|
||||
'timecreated' => $now,
|
||||
'qtype' => 31, // LESSON_PAGE_ENDOFCLUSTER.
|
||||
'pageid' => 0, // By default insert in the beginning.
|
||||
];
|
||||
if (!isset($record['contents_editor'])) {
|
||||
$record['contents_editor'] = [
|
||||
'text' => 'End of cluster '.$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', ['id' => $page->id], '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a end of branch page for testing purposes.
|
||||
*
|
||||
* @param stdClass $lesson instance where to create the page.
|
||||
* @param array $record data for page being generated.
|
||||
* @return stdClass page record.
|
||||
*/
|
||||
public function create_endofbranch(stdClass $lesson, array $record = []): stdClass {
|
||||
global $DB, $CFG;
|
||||
$now = time();
|
||||
$this->pagecount++;
|
||||
$record = $record + [
|
||||
'lessonid' => $lesson->id,
|
||||
'title' => 'End of branch '.$this->pagecount,
|
||||
'timecreated' => $now,
|
||||
'qtype' => 21, // LESSON_PAGE_ENDOFBRANCH.
|
||||
'pageid' => 0, // By default insert in the beginning.
|
||||
];
|
||||
if (!isset($record['contents_editor'])) {
|
||||
$record['contents_editor'] = [
|
||||
'text' => 'End of branch '.$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', ['id' => $page->id], '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a lesson override (either user or group).
|
||||
*
|
||||
|
@ -215,6 +215,89 @@ class generator_test extends \advanced_testcase {
|
||||
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
||||
}
|
||||
|
||||
/**
|
||||
* This tests the generators for cluster, endofcluster and endofbranch pages.
|
||||
*
|
||||
* @covers ::create_cluster
|
||||
* @covers ::create_endofcluster
|
||||
* @covers ::create_endofbranch
|
||||
* @dataProvider create_cluster_pages_provider
|
||||
*
|
||||
* @param string $type Type of page to test: LESSON_PAGE_CLUSTER, LESSON_PAGE_ENDOFCLUSTER or LESSON_PAGE_ENDOFBRANCH.
|
||||
*/
|
||||
public function test_create_cluster_pages(string $type): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course]);
|
||||
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
|
||||
|
||||
$page2data = [
|
||||
'title' => 'Custom title',
|
||||
'contents_editor' => [
|
||||
'text' => 'Custom content',
|
||||
'format' => FORMAT_MOODLE,
|
||||
'itemid' => 0,
|
||||
],
|
||||
'jumpto' => [LESSON_EOL],
|
||||
];
|
||||
|
||||
switch ($type) {
|
||||
case LESSON_PAGE_CLUSTER:
|
||||
$page1 = $lessongenerator->create_cluster($lesson);
|
||||
$page2 = $lessongenerator->create_cluster($lesson, $page2data);
|
||||
break;
|
||||
case LESSON_PAGE_ENDOFCLUSTER:
|
||||
$page1 = $lessongenerator->create_endofcluster($lesson);
|
||||
$page2 = $lessongenerator->create_endofcluster($lesson, $page2data);
|
||||
break;
|
||||
case LESSON_PAGE_ENDOFBRANCH:
|
||||
$page1 = $lessongenerator->create_endofbranch($lesson);
|
||||
$page2 = $lessongenerator->create_endofbranch($lesson, $page2data);
|
||||
break;
|
||||
default:
|
||||
throw new coding_exception('Cluster page type not valid: ' . $type);
|
||||
}
|
||||
|
||||
$records = $DB->get_records('lesson_pages', ['lessonid' => $lesson->id], 'id');
|
||||
$p1answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id, 'pageid' => $page1->id], 'id');
|
||||
$p2answers = $DB->get_records('lesson_answers', ['lessonid' => $lesson->id, 'pageid' => $page2->id], 'id');
|
||||
|
||||
$this->assertCount(2, $records);
|
||||
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
||||
$this->assertEquals($type, $records[$page1->id]->qtype);
|
||||
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
||||
$this->assertEquals($type, $records[$page2->id]->qtype);
|
||||
$this->assertEquals($page2->title, $records[$page2->id]->title);
|
||||
$this->assertEquals($page2data['contents_editor']['text'], $records[$page2->id]->contents);
|
||||
$this->assertCount(1, $p1answers);
|
||||
$this->assertCount(1, $p2answers);
|
||||
$this->assertEquals(LESSON_THISPAGE, array_pop($p1answers)->jumpto);
|
||||
$this->assertEquals(LESSON_EOL, array_pop($p2answers)->jumpto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_create_cluster_pages().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function create_cluster_pages_provider(): array {
|
||||
// Using the page constants here throws an error: Undefined constant "mod_lesson\LESSON_PAGE_CLUSTER".
|
||||
return [
|
||||
'Cluster' => [
|
||||
'type' => '30',
|
||||
],
|
||||
'End of cluster' => [
|
||||
'type' => '31',
|
||||
],
|
||||
'End of branch' => [
|
||||
'type' => '21',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create some pages and their answers.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user