BehatDataGenerators.php 13.2 KB
Newer Older
Son Nguyen's avatar
Son Nguyen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
 * @package    mahara
 * @subpackage test/behat
 * @author     Son Nguyen, Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 * @copyright  portions from Moodle Behat, 2013 David Monllaó
 *
 */

require_once(__DIR__ . '/BehatBase.php');

use Behat\Gherkin\Node\TableNode as TableNode;
use Behat\Behat\Exception\PendingException as PendingException;

/**
 * Class to set up quickly a Given environment.
 *
 */
class BehatDataGenerators extends BehatBase {

    /**
     * @var testing_data_generator
     */
    protected $datagenerator;

    /**
     * Each element specifies:
     * - The data generator suffix used.
     * - The available fields array(fieldname=>fieldtype)
     * - The required fields.
     * - The mapping between other elements references and database field names.
     * @var array
     */
    protected static $elements = array(
        'users' => array(
            'datagenerator' => 'user',
            'available' => array(
                'username'     => 'text',
                'password'     => 'text',
                'email'        => 'text',
                'firstname'    => 'text',
                'lastname'     => 'text',
                'institution'  => 'text',
                'role'         => 'text',
                'authname'     => 'text',
                'remoteusername' => 'text',
49
50
51
52
53
                'studentid'    => 'text',
                'preferredname' => 'text',
                'town'         => 'text',
                'country'      => 'text',
                'occupation'   => 'text',
Son Nguyen's avatar
Son Nguyen committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
            ),
            'required' => array('username', 'password', 'email', 'firstname', 'lastname')
        ),
        'groups' => array(
            'datagenerator' => 'group',
            'available' => array(
                'name'             => 'text',
                'owner'            => 'text',
                'description'      => 'text',
                'grouptype'        => 'text',
                'open'             => 'bool',
                'controlled'       => 'bool',
                'request'          => 'bool',
                'invitefriends'    => 'bool',
                'suggestfriends'   => 'bool',
                'editroles'        => 'text',
                'submittableto'    => 'bool',
                'allowarchives'    => 'bool',
                'editwindowstart'  => 'text',
                'editwindowend'    => 'text',
                'members'          => 'text',
                'staff'            => 'text',
                'admins'           => 'text',
77
                'institution'      => 'text',
78
                'public'           => 'bool',
Son Nguyen's avatar
Son Nguyen committed
79
            ),
80
            'required' => array('name', 'owner')
Son Nguyen's avatar
Son Nguyen committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        ),
        'institutions' => array(
            'datagenerator' => 'institution',
            'available' => array(
                'name'             => 'text',
                'displayname'      => 'text',
                'showonlineusers'  => 'number',
                'registerallowed'  => 'bool',
                'registerconfirm'  => 'bool',
                'lang'             => 'text',
                'theme'            => 'text',
                'dropdownmenu'     => 'bool',
                'skins'            => 'bool',
                'licensemandatory' => 'bool',
                'licensedefault'   => 'text',
                'defaultquota'     => 'number',
                'defaultmembershipperiod' => 'number',
                'maxuseraccounts'  => 'number',
                'expiry'           => 'text',
                'allowinstitutionpublicviews' => 'bool',
Aaron Wells's avatar
Aaron Wells committed
101
                'commentthreaded'  => 'bool',
Son Nguyen's avatar
Son Nguyen committed
102
103
104
                'members'          => 'text',
                'staff'            => 'text',
                'admins'           => 'text',
105
                'authname'         => 'string',
106
                'tags'             => 'bool',
Son Nguyen's avatar
Son Nguyen committed
107
108
109
            ),
            'required' => array('name', 'displayname')
        ),
110
111
112
113
114
115
116
117
        'pages' => array(
            'datagenerator' => 'view',
            'available' => array(
                'title'            => 'text',
                'description'      => 'text',
                'ownertype'        => 'text',
                'ownername'        => 'text',
                'layout'           => 'text',
118
                'tags'             => 'text',
119
            ),
120
            'required' => array('title', 'ownertype', 'ownername')
121
        ),
122
123
124
125
126
127
128
        'blocks' => array(
            'datagenerator' => 'block',
            'available' => array(
                'title'            => 'text',
                'type'             => 'text',
                'data'             => 'text',
                'page'             => 'text',
129
                /*'row'              => 'text',
130
                'column'           => 'text',
131
                'order'            => 'text',*/
132
                'retractable'      => 'text',
133
            ),
134
            'required' => array('title', 'type', 'page')
135
        ),
136
137
138
139
140
141
142
143
144
        'collections' => array(
            'datagenerator' => 'collection',
            'available' => array(
                'title'            => 'text',
                'description'      => 'text',
                'ownertype'        => 'text',
                'ownername'        => 'text',
                'pages'            => 'text',
            ),
145
            'required' => array('title', 'ownertype', 'ownername')
146
        ),
147
148
149
150
        'permissions' => array(
            'datagenerator' => 'permission',
            'available' => array(
                'title'            => 'text',
151
152
153
154
                'accesstype'       => 'text',
                'accessname'       => 'text',
                'allowcomments'    => 'bool',
                'approvecomments'  => 'bool',
155
156
                'role'             => 'text',
                'multiplepermissions'   => 'bool', // Set to true if wanting to add multiple access rules to a view
157
            ),
158
            'required' => array('title', 'accesstype')
159
        ),
Son Nguyen's avatar
Son Nguyen committed
160
161
162
163
164
165
166
167
        'group memberships' => array(
            'datagenerator' => 'group_membership',
            'required' => array('username', 'groupname', 'role')
        ),
        'institution memberships' => array(
            'datagenerator' => 'institution_membership',
            'required' => array('username', 'institutionname', 'role')
        ),
168
169
170
171
172
173
174
175
176
177
178
179
        'messages' => array(
            'datagenerator' => 'message',
            'available' => array(
                'emailtype'        => 'text',
                'to'               => 'text',
                'from'             => 'text',
                'subject'          => 'text',
                'messagebody'      => 'text',
                'read'             => 'bool',
                'url'              => 'text',
                'urltext'          => 'text',
            ),
180
           'required' => array('emailtype', 'to', 'subject')
181
        ),
182
        'journals' => array(
183
            'datagenerator' => 'blog',
184
185
186
187
188
189
190
            'available' => array(
                'owner'            => 'text',
                'ownertype'        => 'text',
                'title'            => 'text',
                'description'      => 'text',
                'tags'             => 'text',
            ),
191
           'required' => array('owner', 'ownertype', 'title')
192
        ),
193
194
        'journalentries' => array(
            'datagenerator' => 'blogpost',
195
196
197
198
199
200
201
202
203
            'available' => array(
                'owner'            => 'text',
                'ownertype'        => 'text',
                'title'            => 'text',
                'entry'            => 'text',
                'blog'             => 'text',
                'tags'             => 'text',
                'draft'            => 'bool',
            ),
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
           'required' => array('owner', 'ownertype', 'title', 'entry')
        ),
        'plans' => array(
            'datagenerator' => 'plan',
            'available' => array(
                'owner'            => 'text',
                'ownertype'        => 'text',
                'title'            => 'text',
                'description'      => 'text',
                'tags'             => 'text',
            ),
           'required' => array('owner', 'ownertype', 'title')
        ),
        'tasks' => array(
          'datagenerator' => 'task',
          'available' => array(
            'owner'                => 'text',
            'ownertype'            => 'text',
            'plan'                 => 'text',
            'title'                => 'text',
            'description'          => 'text',
            'completiondate'       => 'text',
            'completed'            => 'bool',
            'tags'                 => 'text'
          ),
          'required' => array('owner', 'ownertype', 'plan', 'title', 'completiondate')
230
        ),
Son Nguyen's avatar
Son Nguyen committed
231
232
233
234
235
236
237
238
239
240
241
242
    );

