MDL_41126 phpunit: Timestamp should be less than 2038
[moodle.git] / mod / data / tests / generator_test.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/**
18 * PHPUnit data generator tests
19 *
20 * @package mod_data
21 * @category phpunit
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/**
30 * PHPUnit data generator testcase
31 *
32 * @package mod_data
33 * @category phpunit
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_data_generator_testcase extends advanced_testcase {
9bb2b515 38 public function test_generator() {
6b04fdc0 39 global $DB;
cbdf52ba
PS
40
41 $this->resetAfterTest(true);
42
43 $this->assertEquals(0, $DB->count_records('data'));
44
6b04fdc0
PS
45 $course = $this->getDataGenerator()->create_course();
46
cbdf52ba
PS
47 /** @var mod_data_generator $generator */
48 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
49 $this->assertInstanceOf('mod_data_generator', $generator);
50 $this->assertEquals('data', $generator->get_modulename());
51
400eb2d0
DG
52 $generator->create_instance(array('course' => $course->id));
53 $generator->create_instance(array('course' => $course->id));
54 $data = $generator->create_instance(array('course' => $course->id));
cbdf52ba
PS
55 $this->assertEquals(3, $DB->count_records('data'));
56
57 $cm = get_coursemodule_from_instance('data', $data->id);
58 $this->assertEquals($data->id, $cm->instance);
59 $this->assertEquals('data', $cm->modname);
6b04fdc0 60 $this->assertEquals($course->id, $cm->course);
cbdf52ba
PS
61
62 $context = context_module::instance($cm->id);
63 $this->assertEquals($data->cmid, $context->instanceid);
6b04fdc0
PS
64
65 // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
400eb2d0
DG
66 $data = $generator->create_instance(array('course' => $course->id, 'assessed' => 1, 'scale' => 100));
67 $gitem = $DB->get_record('grade_items', array('courseid' => $course->id, 'itemtype' => 'mod',
68 'itemmodule' => 'data', 'iteminstance' => $data->id));
6b04fdc0
PS
69 $this->assertNotEmpty($gitem);
70 $this->assertEquals(100, $gitem->grademax);
71 $this->assertEquals(0, $gitem->grademin);
72 $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
73
cbdf52ba 74 }
400eb2d0
DG
75
76
77 public function test_create_field() {
78 global $DB;
79
80 $this->resetAfterTest(true);
81
82 $this->setAdminUser();
83 $this->assertEquals(0, $DB->count_records('data'));
84
85 $course = $this->getDataGenerator()->create_course();
86
87 /** @var mod_data_generator $generator */
88 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
89 $this->assertInstanceOf('mod_data_generator', $generator);
90 $this->assertEquals('data', $generator->get_modulename());
91
92 $data = $generator->create_instance(array('course' => $course->id));
93 $this->assertEquals(1, $DB->count_records('data'));
94
95 $cm = get_coursemodule_from_instance('data', $data->id);
96 $this->assertEquals($data->id, $cm->instance);
97 $this->assertEquals('data', $cm->modname);
98 $this->assertEquals($course->id, $cm->course);
99
100 $context = context_module::instance($cm->id);
101 $this->assertEquals($data->cmid, $context->instanceid);
102
103 $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
104
105 $count = 1;
106
107 // Creating test Fields with default parameter values.
108 foreach ($fieldtypes as $fieldtype) {
109
110 // Creating variables dynamically.
111 $fieldname = 'field-'.$count;
112 $record = new StdClass();
113 $record->name = $fieldname;
114 $record->type = $fieldtype;
115
116 ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
117
118 $this->assertInstanceOf('data_field_'.$fieldtype , ${$fieldname});
119 $count++;
120 }
121
122 $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
123
124 $addtemplate = $DB->get_record('data', array( 'id' => $data->id ), 'addtemplate');
125 $addtemplate = $addtemplate->addtemplate;
126
127 for ($i = 1; $i < $count; $i++) {
128 $fieldname = 'field-'.$i;
129 $this->assertTrue(strpos($addtemplate, '[['.$fieldname.']]') >= 0);
130 }
131 }
132
133
134 public function test_create_entry() {
135 global $DB;
136
137 $this->resetAfterTest(true);
138
139 $this->setAdminUser();
140 $this->assertEquals(0, $DB->count_records('data'));
141
142 $course = $this->getDataGenerator()->create_course();
143
144 /** @var mod_data_generator $generator */
145 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
146 $this->assertInstanceOf('mod_data_generator', $generator);
147 $this->assertEquals('data', $generator->get_modulename());
148
149 $data = $generator->create_instance(array('course' => $course->id));
150 $this->assertEquals(1, $DB->count_records('data'));
151
152 $cm = get_coursemodule_from_instance('data', $data->id);
153 $this->assertEquals($data->id, $cm->instance);
154 $this->assertEquals('data', $cm->modname);
155 $this->assertEquals($course->id, $cm->course);
156
157 $context = context_module::instance($cm->id);
158 $this->assertEquals($data->cmid, $context->instanceid);
159
160 $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
161
162 $count = 1;
163
164 // Creating test Fields with default parameter values.
165 foreach ($fieldtypes as $fieldtype) {
166
167 // Creating variables dynamically.
168 $fieldname = 'field-'.$count;
169 $record = new StdClass();
170 $record->name = $fieldname;
171 $record->type = $fieldtype;
172
173 ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
174 $this->assertInstanceOf('data_field_'.$fieldtype , ${$fieldname});
175 $count++;
176 }
177
178 $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
179
180 $fields = $DB->get_records('data_fields', array('dataid' => $data->id), 'id');
181
182 $contents = array();
183 $contents[] = array('one', 'two', 'three', 'four');
9f2b916d 184 $contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows.
400eb2d0
DG
185 $contents[] = 'one';
186 $contents[] = array('one', 'two', 'three', 'four');
187 $contents[] = '12345';
188 $contents[] = 'one';
189 $contents[] = 'text for testing';
190 $contents[] = '<p>text area testing<br /></p>';
191 $contents[] = array('example.url', 'sampleurl');
192 $count = 0;
193 $fieldcontents = array();
194 foreach ($fields as $fieldrecord) {
195 $fieldcontents[$fieldrecord->id] = $contents[$count++];
196 }
197
198 $datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data, $fieldcontents);
199
200 $this->assertEquals(1, $DB->count_records('data_records', array( 'dataid' => $data->id )));
201
202 $this->assertEquals(count($contents), $DB->count_records('data_content', array('recordid' => $datarecordid)));
203
204 }
205
206}