MDL-42400 mod_workshop: data generator for workshop

This commit is contained in:
Marina Glancy 2013-10-21 12:33:00 +11:00
parent 20dff4b97e
commit 3bbbb3c902
2 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,100 @@
<?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_workshop data generator.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_workshop data generator class.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_workshop_generator extends testing_module_generator {
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$workshopconfig = get_config('workshop');
// Add default values for workshop.
$record = (array)$record + array(
'strategy' => $workshopconfig->strategy,
'grade' => $workshopconfig->grade,
'gradinggrade' => $workshopconfig->gradinggrade,
'gradedecimals' => $workshopconfig->gradedecimals,
'nattachments' => 1,
'maxbytes' => $workshopconfig->maxbytes,
'latesubmissions' => 0,
'useselfassessment' => 0,
'overallfeedbackmode' => 1,
'overallfeedbackfiles' => 0,
'overallfeedbackmaxbytes' => $workshopconfig->maxbytes,
'useexamples' => 0,
'examplesmode' => $workshopconfig->examplesmode,
'submissionstart' => 0,
'submissionend' => 0,
'phaseswitchassessment' => 0,
'assessmentstart' => 0,
'assessmentend' => 0,
);
if (!isset($record['gradecategory']) || !isset($record['gradinggradecategory'])) {
require_once($CFG->libdir.'/gradelib.php');
$courseid = is_object($record['course']) ? $record['course']->id : $record['course'];
$gradecategories = grade_get_categories_menu($courseid);
reset($gradecategories);
$defaultcategory = key($gradecategories);
$record += array(
'gradecategory' => $defaultcategory,
'gradinggradecategory' => $defaultcategory
);
}
if (!isset($record['instructauthorseditor'])) {
$record['instructauthorseditor'] = array(
'text' => 'Instructions for submission '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
if (!isset($record['instructreviewerseditor'])) {
$record['instructreviewerseditor'] = array(
'text' => 'Instructions for assessment '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
if (!isset($record['conclusioneditor'])) {
$record['conclusioneditor'] = array(
'text' => 'Conclusion '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
return parent::create_instance($record, (array)$options);
}
}

View File

@ -0,0 +1,55 @@
<?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_workshop generator tests
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_workshop.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_workshop_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('workshop', array('course' => $course->id)));
$workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $course));
$records = $DB->get_records('workshop', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($workshop->id, $records));
$params = array('course' => $course->id, 'name' => 'Another workshop');
$workshop = $this->getDataGenerator()->create_module('workshop', $params);
$records = $DB->get_records('workshop', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another workshop', $records[$workshop->id]->name);
}
}