MDL-40047 feedback: Add a test generator for feedback
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?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_feedback data generator.
|
||||
*
|
||||
* @package mod_feedback
|
||||
* @category test
|
||||
* @copyright 2013 Ankit Agarwal
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* mod_feedback data generator class.
|
||||
*
|
||||
* @package mod_feedback
|
||||
* @category test
|
||||
* @copyright 2013 Ankit Agarwal
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_feedback_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Create new feedback module instance
|
||||
*
|
||||
* @param array|stdClass $record
|
||||
* @param array $options
|
||||
* @throws coding_exception
|
||||
* @return stdClass activity record with extra cmid field
|
||||
*/
|
||||
public function create_instance($record = null, array $options = null) {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/mod/feedback/lib.php");
|
||||
|
||||
$this->instancecount++;
|
||||
$i = $this->instancecount;
|
||||
|
||||
$record = (object)(array)$record;
|
||||
$options = (array)$options;
|
||||
|
||||
if (empty($record->course)) {
|
||||
throw new coding_exception('Module generator requires $record->course.');
|
||||
}
|
||||
if (!isset($record->name)) {
|
||||
$record->name = get_string('pluginname', 'feedback') . ' ' . $i;
|
||||
}
|
||||
if (!isset($record->intro)) {
|
||||
$record->intro = 'Test feedback ' . $i;
|
||||
}
|
||||
if (!isset($record->introformat)) {
|
||||
$record->introformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->anonymous)) {
|
||||
$record->anonymous = FEEDBACK_ANONYMOUS_YES;
|
||||
}
|
||||
if (!isset($record->email_notification)) {
|
||||
$record->email_notification = 0;
|
||||
}
|
||||
if (!isset($record->multiple_submit)) {
|
||||
$record->multiple_submit = 0;
|
||||
}
|
||||
if (!isset($record->autonumbering)) {
|
||||
$record->autonumbering = 0;
|
||||
}
|
||||
if (!isset($record->site_after_submit)) {
|
||||
$record->site_after_submit = '';
|
||||
}
|
||||
if (!isset($record->page_after_submit)) {
|
||||
$record->page_after_submit = 'This is page after submit';
|
||||
}
|
||||
if (!isset($record->page_after_submitformat)) {
|
||||
$record->page_after_submitformat = FORMAT_MOODLE;
|
||||
}
|
||||
if (!isset($record->publish_stats)) {
|
||||
$record->publish_stats = 0;
|
||||
}
|
||||
if (!isset($record->timeopen)) {
|
||||
$record->timeopen = 0;
|
||||
}
|
||||
if (!isset($record->timeclose)) {
|
||||
$record->timeclose = 0;
|
||||
}
|
||||
if (!isset($record->timemodified)) {
|
||||
$record->timemodified = time();
|
||||
}
|
||||
if (!isset($record->completionsubmit)) {
|
||||
$record->completionsubmit = 0;
|
||||
}
|
||||
|
||||
// Hack to bypass draft processing of feedback_add_instance.
|
||||
$record->page_after_submit_editor['itemid'] = false;
|
||||
|
||||
$record->coursemodule = $this->precreate_course_module($record->course, $options);
|
||||
$id = feedback_add_instance($record);
|
||||
return $this->post_add_instance($id, $record->coursemodule);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Genarator tests.
|
||||
*
|
||||
* @package mod_feedback
|
||||
* @copyright 2013 Ankit Agarwal
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
global $CFG;
|
||||
|
||||
/**
|
||||
* Genarator tests class.
|
||||
*
|
||||
* @package mod_feedback
|
||||
* @copyright 2013 Ankit Agarwal
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_feedback_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('feedback', array('course' => $course->id)));
|
||||
$feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course->id));
|
||||
$this->assertEquals(1, $DB->count_records('feedback', array('course' => $course->id)));
|
||||
$this->assertTrue($DB->record_exists('feedback', array('course' => $course->id)));
|
||||
$this->assertTrue($DB->record_exists('feedback', array('id' => $feedback->id)));
|
||||
|
||||
$params = array('course' => $course->id, 'name' => 'One more feedback');
|
||||
$feedback = $this->getDataGenerator()->create_module('feedback', $params);
|
||||
$this->assertEquals(2, $DB->count_records('feedback', array('course' => $course->id)));
|
||||
$this->assertEquals('One more feedback', $DB->get_field_select('feedback', 'name', 'id = :id',
|
||||
array('id' => $feedback->id)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user