    /**
     * Normalise values in a given record
     * For example, 'ON' -> 1, 'OFF' -> 0
     * @param array ('field' => 'values', ...) $record
     * @return $record
     */
    public function normalise(&$record) {
        foreach ($record as &$value) {
            $value = trim($value);
            // Normalise boolean values
243
            if (strtolower($value) == 'on' || $value == '1' || $value == 'yes') {
Son Nguyen's avatar
Son Nguyen committed
244
245
                $value = true;
            }
246
            else if (strtolower($value) == 'off' || $value == '0' || $value == 'no') {
Son Nguyen's avatar
Son Nguyen committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                $value = false;
            }
        }
    }

    /**
     * Validate field values in a given record
     *
     * @param array ('fieldname' => 'fieldtype', ...) $availablefields
     * @param array ('fieldname' => 'values', ...) $record
     * @return void
     * @throws MaharaBehatTestException
     */
    public function validate_fields($availablefields, $record) {
        foreach ($record as $fieldname => $fieldvalue) {
            if (!in_array($fieldname, array_keys($availablefields))) {
                throw new MaharaBehatTestException("The field '" . $fieldname . "' is not available.\n".
                                "All available fields are " . implode(',', array_keys($availablefields)));
            }
            if ($availablefields[$fieldname] == 'bool' && !is_bool($fieldvalue)) {
                throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a boolean ('ON'|'OFF', '1'|'0' are accepted boolean values).");
            }
            if ($availablefields[$fieldname] == 'number' && !is_numeric($fieldvalue)) {
                throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a number.");
            }
        }
    }

