Projet

Général

Profil

Paste
Télécharger (6,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper.test @ a192dc0b

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsMapperTestCase.
6
 */
7

    
8
/**
9
 * Helper class with auxiliary functions for feeds mapper module tests.
10
 */
11
class FeedsMapperTestCase extends FeedsWebTestCase {
12

    
13
  // A lookup map to select the widget for each field type.
14
  private static $field_widgets = array(
15
    'date' => 'date_text',
16
    'datestamp' => 'date_text',
17
    'datetime' => 'date_text',
18
    'number_decimal' => 'number',
19
    'email' => 'email_textfield',
20
    'emimage' => 'emimage_textfields',
21
    'emaudio' => 'emaudio_textfields',
22
    'file' => 'file_generic',
23
    'image' => 'image_image',
24
    'link_field' => 'link_field',
25
    'list_boolean' => 'options_onoff',
26
    'list_float' => 'options_select',
27
    'list_integer' => 'options_select',
28
    'list_text' => 'options_select',
29
    'number_float' => 'number',
30
    'number_integer' => 'number',
31
    'nodereference' => 'nodereference_select',
32
    'text' => 'text_textfield',
33
    'text_long' => 'text_textarea',
34
    'text_with_summary' => 'text_textarea_with_summary',
35
    'userreference' => 'userreference_select',
36
   );
37

    
38
  /**
39
   * Assert that a form field for the given field with the given value
40
   * exists in the current form.
41
   *
42
   * @param $field_name
43
   *   The name of the field.
44
   * @param $value
45
   *   The (raw) value expected for the field.
46
   * @param $index
47
   *   The index of the field (for q multi-valued field).
48
   *
49
   * @see FeedsMapperTestCase::getFormFieldsNames()
50
   * @see FeedsMapperTestCase::getFormFieldsValues()
51
   */
52
  protected function assertNodeFieldValue($field_name, $value, $index = 0) {
53
    $names = $this->getFormFieldsNames($field_name, $index);
54
    $values = $this->getFormFieldsValues($field_name, $value);
55
    foreach ($names as $k => $name) {
56
      $value = $values[$k];
57
      $this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name)));
58
    }
59
  }
60

    
61
  /**
62
   * Assert that a form field for the given field with the given value
63
   * does not exist in the current form.
64
   *
65
   * @param $field_name
66
   *   The name of the field.
67
   * @param $value
68
   *   The (raw) value of the field.
69
   * @param $index
70
   *   The index of the field (for q multi-valued field).
71
   *
72
   * @see FeedsMapperTestCase::getFormFieldsNames()
73
   * @see FeedsMapperTestCase::getFormFieldsValues()
74
   */
75
  protected function assertNoNodeFieldValue($field_name, $value, $index = 0) {
76
    $names = $this->getFormFieldsNames($field_name, $index);
77
    $values = $this->getFormFieldsValues($field_name, $value);
78
    foreach ($names as $k => $name) {
79
      $value = $values[$k];
80
      $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)));
81
    }
82
  }
83

    
84
  /**
85
   * Returns the form fields names for a given CCK field. Default implementation
86
   * provides support for a single form field with the following name pattern
87
   * <code>"field_{$field_name}[{$index}][value]"</code>
88
   *
89
   * @param $field_name
90
   *   The name of the CCK field.
91
   * @param $index
92
   *   The index of the field (for q multi-valued field).
93
   *
94
   * @return
95
   *   An array of form field names.
96
   */
97
  protected function getFormFieldsNames($field_name, $index) {
98
    return array("field_{$field_name}[und][{$index}][value]");
99
  }
100

    
101
  /**
102
   * Returns the form fields values for a given CCK field. Default implementation
103
   * returns a single element array with $value casted to a string.
104
   *
105
   * @param $field_name
106
   *   The name of the CCK field.
107
   * @param $value
108
   *   The (raw) value expected for the CCK field.
109
   * @return An array of form field values.
110
   */
111
  protected function getFormFieldsValues($field_name, $value) {
112
    return array((string)$value);
113
  }
114

    
115
  /**
116
   * Create a new content-type, and add a field to it. Mostly copied from
117
   * cck/tests/content.crud.test ContentUICrud::testAddFieldUI
118
   *
119
   * @param $settings
120
   *   (Optional) An array of settings to pass through to
121
   *   drupalCreateContentType().
122
   * @param $fields
123
   *   (Optional) an keyed array of $field_name => $field_type used to add additional
124
   *   fields to the new content type.
125
   *
126
   * @return
127
   *   The machine name of the new content type.
128
   *
129
   * @see DrupalWebTestCase::drupalCreateContentType()
130
   */
131
  final protected function createContentType(array $settings = array(), array $fields = array()) {
132
    $type = $this->drupalCreateContentType($settings);
133
    $typename = $type->type;
134

    
135
    $admin_type_url = 'admin/structure/types/manage/' . str_replace('_', '-', $typename);
136

    
137
    // Create the fields
138
    foreach ($fields as $field_name => $options) {
139
      if (is_string($options)) {
140
        $options = array('type' => $options);
141
      }
142
      $field_type = isset($options['type']) ? $options['type'] : 'text';
143
      $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
144
      $this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
145
      $label = $field_name . '_' . $field_type . '_label';
146
      $edit = array(
147
        'fields[_add_new_field][label]' => $label,
148
        'fields[_add_new_field][field_name]' => $field_name,
149
        'fields[_add_new_field][type]' => $field_type,
150
        'fields[_add_new_field][widget_type]' => $field_widget,
151
      );
152
      $this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
153

    
154
      // (Default) Configure the field.
155
      $edit = isset($options['settings']) ? $options['settings'] : array();
156
      $this->drupalPost(NULL, $edit, 'Save field settings');
157
      $this->assertText('Updated field ' . $label . ' field settings.');
158

    
159
      $edit = isset($options['instance_settings']) ? $options['instance_settings'] : array();
160
      $this->drupalPost(NULL, $edit, 'Save settings');
161
      $this->assertText('Saved ' . $label . ' configuration.');
162
    }
163

    
164
    return $typename;
165
  }
166

    
167
  /**
168
   * Select the widget for the field. Default implementation provides widgets
169
   * for Date, Number, Text, Node reference, User reference, Email, Emfield,
170
   * Filefield, Image, and Link.
171
   *
172
   * Extracted as a method to allow test implementations to add widgets for
173
   * the tested CCK field type(s). $field_name allow to test the same
174
   * field type with different widget (is this useful ?)
175
   *
176
   * @param $field_name
177
   *   The name of the field.
178
   * @param $field_type
179
   *   The CCK type of the field.
180
   *
181
   * @return
182
   *   The widget for this field, or NULL if the field_type is not
183
   *   supported by this test class.
184
   */
185
  protected function selectFieldWidget($field_name, $field_type) {
186
    $field_widgets = FeedsMapperTestCase::$field_widgets;
187
    return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
188
  }
189

    
190
}