MDL-41126 mod_data: optimised data entry generator
[moodle.git] / mod / data / tests / generator / lib.php
CommitLineData
cbdf52ba
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
d5bd76f4 18 * Data generator class for mod_data.
cbdf52ba
PS
19 *
20 * @package mod_data
6b219869 21 * @category test
cbdf52ba
PS
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
d5bd76f4
AN
30 * Data generator class for mod_data.
31 *
400eb2d0 32 * Currently, the field types in the ignoredfieldtypes array aren't supported.
cbdf52ba
PS
33 *
34 * @package mod_data
6b219869 35 * @category test
cbdf52ba
PS
36 * @copyright 2012 Petr Skoda {@link http://skodak.org}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
5c3c2c81 39class mod_data_generator extends testing_module_generator {
cbdf52ba 40
400eb2d0
DG
41 /**
42 * @var int keep track of how many database fields have been created.
43 */
44 protected $databasefieldcount = 0;
45
46 /**
47 * @var int keep track of how many database records have been created.
48 */
49 protected $databaserecordcount = 0;
50
51 /**
52 * @var The field types which not handled by the generator as of now.
53 */
54 protected $ignoredfieldtypes = array('latlong', 'file', 'picture');
55
56
57 /**
58 * To be called from data reset code only,
59 * do not use in tests.
60 * @return void
61 */
62 public function reset() {
63 $this->databasefieldcount = 0;
64 $this->databaserecordcount = 0;
65
66 parent::reset();
67 }
68
69 /**
70 * Creates a mod_data instance
71 *
72 * @param array $record
73 * @param array $options
74 * @return StdClass
75 */
cbdf52ba 76 public function create_instance($record = null, array $options = null) {
19491e68
AN
77 // Note, the parent class does not type $record to cast to array and then to object.
78 $record = (object) (array) $record;
cbdf52ba 79
6b04fdc0
PS
80 if (!isset($record->assessed)) {
81 $record->assessed = 0;
82 }
83 if (!isset($record->scale)) {
84 $record->scale = 0;
85 }
cbdf52ba 86
19491e68 87 return parent::create_instance((array) $record, $options);
cbdf52ba 88 }
400eb2d0 89
400eb2d0
DG
90 /**
91 * Creates a field for a mod_data instance.
92 * Currently, the field types in the ignoredfieldtypes array aren't supported.
93 *
94 * @param StdClass $record
95 * @param mod_data $data
96 * @return data_field_{type}
97 */
19491e68 98 public function create_field(stdClass $record = null, $data = null) {
400eb2d0
DG
99 $record = (array) $record;
100
101 if (in_array($record['type'], $this->ignoredfieldtypes)) {
102 throw new coding_exception('$record\'s type value must not be same as values in ignoredfieldtypes
103 in phpunit_util::create_field()');
104 return false;
105 }
106
107 $this->databasefieldcount++;
108
109 if (!isset($data->course)) {
110 throw new coding_exception('course must be present in phpunit_util::create_field() $data');
111 }
112
113 if (!isset($data->id)) {
114 throw new coding_exception('dataid must be present in phpunit_util::create_field() $data');
115 } else {
116 $record['dataid'] = $data->id;
117 }
118
119 if (!isset($record['type'])) {
120 throw new coding_exception('type must be present in phpunit_util::create_field() $record');
121 }
122
123 if (!isset($record['required'])) {
124 $record['required'] = 0;
125 }
126
127 if (!isset($record['name'])) {
128 $record['name'] = "testField - " . $this->databasefieldcount;
129 }
130
131 if (!isset($record['description'])) {
132 $record['description'] = " This is testField - " . $this->databasefieldcount;
133 }
134
135 if (!isset($record['param1'])) {
136 if (in_array($record['type'], array('checkbox', 'menu', 'multimenu', 'radiobutton'))) {
137 $record['param1'] = implode("\n", array('one', 'two', 'three', 'four'));
138 } else if (($record['type'] === 'text') || ($record['type'] === 'url')) {
139 $record['param1'] = 1;
140 } else {
141 $record['param1'] = '';
142 }
143 }
144
145 if (!isset($record['param2'])) {
146
147 if ($record['type'] === 'textarea') {
148 $record['param2'] = 60;
149 } else {
150 $record['param2'] = '';
151 }
152 }
153
154 if (!isset($record['param3'])) {
155
156 if (($record['type'] === 'textarea')) {
157 $record['param3'] = 35;
158 } else {
159 $record['param3'] = '';
160 }
161 }
162
163 if (!isset($record['param4'])) {
164
165 if (($record['type'] === 'textarea')) {
166 $record['param4'] = 1;
167 }
168 }
169
170 if (!isset($record['param5'])) {
171 if (($record['type'] === 'textarea')) {
172 $record['param5'] = 0;
173 }
174 }
175
176 $record = (object) $record;
177
178 $field = data_get_field($record, $data);
179 $field->insert_field();
180
181 data_generate_default_template($data, 'addtemplate', 0, false, true);
182
183 return $field;
184 }
185
186 /**
187 * Creates a field for a mod_data instance.
188 * Currently, the field types in the ignoredfieldtypes array aren't supported.
189 * The developers using the generator must adhere to the following format :
190 *
191 * Syntax : $contents[ fieldid ] = fieldvalue
192 * $contents['checkbox'] = array('val1', 'val2', 'val3' .....)
193 * $contents['data'] = 'dd-mm-yyyy'
194 * $contents['menu'] = 'value';
195 * $contents['multimenu'] = array('val1', 'val2', 'val3' .....)
196 * $contents['number'] = 'numeric value'
197 * $contents['radiobuton'] = 'value'
198 * $contents['text'] = 'text'
199 * $contents['textarea'] = 'text'
200 * $contents['url'] = 'example.url' or array('example.url', 'urlname')
201 *
202 * @param mod_data $data
203 * @param array $contents
204 * @return data_field_{type}
205 */
19491e68 206 public function create_entry($data, array $contents) {
400eb2d0
DG
207 global $DB;
208
209 $this->databaserecordcount++;
210
211 $recordid = data_add_record($data);
212
d5bd76f4 213 $fields = $DB->get_records('data_fields', array('dataid' => $data->id));
400eb2d0
DG
214
215 // Validating whether required field are filled.
216 foreach ($fields as $field) {
2ac7ea86 217 $fieldhascontent = true;
400eb2d0
DG
218
219 if (in_array($field->type, $this->ignoredfieldtypes)) {
220 continue;
221 }
222
223 $field = data_get_field($field, $data);
224
225 $fieldid = $field->field->id;
226
227 if ($field->type === 'date') {
228 $values = array();
229
230 $temp = explode('-', $contents[$fieldid], 3);
231
2ac7ea86
DG
232 $values['field_' . $fieldid . '_day'] = (int)trim($temp[0]);
233 $values['field_' . $fieldid . '_month'] = (int)trim($temp[1]);
234 $values['field_' . $fieldid . '_year'] = (int)trim($temp[2]);
235
236 // Year should be less than 2038, so it can be handled by 32 bit windows.
237 if ($values['field_' . $fieldid . '_year'] > 2038) {
238 throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond ' .
239 '2038. Please use year less than 2038.');
240 }
241
242 $contents[$fieldid] = $values;
400eb2d0
DG
243
244 foreach ($values as $fieldname => $value) {
2ac7ea86
DG
245 if (!$field->notemptyfield($value, $fieldname)) {
246 $fieldhascontent = false;
400eb2d0
DG
247 }
248 }
249 } else if ($field->type === 'textarea') {
250 $values = array();
251
d5bd76f4
AN
252 $values['field_' . $fieldid] = $contents[$fieldid];
253 $values['field_' . $fieldid . '_content1'] = 1;
400eb2d0 254
2ac7ea86
DG
255 $contents[$fieldid] = $values;
256
400eb2d0 257 foreach ($values as $fieldname => $value) {
2ac7ea86
DG
258 if (!$field->notemptyfield($value, $fieldname)) {
259 $fieldhascontent = false;
400eb2d0
DG
260 }
261 }
262 } else if ($field->type === 'url') {
263 $values = array();
264
265 if (is_array($contents[$fieldid])) {
266 foreach ($contents[$fieldid] as $key => $value) {
d5bd76f4 267 $values['field_' . $fieldid . '_' . $key] = $value;
400eb2d0
DG
268 }
269 } else {
d5bd76f4 270 $values['field_' . $fieldid . '_0'] = $contents[$fieldid];
400eb2d0
DG
271 }
272
2ac7ea86
DG
273 $contents[$fieldid] = $values;
274
400eb2d0 275 foreach ($values as $fieldname => $value) {
2ac7ea86
DG
276 if (!$field->notemptyfield($value, $fieldname)) {
277 $fieldhascontent = false;
400eb2d0
DG
278 }
279 }
400eb2d0 280 } else {
d5bd76f4 281 if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) {
400eb2d0
DG
282 continue;
283 }
284 }
285
286 if ($field->field->required && !$fieldhascontent) {
287 return false;
288 }
289 }
290
291 foreach ($contents as $fieldid => $content) {
2ac7ea86 292 $field = data_get_field_from_id($fieldid, $data);
400eb2d0 293
2ac7ea86 294 if (is_array($content)) {
400eb2d0 295
2ac7ea86 296 foreach ($content as $fieldname => $value) {
400eb2d0
DG
297 $field->update_content($recordid, $value, $fieldname);
298 }
299
2ac7ea86
DG
300 } else {
301 $field->update_content($recordid, $content);
400eb2d0 302 }
400eb2d0
DG
303 }
304
305 return $recordid;
306 }
cbdf52ba 307}