123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- /**
- * @file
- * Contains FeedsMapperTestCase.
- */
- /**
- * Helper class with auxiliary functions for feeds mapper module tests.
- */
- class FeedsMapperTestCase extends FeedsWebTestCase {
- // A lookup map to select the widget for each field type.
- private static $field_widgets = array(
- 'date' => 'date_text',
- 'datestamp' => 'date_text',
- 'datetime' => 'date_text',
- 'number_decimal' => 'number',
- 'email' => 'email_textfield',
- 'emimage' => 'emimage_textfields',
- 'emaudio' => 'emaudio_textfields',
- 'file' => 'file_generic',
- 'image' => 'image_image',
- 'link_field' => 'link_field',
- 'list_boolean' => 'options_onoff',
- 'list_float' => 'options_select',
- 'list_integer' => 'options_select',
- 'list_text' => 'options_select',
- 'number_float' => 'number',
- 'number_integer' => 'number',
- 'nodereference' => 'nodereference_select',
- 'text' => 'text_textfield',
- 'text_long' => 'text_textarea',
- 'text_with_summary' => 'text_textarea_with_summary',
- 'userreference' => 'userreference_select',
- );
- /**
- * Assert that a form field for the given field with the given value
- * exists in the current form.
- *
- * @param $field_name
- * The name of the field.
- * @param $value
- * The (raw) value expected for the field.
- * @param $index
- * The index of the field (for q multi-valued field).
- *
- * @see FeedsMapperTestCase::getFormFieldsNames()
- * @see FeedsMapperTestCase::getFormFieldsValues()
- */
- protected function assertNodeFieldValue($field_name, $value, $index = 0) {
- $names = $this->getFormFieldsNames($field_name, $index);
- $values = $this->getFormFieldsValues($field_name, $value);
- foreach ($names as $k => $name) {
- $value = $values[$k];
- $this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name)));
- }
- }
- /**
- * Assert that a form field for the given field with the given value
- * does not exist in the current form.
- *
- * @param $field_name
- * The name of the field.
- * @param $value
- * The (raw) value of the field.
- * @param $index
- * The index of the field (for q multi-valued field).
- *
- * @see FeedsMapperTestCase::getFormFieldsNames()
- * @see FeedsMapperTestCase::getFormFieldsValues()
- */
- protected function assertNoNodeFieldValue($field_name, $value, $index = 0) {
- $names = $this->getFormFieldsNames($field_name, $index);
- $values = $this->getFormFieldsValues($field_name, $value);
- foreach ($names as $k => $name) {
- $value = $values[$k];
- $this->assertNoFieldByName($name, $value, t('Did not find form field %name for %field_name with the value %value.', array('%name' => $name, '%field_name' => $field_name, '%value' => $value)));
- }
- }
- /**
- * Returns the form fields names for a given CCK field. Default implementation
- * provides support for a single form field with the following name pattern
- * <code>"field_{$field_name}[{$index}][value]"</code>
- *
- * @param $field_name
- * The name of the CCK field.
- * @param $index
- * The index of the field (for q multi-valued field).
- *
- * @return
- * An array of form field names.
- */
- protected function getFormFieldsNames($field_name, $index) {
- return array("field_{$field_name}[und][{$index}][value]");
- }
- /**
- * Returns the form fields values for a given CCK field. Default implementation
- * returns a single element array with $value casted to a string.
- *
- * @param $field_name
- * The name of the CCK field.
- * @param $value
- * The (raw) value expected for the CCK field.
- * @return An array of form field values.
- */
- protected function getFormFieldsValues($field_name, $value) {
- return array((string)$value);
- }
- /**
- * Create a new content-type, and add a field to it. Mostly copied from
- * cck/tests/content.crud.test ContentUICrud::testAddFieldUI
- *
- * @param $settings
- * (Optional) An array of settings to pass through to
- * drupalCreateContentType().
- * @param $fields
- * (Optional) an keyed array of $field_name => $field_type used to add additional
- * fields to the new content type.
- *
- * @return
- * The machine name of the new content type.
- *
- * @see DrupalWebTestCase::drupalCreateContentType()
- */
- final protected function createContentType(array $settings = array(), array $fields = array()) {
- $type = $this->drupalCreateContentType($settings);
- $typename = $type->type;
- $admin_type_url = 'admin/structure/types/manage/' . str_replace('_', '-', $typename);
- // Create the fields
- foreach ($fields as $field_name => $options) {
- if (is_string($options)) {
- $options = array('type' => $options);
- }
- $field_type = isset($options['type']) ? $options['type'] : 'text';
- $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
- $this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
- $label = $field_name . '_' . $field_type . '_label';
- $edit = array(
- 'fields[_add_new_field][label]' => $label,
- 'fields[_add_new_field][field_name]' => $field_name,
- 'fields[_add_new_field][type]' => $field_type,
- 'fields[_add_new_field][widget_type]' => $field_widget,
- );
- $this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
- // (Default) Configure the field.
- $edit = isset($options['settings']) ? $options['settings'] : array();
- $this->drupalPost(NULL, $edit, 'Save field settings');
- $this->assertText('Updated field ' . $label . ' field settings.');
- $edit = isset($options['instance_settings']) ? $options['instance_settings'] : array();
- $this->drupalPost(NULL, $edit, 'Save settings');
- $this->assertText('Saved ' . $label . ' configuration.');
- }
- return $typename;
- }
- /**
- * Select the widget for the field. Default implementation provides widgets
- * for Date, Number, Text, Node reference, User reference, Email, Emfield,
- * Filefield, Image, and Link.
- *
- * Extracted as a method to allow test implementations to add widgets for
- * the tested CCK field type(s). $field_name allow to test the same
- * field type with different widget (is this useful ?)
- *
- * @param $field_name
- * The name of the field.
- * @param $field_type
- * The CCK type of the field.
- *
- * @return
- * The widget for this field, or NULL if the field_type is not
- * supported by this test class.
- */
- protected function selectFieldWidget($field_name, $field_type) {
- $field_widgets = FeedsMapperTestCase::$field_widgets;
- return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
- }
- }
|