/** * Fills random values in fields. * * @param array $options * An associative options array. It can have the following keys: * (a) skip: An array of field names which are not to be filled. * (b) required_fields_only: TRUE if only required fields are to be filled * and FALSE if all fields are to be filled. * * @return Response * Response object. */ public function fillRandomValues($options = array()) { $options += array('skip' => array(), 'required_fields_only' => TRUE); $response = parent::fillRandomValues($options); if (!$response->getSuccess()) { return $response; } $fields = $response->getVar(); if (($this->isSubjectRequired() || !$options['required_fields_only']) && $this->hasSubjectAccess()) { $response = $this->fillSubjectRandomValues(); if (!$response->getSuccess()) { return new Response(FALSE, $fields, $response->getMsg()); } $fields['subject'] = $response->getVar(); } if ($this->isAuthorSubfieldToBeFilled('name', $options['required_fields_only'])) { $response = $this->fillFieldValues(array('author', 'name'), Utils::getRandomText(10)); if (!$response->getSuccess()) { return new Response(FALSE, $fields, $response->getMsg()); } $fields['name'] = $response->getVar(); } if ($this->isAuthorSubfieldToBeFilled('mail', $options['required_fields_only'])) { $response = $this->fillFieldValues(array('author', 'mail'), Utils::getRandomEmail()); if (!$response->getSuccess()) { return new Response(FALSE, $fields, $response->getMsg()); } $fields['mail'] = $response->getVar(); } if ($this->isAuthorSubfieldToBeFilled('homepage', $options['required_fields_only'])) { $response = $this->fillFieldValues(array('account', 'homepage'), Utils::getRandomUrl()); if (!$response->getSuccess()) { return new Response(FALSE, $fields, $response->getMsg()); } $fields['homepage'] = $response->getVar(); } return new Response(TRUE, $fields, ""); }
/** * Fill form with default values. These default values are what you define in * this function and are different from Drupal's default values for the * fields. * * @param array $options * An associative options array. It can have the following keys: * (a) skip: An array of field names which are not to be filled. * (b) required_fields_only: TRUE if only required fields are to be filled * and FALSE if all fields are to be filled. * * @return Response * Response object. */ public function fillRandomValues($options = array()) { $options += array('skip' => array(), 'required_fields_only' => TRUE); $response = parent::fillRandomValues($options); if (!$response->getSuccess()) { return $response; } $fields = $response->getVar(); // @todo Check about field access. if ((!$options['required_fields_only'] || $this->isTitleRequired()) && !in_array('title', $options['skip'])) { // Check if the field is required. We use '#required' key in form array // since it can be set or unset using custom code. // Field is not required. There is no need to fill this field. // @todo Provide the ability to pass max_length in $options array. $response = $this->fillTitleRandomValues(); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['title'] = $response->getVar(); } // @todo Check in $skip array. if ($this->hasFieldAccess(array('options', 'status')) && isset($options['status'])) { $status = NULL; switch ($options['status']) { case 'random': $status = Utils::getRandomBool(); break; case 'published': $status = 1; break; case 'unpublished': $status = 0; break; } if (!is_null($status)) { $response = $this->fillStatusValues($status); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['status'] = $status; } } // @todo Check in $skip array. if ($this->hasFieldAccess(array('revision_information', 'revision')) && isset($options['revision'])) { $revision = NULL; $revision_log = NULL; switch ($options['revision']) { case 'random': $revision = Utils::getRandomBool(); break; case TRUE: $revision = 1; break; case FALSE: $revision = 0; break; } if (!is_null($revision)) { $response = $this->fillRevisionValues($revision); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['revision'] = $revision; if ($revision && $this->hasFieldAccess(array('revision_information', 'log')) && isset($options['revision_log'])) { switch ($options['revision_log']) { case 'random': $revision_log = Utils::getRandomBool() ? Utils::getRandomText(25) : NULL; break; case TRUE: $revision_log = Utils::getRandomText(25); break; case FALSE: $revision_log = ''; break; } if (!is_null($revision_log)) { $response = $this->fillLogValues($revision_log); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['log'] = $revision_log; } } } } // @todo Check $skip array. if ($this->hasFieldAccess(array('author', 'name')) && isset($options['change_author']) && $options['change_author']) { // We'll need to create new author first. // Masquerade as user 1. list($superAdmin, $originalUser, $originalState) = User::masquerade(1); $response = User::createRandom(); // Return to original user. User::unmasquerade($originalUser, $originalState); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $userObject = $response->getVar(); $name = $userObject->getNameValues(); $this->fillNameValues($name); $fields['name'] = $name; } // @todo Check $skip array. if ($this->hasFieldAccess(array('author', 'date')) && isset($options['change_published_date']) && $options['change_published_date']) { $now = time(); $start = isset($options['start_published_date']) ? Utils::formatDate($options['start_published_date'], 'integer') : $now - 3 * 365 * 24 * 60 * 60; $end = isset($options['end_published_date']) ? Utils::formatDate($options['end_published_date'], 'integer') : $now + 3 * 365 * 24 * 60 * 60; $date = Utils::getRandomDate('Y-m-d H:i:s', $start, $end); $response = $this->fillDateValues($date); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['date'] = $date; } return new Response(TRUE, $fields, ""); }
/** * Fill form with default values. These default values are what you define in * this function and are different from Drupal's default values for the * fields. * * @param array $options * An associative options array. It can have the following keys: * (a) skip: An array of field names which are not to be filled. * (b) required_fields_only: TRUE if only required fields are to be filled * and FALSE if all fields are to be filled. * * @return array * An array with the following values: * (1) $success: TRUE if fields were filled successfully and FALSE * otherwise. * (2) $fields: An associative array of field values that are to be filled * keyed by field name. * (3) $msg: Error message if $success is FALSE, and an empty string * otherwise. */ public function fillRandomValues($options = array()) { $options += array('skip' => array(), 'required_fields_only' => TRUE); $response = parent::fillRandomValues($options); if (!$response->getSuccess()) { return $response; } $fields = $response->getVar(); if (!$options['required_fields_only'] || $this->isDescriptionRequired()) { // Check if the field is required. We use '#required' key in form array // since it can be set or unset using custom code. // Field is required or we need to fill all fields. if (!in_array('description', $options['skip'])) { $description = array('value' => Utils::getRandomText(100), 'format' => 'plain_text'); $response = $this->fillDescriptionValues($description); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['description'] = $description['value']; $fields['format'] = $description['format']; } } // Fill name at the end so that there is less chance of getting non-unique // value in the database. if (!in_array('name', $options['skip'])) { // Make sure that taxonomy term name is not repeated so that deleting // entities at the end is easier. $name = TaxonomyTerm::getUniqueName($this->vocabulary->machine_name); $response = $this->fillNameValues($name); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields['name'] = $name; } return new Response(TRUE, $fields, ""); }