示例#1
0
 /**
  * Fills provided float values in the field.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param mixed $values
  *   Following formats are acceptable:
  *   (a) 23
  *   (b) array(23, -89)
  *   (c) array(array('value' => 23), array('value' => -89))
  *
  * @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 fillValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $field_class = get_called_class();
     $values = $field_class::normalizeInput($values);
     $input = $field_class::formatValuesForInput($values);
     $response = $formObject->fillMultiValued($field_name, $input);
     if (!$response->getSuccess()) {
         $response->normalizeVar();
         return $response;
     }
     $response->setVar(Utils::normalize($values));
     return $response;
 }
示例#2
0
 /**
  * Fills provided decimal values in the field.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param mixed $values
  *   Following formats are acceptable:
  *   (a) 23.67
  *   (b) array(23.34, -89.12)
  *   (c) array(array('value' => 23.34), array('value' => -89.12))
  *
  * @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 fillValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $field_class = get_called_class();
     list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
     $decimal_separator = $field['settings']['decimal_separator'];
     $values = $field_class::normalizeInput($values, $decimal_separator);
     $input = $field_class::addCorrectDecimalSeparator($values, $decimal_separator);
     $input = $field_class::formatValuesForInput($input);
     $response = $formObject->fillMultiValued($field_name, $input);
     if (!$response->getSuccess()) {
         $response->normalizeVar();
         return $response;
     }
     $response->setVar(Utils::normalize($values));
     return $response;
 }
示例#3
0
 public static function fillPhoneTextfieldValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     if (is_string($values) || is_numeric($values)) {
         $values = array($values);
     }
     $input = array();
     $index = 0;
     foreach ($values as $key => $value) {
         $input[$index] = array('email' => $value);
         $triggering_element_name = $field_name . '_add_more';
         //$triggering_element_value = 'Add another item';
         $formObject->pressButton($triggering_element_name, array('ajax' => TRUE));
         $index++;
     }
     //$formObject->setValues($field_name, array(LANGUAGE_NONE => $input));
     return new Response(TRUE, Utils::normalize($input), "");
 }
示例#4
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);
 }
示例#5
0
 public static function normalize($values)
 {
     if (is_string($values) || is_numeric($values)) {
         return strval($values);
     }
     $output = array();
     if (is_array($values)) {
         if ((bool) count(array_filter(array_keys($values), 'is_string'))) {
             if (sizeof($values) == 1 && array_key_exists('url', $values)) {
                 // $values is of the form array('url' => 'URL 1').
                 $output = $values['url'];
             } else {
                 // $values can be of the form array('url' => 'URL 1', 'title' => 'Title 1')
                 $output = $values;
             }
             return $output;
         } else {
             // This is not an associative array.
             foreach ($values as $val) {
                 if (is_string($val) || is_numeric($val)) {
                     // $values is of the form array('URL 1', 'URL 2').
                     $output[] = strval($val);
                 } elseif (is_array($val)) {
                     if ((bool) count(array_filter(array_keys($val), 'is_string'))) {
                         if (sizeof($val) == 1 && array_key_exists('url', $val)) {
                             // $values is of the form array(array('url' => 'URL 1'), array('url' => 'URL 2')).
                             $output[] = $val['url'];
                         } else {
                             // $values is of the form array(array('url' => 'URL 1', 'title' => 'Title 1'), array('url' => 'URL 2', 'title' => 'Title 2')).
                             $output[] = $val;
                         }
                     }
                 }
             }
             return Utils::normalize($output);
         }
     }
     $output = Utils::normalize($values);
     /*if (is_array($values)) {
           if (sizeof($values) == 1) {
             if ((bool) count(array_filter(array_keys($values), 'is_string'))) {
               // Array is associative.
               if (array_key_exists('url', $values)) {
                 // $values is of the form array('url' => 'URL 1').
                 return strval($values['url']);
               }
             }
             else {
               // Array is not associative.
               // Check whether it's an array of array.
               foreach ($values as $val) {
                 if (is_string($val) || is_numeric($val)) {
                   // $values is of the form array('URL 1')
                   return strval($val);
                 }
     
                 if (is_array($val)) {
                   if (sizeof($val) == 1) {
                     if ((bool) count(array_filter(array_keys($val), 'is_string'))) {
                       // Array is associative.
                       if (array_key_exists('url', $val)) {
                         return strval($val['url']);
                       }
                     }
                   }
                 }
               }
             }
           }
         }*/
     return $output;
 }
