Skip to content

Commit

Permalink
MDL-77581 behat: Create generators for lesson clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 9, 2023
1 parent 71dcb7b commit cfbfeac
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 2 deletions.
25 changes: 24 additions & 1 deletion mod/lesson/pagetypes/cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ public function valid_page_and_view(&$validpages, &$pageviews) {
}
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 {
Expand Down Expand Up @@ -179,4 +202,4 @@ public function construction_override($pageid, lesson $lesson) {
$lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
}
}
}
23 changes: 23 additions & 0 deletions mod/lesson/pagetypes/endofbranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ public function add_page_link($previd) {
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 {
Expand Down
23 changes: 23 additions & 0 deletions mod/lesson/pagetypes/endofcluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ public function add_page_link($previd) {
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 {
Expand Down
102 changes: 101 additions & 1 deletion mod/lesson/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -180,7 +181,16 @@ private function perform_create_page(array $record): ?stdClass {
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");
Expand Down Expand Up @@ -524,6 +534,96 @@ public function create_question_numeric($lesson, $record = array()) {
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).
*
Expand Down
83 changes: 83 additions & 0 deletions mod/lesson/tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,89 @@ public function test_create_question_shortanswer() {
$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.
*
Expand Down

0 comments on commit cfbfeac

Please sign in to comment.