if (!($data = $DB->get_record('data', array('id' => $d)))) { print_error('invalidid', 'data'); } if (!($course = $DB->get_record('course', array('id' => $data->course)))) { print_error('coursemisconf'); } if (!($cm = get_coursemodule_from_instance('data', $data->id, $course->id))) { print_error('invalidcoursemodule'); } } require_login($course, false, $cm); $context = context_module::instance($cm->id); require_capability('mod/data:managetemplates', $context); if ($useeditor !== null) { // The useeditor param was set. Update the value for this template. data_set_config($data, "editor_{$mode}", !!$useeditor); } if (!$DB->count_records('data_fields', array('dataid' => $data->id))) { // Brand new database! redirect($CFG->wwwroot . '/mod/data/field.php?d=' . $data->id); // Redirect to field entry } // Trigger an event for viewing templates. $event = \mod_data\event\template_viewed::create(array('context' => $context, 'courseid' => $course->id, 'other' => array('dataid' => $data->id))); $event->add_record_snapshot('data', $data); $event->trigger(); /// Print the page header $strdata = get_string('modulenameplural', 'data'); // For the javascript for inserting template tags: initialise the default textarea to // 'edit_template' - it is always present in all different possible views. if ($mode == 'singletemplate') {
/** * Tests for data_set_config. * * @dataProvider data_set_config_provider * @param object $database The example row for the entry * @param string $key The config key to set * @param mixed $value The value of the key * @param bool $expectupdate Whether we expected an update * @param mixed $newconfigvalue The expected value */ public function test_data_set_config($database, $key, $value, $expectupdate, $newconfigvalue) { global $DB; // Mock the database. // Note: Use the actual test class here rather than the abstract because are testing concrete methods. $this->DB = $DB; $DB = $this->getMockBuilder(get_class($DB))->setMethods(['set_field'])->getMock(); $DB->expects($this->exactly((int) $expectupdate))->method('set_field')->with('data', 'config', $newconfigvalue, ['id' => $database->id]); // Perform the update. data_set_config($database, $key, $value); // Ensure that the value was updated by reference in $database. $config = json_decode($database->config); $this->assertEquals($value, $config->{$key}); }