public function checkTaxonomyTermReferenceViews($field_name, $testClass, $values, $view_mode = 'default') { if (is_array($values)) { $labels = array(); foreach ($values as $value) { if (is_string($value)) { $labels[] = $value; } else { $labels[] = $value->getLabel(); } } $testClass->assertEquals($labels, call_user_func(array($this, "view" . Utils::makeTitleCase($field_name)), $view_mode), "Values of " . $field_name . " for " . $view_mode . " do not match."); } elseif (is_object($values)) { $testClass->assertEquals(Utils::getLabel($values), call_user_func(array($this, "view" . Utils::makeTitleCase($field_name)), $view_mode), "Values of " . $field_name . " for " . $view_mode . " do not match."); } else { $testClass->assertEquals($values, call_user_func(array($this, "view" . Utils::makeTitleCase($field_name)), $view_mode), "Values of " . $field_name . " for " . $view_mode . " do not match."); } }
/** * 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); }