示例#6
0
 /**
  * Create new entities with default field values.
  *
  * @param int $num
  *   Number of entities to create.
  * @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 static function createRandom($num = 1, $options = array())
 {
     // First get the references that need to be created.
     static::processBeforeCreateRandom($options);
     // We need to use "static" here and not "self" since "getFormClass" needs to
     // be called from individual Entity class to get the correct value.
     $formClass = static::getFormClassName();
     $output = array();
     for ($i = 0; $i < $num; $i++) {
         // Instantiate the form class.
         $classForm = new $formClass();
         if (!$classForm->getInitialized()) {
             return new Response(FALSE, $output, $classForm->getErrors());
         }
         // Fill default values in the form. We don't check whether the created
         // entity has the correct field since some custom function could be
         // changing the field values on creation. For checking field values on
         // entity creation, a form needs to be initialized in the test.
         $response = $classForm->fillRandomValues($options);
         if (!$response->getSuccess()) {
             $response->setVar($output);
             return $response;
         }
         // Submit the form to create the entity.
         $response = $classForm->submit();
         if (!$response->getSuccess()) {
             return new Response(FALSE, $output, "Could not create " . get_called_class() . " entity: " . $response->getMsg());
         }
         // Make sure that there is an id.
         $object = $response->getVar();
         if (!$object->getId()) {
             return new Response(FALSE, $output, "Could not get Id of the created " . get_called_class() . " entity: " . $response->getMsg());
         }
         // Store the created entity in the output array.
         $output[] = $object;
     }
     return new Response(TRUE, Utils::normalize($output), "");
 }
 /**
  * Fill taxonomy autocomplete values.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $values
  *   An array of values.
  *
  * @return mixed
  *   A path or an array of paths of images which are to be uploaded.
  */
 public static function fillTaxonomyAutocompleteValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     $vocabulary = NULL;
     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'];
     }
     $field_class = get_called_class();
     $names = $field_class::convertValues($values, $vocabulary, TRUE, FALSE);
     $field_value = is_array($names) ? implode(",", $names) : $names;
     $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $field_value));
     if (!$response->getSuccess()) {
         return $response;
     }
     $termObjects = TaxonomyTerm::createTermObjectsFromNames($names, $vocabulary, FALSE);
     $response->setVar(Utils::normalize($termObjects));
     return $response;
 }
示例#8
0
 /**
  * Fills the field of the form with provided values.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param string|array $values
  *   Following are the acceptable formats:
  *   (a) "0", "1"
  *   (b) array(0, 1)
  *
  * @return string|array
  *   Values that are filled in the field. Following are the returned formats:
  *   (a) 0, 1
  *   (b) array(0, 1)
  *   If multiple values are filled, then the return variable will be in
  *   format (b), otherwise it will be in format (a).
  */
 public static function fillOptionsOnOffValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     if (is_string($values) || is_numeric($values)) {
         // $values is in acceptable format (a).
         $values = array($values);
     }
     $response = NULL;
     $input = array();
     $output = array();
     if (sizeof($values)) {
         if (sizeof($values) == 1 && ($values[0] === 0 || $values[0] === "0")) {
             if (method_exists($formObject, 'getEntityObject')) {
                 $entityObject = $formObject->getEntityObject();
                 list($field, $instance, $num) = Field::getFieldDetails($entityObject, $field_name);
                 //if ($num == 1) {
                 // This is a single checkbox and value input is 0 so set it to NULL.
                 $input = NULL;
                 $output[0] = 0;
                 /*}
                   else {
                     // This is a multi-valued field so 0 is a key. Set it to be a string.
                     $input[0] = "0";
                     $output[0] = 0;
                   }*/
             }
         } else {
             foreach ($values as $key => $value) {
                 if (is_string($value) || is_numeric($value)) {
                     // $values is in acceptable format (b).
                     $output[] = $value;
                     $input[$value] = $value === 0 ? strval($value) : $value;
                 }
             }
         }
         $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input));
         if (!$response->getSuccess()) {
             $response->setVar(Utils::normalize($output));
             return $response;
         }
     }
     return new Response(TRUE, Utils::normalize($output), "");
 }
示例#9
0
 public static function getFileGenericValues(Entity $entityObject, $field_name, $post_process = FALSE)
 {
     $field = $entityObject->getFieldItems($field_name);
     if (!$post_process) {
         return $field;
     }
     $output = array();
     foreach ($field as $fid => $file) {
         $output[] = $fid;
     }
     return Utils::normalize($output);
 }
