/** * 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); }
/** * Fill text area field with random long text 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 the field could be filled with provided values. * (2) $values: Values that were filled. * (3) $msg: Error message if $success is FALSE and empty otherwise. */ public static function fillRandomValues(Form $formObject, $field_name, $options = array()) { $num = 1; if (method_exists($formObject, 'getEntityObject')) { // This is an entity form. list($field, $instance, $num) = $formObject->getFieldDetails($field_name); $text_processing = $instance['settings']['text_processing']; } $field_class = get_called_class(); $values = $field_class::generateValues($num, $text_processing); $function = "fill" . Utils::makeTitleCase($field_name) . "Values"; return $formObject->{$function}($values); }
/** * Fill email 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 the field could be filled with provided values. * (2) $values: Values that were filled. * (3) $msg: Error message if $success is FALSE and empty otherwise. */ public static function fillRandomValues(Form $formObject, $field_name, $options = array()) { $num = 1; if (method_exists($formObject, 'getEntityObject')) { // This is an entity form. list($field, $instance, $num) = $formObject->getFieldDetails($field_name); } $values = array(); for ($i = 0; $i < $num; $i++) { $values[] = Utils::getRandomEmail(); } $function = "fill" . Utils::makeTitleCase($field_name) . "Values"; return $formObject->{$function}($values); }
public static function fillOptionsSelectRandomValues(Form $formObject, $field_name) { $num = 1; $allowed_values = array(); if (method_exists($formObject, 'getEntityObject')) { // This is an entity form. list($field, $instance, $num) = $formObject->getFieldDetails($field_name); $allowed_values = array_keys($field['settings']['allowed_values']); } $field_class = get_called_class(); $values = $field_class::generateListValues($allowed_values, $num); $function = "fill" . Utils::makeTitleCase($field_name) . "Values"; return $formObject->{$function}($values); }
/** * This function is for get reference product from recurring entity * @return bool|Recurring */ public function getCommerceRecurringRefProductValues() { $recurring_entity = $this->getEntity(); $ref_product = field_get_items('commerce_recurring', $recurring_entity, 'commerce_recurring_ref_product'); if (!empty($ref_product) && isset($ref_product[0]['target_id'])) { $product_id = $ref_product[0]['target_id']; $product = commerce_product_load($product_id); $product_class = Utils::makeTitleCase($product->type); $field_class = "RedTest\\entities\\CommerceProduct\\" . $product_class; $product_obj = new $field_class($product_id); return new Response(TRUE, $product_obj, ""); } else { return new Response(FALSE, NULL, 'Reference product not available.'); } }
/** * 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); }
/** * 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); }
/** * This function is used for node form submit. */ public function submit() { //$this->fillValues(array('op' => t('Save'))); $this->includeFile('inc', 'node', 'node.pages'); $this->processBeforeSubmit(); $response = $this->pressButton(t('Save'), array(), $this->getEntityObject()->getEntity()); $nodeObject = NULL; if ($response->getSuccess()) { // Get the node from form_state. $form_state = $this->getFormState(); $node = $form_state['node']; $type = $node->type; $classname = Utils::makeTitleCase($type); $class_fullname = "RedTest\\entities\\Node\\" . $classname; $nodeObject = new $class_fullname($node->nid); $this->setEntityObject($nodeObject); // Store the created node in $entities so that it can later be deleted. global $entities; $entities['node'][$node->nid] = $nodeObject; } $this->processAfterSubmit(); $response->setVar($nodeObject); return $response; }
/** * Fills taxonomy term reference checkboxes field of a form with provided * values. * * @param Form $formObject * Form object. * @param string $field_name * Field name. * @param int|object|array $values * An integer taxonomy term id, a term object or an array of tids or term * objects. Here are the acceptable formats: * (a) 23 * (b) array( * 'tid' => 23, * ) * (c) (object) array( * 'tid' => 23, * ) * (d) (Tag) array( * 'entity' => Entity object, * 'entity_term' => 'taxonomy_term', * ) * (e) array(23, 3) * (f) array( * array( * 'tid' => 23, * ), * array( * 'tid' => 3, * ), * ) * (g) array( * (object) array( * 'tid' => 23, * ), * (object) array( * 'tid' => 3, * ), * ) * (h) array( * (Tag) array( * 'entity' => Entity object, * 'entity_term' => 'taxonomy_term', * ), * (Tag) array( * 'entity' => Entity object, * 'entity_term' => 'taxonomy_term', * ), * ) * * @return array */ public static function fillOptionsSelectValues(Form $formObject, $field_name, $values) { if (!Field::hasFieldAccess($formObject, $field_name)) { return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible."); } $vocabulary = ''; 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']; } $tids = array(); if (is_object($values)) { $parent_class = get_parent_class($values); if ($parent_class == "RedTest\\core\\entities\\TaxonomyTerm") { $tids = array(Utils::getId($values)); } else { $tids = array($values->tid); } } elseif (is_array($values)) { if (array_key_exists('tid', $values)) { $tids = array($values['tid']); } foreach ($values as $key => $value) { if (is_numeric($value)) { $tids[] = $value; } elseif (is_object($value)) { $parent_class = get_parent_class($value); if ($parent_class == "RedTest\\core\\entities\\TaxonomyTerm") { $tids[] = Utils::getId($value); } else { $tids[] = $value->tid; } } elseif (is_array($value)) { $tids[] = $value['tid']; } } } $termObjects = array(); foreach ($tids as $tid) { $term_class = "RedTest\\entities\\TaxonomyTerm\\" . Utils::makeTitleCase($vocabulary); $termObjects[] = new $term_class($tid); } return $formObject->fillValues($field_name, array(LANGUAGE_NONE => $tids)); }
/** * Register a new user. * * @param string $username * Username. * @param string $email * Email address. * @param string $password * Password. * @param array $options * Options array. Usually this will have the following 3 keys: * (a) roles: An array of roles that the newsly created user has to be * assigned. * (b) skip: An array of fields that need to be skipped during * registration. * (c) required_fields_only: Whether only required fields in the * registration field are to be filled. * * @return mixed $user * User object if the user logged in successfully and an array of errors, * otherwise. */ public static function registerUser($username, $email, $password, $options = array()) { $options += array('roles' => array(), 'skip' => array(), 'required_fields_only' => TRUE); $userRegisterForm = new UserForms\UserRegisterForm(); $userRegisterForm->fillFieldValues(array('account', 'name'), $username); $userRegisterForm->fillFieldValues(array('account', 'mail'), $email); $userRegisterForm->fillFieldValues(array('pass', 'pass1'), $password); $userRegisterForm->fillFieldValues(array('pass', 'pass2'), $password); $userObject = new User(); $field_instances = $userObject->getFieldInstances(); $fields = array(); foreach ($field_instances as $field_name => $field_info) { if (!$field_info['settings']['user_register_form']) { continue; } if (!in_array($field_name, $options['skip']) && ($userRegisterForm->isRequired($field_name) || $field_info['required'] || !$options['required_fields_only'])) { $function = 'fill' . Utils::makeTitleCase($field_name) . 'RandomValues'; $response = $userRegisterForm->{$function}(); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields[$field_name] = $response->getVar(); } } $response = $userRegisterForm->submit(); if (!$response->getSuccess()) { return $response; } /** * @todo Find a better way to make the user active and add roles than using user_save(). */ try { $roles = self::formatRoles($options['roles']); } catch (\Exception $e) { return new Response(FALSE, NULL, $e->getMessage()); } $userObject = $response->getVar(); if (!$userObject->getStatusValues() || sizeof($roles)) { $account = $userObject->getEntity(); $edit['status'] = TRUE; $edit['roles'] = $account->roles + $roles; $account = user_save($account, $edit); if (!$account) { return new Response(FALSE, NULL, "Could not make the user active or could not add roles."); } $userObject = new User(Utils::getId($userObject)); } // Add password key so that it can be used later to log in. $form_state = $userRegisterForm->getFormState(); $account = $userObject->getEntity(); $account->password = $form_state['user']->password; $userObject->setEntity($account); return new Response(TRUE, $userObject, ""); }
public static function checkImageImageValues(Entity $entity, $field_name, $values) { $function = "get" . Utils::makeTitleCase($field_name) . "Values"; $actual_values = $entity->{$function}(); $field_class = get_called_class(); return $field_class::compareImageImageValues($actual_values, $values); }
public static function hasFieldAccess(Form $formObject, $field_name) { if (is_string($field_name)) { $access_function = "has" . Utils::makeTitleCase($field_name) . "Access"; $access = $formObject->{$access_function}(); } else { $access = $formObject->hasFieldAccess($field_name); } return $access; }
public function checkFieldPermissions($testClass, $viewSkip = array(), $editSkip = array()) { foreach ($this->getFieldInstances() as $field_name => $instance) { if (!in_array($field_name, $viewSkip)) { $function = "has" . Utils::makeTitleCase($field_name) . "ViewAccess"; $testClass->assertTrue(call_user_func(array($this, $function)), "User does not have view access to " . $field_name); } if (!in_array($field_name, $editSkip)) { $function = "has" . Utils::makeTitleCase($field_name) . "UpdateAccess"; $testClass->assertTrue(call_user_func(array($this, $function)), "User does not have edit access to " . $field_name); } } }
/** * Submit the comment form. * * @return Response * Response object. */ public function submit() { $this->includeFile('inc', 'comment', 'comment.pages'); $this->processBeforeSubmit(); $response = $this->pressButton(t('Save'), array(), $this->getEntityObject()->getEntity()); $commentObject = NULL; if ($response->getSuccess()) { // Get the comment from form_state. $form_state = $this->getFormState(); $comment = $form_state['comment']; $node_type = str_replace('comment_node_', '', $comment->node_type); $classname = Utils::makeTitleCase($node_type) . 'Comment'; $class_fullname = "RedTest\\entities\\Comment\\" . $classname; $commentObject = new $class_fullname($comment->cid); if (!$commentObject->getInitialized()) { return new Response(FALSE, NULL, $commentObject->getErrors()); } $this->setEntityObject($commentObject); // Store the created node in $entities so that it can later be deleted. global $entities; $entities['comment'][$comment->cid] = $commentObject; } $this->processAfterSubmit(); return new Response($response->getSuccess(), $commentObject, $response->getMsg()); }
/** * Create taxonomy terms before a new entity form is invoked. * * @param Form $formObject * Form object. * @param string $field_name * Field name. * @param array $options * Options array. "references" key is used here. * * @return array * An array with two values: * (1) $success: Whether the function executed successfully. * (2) $msg: A message if $success is FALSE. */ public static function processBeforeCreateRandom(Form $formObject, $field_name, &$options) { $field_info = self::getFieldInfo($field_name); $vocabulary = $field_info['settings']['allowed_values'][0]['vocabulary']; $cardinality = $field_info['cardinality']; $num = 1; if ($cardinality == -1) { // -1 denotes that cardinality is unlimited. $num = 5; } else { $num = intval($cardinality); } // Check if $references has these terms already. $options += array('references' => array()); $references =& $options['references']; if (isset($references['taxonomy_terms'][$vocabulary])) { if (sizeof($references['taxonomy_terms'][$vocabulary]) < $num) { $num -= sizeof($references['taxonomy_terms'][$vocabulary]); } else { $num = 0; } } else { $references['taxonomy_terms'][$vocabulary] = array(); } if ($num) { $base_path = "RedTest\\entities\\TaxonomyTerm\\"; $class = $base_path . Utils::makeTitleCase($vocabulary); // Masquerade as user 1 so that there is no access problem. list($superUserObject, $userObject, $old_state) = User::masquerade(1); $response = $class::createRandom($num); if (!$response->getSuccess()) { return new Response(FALSE, NULL, "Could not create terms of vocabulary {$vocabulary} attached to {$field_name}: " . $response->getMsg()); } User::unmasquerade($userObject, $old_state); $termObjects = $response->getVar(); if (is_object($termObjects)) { $termObjects = array($termObjects); } $references['taxonomy_terms'][$vocabulary] = array_merge($references['taxonomy_terms'][$vocabulary], $termObjects); } return new Response(TRUE, NULL, ""); }
/** * 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); }
/** * @param $tids * * @return array */ public static function createTermObjectsFromTids($tids, $vocabulary = NULL, $false_on_invalid = TRUE) { $terms = taxonomy_term_load_multiple($tids); $termObjects = array(); foreach ($tids as $tid) { if (empty($terms[$tid])) { if ($false_on_invalid) { $termObjects[] = FALSE; } else { $termObjects = $tid; } continue; } $vocabulary = $terms[$tid]->vocabulary_machine_name; $term_class = "RedTest\\entities\\TaxonomyTerm\\" . Utils::makeTitleCase($vocabulary); $termObjects[] = new $term_class($tid); } return $termObjects; }
/** * Fill values in a multi-valued field. * * @param string $field_name * Field name. * @param array $values * Field values array. * @param int $offset * Offset for replacement. In some fields, an empty valued field has NULL * value in form_state. Use 1 for such a field. In other cases, an empty * multi-values field has one value which is empty. Use 0 in such a case. * * @return array * An array with the following values: * (a) $success: Whether multi-valued field could be filled. * (b) $return: The actual values filled. * (c) $msg: An error message if the values could not be filled, an empty * string otherwise. */ public function fillMultiValued($field_name, $values, $offset = 0) { if (is_null($values)) { $values = array(); } if (is_string($values) || is_numeric($values)) { $values = array($values); } $field = $this->getFieldInfo($field_name); $short_field_class = Utils::makeTitleCase($field['type']); $field_class = "RedTest\\core\\fields\\" . $short_field_class; $original_values = $this->getValues($field_name); $original_values = !empty($original_values[LANGUAGE_NONE]) ? $original_values[LANGUAGE_NONE] : array(); if (isset($original_values['add_more'])) { // If the form has an "add_more" key, that means that there is one less // field that is available to us for filling without pressing "Add More" button. $offset -= 1; } $threshold = sizeof($original_values) + $offset; // $input_replace is an array of input values that can be replaced into // existing fields without pressing "Add More" button. $input_replace = array_slice($values, 0, $threshold, TRUE); $input = $input_replace; $return = array(); if (sizeof($values) > $threshold) { // Number of input values is more than the number of fields available // without pressing Add More button. We fill the available fields with the // input values and for each remaining input value, we need to press "Add // More" button. $response = $this->fillValues($field_name, array(LANGUAGE_NONE => $input)); if (!$response->getSuccess()) { $response->setVar($input); return $response; } // $input_add is the remaining input values for which we need to press // "Add More" button. $input_add = array_slice($values, $threshold, NULL, TRUE); foreach ($input_add as $key => $value) { $triggering_element_name = $field_class::getTriggeringElementName($field_name, $key); $response = $this->pressButton($triggering_element_name, array('ajax' => TRUE)); if (!$response->getSuccess()) { $response->setVar($input); return $response; } $input[] = $value; $response = $this->fillValues($field_name, array(LANGUAGE_NONE => $input)); if (!$response->getSuccess()) { $response->setVar($input); return $response; } } $return = $input; } elseif (sizeof($input) < $threshold - 1) { // Number of input values is less than the number of fields available // without pressing Add More button. We clear out all the available fields // and fill them with the new values. $return = $input; for ($i = sizeof($input); $i < $threshold - 1; $i++) { $input[] = $field_class::getEmptyValue($this, $field_name); } $response = $this->fillValues($field_name, array(LANGUAGE_NONE => $input)); } else { $return = $input; if (is_array($input) && !sizeof($input)) { // $input is an empty array, which means we need to make it empty. $input[] = $field_class::getEmptyValue($this, $field_name); } $response = $this->fillValues($field_name, array(LANGUAGE_NONE => $input)); } return new Response($response->getSuccess(), $return, $response->getMsg()); }