/** * Default constructor for the node object. Do not call this class directly. * Create a separate class for each content type and use its constructor. * * @param int $nid * Nid if an existing node is to be loaded. */ public function __construct($nid = NULL) { $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); if (!is_null($nid) && is_numeric($nid)) { $node = node_load($nid); if (!$node) { $this->setErrors("Node with nid {$nid} does not exist."); $this->setInitialized(FALSE); return; } if ($node->type != $type) { $this->setErrors("Node's type doesn't match the class."); $this->setInitialized(FALSE); return; } parent::__construct($node); } else { global $user; $node = (object) array('title' => NULL, 'type' => $type, 'language' => LANGUAGE_NONE, 'is_new' => TRUE, 'name' => $user->name); node_object_prepare($node); parent::__construct($node); } $this->setInitialized(TRUE); }
/** * 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); }
public static function tearDownAfterClass() { User::logout(); if (static::$deleteCreatedEntities) { Utils::deleteCreatedEntities(); } }
/** * Default constructor for the Commerce Product object. Do not call this * class directly. Create a separate class for each product type and use its * constructor. * * @param int $product_id * Product id if an existing product is to be loaded. */ public function __construct($product_id = NULL) { $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); if (!is_null($product_id)) { $product = NULL; if (is_numeric($product_id)) { $product = commerce_product_load($product_id); } if ($product && $product->type == $type) { parent::__construct($product); return; } // SKU might have been passed instead. $product = commerce_product_load_by_sku($product_id); if ($product && $product->type == $type) { parent::__construct($product); return; } if (!$product) { $this->setErrors("Product with id or sku {$product_id} and type {$type} does not exist."); $this->setInitialized(FALSE); return; } } else { $product = commerce_product_new($type); parent::__construct($product); } }
public static function tearDownAfterClass() { User::logout(); if (static::$deleteCreatedEntities) { Utils::deleteCreatedEntities(); } if (static::$deleteMailLog && module_exists('redtest_helper_mail_logger')) { Mail::delete(); } }
/** * Fill Email field with specified values. * * @param Form $formObject * Form object. * @param string $field_name * Field name. * @param array|string $values * * @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 fillValues(Form $formObject, $field_name, $values) { if (!Field::hasFieldAccess($formObject, $field_name)) { return array(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible."); } $field_class = get_called_class(); $values = $field_class::normalizeInputForCompare($values); $values = $field_class::formatValuesForInput($values); $response = $formObject->fillMultiValued($field_name, $values); $response->normalizeVar(); return $response; }
/** * Fills text area field with provided values. * * @param Form $formObject * Form object. * @param string $field_name * Field name. * @param string|array $values * Either a string or an array. If it's a string, then it is assumed that * the field has only one value. If it is an array of strings, then it is * assumed that the field is multi-valued and the strings in the array * correspond to multiple text values of this field. If it is an array of * arrays, then it is assumed that the field is multi-valued and the inside * array can have the keys 'value' or 'format' which will be set * in form_state. Here are a few examples this parameter can take: * "<p>This is text string.</p>", or * array("<p>This is text string 1.</p>", "This is text string 2."), or * array( * array( * 'value' => "This is text string 1.", * 'format' => 'filtered_html', * ), * array( * 'value' => "This is text string 2.", * 'format' => 'plain_text', * ), * ); * * @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 fillValues(Form $formObject, $field_name, $values) { if (!Field::hasFieldAccess($formObject, $field_name)) { return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible."); } $defaults = array(); if (!empty($format)) { $defaults['format'] = $format; } $field_class = get_called_class(); return $field_class::fillTextValues($formObject, $field_name, $values, $defaults); }
/** * 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.'); } }
/** * 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; }
/** * Default constructor for the node object. Do not call this class directly. * Create a separate class for each content type and use its constructor. * * @param int $nid * Nid if an existing node is to be loaded. */ public function __construct($nid = NULL) { $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); if (!is_null($nid) && is_numeric($nid)) { $node = node_load($nid); if ($node->type == $type) { parent::__construct($node); } } else { global $user; $node = (object) array('title' => NULL, 'type' => $type, 'language' => LANGUAGE_NONE, 'is_new' => TRUE, 'name' => $user->name); node_object_prepare($node); parent::__construct($node); } $this->setInitialized(TRUE); }
/** * Default constructor for the Commerce Customer Profile. * * @param int $order_id * Order id if an existing order is to be loaded. */ public function __construct($profile_id = NULL) { if (!is_null($profile_id) && is_numeric($profile_id)) { $profile = commerce_customer_profile_load($profile_id); if (!$profile) { $this->setErrors("Profile with id {$profile} does not exist."); $this->setInitialized(FALSE); return; } } else { global $user; $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); $profile = commerce_customer_profile_new($type, $user->uid); } parent::__construct($profile); }
/** * 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; }
/** * Fill random float values in the float 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::getRandomFloat($min, $max, $num); $function = "fill" . Utils::makeTitleCase($field_name) . "Values"; return $formObject->{$function}($values); }
/** * This function will create Billing profile * * @param object $order * Order object */ public function createBillingProfileProgrammatically($order) { $profile_id = $this->getEntity(); global $user; $wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile_id); $wrapper->uid = $user->uid; $wrapper->commerce_customer_address->country = 'US'; $wrapper->commerce_customer_address->name_line = Utils::getRandomString(); $wrapper->commerce_customer_address->organisation_name = Utils::getRandomString(); $wrapper->commerce_customer_address->administrative_area = 'CA'; $wrapper->commerce_customer_address->locality = 'Sunnyvale'; $wrapper->commerce_customer_address->dependent_locality = ''; $wrapper->commerce_customer_address->postal_code = 94087; $wrapper->commerce_customer_address->thoroughfare = "929 E. El Camino Real"; $wrapper->commerce_customer_address->premise = "Apt. 424"; $wrapper->commerce_customer_address->phone_number = '(973) 328-6490'; commerce_customer_profile_save($profile_id); return new Response(TRUE, $profile_id, ""); }
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), ""); }
/** * Default constructor for the Commerce Line Item object. Do not call this * class directly. Create a separate class for each line item type and use * its constructor. * * You can also pass a second argument, and it will be interpreted as * order_id. It is used for creating a new line item. * * @param int $line_item_id * Product id if an existing product is to be loaded. */ public function __construct($line_item_id = NULL) { $args = func_get_args(); array_shift($args); $order_id = array_shift($args); if (is_null($order_id)) { $order_id = 0; } if (!is_null($line_item_id) && is_numeric($line_item_id)) { $line_item = commerce_line_item_load($line_item_id); if (!$line_item) { $this->setErrors("Line item with id {$line_item_id} does not exist."); $this->setInitialized(FALSE); return; } } else { $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); $line_item = commerce_line_item_new($type, $order_id); } parent::__construct($line_item); }
/** * 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 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()); }
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; }
/** * Whether the function name matches the pattern for determining whether * field values need to be checked. * * @param string $name * Function name. * * @return bool * TRUE if it matches and FALSE otherwise. */ private function isCheckFieldValuesFunction($name) { return Utils::startsWith($name, 'check') && Utils::endsWith($name, '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 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."); } $formObject->emptyField($field_name); if (is_string($values)) { $values = array($values); } $input = array(); if (sizeof($values)) { foreach ($values as $key => $value) { if (is_string($value) || is_numeric($value)) { $input[$value] = $value; } } $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input)); } if (isset($response)) { $response->setVar($input); } else { $response = new Response(TRUE, $input, ''); } return $response; }
/** * Magic method. This function will be executed when a matching function is * not found. Currently this supports four kinds of functions: * fill<FieldName>RandomValues(), fill<FieldName>Values, * has<FieldName>Access, is<FieldName>Required. * * @param string $name * Called function name. * @param string $arguments * Function arguments. * * @return mixed $output * Output depends on which function ultimately gets called. */ public function __call($name, $arguments) { if ($this->isFillFieldRandomValuesFunction($name)) { // Function name starts with "fill" and ends with "RandomValues". $field_name = Utils::makeSnakeCase(substr($name, 4, -12)); array_unshift($arguments, $field_name); return call_user_func_array(array($this, 'fillFieldRandomValues'), $arguments); } elseif ($this->isFillFieldValuesFunction($name)) { // Function name starts with "fill" and ends with "Values". $field_name = Utils::makeSnakeCase(substr($name, 4, -6)); $arguments = array_shift($arguments); return $this->fillFieldValues($field_name, $arguments); } elseif (strpos($name, 'has') === 0 && strrpos($name, 'Access') == strlen($name) - 6) { // Function name starts with "has" and ends with "Access". Function name // is not one of "hasCreateAccess", "hasUpdateAccess", "hasViewAccess" or // "hasDeleteAccess" otherwise code execution would not have reached this // function. This means that we are checking if a field is accessible. $field_name = Utils::makeSnakeCase(substr($name, 3, -6)); return $this->hasFieldAccess($field_name); } elseif (strpos($name, 'is') === 0 && strrpos($name, 'Required') == strlen($name) - 8) { // Function name starts with "is" and ends with "Required". We are // checking if a field is required or not. $field_name = Utils::makeSnakeCase(substr($name, 2, -8)); $arguments = array_shift($arguments); return $this->isRequired($field_name, $arguments); } }
/** * 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, ""); }
/** * 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)); }
public function getStripToken() { $form_state = $this->getFormState(); $strip_token = Utils::getStripeToken($form_state['values'])->verify($this); return $strip_token; }
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); }
/** * 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), ""); }
/** * 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, ""); }
/** * 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 array * An array with the following values: * (1) $success: TRUE if entities were created successfully and FALSE * otherwise. * (2) $objects: A single entity object or an associative array of entity * objects that are created. * (3) $msg: Error message if $success is FALSE, and an empty string * otherwise. */ public static function createRandom($num = 1, $options = array()) { 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), ""); }