Пример #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);
 }
Пример #2
0
 /**
  * Fill random decimal values in the decimal 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;
     $min = -255;
     $max = 255;
     $scale = 2;
     $decimal_separator = '.';
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         if (!empty($instance['settings']['min'])) {
             $min = $instance['settings']['min'];
         }
         if (!empty($instance['settings']['max'])) {
             $max = $instance['settings']['max'];
         }
         $scale = $field['settings']['scale'];
         $decimal_separator = $field['settings']['decimal_separator'];
     }
     $values = array();
     for ($i = 0; $i < $num; $i++) {
         // We are assuming that precision is set correctly to accommodate min and
         // max values.
         $min_int = $min * pow(10, $scale);
         $max_int = $max * pow(10, $scale);
         $number = Utils::getRandomInt($min_int, $max_int) / pow(10, $scale);
         $number = str_replace(".", $decimal_separator, $number);
         $values[] = $number;
     }
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($values);
 }
Пример #3
0
 /**
  * Fill random integer values in the integer 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;
     $min = -255;
     $max = 255;
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         if (!empty($instance['settings']['min'])) {
             $min = $instance['settings']['min'];
         }
         if (!empty($instance['settings']['max'])) {
             $max = $instance['settings']['max'];
         }
     }
     $values = Utils::getRandomInt($min, $max, $num);
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($values);
 }
Пример #4
0
 /**
  * Returns the number of values to create based on field's cardinality. If
  * cardinality is unlimited, then return 2 or 3 randomly. If cardinality is 1,
  * then return 1. If cardinality is any other value, then return a random
  * number between 2 and the maximum number allowed.
  *
  * @param int $cardinality
  *   Field's cardinality.
  *
  * @return int
  *   Number of field values to create.
  *
  */
 private static function getNumberOfItemsFromCardinality($cardinality)
 {
     if ($cardinality == -1) {
         // -1 denotes that cardinality is unlimited.
         $num = Utils::getRandomInt(2, 3);
         return $num;
     } elseif ($cardinality == 1) {
         $num = 1;
         return $num;
     } else {
         $num = Utils::getRandomInt(2, $cardinality);
         return $num;
     }
 }
Пример #5
0
 /**
  * Fill single checkbox field with default values.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  *
  * @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 fillOptionsOnOffRandomValues(Form $formObject, $field_name)
 {
     $required = FALSE;
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $required = $instance['required'];
     }
     $values = $required || Utils::getRandomInt(0, 1) ? 1 : 0;
     $function = "fill" . Utils::makeTitleCase($field_name) . "Values";
     return $formObject->{$function}($values);
 }
Пример #6
0
 /**
  * Returns the number of values to be filled in the field based on the
  * field's cardinality. If cardinality is unlimited, then a random integer
  * between 2 and 5 (inclusive) is returned. If cardinality is 1, then 1 is
  * returned. If cardinality is any other number, then a random integer
  * between 2 and that integer is returned.
  *
  * @param int $cardinality
  *   Field's cardinality.
  *
  * @return int
  *   Number of values to be filled in the field.
  */
 private function getNumberOfItemsFromCardinality($cardinality)
 {
     if ($cardinality == -1) {
         $num = Utils::getRandomInt(2, 5);
         return $num;
     } elseif ($cardinality == 1) {
         $num = 1;
         return $num;
     } else {
         $num = Utils::getRandomInt(2, $cardinality);
         return $num;
     }
 }