Projet

Général

Profil

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

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

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_text' => 'options_select',
26
    'number_float' => 'number',
27
    'number_integer' => 'number',
28
    'nodereference' => 'nodereference_select',
29
    'text' => 'text_textfield',
30
    'text_long' => 'text_textarea',
31
    'text_with_summary' => 'text_textarea_with_summary',
32
    'userreference' => 'userreference_select',
33
   );
34

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

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

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

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

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

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

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

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

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

    
161
    return $typename;
162
  }
163

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

    
187
}