mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-42400 mod_wiki: data generator for wiki
This commit is contained in:
parent
7b22ba41b7
commit
20dff4b97e
@ -21,6 +21,7 @@ class wiki_parser_proxy {
|
||||
}
|
||||
|
||||
$type = strtolower($type);
|
||||
self::$parsers[$type] = null; // Reset the current parser because it may have other options.
|
||||
if(self::create_parser_instance($type)) {
|
||||
return self::$parsers[$type]->parse($string, $options);
|
||||
}
|
||||
@ -56,15 +57,11 @@ class wiki_parser_proxy {
|
||||
|
||||
private static function create_parser_instance($type) {
|
||||
if(empty(self::$parsers[$type])) {
|
||||
if(include(self::$basepath."markups/$type.php")) {
|
||||
$class = strtolower($type)."_parser";
|
||||
if(class_exists($class)) {
|
||||
self::$parsers[$type] = new $class;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
include_once(self::$basepath."markups/$type.php");
|
||||
$class = strtolower($type)."_parser";
|
||||
if(class_exists($class)) {
|
||||
self::$parsers[$type] = new $class;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
|
123
mod/wiki/tests/generator/lib.php
Normal file
123
mod/wiki/tests/generator/lib.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* mod_wiki data generator.
|
||||
*
|
||||
* @package mod_wiki
|
||||
* @category test
|
||||
* @copyright 2013 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* mod_wiki data generator class.
|
||||
*
|
||||
* @package mod_wiki
|
||||
* @category test
|
||||
* @copyright 2013 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_wiki_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) {
|
||||
// Add default values for wiki.
|
||||
$record = (array)$record + array(
|
||||
'wikimode' => 'collaborative',
|
||||
'firstpagetitle' => 'Front page for wiki '.($this->instancecount+1),
|
||||
'defaultformat' => 'html',
|
||||
'forceformat' => 0
|
||||
);
|
||||
|
||||
return parent::create_instance($record, (array)$options);
|
||||
}
|
||||
|
||||
public function create_content($wiki, $record = array()) {
|
||||
$record = (array)$record + array(
|
||||
'wikiid' => $wiki->id
|
||||
);
|
||||
return $this->create_page($wiki, $record);
|
||||
}
|
||||
|
||||
public function create_first_page($wiki, $record = array()) {
|
||||
$record = (array)$record + array(
|
||||
'title' => $wiki->firstpagetitle,
|
||||
);
|
||||
return $this->create_page($wiki, $record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a page in wiki.
|
||||
*
|
||||
* @param stdClass wiki object returned from create_instance (if known)
|
||||
* @param stdClass|array $record data to insert as wiki entry.
|
||||
* @return stdClass
|
||||
* @throws coding_exception if neither $record->wikiid nor $wiki->id is specified
|
||||
*/
|
||||
public function create_page($wiki, $record = array()) {
|
||||
global $CFG, $USER;
|
||||
require_once($CFG->dirroot.'/mod/wiki/locallib.php');
|
||||
$this->pagecount++;
|
||||
$record = (array)$record + array(
|
||||
'title' => 'wiki page '.$this->pagecount,
|
||||
'wikiid' => $wiki->id,
|
||||
'subwikiid' => 0,
|
||||
'group' => 0,
|
||||
'content' => 'Wiki page content '.$this->pagecount,
|
||||
'format' => $wiki->defaultformat
|
||||
);
|
||||
if (empty($record['wikiid']) && empty($record['subwikiid'])) {
|
||||
throw new coding_exception('wiki page generator requires either wikiid or subwikiid');
|
||||
}
|
||||
if (!$record['subwikiid']) {
|
||||
if (!isset($record['userid'])) {
|
||||
$record['userid'] = ($wiki->wikimode == 'individual') ? $USER->id : 0;
|
||||
}
|
||||
if ($subwiki = wiki_get_subwiki_by_group($record['wikiid'], $record['group'], $record['userid'])) {
|
||||
$record['subwikiid'] = $subwiki->id;
|
||||
} else {
|
||||
$record['subwikiid'] = wiki_add_subwiki($record['wikiid'], $record['group'], $record['userid']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title'])) {
|
||||
$rv = wiki_save_page($wikipage, $record['content'], $USER->id);
|
||||
return $rv['page'];
|
||||
}
|
||||
|
||||
$pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id);
|
||||
$wikipage = wiki_get_page($pageid);
|
||||
$rv = wiki_save_page($wikipage, $record['content'], $USER->id);
|
||||
return $rv['page'];
|
||||
}
|
||||
}
|
121
mod/wiki/tests/generator_test.php
Normal file
121
mod/wiki/tests/generator_test.php
Normal file
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* mod_wiki generator tests
|
||||
*
|
||||
* @package mod_wiki
|
||||
* @category test
|
||||
* @copyright 2013 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
/**
|
||||
* Genarator tests class for mod_wiki.
|
||||
*
|
||||
* @package mod_wiki
|
||||
* @category test
|
||||
* @copyright 2013 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_wiki_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('wiki', array('course' => $course->id)));
|
||||
$wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
|
||||
$records = $DB->get_records('wiki', array('course' => $course->id), 'id');
|
||||
$this->assertEquals(1, count($records));
|
||||
$this->assertTrue(array_key_exists($wiki->id, $records));
|
||||
|
||||
$params = array('course' => $course->id, 'name' => 'Another wiki');
|
||||
$wiki = $this->getDataGenerator()->create_module('wiki', $params);
|
||||
$records = $DB->get_records('wiki', array('course' => $course->id), 'id');
|
||||
$this->assertEquals(2, count($records));
|
||||
$this->assertEquals('Another wiki', $records[$wiki->id]->name);
|
||||
}
|
||||
|
||||
public function test_create_content() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
|
||||
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
|
||||
|
||||
$page1 = $wikigenerator->create_first_page($wiki);
|
||||
$page2 = $wikigenerator->create_content($wiki);
|
||||
$page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title'));
|
||||
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
|
||||
$this->assertEquals(1, count($subwikis));
|
||||
$subwikiid = key($subwikis);
|
||||
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
|
||||
$this->assertEquals(3, count($records));
|
||||
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
||||
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
||||
$this->assertEquals($page3->id, $records[$page3->id]->id);
|
||||
$this->assertEquals('Custom title', $records[$page3->id]->title);
|
||||
}
|
||||
|
||||
public function test_create_content_individual() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$wiki = $this->getDataGenerator()->create_module('wiki',
|
||||
array('course' => $course, 'wikimode' => 'individual'));
|
||||
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
|
||||
|
||||
$page1 = $wikigenerator->create_first_page($wiki);
|
||||
$page2 = $wikigenerator->create_content($wiki);
|
||||
$page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title for admin'));
|
||||
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
|
||||
$this->assertEquals(1, count($subwikis));
|
||||
$subwikiid = key($subwikis);
|
||||
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
|
||||
$this->assertEquals(3, count($records));
|
||||
$this->assertEquals($page1->id, $records[$page1->id]->id);
|
||||
$this->assertEquals($page2->id, $records[$page2->id]->id);
|
||||
$this->assertEquals($page3->id, $records[$page3->id]->id);
|
||||
$this->assertEquals('Custom title for admin', $records[$page3->id]->title);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$role = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
|
||||
$this->setUser($user);
|
||||
|
||||
$page1s = $wikigenerator->create_first_page($wiki);
|
||||
$page2s = $wikigenerator->create_content($wiki);
|
||||
$page3s = $wikigenerator->create_content($wiki, array('title' => 'Custom title for student'));
|
||||
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
|
||||
$this->assertEquals(2, count($subwikis));
|
||||
next($subwikis);
|
||||
$subwikiid = key($subwikis);
|
||||
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
|
||||
$this->assertEquals(3, count($records));
|
||||
$this->assertEquals($page1s->id, $records[$page1s->id]->id);
|
||||
$this->assertEquals($page2s->id, $records[$page2s->id]->id);
|
||||
$this->assertEquals($page3s->id, $records[$page3s->id]->id);
|
||||
$this->assertEquals('Custom title for student', $records[$page3s->id]->title);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user