mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-42400 generator: Add function testing_module_generator::create_content()
also implement it for mod_book and mod_forum to call existing create_page, create_discussion/post functions
This commit is contained in:
parent
7fbe33fcf1
commit
b6affde2cb
@ -269,6 +269,18 @@ abstract class testing_module_generator extends component_generator_base {
|
||||
$instance->cmid = $moduleinfo->coursemodule;
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a piece of content for the module.
|
||||
* User is usually taken from global $USER variable.
|
||||
* @param stdClass $instance object returned from create_instance() call
|
||||
* @param stdClass|array $record
|
||||
* @return stdClass generated object
|
||||
* @throws coding_exception if function is not implemented by module
|
||||
*/
|
||||
public function create_content($instance, $record = array()) {
|
||||
throw new coding_exception('Module generator for '.$this->get_modulename().' does not implement method create_content()');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,4 +120,11 @@ class mod_book_generator extends testing_module_generator {
|
||||
return $record;
|
||||
}
|
||||
|
||||
public function create_content($instance, $record = array()) {
|
||||
$record = (array)$record + array(
|
||||
'bookid' => $instance->id
|
||||
);
|
||||
return $this->create_chapter($record);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -66,6 +66,9 @@ class mod_book_generator_testcase extends advanced_testcase {
|
||||
$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)));
|
||||
|
||||
$chapter = $bookgenerator->create_content($book);
|
||||
$this->assertEquals(3, $DB->count_records('book_chapters', array('bookid' => $book->id)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -208,4 +208,27 @@ class mod_forum_generator extends testing_module_generator {
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
public function create_content($instance, $record = array()) {
|
||||
global $USER, $DB;
|
||||
$record = (array)$record + array(
|
||||
'forum' => $instance->id,
|
||||
'userid' => $USER->id,
|
||||
'course' => $instance->course
|
||||
);
|
||||
if (empty($record['discussion']) && empty($record['parent'])) {
|
||||
// Create discussion.
|
||||
$discussion = $this->create_discussion($record);
|
||||
$post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
|
||||
} else {
|
||||
// Create post.
|
||||
if (empty($record['parent'])) {
|
||||
$record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
|
||||
} else if (empty($record['discussion'])) {
|
||||
$record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
|
||||
}
|
||||
$post = $this->create_post($record);
|
||||
}
|
||||
return $post;
|
||||
}
|
||||
}
|
||||
|
@ -146,4 +146,42 @@ class mod_forum_generator_testcase extends advanced_testcase {
|
||||
$this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
|
||||
array('discussion' => $discussion->id)));
|
||||
}
|
||||
|
||||
public function test_create_content() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create a bunch of users
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create course and forum.
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$forum = self::getDataGenerator()->create_module('forum', array('course' => $course));
|
||||
|
||||
$generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
||||
// This should create discussion.
|
||||
$post1 = $generator->create_content($forum);
|
||||
// This should create posts in the discussion.
|
||||
$post2 = $generator->create_content($forum, array('parent' => $post1->id));
|
||||
$post3 = $generator->create_content($forum, array('discussion' => $post1->discussion));
|
||||
// This should create posts answering another post.
|
||||
$post4 = $generator->create_content($forum, array('parent' => $post2->id));
|
||||
|
||||
$discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id));
|
||||
$postrecords = $DB->get_records('forum_posts');
|
||||
$postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion));
|
||||
$this->assertEquals(1, count($discussionrecords));
|
||||
$this->assertEquals(4, count($postrecords));
|
||||
$this->assertEquals(4, count($postrecords2));
|
||||
$this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost);
|
||||
$this->assertEquals($post1->id, $postrecords[$post2->id]->parent);
|
||||
$this->assertEquals($post1->id, $postrecords[$post3->id]->parent);
|
||||
$this->assertEquals($post2->id, $postrecords[$post4->id]->parent);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user