    /**
     * Creates the specified element.
     *
     * @Given /^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/
     *
     * @throws MaharaBehatTestException
     * @throws PendingException
     * @param string    $elementname The name of the entity to add
     * @param TableNode $data
     */
    public function the_following_exist($elementname, TableNode $data) {

        // Now that we need them require the data generators.
        require_once(get_config('docroot') . '/testing/classes/generator/lib.php');

        if (empty(self::$elements[$elementname])) {
            throw new PendingException($elementname . ' data generator is not implemented');
        }

        $this->datagenerator = TestingUtil::get_data_generator();

        $elementdatagenerator = self::$elements[$elementname]['datagenerator'];
        $availablefields = self::$elements[$elementname]['available'];
        $requiredfields = self::$elements[$elementname]['required'];
        if (!empty(self::$elements[$elementname]['switchids'])) {
            $switchids = self::$elements[$elementname]['switchids'];
        }

        foreach ($data->getHash() as $elementdata) {

            // Normalise field values
            $this->normalise($elementdata);
            // Validate available fields for given element
            $this->validate_fields($availablefields, $elementdata);
            // Check if all the required fields are there.
            foreach ($requiredfields as $requiredfield) {
                if (!isset($elementdata[$requiredfield])) {
                    throw new MaharaBehatTestException($elementname . ' requires the field ' . $requiredfield . ' to be specified');
                }
            }

            // Preprocess the entities that requires a special treatment.
            if (method_exists($this, 'preprocess_' . $elementdatagenerator)) {
                $elementdata = $this->{'preprocess_' . $elementdatagenerator}($elementdata);
            }

            // Creates element.
            $methodname = 'create_' . $elementdatagenerator;
            if (method_exists($this->datagenerator, $methodname)) {
                // Using data generators directly.
                $this->datagenerator->{$methodname}($elementdata);
            }
            else if (method_exists($this, 'process_' . $elementdatagenerator)) {
                // Using an alternative to the direct data generator call.
                $this->{'process_' . $elementdatagenerator}($elementdata);
            }
            else {
                throw new PendingException($elementname . ' the create_ or process_ method is not implemented');
            }
        }

    }

}