BehatDataGenerators.php 19.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
        ),
        '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',
96
                'defaultquota'     => 'text',
Son Nguyen's avatar
Son Nguyen committed
97 98 99 100
                '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
                'retractable'      => 'text'
130
            ),
131
            'required' => array('title', 'type', 'page')
132
        ),
133 134 135 136 137 138 139 140 141
        'collections' => array(
            'datagenerator' => 'collection',
            'available' => array(
                'title'            => 'text',
                'description'      => 'text',
                'ownertype'        => 'text',
                'ownername'        => 'text',
                'pages'            => 'text',
            ),
142
            'required' => array('title', 'ownertype', 'ownername')
143
        ),
144 145 146 147
        'permissions' => array(
            'datagenerator' => 'permission',
            'available' => array(
                'title'            => 'text',
148 149 150 151
                'accesstype'       => 'text',
                'accessname'       => 'text',
                'allowcomments'    => 'bool',
                'approvecomments'  => 'bool',
152 153
                'role'             => 'text',
                'multiplepermissions'   => 'bool', // Set to true if wanting to add multiple access rules to a view
154
            ),
155
            'required' => array('title', 'accesstype')
156
        ),
Son Nguyen's avatar
Son Nguyen committed
157 158 159 160 161 162 163 164
        'group memberships' => array(
            'datagenerator' => 'group_membership',
            'required' => array('username', 'groupname', 'role')
        ),
        'institution memberships' => array(
            'datagenerator' => 'institution_membership',
            'required' => array('username', 'institutionname', 'role')
        ),
165 166 167 168 169 170 171 172 173 174 175 176
        'messages' => array(
            'datagenerator' => 'message',
            'available' => array(
                'emailtype'        => 'text',
                'to'               => 'text',
                'from'             => 'text',
                'subject'          => 'text',
                'messagebody'      => 'text',
                'read'             => 'bool',
                'url'              => 'text',
                'urltext'          => 'text',
            ),
177
           'required' => array('emailtype', 'to', 'subject')
178
        ),
179
        'journals' => array(
180
            'datagenerator' => 'blog',
181 182 183 184 185 186 187
            'available' => array(
                'owner'            => 'text',
                'ownertype'        => 'text',
                'title'            => 'text',
                'description'      => 'text',
                'tags'             => 'text',
            ),
188
           'required' => array('owner', 'ownertype', 'title')
189
        ),
190 191
        'journalentries' => array(
            'datagenerator' => 'blogpost',
192 193 194 195 196 197 198 199 200
            'available' => array(
                'owner'            => 'text',
                'ownertype'        => 'text',
                'title'            => 'text',
                'entry'            => 'text',
                'blog'             => 'text',
                'tags'             => 'text',
                'draft'            => 'bool',
            ),
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
           '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'
          ),
226
          'required' => array('owner', 'ownertype', 'plan', 'title', 'completiondate', 'completed')
227
        ),
