Ejemplo n.º 1
0
 /**
  * Fill generic file field with random files.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $options
  *   Options array.
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether default values could be filled in the field.
  *   (2) $values: Values that were filled for the field.
  *   (3) $msg: Message in case there is an error. This will be empty if
  *   $success is TRUE.
  */
 public static function fillRandomValues(Form $formObject, $field_name, $options = array())
 {
     $num = 1;
     $file_extensions = 'txt';
     $scheme = 'public';
     $show_description = FALSE;
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $file_extensions = $instance['settings']['file_extensions'];
         $scheme = $field['settings']['uri_scheme'];
         $show_description = $instance['settings']['description_field'];
     }
     $extensions = str_replace(" ", "|", $file_extensions);
     $files = file_scan_directory('tests/assets', '/^.*\\.(' . $extensions . ')$/i');
     $filenames = array();
     foreach ($files as $file_name => $file_array) {
         $filenames[] = $file_array->uri;
     }
     if (!sizeof($filenames)) {
         return new Response(FALSE, array(), "Could not find a file to attach with any of the following extensions: " . $file_extensions);
     }
     $files = array();
     for ($i = 0; $i < $num; $i++) {
         if ($show_description) {
             $files[] = array('uri' => $filenames[Utils::getRandomInt(0, sizeof($filenames) - 1)], 'description' => Utils::getRandomText(20), 'scheme' => $scheme);
         } else {
             $files[] = array('uri' => $filenames[Utils::getRandomInt(0, sizeof($filenames) - 1)], 'scheme' => $scheme);
         }
     }
     $files = Utils::normalize($files);
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($files);
 }
Ejemplo n.º 2
0
 /**
  * Fill link field with random values.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $options
  *   Options array.
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether default values could be filled in the field.
  *   (2) $values: Values that were filled for the field.
  *   (3) $msg: Message in case there is an error. This will be empty if
  *   $success is TRUE.
  */
 public static function fillRandomValues(Form $formObject, $field_name, $options = array())
 {
     $num = 1;
     $show_url = 0;
     $show_title = 'required';
     $link_target = 'default';
     $show_link_class = 0;
     $show_link_title = 0;
     $title_maxlength = 128;
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $show_title = $instance['settings']['title'];
         $show_url = $instance['settings']['url'];
         $title_maxlength = $instance['settings']['title_maxlength'];
         $link_target = $instance['settings']['attributes']['target'];
         $show_link_class = $instance['settings']['attributes']['configurable_class'];
         $show_link_title = $instance['settings']['attributes']['configurable_title'];
     }
     $values = array();
     for ($i = 0; $i < $num; $i++) {
         $value = array();
         if ($show_url !== 'optional' || Utils::getRandomBool()) {
             $value['url'] = Utils::getRandomUrl();
         }
         if ($show_title == 'required' || empty($value['url']) || $show_title == 'optional' && Utils::getRandomBool()) {
             $value['title'] = Utils::getRandomText($title_maxlength);
         }
         if ($link_target == 'user' && Utils::getRandomBool()) {
             $value['attributes']['target'] = '_blank';
         }
         if ($show_link_class) {
             $value['attributes']['class'] = Utils::getRandomString(10);
         }
         if ($show_link_title) {
             $value['attributes']['title'] = Utils::getRandomText(15);
         }
         $values[] = $value;
     }
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($values);
 }
Ejemplo n.º 3
0
 /**
  * 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, "");
 }
Ejemplo n.º 4
0
 /**
  * Fill image field with random images.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $options
  *   Options array.
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether default values could be filled in the field.
  *   (2) $values: Values that were filled for the field.
  *   (3) $msg: Message in case there is an error. This will be empty if
  *   $success is TRUE.
  */
 public static function fillRandomValues(Form $formObject, $field_name, $options = array())
 {
     $num = 1;
     $show_title = FALSE;
     $show_alt = FALSE;
     $scheme = 'public';
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $scheme = $field['settings']['uri_scheme'];
         $file_extensions = explode(' ', $instance['settings']['file_extensions']);
         $max_filesize = $instance['settings']['max_filesize'];
         $max_resolution = $instance['settings']['max_resolution'];
         $min_resolution = $instance['settings']['min_resolution'];
         $show_title = $instance['settings']['alt_field'];
         $show_alt = $instance['settings']['title_field'];
     }
     $min_width = '';
     $max_width = '';
     $min_height = '';
     $max_height = '';
     if (!empty($min_resolution)) {
         list($min_width, $min_height) = explode('x', $min_resolution);
     }
     if (!empty($max_resolution)) {
         list($max_width, $max_height) = explode('x', $max_resolution);
     }
     $files = file_scan_directory('tests/assets', '/.*\\.(' . implode('|', $file_extensions) . ')$/', array('recurse' => TRUE));
     $valid_files = array();
     foreach ($files as $uri => $file) {
         $image_info = image_get_info($uri);
         if (!empty($max_filesize) && $image_info['file_size'] > parse_size($max_filesize)) {
             continue;
         }
         if (!empty($min_width) && $image_info['width'] < $min_width) {
             continue;
         }
         if (!empty($max_width) && $image_info['width'] > $max_width) {
             continue;
         }
         if (!empty($min_height) && $image_info['height'] < $min_height) {
             continue;
         }
         if (!empty($max_height) && $image_info['height'] > $max_height) {
             continue;
         }
         $valid_files[$uri] = get_object_vars($file);
     }
     if (empty($valid_files)) {
         return new Response(FALSE, array(), "Appropriate image could not be found for {$field_name}.");
     }
     $files = array();
     foreach (range(0, $num - 1) as $index) {
         $files[$index] = $valid_files[array_rand($valid_files)];
         if ($show_title) {
             $files[$index]['title'] = Utils::getRandomText(20);
         }
         if ($show_alt) {
             $files[$index]['alt'] = Utils::getRandomText(20);
         }
         $files[$index]['scheme'] = $scheme;
     }
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($files);
 }
