1 | <?php |
---|
2 | module_load_include('test', 'feeds', 'tests/feeds'); |
---|
3 | |
---|
4 | /** |
---|
5 | * @file |
---|
6 | * Helper class with auxiliary functions for feeds mapper module tests. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * Base class for implementing Feeds Mapper test cases. |
---|
11 | */ |
---|
12 | class FeedsMapperTestCase extends FeedsWebTestCase { |
---|
13 | |
---|
14 | // A lookup map to select the widget for each field type. |
---|
15 | private static $field_widgets = array( |
---|
16 | 'date' => 'date_text', |
---|
17 | 'datestamp' => 'date_text', |
---|
18 | 'datetime' => 'date_text', |
---|
19 | 'number_decimal' => 'number', |
---|
20 | 'email' => 'email_textfield', |
---|
21 | 'emimage' => 'emimage_textfields', |
---|
22 | 'emaudio' => 'emaudio_textfields', |
---|
23 | 'filefield' => 'filefield_widget', |
---|
24 | 'image' => 'imagefield_widget', |
---|
25 | 'link' => 'link', |
---|
26 | 'number_float' => 'number', |
---|
27 | 'number_integer' => 'number', |
---|
28 | 'nodereference' => 'nodereference_select', |
---|
29 | 'text' => 'text_textfield', |
---|
30 | 'userreference' => 'userreference_select', |
---|
31 | ); |
---|
32 | |
---|
33 | /** |
---|
34 | * Assert that a form field for the given CCK field with the given value |
---|
35 | * exists in the current form. |
---|
36 | * |
---|
37 | * @param $field_name |
---|
38 | * The name of the CCK field. |
---|
39 | * @param $value |
---|
40 | * The (raw) value expected for the CCK field. |
---|
41 | * @param $index |
---|
42 | * The index of the field (for q multi-valued field). |
---|
43 | * |
---|
44 | * @see FeedsMapperTestCase::getFormFieldsNames() |
---|
45 | * @see FeedsMapperTestCase::getFormFieldsValues() |
---|
46 | */ |
---|
47 | protected function assertCCKFieldValue($field_name, $value, $index = 0) { |
---|
48 | $names = $this->getFormFieldsNames($field_name, $index); |
---|
49 | $values = $this->getFormFieldsValues($field_name, $value); |
---|
50 | foreach ($names as $k => $name) { |
---|
51 | $value = $values[$k]; |
---|
52 | $this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name))); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Returns the form fields names for a given CCK field. Default implementation |
---|
58 | * provides support for a single form field with the following name pattern |
---|
59 | * <code>"field_{$field_name}[{$index}][value]"</code> |
---|
60 | * |
---|
61 | * @param $field_name |
---|
62 | * The name of the CCK field. |
---|
63 | * @param $index |
---|
64 | * The index of the field (for q multi-valued field). |
---|
65 | * |
---|
66 | * @return |
---|
67 | * An array of form field names. |
---|
68 | */ |
---|
69 | protected function getFormFieldsNames($field_name, $index) { |
---|
70 | return array("field_{$field_name}[{$index}][value]"); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Returns the form fields values for a given CCK field. Default implementation |
---|
75 | * returns a single element array with $value casted to a string. |
---|
76 | * |
---|
77 | * @param $field_name |
---|
78 | * The name of the CCK field. |
---|
79 | * @param $value |
---|
80 | * The (raw) value expected for the CCK field. |
---|
81 | * @return An array of form field values. |
---|
82 | */ |
---|
83 | protected function getFormFieldsValues($field_name, $value) { |
---|
84 | return array((string)$value); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Create a new content-type, and add a field to it. Mostly copied from |
---|
89 | * cck/tests/content.crud.test ContentUICrud::testAddFieldUI |
---|
90 | * |
---|
91 | * @param $settings |
---|
92 | * (Optional) An array of settings to pass through to |
---|
93 | * drupalCreateContentType(). |
---|
94 | * @param $fields |
---|
95 | * (Optional) an keyed array of $field_name => $field_type used to add additional |
---|
96 | * fields to the new content type. |
---|
97 | * |
---|
98 | * @return |
---|
99 | * The machine name of the new content type. |
---|
100 | * |
---|
101 | * @see DrupalWebTestCase::drupalCreateContentType() |
---|
102 | */ |
---|
103 | final protected function createContentType(array $settings = array(), array $fields = array()) { |
---|
104 | $type = $this->drupalCreateContentType($settings); |
---|
105 | $typename = $type->type; |
---|
106 | |
---|
107 | $admin_type_url = 'admin/content/node-type/'. str_replace('_', '-', $typename); |
---|
108 | |
---|
109 | // Create the fields |
---|
110 | foreach ($fields as $field_name => $options) { |
---|
111 | if (is_string($options)) { |
---|
112 | $options = array('type' => $options); |
---|
113 | } |
---|
114 | $field_type = isset($options['type']) ? $options['type'] : 'text'; |
---|
115 | $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type); |
---|
116 | $this->assertTrue($field_widget !== NULL, "Field type $field_type supported"); |
---|
117 | $label = $field_name . '_' . $field_type . '_label'; |
---|
118 | $edit = array( |
---|
119 | '_add_new_field[label]' => $label, |
---|
120 | '_add_new_field[field_name]' => $field_name, |
---|
121 | '_add_new_field[type]' => $field_type, |
---|
122 | '_add_new_field[widget_type]' => $field_widget, |
---|
123 | ); |
---|
124 | $this->drupalPost($admin_type_url . '/fields', $edit, 'Save'); |
---|
125 | |
---|
126 | // (Default) Configure the field. |
---|
127 | $edit = isset($options['settings']) ? $options['settings'] : array(); |
---|
128 | $this->drupalPost(NULL, $edit, 'Save field settings'); |
---|
129 | $this->assertText('Added field ' . $label); |
---|
130 | } |
---|
131 | |
---|
132 | return $typename; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Select the widget for the field. Default implementation provides widgets |
---|
137 | * for Date, Number, Text, Node reference, User reference, Email, Emfield, |
---|
138 | * Filefield, Image, and Link. |
---|
139 | * |
---|
140 | * Extracted as a method to allow test implementations to add widgets for |
---|
141 | * the tested CCK field type(s). $field_name allow to test the same |
---|
142 | * field type with different widget (is this useful ?) |
---|
143 | * |
---|
144 | * @param $field_name |
---|
145 | * The name of the field. |
---|
146 | * @param $field_type |
---|
147 | * The CCK type of the field. |
---|
148 | * |
---|
149 | * @return |
---|
150 | * The widget for this field, or NULL if the field_type is not |
---|
151 | * supported by this test class. |
---|
152 | */ |
---|
153 | protected function selectFieldWidget($field_name, $field_type) { |
---|
154 | $field_widgets = FeedsMapperTestCase::$field_widgets; |
---|
155 | return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL; |
---|
156 | } |
---|
157 | } |
---|