weekly release 2.6dev
[moodle.git] / lib / tests / behat / behat_data_generators.php
CommitLineData
f0200d14 1<?php
f0200d14
DM
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 * Data generators for acceptance testing.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../behat/behat_base.php');
f0200d14
DM
29
30use Behat\Gherkin\Node\TableNode as TableNode;
31use Behat\Behat\Exception\PendingException as PendingException;
32
33/**
34 * Class to set up quickly a Given environment.
35 *
36 * Acceptance tests are block-boxed, so this steps definitions should only
37 * be used to set up the test environment as we are not replicating user steps.
38 *
d2eca4cd 39 * All data generators should be in lib/testing/generator/*, shared between phpunit
f0200d14 40 * and behat and they should be called from here, if possible using the standard
d2eca4cd
DM
41 * 'create_$elementname($options)' and if it's not possible (data generators arguments will not be
42 * always the same) or the element is not suitable to be a data generator, create a
43 * 'process_$elementname($options)' method and use the data generator from there if possible.
f0200d14
DM
44 *
45 * @todo If the available elements list grows too much this class must be split into smaller pieces
46 * @package core
47 * @category test
48 * @copyright 2012 David MonllaĆ³
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50 */
51class behat_data_generators extends behat_base {
52
46ac40cd
DM
53 /**
54 * @var testing_data_generator
55 */
f0200d14
DM
56 protected $datagenerator;
57
58 /**
59 * Each element specifies:
60 * - The data generator sufix used.
61 * - The required fields.
62 * - The mapping between other elements references and database field names.
63 * @var array
64 */
65 protected static $elements = array(
66 'users' => array(
67 'datagenerator' => 'user',
68 'required' => array('username')
69 ),
70 'categories' => array(
71 'datagenerator' => 'category',
72 'required' => array('idnumber'),
73 'switchids' => array('category' => 'parent')
74 ),
75 'courses' => array(
76 'datagenerator' => 'course',
77 'required' => array('shortname'),
78 'switchids' => array('category' => 'category')
79 ),
80 'groups' => array(
81 'datagenerator' => 'group',
82 'required' => array('idnumber', 'course'),
83 'switchids' => array('course' => 'courseid')
84 ),
85 'groupings' => array(
86 'datagenerator' => 'grouping',
87 'required' => array('idnumber', 'course'),
88 'switchids' => array('course' => 'courseid')
89 ),
90 'course enrolments' => array(
91 'datagenerator' => 'enrol_user',
92 'required' => array('user', 'course', 'role'),
93 'switchids' => array('user' => 'userid', 'course' => 'courseid', 'role' => 'roleid')
94
95 ),
d2eca4cd
DM
96 'system role assigns' => array(
97 'datagenerator' => 'role_assign',
98 'required' => array('user', 'role'),
99 'switchids' => array('user' => 'userid', 'role' => 'roleid')
100 ),
f0200d14
DM
101 'group members' => array(
102 'datagenerator' => 'group_member',
103 'required' => array('user', 'group'),
104 'switchids' => array('user' => 'userid', 'group' => 'groupid')
105 ),
106 'grouping groups' => array(
107 'datagenerator' => 'grouping_group',
108 'required' => array('grouping', 'group'),
109 'switchids' => array('grouping' => 'groupingid', 'group' => 'groupid')
23349a73
DM
110 ),
111 'cohorts' => array(
112 'datagenerator' => 'cohort',
113 'required' => array('idnumber')
f0200d14
DM
114 )
115 );
116
117 /**
118 * Creates the specified element. More info about available elements in http://docs.moodle.org/dev/Acceptance_testing#Fixtures.
119 *
120 * @Given /^the following "(?P<element_string>(?:[^"]|\\")*)" exists:$/
121 *
122 * @throws Exception
123 * @throws PendingException
124 * @param string $elementname The name of the entity to add
125 * @param TableNode $data
126 */
127 public function the_following_exists($elementname, TableNode $data) {
128
c29e3e24
DM
129 // Now that we need them require the data generators.
130 require_once(__DIR__ . '/../../testing/generator/lib.php');
131
f0200d14
DM
132 if (empty(self::$elements[$elementname])) {
133 throw new PendingException($elementname . ' data generator is not implemented');
134 }
135
136 $this->datagenerator = testing_util::get_data_generator();
137
138 $elementdatagenerator = self::$elements[$elementname]['datagenerator'];
139 $requiredfields = self::$elements[$elementname]['required'];
140 if (!empty(self::$elements[$elementname]['switchids'])) {
141 $switchids = self::$elements[$elementname]['switchids'];
142 }
143
144 foreach ($data->getHash() as $elementdata) {
145
146 // Check if all the required fields are there.
147 foreach ($requiredfields as $requiredfield) {
148 if (!isset($elementdata[$requiredfield])) {
149 throw new Exception($elementname . ' requires the field ' . $requiredfield . ' to be specified');
150 }
151 }
152
153 // Switch from human-friendly references to ids.
154 if (isset($switchids)) {
155 foreach ($switchids as $element => $field) {
156 $methodname = 'get_' . $element . '_id';
157
158 // Not all the switch fields are required, default vars will be assigned by data generators.
159 if (isset($elementdata[$element])) {
160 // Temp $id var to avoid problems when $element == $field.
161 $id = $this->{$methodname}($elementdata[$element]);
162 unset($elementdata[$element]);
163 $elementdata[$field] = $id;
164 }
165 }
166 }
167
168 // Preprocess the entities that requires a special treatment.
169 if (method_exists($this, 'preprocess_' . $elementdatagenerator)) {
170 $elementdata = $this->{'preprocess_' . $elementdatagenerator}($elementdata);
171 }
172
173 // Creates element.
174 $methodname = 'create_' . $elementdatagenerator;
175 if (method_exists($this->datagenerator, $methodname)) {
176 // Using data generators directly.
177 $this->datagenerator->{$methodname}($elementdata);
178
d2eca4cd
DM
179 } else if (method_exists($this, 'process_' . $elementdatagenerator)) {
180 // Using an alternative to the direct data generator call.
181 $this->{'process_' . $elementdatagenerator}($elementdata);
f0200d14
DM
182 } else {
183 throw new PendingException($elementname . ' data generator is not implemented');
184 }
185 }
186
187 }
188
189 /**
190 * If password is not set it uses the username.
191 * @param array $data
192 * @return array
193 */
194 protected function preprocess_user($data) {
195 if (!isset($data['password'])) {
196 $data['password'] = $data['username'];
197 }
198 return $data;
199 }
200
201
202 /**
203 * Adapter to enrol_user() data generator.
204 * @throws Exception
d2eca4cd 205 * @param array $data
f0200d14
DM
206 * @return void
207 */
d2eca4cd 208 protected function process_enrol_user($data) {
a8a48495 209 global $SITE;
f0200d14
DM
210
211 if (empty($data['roleid'])) {
212 throw new Exception('\'course enrolments\' requires the field \'role\' to be specified');
213 }
214
215 if (!isset($data['userid'])) {
216 throw new Exception('\'course enrolments\' requires the field \'user\' to be specified');
217 }
218
219 if (!isset($data['courseid'])) {
220 throw new Exception('\'course enrolments\' requires the field \'course\' to be specified');
221 }
222
223 if (!isset($data['enrol'])) {
224 $data['enrol'] = 'manual';
225 }
226
a8a48495
DM
227 // If the provided course shortname is the site shortname we consider it a system role assign.
228 if ($data['courseid'] == $SITE->id) {
229 // Frontpage course assign.
230 $context = context_course::instance($data['courseid']);
231 role_assign($data['roleid'], $data['userid'], $context->id);
232
233 } else {
234 // Course assign.
235 $this->datagenerator->enrol_user($data['userid'], $data['courseid'], $data['roleid'], $data['enrol']);
236 }
237
f0200d14
DM
238 }
239
d2eca4cd
DM
240 /**
241 * Assigns a role to a user at system level.
242 * @throws Exception
243 * @param array $data
244 * @return void
245 */
246 protected function process_role_assign($data) {
247
248 if (empty($data['roleid'])) {
249 throw new Exception('\'system role assigns\' requires the field \'role\' to be specified');
250 }
251
252 if (!isset($data['userid'])) {
253 throw new Exception('\'system role assigns\' requires the field \'user\' to be specified');
254 }
255
256 $context = context_system::instance();
257 role_assign($data['roleid'], $data['userid'], $context->id);
f0200d14
DM
258 }
259
260 /**
261 * Gets the user id from it's username.
262 * @throws Exception
46ac40cd 263 * @param string $username
f0200d14
DM
264 * @return int
265 */
266 protected function get_user_id($username) {
267 global $DB;
268
269 if (!$id = $DB->get_field('user', 'id', array('username' => $username))) {
1f9ffbdb 270 throw new Exception('The specified user with username "' . $username . '" does not exist');
f0200d14
DM
271 }
272 return $id;
273 }
274
275 /**
276 * Gets the role id from it's shortname.
277 * @throws Exception
46ac40cd 278 * @param string $roleshortname
f0200d14
DM
279 * @return int
280 */
281 protected function get_role_id($roleshortname) {
282 global $DB;
283
284 if (!$id = $DB->get_field('role', 'id', array('shortname' => $roleshortname))) {
1f9ffbdb 285 throw new Exception('The specified role with shortname"' . $roleshortname . '" does not exist');
f0200d14
DM
286 }
287
288 return $id;
289 }
290
291 /**
292 * Gets the category id from it's idnumber.
293 * @throws Exception
294 * @param string $idnumber
295 * @return int
296 */
297 protected function get_category_id($idnumber) {
298 global $DB;
299
300 // If no category was specified use the data generator one.
301 if ($idnumber == false) {
302 return null;
303 }
304
305 if (!$id = $DB->get_field('course_categories', 'id', array('idnumber' => $idnumber))) {
1f9ffbdb 306 throw new Exception('The specified category with idnumber "' . $idnumber . '" does not exist');
f0200d14
DM
307 }
308
309 return $id;
310 }
311
312 /**
313 * Gets the course id from it's shortname.
314 * @throws Exception
315 * @param string $shortname
316 * @return int
317 */
318 protected function get_course_id($shortname) {
319 global $DB;
320
321 if (!$id = $DB->get_field('course', 'id', array('shortname' => $shortname))) {
1f9ffbdb 322 throw new Exception('The specified course with shortname"' . $shortname . '" does not exist');
f0200d14
DM
323 }
324 return $id;
325 }
326
327 /**
328 * Gets the group id from it's idnumber.
329 * @throws Exception
330 * @param string $idnumber
331 * @return int
332 */
333 protected function get_group_id($idnumber) {
334 global $DB;
335
336 if (!$id = $DB->get_field('groups', 'id', array('idnumber' => $idnumber))) {
1f9ffbdb 337 throw new Exception('The specified group with idnumber "' . $idnumber . '" does not exist');
f0200d14
DM
338 }
339 return $id;
340 }
341
342 /**
343 * Gets the grouping id from it's idnumber.
344 * @throws Exception
345 * @param string $idnumber
346 * @return int
347 */
348 protected function get_grouping_id($idnumber) {
349 global $DB;
350
351 if (!$id = $DB->get_field('groupings', 'id', array('idnumber' => $idnumber))) {
1f9ffbdb 352 throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
f0200d14
DM
353 }
354 return $id;
355 }
356}