MDL_41126 phpunit: Timestamp should be less than 2038
[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/**
400eb2d0
DG
18 * mod_data data generator class
19 * Currently, the field types in the ignoredfieldtypes array aren't supported.
cbdf52ba
PS
20 *
21 * @package mod_data
6b219869 22 * @category test
cbdf52ba
PS
23 * @copyright 2012 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29
30/**
5c3c2c81 31 * Database module data generator class
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) {
cbdf52ba 77 $record = (object)(array)$record;
cbdf52ba 78
6b04fdc0
PS
79 if (!isset($record->assessed)) {
80 $record->assessed = 0;
81 }
82 if (!isset($record->scale)) {
83 $record->scale = 0;
84 }
cbdf52ba 85
7fbe33fc 86 return parent::create_instance($record, (array)$options);
cbdf52ba 87 }
400eb2d0
DG
88
89
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 */
98 public function create_field($record = null, $data = null) {
99 global $DB;
100
101 $record = (array) $record;
102
103 if (in_array($record['type'], $this->ignoredfieldtypes)) {
104 throw new coding_exception('$record\'s type value must not be same as values in ignoredfieldtypes
105 in phpunit_util::create_field()');
106 return false;
107 }
108
109 $this->databasefieldcount++;
110
111 if (!isset($data->course)) {
112 throw new coding_exception('course must be present in phpunit_util::create_field() $data');
113 }
114
115 if (!isset($data->id)) {
116 throw new coding_exception('dataid must be present in phpunit_util::create_field() $data');
117 } else {
118 $record['dataid'] = $data->id;
119 }
120
121 if (!isset($record['type'])) {
122 throw new coding_exception('type must be present in phpunit_util::create_field() $record');
123 }
124
125 if (!isset($record['required'])) {
126 $record['required'] = 0;
127 }
128
129 if (!isset($record['name'])) {
130 $record['name'] = "testField - " . $this->databasefieldcount;
131 }
132
133 if (!isset($record['description'])) {
134 $record['description'] = " This is testField - " . $this->databasefieldcount;
135 }
136
137 if (!isset($record['param1'])) {
138 if (in_array($record['type'], array('checkbox', 'menu', 'multimenu', 'radiobutton'))) {
139 $record['param1'] = implode("\n", array('one', 'two', 'three', 'four'));
140 } else if (($record['type'] === 'text') || ($record['type'] === 'url')) {
141 $record['param1'] = 1;
142 } else {
143 $record['param1'] = '';
144 }
145 }
146
147 if (!isset($record['param2'])) {
148
149 if ($record['type'] === 'textarea') {
150 $record['param2'] = 60;
151 } else {
152 $record['param2'] = '';
153 }
154 }
155
156 if (!isset($record['param3'])) {
157
158 if (($record['type'] === 'textarea')) {
159 $record['param3'] = 35;
160 } else {
161 $record['param3'] = '';
162 }
163 }
164
165 if (!isset($record['param4'])) {
166
167 if (($record['type'] === 'textarea')) {
168 $record['param4'] = 1;
169 }
170 }
171
172 if (!isset($record['param5'])) {
173 if (($record['type'] === 'textarea')) {
174 $record['param5'] = 0;
175 }
176 }
177
178 $record = (object) $record;
179
180 $field = data_get_field($record, $data);
181 $field->insert_field();
182
183 data_generate_default_template($data, 'addtemplate', 0, false, true);
184
185 return $field;
186 }
187
188 /**
189 * Creates a field for a mod_data instance.
190 * Currently, the field types in the ignoredfieldtypes array aren't supported.
191 * The developers using the generator must adhere to the following format :
192 *
193 * Syntax : $contents[ fieldid ] = fieldvalue
194 * $contents['checkbox'] = array('val1', 'val2', 'val3' .....)
195 * $contents['data'] = 'dd-mm-yyyy'
196 * $contents['menu'] = 'value';
197 * $contents['multimenu'] = array('val1', 'val2', 'val3' .....)
198 * $contents['number'] = 'numeric value'
199 * $contents['radiobuton'] = 'value'
200 * $contents['text'] = 'text'
201 * $contents['textarea'] = 'text'
202 * $contents['url'] = 'example.url' or array('example.url', 'urlname')
203 *
204 * @param mod_data $data
205 * @param array $contents
206 * @return data_field_{type}
207 */
208 public function create_entry($data, $contents) {
209 global $DB;
210
211 $this->databaserecordcount++;
212
213 $recordid = data_add_record($data);
214
215 $fields = $DB->get_records('data_fields', array( 'dataid' => $data->id));
216
217 // Validating whether required field are filled.
218 foreach ($fields as $field) {
219 $fieldhascontent = false;
220
221 if (in_array($field->type, $this->ignoredfieldtypes)) {
222 continue;
223 }
224
225 $field = data_get_field($field, $data);
226
227 $fieldid = $field->field->id;
228
229 if ($field->type === 'date') {
230 $values = array();
231
232 $temp = explode('-', $contents[$fieldid], 3);
233
234 $values['field_'.$fieldid.'_day'] = $temp[0];
235 $values['field_'.$fieldid.'_month'] = $temp[1];
236 $values['field_'.$fieldid.'_year'] = $temp[2];
237
238 foreach ($values as $fieldname => $value) {
239 if ($field->notemptyfield($value, $fieldname)) {
240 continue 2;
241 }
242 }
243 } else if ($field->type === 'textarea') {
244 $values = array();
245
246 $values['field_'.$fieldid] = $contents[$fieldid];
247 $values['field_'.$fieldid.'_content1'] = 1;
248
249 foreach ($values as $fieldname => $value) {
250 if ($field->notemptyfield ($value, $fieldname)) {
251 continue 2;
252 }
253 }
254 } else if ($field->type === 'url') {
255 $values = array();
256
257 if (is_array($contents[$fieldid])) {
258 foreach ($contents[$fieldid] as $key => $value) {
259 $values['field_'.$fieldid.'_'.$key] = $value;
260 }
261 } else {
262 $values['field_'.$fieldid.'_0'] = $contents[$fieldid];
263 }
264
265 foreach ($values as $fieldname => $value) {
266 if ($field->notemptyfield ($value, $fieldname)) {
267 continue 2;
268 }
269 }
270
271 } else {
272 if ($field->notemptyfield ($contents[$fieldid], 'field_'.$fieldid.'_0')) {
273 continue;
274 }
275 }
276
277 if ($field->field->required && !$fieldhascontent) {
278 return false;
279 }
280 }
281
282 foreach ($contents as $fieldid => $content) {
283
284 $field = $DB->get_record('data_fields', array( 'id' => $fieldid));
285 $field = data_get_field($field, $data);
286
287 if (in_array($field->field->type, $this->ignoredfieldtypes)) {
288 continue;
289 }
290
291 if ($field->type === 'date') {
292 $values = array();
293
294 $temp = explode('-', $content, 3);
295
9f2b916d
RT
296 $values['field_'.$fieldid.'_day'] = (int)trim($temp[0]);
297 $values['field_'.$fieldid.'_month'] = (int)trim($temp[1]);
298 $values['field_'.$fieldid.'_year'] = (int)trim($temp[2]);
299
300 // Year should be less than 2038, so it can be handled by 32 bit windows.
301 if ($values['field_'.$fieldid.'_year'] > 2038) {
302 throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond '.
303 '2038. Please use year less than 2038.');
304 }
400eb2d0
DG
305
306 foreach ($values as $fieldname => $value) {
9f2b916d 307 $field->update_content($recordid, $value, $fieldname);
400eb2d0
DG
308 }
309
310 continue;
311 }
312
313 if ($field->type === 'textarea') {
314 $values = array();
315
316 $values['field_'.$fieldid] = $content;
317 $values['field_'.$fieldid.'_content1'] = 1;
318
319 foreach ($values as $fieldname => $value) {
320 $field->update_content($recordid, $value, $fieldname);
321 }
322
323 continue;
324 }
325
326 if ($field->type === 'url') {
327 $values = array();
328
329 if (is_array($content)) {
330 foreach ($content as $key => $value) {
331 $values['field_'.$fieldid.'_'.$key] = $value;
332 }
333 } else {
334 $values['field_'.$fieldid.'_0'] = $content;
335 }
336
337 foreach ($values as $fieldname => $value) {
338 $field->update_content($recordid, $value, $fieldname);
339 }
340
341 continue;
342 }
343
344 $field->update_content($recordid, $contents[$fieldid]);
345 }
346
347 return $recordid;
348 }
cbdf52ba 349}