Projet

Général

Profil

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

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

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

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

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

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

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

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

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

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

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

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

    
167
    return $typename;
168
  }
169

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

    
193
}