示例#10
0
 /**
  * Create new users with default field values.
  *
  * @param int $num
  *   Number of entities to create.
  * @param array $options
  *   Options array. This array can have "roles" key that provides an array of
  *   role names that the newly created user will need to be assigned.
  *
  * @return Response
  *   Response object.
  */
 public static function createRandom($num = 1, $options = array())
 {
     $options += array('roles' => array(), 'required_fields_only' => TRUE);
     $output = array();
     for ($i = 0; $i < $num; $i++) {
         // Get a random username.
         do {
             $username = Utils::getRandomString(20);
         } while (!is_null(user_validate_name($username)) || user_load_by_name($username));
         // Get a random email address.
         do {
             $email = $username . '@' . Utils::getRandomString(20) . '.com';
         } while (!is_null(user_validate_mail($email)) || user_load_by_mail($email));
         // Get a random password.
         $password = Utils::getRandomString();
         $response = User::registerUser($username, $email, $password, $options['roles']);
         if (!$response->getSuccess()) {
             $response->setVar($output);
             return $response;
         }
         $output[] = $response->getVar();
     }
     return new Response(TRUE, Utils::normalize($output), "");
 }
示例#11
0
 /**
  * Generate a list of values.
  *
  * @param array $allowed_values
  *   Allowed values.
  * @param int $num
  *   Number of values to select.
  *
  * @return mixed|array
  *   A float, int or text if only one value was to be returned, an array of
  *   floats, int or text otherwise.
  */
 protected static function generateListValues($allowed_values, $num = 1)
 {
     $selected_keys = array_rand($allowed_values, min($num, sizeof($allowed_values)));
     if (is_numeric($selected_keys)) {
         $selected_keys = array($selected_keys);
     }
     $values = array();
     foreach ($selected_keys as $selected_key) {
         $values[] = $allowed_values[$selected_key];
     }
     return Utils::normalize($values);
 }
示例#12
0
 /**
  * Create new entities with random field values.
  *
  * @param int $num
  *   Number of entities to create.
  * @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 static function createRandom($num = 1, $options = array())
 {
     if (!is_numeric($num)) {
         return new Response(FALSE, NULL, 'Number of comments to be created has to be an integer.');
     }
     if (!isset($options['nid']) && !isset($options['pid'])) {
         return new Response(FALSE, NULL, "Neither nid or pid is specified while creating a comment.");
     }
     $options += array('nid' => NULL, 'pid' => NULL);
     // First get the references that need to be created.
     static::processBeforeCreateRandom($options);
     // We need to use "static" here and not "self" since "getFormClass" needs to
     // be called from individual Entity class to get the correct value.
     $formClass = static::getFormClassName();
     $output = array();
     for ($i = 0; $i < $num; $i++) {
         // Instantiate the form class.
         $classForm = new $formClass(NULL, $options['nid'], $options['pid']);
         if (!$classForm->getInitialized()) {
             return new Response(FALSE, $output, $classForm->getErrors());
         }
         // Fill default values in the form. We don't check whether the created
         // entity has the correct field since some custom function could be
         // changing the field values on creation. For checking field values on
         // entity creation, a form needs to be initialized in the test.
         $response = $classForm->fillRandomValues($options);
         if (!$response->getSuccess()) {
             return new Response(FALSE, $output, $response->getMsg());
         }
         // Submit the form to create the entity.
         $response = $classForm->submit();
         if (!$response->getSuccess()) {
             return new Response(FALSE, $output, "Could not create " . get_called_class() . " entity: " . $response->getMsg());
         }
         // Make sure that there is an id.
         if (!$response->getVar()->getId()) {
             return new Response(FALSE, $output, "Could not get Id of the created " . get_called_class() . " entity: " . $response->getMsg());
         }
         // Store the created entity in the output array.
         $output[] = $response->getVar();
     }
     return new Response(TRUE, Utils::normalize($output), "");
 }
示例#13
0
 /**
  * Create new users with default field values.
  *
  * @param int $num
  *   Number of entities to create.
  * @param array $options
  *   Options array. This array can have "roles" key that provides an array of
  *   role names that the newly created user will need to be assigned.
  *
  * @return Response
  *   Response object.
  */
 public static function createRandom($num = 1, $options = array())
 {
     if (!is_numeric($num)) {
         return new Response(FALSE, NULL, 'Number of users to be created has to be an integer.');
     }
     $options += array('roles' => array(), 'required_fields_only' => TRUE);
     // First get the references that need to be created.
     //static::processBeforeCreateRandom($options);
     $output = array();
     for ($i = 0; $i < $num; $i++) {
         // Get a random username.
         do {
             $username = Utils::getRandomString(20);
         } while (!is_null(user_validate_name($username)) || user_load_by_name($username));
         // Get a random email address.
         do {
             $email = $username . '@' . Utils::getRandomString(20) . '.com';
         } while (!is_null(user_validate_mail($email)) || user_load_by_mail($email));
         // Get a random password.
         $password = Utils::getRandomString();
         $response = User::registerUser($username, $email, $password, $options);
         if (!$response->getSuccess()) {
             $response->setVar($output);
             return $response;
         }
         $output[] = $response->getVar();
     }
     return new Response(TRUE, Utils::normalize($output), "");
 }