228 229 230 231 232 233 234 235 236
        'forums' => array(
          'datagenerator' => 'forum',
          'available' => array(
            'title'                => 'text',
            'description'          => 'text',
            'group'                => 'text',
            'creator'              => 'text'
          ),
          'required' => array('title','description','group','creator')
237 238 239 240 241 242 243 244 245 246 247 248
        ),
        'forumposts' => array(
          'datagenerator' => 'forumpost',
          'available' => array(
            'group'                => 'text',
            'forum'                => 'text',
            'subject'              => 'text',
            'message'              => 'text',
            'user'                 => 'text',
            'topic'                => 'text',
          ),
          'required' => array('group', 'message', 'user')
249 250
        ),
        'personalinformation' => array(
251
          'datagenerator' => 'resume_personalinformation',
252 253 254 255 256 257 258 259 260 261 262 263
          'available' => array(
            'user'                 => 'text',
            'dateofbirth'          => 'text',
            'placeofbirth'         => 'text',
            'citizenship'          => 'text',
            'visastatus'           => 'text',
            'gender'               => 'text',
            'maritalstatus'        => 'text'
          ),
          'required' => array('user', 'dateofbirth')
        ),
        'goals and skills' => array(
264
          'datagenerator' => 'resume_goalsandskills',
265 266 267 268 269
          'available' => array(
            'user'                 => 'text',
            'goaltype/skilltype'   => 'text',
            'title'                => 'text',
            'description'          => 'text',
270
            'attachment'           => 'text'
271 272 273 274
          ),
          'required' => array('user','goaltype/skilltype','title')
        ),
        'interests' => array(
275
          'datagenerator' => 'resume_interests',
276 277 278 279 280 281 282 283
          'available' => array(
            'user'                 => 'text',
            'interest'             => 'text',
            'description'          => 'text'
          ),
          'required' => array('user','interest','description')
        ),
        'coverletters' => array(
284
          'datagenerator' => 'resume_coverletter',
285 286 287 288 289 290 291
          'available' => array(
            'user'                 => 'text',
            'content'              => 'text'
          ),
          'required' => array('user','content')
        ),
        'educationhistory' => array(
292
          'datagenerator' => 'resume_educationhistory',
293 294 295 296 297 298 299 300
          'available' => array(
            'user'                   => 'text',
            'startdate'              => 'text',
            'enddate'                => 'text',
            'institution'            => 'text',
            'institutionaddress'     => 'text',
            'qualtype'               => 'text',
            'qualname'               => 'text',
301 302
            'qualdescription'        => 'text',
            'attachment'             => 'text'
303 304 305 306
          ),
          'required' => array('user','startdate','institution')
        ),
        'employmenthistory' => array(
307
          'datagenerator' => 'resume_employmenthistory',
308 309 310 311 312 313 314
          'available' => array(
            'user'                   => 'text',
            'startdate'              => 'text',
            'enddate'                => 'text',
            'employer'               => 'text',
            'employeraddress'        => 'text',
            'jobtitle'               => 'text',
315 316
            'positiondescription'    => 'text',
            'attachment'             => 'text'
317 318 319 320
          ),
          'required' => array ('user','startdate','employer','jobtitle')
        ),
        'contactinformation' => array(
321
          'datagenerator' => 'resume_contactinformation',
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
          'available' => array(
            'user'                     => 'text',
            'email'                    => 'text',
            'officialwebsite'          => 'text',
            'personalwebsite'          => 'text',
            'blogaddress'              => 'text',
            'town'                     => 'text',
            'city/region'              => 'text',
            'country'                  => 'text',
            'homenumber'               => 'text',
            'businessnumber'           => 'text',
            'mobilenumber'             => 'text',
            'faxnumber'                => 'text',
          ),
          'required' => array('user','email')
        ),
338 339
        'achievements' => array(
          'datagenerator' => 'resume_certification',
340 341 342 343
          'available' => array(
            'user'                    => 'text',
            'date'                    => 'text',
            'title'                   => 'text',
344 345
            'description'             => 'text',
            'attachment'              => 'text'
346 347 348 349
          ),
          'required' => array('user','title')
        ),
        'books and publications' => array(
350
          'datagenerator' => 'resume_book',
351 352 353 354 355 356
          'available' => array(
            'user'                     => 'text',
            'date'                     => 'text',
            'title'                    => 'text',
            'contribution'             => 'text',
            'description'              => 'text',
357 358
            'url'                      => 'text',
            'attachment'               => 'text'
359 360 361 362
          ),
          'required' => array('user','date','title','contribution')
        ),
        'professionalmemberships' => array(
363
          'datagenerator' => 'resume_membership',
364 365 366 367 368
          'available' => array(
            'user'                     => 'text',
            'startdate'                => 'text',
            'enddate'                  => 'text',
            'title'                    => 'text',
369 370
            'description'              => 'text',
            'attachment'               => 'text'
371 372
          ),
          'required' => array('user','startdate','title')
373
        )
374 375
      );

Son Nguyen's avatar
Son Nguyen committed
376 377 378 379 380 381 382

    /**
     * Normalise values in a given record
     * For example, 'ON' -> 1, 'OFF' -> 0
     * @param array ('field' => 'values', ...) $record
     * @return $record
     */
383 384 385 386 387 388 389 390 391 392 393
     public function normalise($availablefields, &$record) {
         foreach ($record as $fieldname => &$value) {
             if ($availablefields[$fieldname] == 'bool') {
                 $value = trim($value);
                 // Normalise boolean values
                 if (strtolower($value) == 'on' || $value == '1' || $value == 'yes' || $value == 'true') {
                     $value = true;
                 }
                 else if (strtolower($value) == 'off' || $value == '0' || $value == 'no' || $value == 'false') {
                     $value = false;
                 }
394 395 396
             }
         }
     }
Son Nguyen's avatar
Son Nguyen committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

    /**
     * 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)) {
413
                throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a boolean ('ON'|'OFF', '1'|'0', 'true'|'false', 'yes'|'no' are accepted boolean values).");
Son Nguyen's avatar
Son Nguyen committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
            }
            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
452
            $this->normalise($availablefields, $elementdata);
Son Nguyen's avatar
Son Nguyen committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
            // 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');
            }
        }

    }

}