Ejemplo n.º 5
0
 /**
  * Returns a string or an array of randomly generated textfield values.
  *
  * @param int $num
  *   Number of values to return.
  * @param bool $generate_format
  *   Whether to generate the format of the textfield.
  * @param int $max_length
  *   Maximum length of the text field.
  *
  * @return string|array
  *   A string if only one value was to be returned, an array of strings
  *   otherwise.
  */
 protected static function generateValues($num, $generate_format = FALSE, $generate_summary = FALSE, $max_length = 100, $newline = TRUE)
 {
     $filter_formats = array();
     if ($generate_format) {
         global $user;
         $filter_formats = array_keys(filter_formats($user));
     }
     $values = array();
     for ($i = 0; $i < $num; $i++) {
         $values[$i]['value'] = Utils::getRandomText($max_length);
         if (!$newline) {
             $values[$i]['value'] = str_replace(PHP_EOL, " ", $values[$i]['value']);
         }
         if ($generate_format) {
             $values[$i]['format'] = $filter_formats[array_rand($filter_formats)];
         }
         if ($generate_summary) {
             $values[$i]['summary'] = Utils::getRandomText($max_length);
         }
     }
     return Utils::normalize($values);
 }
 /**
  * Fill random taxonomy term values in the taxonomy term reference field.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $options
  *   Options array.
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether values could be filled in the field.
  *   (2) $values: Values that were filled for the field.
  *   (3) $msg: Message in case there is an error. This will be empty if
  *   $success is TRUE.
  */
 public static function fillRandomValues(Form $formObject, $field_name, $options = array())
 {
     $num = 1;
     //$vocabulary = '';
     $widget_type = '';
     $references = array();
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $vocabulary = $field['settings']['allowed_values'][0]['vocabulary'];
         $widget_type = $instance['widget']['type'];
         if (isset($options['references']['taxonomy_terms'][$vocabulary])) {
             foreach ($options['references']['taxonomy_terms'][$vocabulary] as $term) {
                 if ($termObject = static::isTermObject($term, $vocabulary)) {
                     $references[] = $termObject;
                 }
             }
         }
         if (sizeof($references)) {
             $num = min($num, sizeof($references));
         }
         shuffle($references);
         $references = array_slice($references, 0, $num);
     }
     // Create new taxonomy terms in the specified vocabulary.
     /*$vocabulary_class = Utils::makeTitleCase($vocabulary);
       $vocabulary_class = "RedTest\\entities\\TaxonomyTerm\\" . $vocabulary_class;*/
     $termObjects = array();
     for ($i = 0; $i < $num; $i++) {
         if (in_array($widget_type, array('taxonomy_autocomplete', 'autocomplete_deluxe_taxonomy'))) {
             if (sizeof($references)) {
                 // Use taxonomy terms that are provided.
                 $termObjects[] = Utils::getLabel($references[$i]);
             } else {
                 // Instead of creating a new term, we just pass its name so that
                 // Drupal creates a new one automatically.
                 $termObjects[] = Utils::getRandomText(20);
             }
         } elseif (sizeof($references)) {
             // For select lists and radio buttons, we can not create a new term
             // here. The reason is that if the form has an AJAX-based Add More
             // button, then it will be cached. So all the options in the select or
             // checkbox/radio list will be the original options even though new
             // taxonomy terms are created later. It's like creating a new taxonomy
             // term after a form with taxonomy term is already opened in another
             // tab.
             /*list($success, $termObject, $msg) = $vocabulary_class::createDefault();
               if (!$success) {
                 return array(
                   FALSE,
                   $termObjects,
                   "Could not create taxonomy terms for the field " . $field_name . ": " . $msg
                 );
               }*/
             $termObjects[] = Utils::getLabel($references[$i]);
         } else {
             // $references is an empty array.
             return new Response(FALSE, NULL, "Could not find any existing taxonomy term that can be referenced by {$field_name}.");
         }
     }
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($termObjects);
 }
Ejemplo n.º 7
0
 /**
  * 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, "");
 }
Ejemplo n.º 8
0
 /**
  * Returns a unique term name that doesn't already exist.
  *
  * @param null|string $vocabulary_machine_name
  *   Vocabulary machine name if the term name is supposed to be unique in
  *   that vocabulary or NULL if the term name is supposed to be unique across
  *   all the vocabularies.
  *
  * @return string
  *   Unique term name.
  */
 public static function getUniqueName($vocabulary_machine_name = NULL)
 {
     do {
         $name = Utils::getRandomText(20);
         $terms = taxonomy_get_term_by_name($name, $vocabulary_machine_name);
     } while (sizeof($terms));
     return $name;
 }
Ejemplo n.º 9
0
 /**
  * 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, "");
 }