Projet

Général

Profil

Paste
Télécharger (5,61 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Helper class with auxiliary functions for feeds mapper module tests.
6
 */
7

    
8
/**
9
 * Base class for implementing Feeds Mapper test cases.
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
    'filefield' => 'filefield_widget',
23
    'image' => 'imagefield_widget',
24
    'link_field' => 'link_field',
25
    'number_float' => 'number',
26
    'number_integer' => 'number',
27
    'nodereference' => 'nodereference_select',
28
    'text' => 'text_textfield',
29
    'userreference' => 'userreference_select',
30
   );
31

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

    
55
  /**
56
   * Returns the form fields names for a given CCK field. Default implementation
57
   * provides support for a single form field with the following name pattern
58
   * <code>"field_{$field_name}[{$index}][value]"</code>
59
   *
60
   * @param $field_name
61
   *   The name of the CCK field.
62
   * @param $index
63
   *   The index of the field (for q multi-valued field).
64
   *
65
   * @return
66
   *   An array of form field names.
67
   */
68
  protected function getFormFieldsNames($field_name, $index) {
69
    return array("field_{$field_name}[und][{$index}][value]");
70
  }
71

    
72
  /**
73
   * Returns the form fields values for a given CCK field. Default implementation
74
   * returns a single element array with $value casted to a string.
75
   *
76
   * @param $field_name
77
   *   The name of the CCK field.
78
   * @param $value
79
   *   The (raw) value expected for the CCK field.
80
   * @return An array of form field values.
81
   */
82
  protected function getFormFieldsValues($field_name, $value) {
83
    return array((string)$value);
84
  }
85

    
86
  /**
87
   * Create a new content-type, and add a field to it. Mostly copied from
88
   * cck/tests/content.crud.test ContentUICrud::testAddFieldUI
89
   *
90
   * @param $settings
91
   *   (Optional) An array of settings to pass through to
92
   *   drupalCreateContentType().
93
   * @param $fields
94
   *   (Optional) an keyed array of $field_name => $field_type used to add additional
95
   *   fields to the new content type.
96
   *
97
   * @return
98
   *   The machine name of the new content type.
99
   *
100
   * @see DrupalWebTestCase::drupalCreateContentType()
101
   */
102
  final protected function createContentType(array $settings = array(), array $fields = array()) {
103
    $type = $this->drupalCreateContentType($settings);
104
    $typename = $type->type;
105

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

    
108
    // Create the fields
109
    foreach ($fields as $field_name => $options) {
110
      if (is_string($options)) {
111
        $options = array('type' => $options);
112
      }
113
      $field_type = isset($options['type']) ? $options['type'] : 'text';
114
      $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
115
      $this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
116
      $label = $field_name . '_' . $field_type . '_label';
117
      $edit = array(
118
        'fields[_add_new_field][label]' => $label,
119
        'fields[_add_new_field][field_name]' => $field_name,
120
        'fields[_add_new_field][type]' => $field_type,
121
        'fields[_add_new_field][widget_type]' => $field_widget,
122
      );
123
      $this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
124

    
125
      // (Default) Configure the field.
126
      $edit = isset($options['settings']) ? $options['settings'] : array();
127
      $this->drupalPost(NULL, $edit, 'Save field settings');
128
      $this->assertText('Updated field ' . $label . ' field settings.');
129

    
130
      $edit = isset($options['instance_settings']) ? $options['instance_settings'] : array();
131
      $this->drupalPost(NULL, $edit, 'Save settings');
132
      $this->assertText('Saved ' . $label . ' configuration.');
133
    }
134

    
135
    return $typename;
136
  }
137

    
138
  /**
139
   * Select the widget for the field. Default implementation provides widgets
140
   * for Date, Number, Text, Node reference, User reference, Email, Emfield,
141
   * Filefield, Image, and Link.
142
   *
143
   * Extracted as a method to allow test implementations to add widgets for
144
   * the tested CCK field type(s). $field_name allow to test the same
145
   * field type with different widget (is this useful ?)
146
   *
147
   * @param $field_name
148
   *   The name of the field.
149
   * @param $field_type
150
   *   The CCK type of the field.
151
   *
152
   * @return
153
   *   The widget for this field, or NULL if the field_type is not
154
   *   supported by this test class.
155
   */
156
  protected function selectFieldWidget($field_name, $field_type) {
157
    $field_widgets = FeedsMapperTestCase::$field_widgets;
158
    return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
159
  }
160
}