MDL-61937 mod_data: generator for all field types
This commit is contained in:
@@ -156,6 +156,10 @@ class data_field_file extends data_field_base {
|
||||
|
||||
// Should always be available since it is set by display_add_field before initializing the draft area.
|
||||
$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
|
||||
if (!$content) {
|
||||
$content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
|
||||
$content->id = $DB->insert_record('data_content', $content);
|
||||
}
|
||||
|
||||
file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);
|
||||
|
||||
|
||||
@@ -225,6 +225,10 @@ class data_field_picture extends data_field_base {
|
||||
|
||||
// Should always be available since it is set by display_add_field before initializing the draft area.
|
||||
$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
|
||||
if (!$content) {
|
||||
$content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
|
||||
$content->id = $DB->insert_record('data_content', $content);
|
||||
}
|
||||
|
||||
$names = explode('_', $name);
|
||||
switch ($names[2]) {
|
||||
|
||||
@@ -29,8 +29,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
/**
|
||||
* Data generator class for mod_data.
|
||||
*
|
||||
* Currently, the field types in the ignoredfieldtypes array aren't supported.
|
||||
*
|
||||
* @package mod_data
|
||||
* @category test
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
@@ -48,12 +46,6 @@ class mod_data_generator extends testing_module_generator {
|
||||
*/
|
||||
protected $databaserecordcount = 0;
|
||||
|
||||
/**
|
||||
* @var The field types which not handled by the generator as of now.
|
||||
*/
|
||||
protected $ignoredfieldtypes = array('latlong', 'file', 'picture');
|
||||
|
||||
|
||||
/**
|
||||
* To be called from data reset code only,
|
||||
* do not use in tests.
|
||||
@@ -89,7 +81,6 @@ class mod_data_generator extends testing_module_generator {
|
||||
|
||||
/**
|
||||
* Creates a field for a mod_data instance.
|
||||
* Currently, the field types in the ignoredfieldtypes array aren't supported.
|
||||
*
|
||||
* @param StdClass $record
|
||||
* @param mod_data $data
|
||||
@@ -98,12 +89,6 @@ class mod_data_generator extends testing_module_generator {
|
||||
public function create_field(stdClass $record = null, $data = null) {
|
||||
$record = (array) $record;
|
||||
|
||||
if (in_array($record['type'], $this->ignoredfieldtypes)) {
|
||||
throw new coding_exception('$record\'s type value must not be same as values in ignoredfieldtypes
|
||||
in phpunit_util::create_field()');
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->databasefieldcount++;
|
||||
|
||||
if (!isset($data->course)) {
|
||||
@@ -143,6 +128,8 @@ class mod_data_generator extends testing_module_generator {
|
||||
$record['param1'] = implode("\n", array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4'));
|
||||
} else if (($record['type'] === 'text') || ($record['type'] === 'url')) {
|
||||
$record['param1'] = 1;
|
||||
} else if ($record['type'] == 'latlong') {
|
||||
$record['param1'] = 'Google Maps';
|
||||
} else {
|
||||
$record['param1'] = '';
|
||||
}
|
||||
@@ -152,6 +139,8 @@ class mod_data_generator extends testing_module_generator {
|
||||
|
||||
if ($record['type'] === 'textarea') {
|
||||
$record['param2'] = 60;
|
||||
} else if ($record['type'] == 'latlong') {
|
||||
$record['param2'] = -1;
|
||||
} else {
|
||||
$record['param2'] = '';
|
||||
}
|
||||
@@ -161,6 +150,8 @@ class mod_data_generator extends testing_module_generator {
|
||||
|
||||
if (($record['type'] === 'textarea')) {
|
||||
$record['param3'] = 35;
|
||||
} else if ($record['type'] == 'picture' || $record['type'] == 'file') {
|
||||
$record['param3'] = 0;
|
||||
} else {
|
||||
$record['param3'] = '';
|
||||
}
|
||||
@@ -193,7 +184,6 @@ class mod_data_generator extends testing_module_generator {
|
||||
* Creates a field for a mod_data instance.
|
||||
* Keep in mind the default data field params created in create_field() function!
|
||||
* ...if you haven't provided your own custom data field parameters there.
|
||||
* Currently, the field types in the ignoredfieldtypes array aren't supported.
|
||||
* The developers using the generator must adhere to the following format :
|
||||
*
|
||||
* Syntax : $contents[ fieldid ] = fieldvalue
|
||||
@@ -206,16 +196,19 @@ class mod_data_generator extends testing_module_generator {
|
||||
* $contents['text'] = 'text'
|
||||
* $contents['textarea'] = 'text'
|
||||
* $contents['url'] = 'example.url' or array('example.url', 'urlname')
|
||||
* $contents['latlong'] = array('value for lattitude', 'value for longitude')
|
||||
* $contents['file'] = 'filename or draftitemid'
|
||||
* $contents['picture'] = array('filename or draftitemid', 'alternative text')
|
||||
*
|
||||
* @param mod_data $data
|
||||
* @param stdClass $data record from table {data}
|
||||
* @param array $contents
|
||||
* @param int $groupid
|
||||
* @param array $tags
|
||||
* @param array $options
|
||||
* @return data_field_{type}
|
||||
* @return int id of the generated record in table {data_records}
|
||||
*/
|
||||
public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null) {
|
||||
global $DB;
|
||||
global $DB, $USER, $CFG;
|
||||
|
||||
$this->databaserecordcount++;
|
||||
|
||||
@@ -233,10 +226,6 @@ class mod_data_generator extends testing_module_generator {
|
||||
foreach ($fields as $field) {
|
||||
$fieldhascontent = true;
|
||||
|
||||
if (in_array($field->type, $this->ignoredfieldtypes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = data_get_field($field, $data);
|
||||
|
||||
$fieldid = $field->field->id;
|
||||
@@ -293,6 +282,52 @@ class mod_data_generator extends testing_module_generator {
|
||||
$fieldhascontent = false;
|
||||
}
|
||||
|
||||
} else if ($field->type === 'latlong') {
|
||||
$values = array();
|
||||
|
||||
foreach ($contents[$fieldid] as $key => $value) {
|
||||
$values['field_' . $fieldid . '_' . $key] = $value;
|
||||
}
|
||||
|
||||
$contents[$fieldid] = $values;
|
||||
$fieldname = 'field_' . $fieldid . '_0';
|
||||
if (!$field->notemptyfield($values[$fieldname], $fieldname)) {
|
||||
$fieldhascontent = false;
|
||||
}
|
||||
|
||||
} else if ($field->type === 'file' || $field->type === 'picture') {
|
||||
if (is_array($contents[$fieldid])) {
|
||||
list($itemid, $alttext) = $contents[$fieldid];
|
||||
} else {
|
||||
$itemid = $contents[$fieldid];
|
||||
$alttext = '';
|
||||
}
|
||||
|
||||
if (strlen($itemid) && !is_numeric($itemid)) {
|
||||
// We expect draftarea item id here but it can also be a filename, in this case provider will generate file.
|
||||
$filename = $itemid;
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
$itemid = file_get_unused_draft_itemid();
|
||||
get_file_storage()->create_file_from_string(['component' => 'user', 'filearea' => 'draft',
|
||||
'contextid' => $usercontext->id, 'itemid' => $itemid, 'filepath' => '/',
|
||||
'filename' => $filename],
|
||||
file_get_contents($CFG->dirroot.'/mod/data/pix/icon.png'));
|
||||
}
|
||||
|
||||
$fieldname = 'field_' . $fieldid . '_file';
|
||||
if ($field->type === 'file') {
|
||||
$contents[$fieldid] = $itemid;
|
||||
} else {
|
||||
$contents[$fieldid] = [
|
||||
$fieldname => $itemid,
|
||||
'field_' . $fieldid . '_alttext' => $alttext
|
||||
];
|
||||
}
|
||||
|
||||
if (!$field->notemptyfield($itemid, $fieldname)) {
|
||||
$fieldhascontent = false;
|
||||
}
|
||||
|
||||
} else {
|
||||
if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) {
|
||||
continue;
|
||||
@@ -307,7 +342,7 @@ class mod_data_generator extends testing_module_generator {
|
||||
foreach ($contents as $fieldid => $content) {
|
||||
$field = data_get_field_from_id($fieldid, $data);
|
||||
|
||||
if (is_array($content) and in_array($field->type, array('date', 'textarea', 'url'))) {
|
||||
if (is_array($content) and in_array($field->type, array('date', 'textarea', 'url', 'picture', 'latlong'))) {
|
||||
|
||||
foreach ($content as $fieldname => $value) {
|
||||
$field->update_content($recordid, $value, $fieldname);
|
||||
|
||||
@@ -159,7 +159,8 @@ class mod_data_generator_testcase extends advanced_testcase {
|
||||
$context = context_module::instance($cm->id);
|
||||
$this->assertEquals($data->cmid, $context->instanceid);
|
||||
|
||||
$fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url');
|
||||
$fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
|
||||
'latlong', 'file', 'picture');
|
||||
|
||||
$count = 1;
|
||||
|
||||
@@ -192,6 +193,9 @@ class mod_data_generator_testcase extends advanced_testcase {
|
||||
$contents[] = 'text for testing';
|
||||
$contents[] = '<p>text area testing<br /></p>';
|
||||
$contents[] = array('example.url', 'sampleurl');
|
||||
$contents[] = [-31.9489873, 115.8382036]; // Latlong.
|
||||
$contents[] = 'Filename.pdf'; // File - filename.
|
||||
$contents[] = array('Cat1234.jpg', 'Cat'); // Picture - filename with alt text.
|
||||
$count = 0;
|
||||
$fieldcontents = array();
|
||||
foreach ($fields as $fieldrecord) {
|
||||
@@ -200,6 +204,7 @@ class mod_data_generator_testcase extends advanced_testcase {
|
||||
|
||||
$tags = ['Cats', 'mice'];
|
||||
|
||||
$this->setUser($user1);
|
||||
$datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data,
|
||||
$fieldcontents, $groupa->id, $tags);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user