forked from backdrop-contrib/conditional_fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_fields.test
323 lines (295 loc) · 13.5 KB
/
conditional_fields.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* @file
* Test Conditional Fields functionality and UI.
*/
class ConditionalFieldsTestCase extends BackdropWebTestCase {
protected $dependee_on_value;
protected $dependee_off_value;
protected $dependents;
public static function getInfo() {
return array(
'name' => 'Conditional Fields',
'description' => 'Test Conditional Fields functionality.',
'group' => 'Conditional Fields',
);
}
public function setUp() {
// Load all core modules that define field types.
parent::setUp('conditional_fields_test', 'list', 'text', 'number', 'taxonomy', 'image');
// Create and log in user.
$web_user = $this->backdropCreateUser(array('create conditional_fields_test content', 'edit any conditional_fields_test content'));
$this->backdropLogin($web_user);
// Create a vocabulary and two terms to test the term reference field.
$vocabulary = new stdClass();
$vocabulary->name = $this->randomName();
$vocabulary->machine_name = backdrop_strtolower($this->randomName());
taxonomy_vocabulary_save($vocabulary);
$term = new stdClass();
$term->name = 'Foo';
$term->vid = $vocabulary->vid;
taxonomy_term_save($term);
$term = new stdClass();
$term->name = 'Bar';
$term->vid = $vocabulary->vid;
taxonomy_term_save($term);
// Create dependee.
$this->dependee_on_value = $this->randomString();
$this->dependee_off_value = $this->randomString();
$dependee = array(
'field_name' => 'dependee',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array($this->dependee_on_value => t('Dependent fields are visible'), $this->dependee_off_value => t('Dependent fields are invisible')),
),
);
field_create_field($dependee);
$dependee_instance = array(
'field_name' => 'dependee',
'entity_type' => 'node',
'bundle' => 'conditional_fields_test',
'label' => 'dependee_label',
);
field_create_instance($dependee_instance);
// Reload the instance because we need the instance id.
$dependee_instance = field_info_instance('node', 'dependee', 'conditional_fields_test');
// Prepare one field for each widget type/field type combination.
foreach (field_info_widget_types() as $widget_type => $widget_info) {
// TODO: test files and images.
if ($widget_type == 'file_generic' || $widget_type == 'image_image') {
continue;
}
foreach ($widget_info['field types'] as $field_type) {
$field_name = 'dependent_' . backdrop_strtolower($this->randomName());
$dependent = array(
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => 1,
);
// Some fields need allowed values to function properly.
if ($widget_info['module'] == 'options') {
$dependent['settings']['allowed_values'] = array('Foo', 'Bar');
}
if ($field_type == 'taxonomy_term_reference') {
$dependent['settings']['allowed_values'] = array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => '0',
),
);
}
$dependent_instance = array(
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'conditional_fields_test',
'widget' => array(
'type' => $widget_type,
'settings' => array('display_label' => 1), // Used by boolean on/off checkbox.
),
'required' => TRUE,
'label' => $widget_type . '_' . $field_type . '_label',
);
// Radios spit an error if submitted with no value.
if ($widget_type == 'options_buttons' && $widget_info['module'] == 'list') {
// This is the "Bar" option.
$dependent_instance['default_value'] = 1;
}
$this->dependents[] = array(
'field_type' => $field_type,
'widget_type' => $widget_type,
'widget' => $widget_info,
'field' => $dependent,
'instance' => $dependent_instance,
);
// Create a multiple value version of most fields.
if ($widget_type == 'options_onoff') {
continue;
}
$dependent['cardinality'] = -1;
$dependent['field_name'] = $dependent_instance['field_name'] = $field_name . '_multiple';
$dependent_instance['label'] .= '_multiple';
$this->dependents[] = array(
'field_type' => $field_type,
'widget_type' => $widget_type,
'widget' => $widget_info,
'field' => $dependent,
'instance' => $dependent_instance,
);
}
}
// Create fields and dependencies.
$dependency_options = array(
'value_form' => $this->dependee_on_value,
'value' => array(array('value' => $this->dependee_on_value)),
);
foreach ($this->dependents as $dependent) {
field_create_field($dependent['field']);
field_create_instance($dependent['instance']);
$dependent_instance = field_info_instance('node', $dependent['field']['field_name'], 'conditional_fields_test');
conditional_fields_dependency_insert($dependee_instance['id'], $dependent_instance['id'], $dependency_options);
}
}
/**
* Tests field dependencies on a node.
*/
public function testNodeDependencies() {
// Try to submit a node with triggered dependencies.
// The submit should fail because the dependent fields are required.
$langcode = LANGUAGE_NONE;
$edit = array(
"dependee[$langcode]" => $this->dependee_on_value,
);
$this->backdropPost('node/add/conditional-fields-test', $edit, t('Save'));
foreach ($this->dependents as $dependent) {
// Skip fields that always have a default value.
if (($dependent['widget_type'] == 'options_buttons' && $dependent['field']['cardinality'] == 1) || (in_array($dependent['widget_type'], array('options_select', 'options_buttons')) && $dependent['field_type'] == 'taxonomy_term_reference')) {
continue;
}
// Multiple value textfields are dumb and can't find their own name.
if ($dependent['field']['cardinality'] == -1 && in_array($dependent['widget_type'], array('number', 'text_textfield', 'text_textarea', 'text_textarea_with_summary'))) {
$name = '';
}
else {
$name = $dependent['widget_type'] . '_' . $dependent['field_type'] . '_label';
$name .= $dependent['field']['cardinality'] == -1 ? '_multiple' : '';
}
$this->assertRaw(t('!name field is required.', array('!name' => $name)), 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value required ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' and no value fails validation');
}
// Fill the dependents with values and save the node.
$edit = array(
"dependee[$langcode]" => $this->dependee_on_value,
);
foreach ($this->dependents as $dependent) {
// Text fields have structure field_name[langcode][delta][value].
if (in_array($dependent['widget_type'], array('number', 'text_textfield', 'text_textarea', 'text_textarea_with_summary'))) {
$edit[$dependent['field']['field_name'] . "[$langcode][0][value]"] = '1';
}
elseif ($dependent['widget_type'] == 'options_select') {
if ($dependent['field']['cardinality'] == 1) {
// Single value select fields have structure field_name[langcode].
$edit[$dependent['field']['field_name'] . "[$langcode]"] = '1';
}
else {
// Multiple value select fields have structure field_name[langcode][].
$edit[$dependent['field']['field_name'] . "[$langcode][]"] = '1';
}
}
elseif (in_array($dependent['widget_type'], array('options_buttons', 'options_onoff', 'taxonomy_autocomplete'))) {
if ($dependent['field']['cardinality'] == 1 || $dependent['widget_type'] == 'taxonomy_autocomplete') {
// Radios and autocomplete fields have structure field_name[langcode].
$edit[$dependent['field']['field_name'] . "[$langcode]"] = '1';
}
else {
// Checkboxes have structure field_name[langcode][delta].
$edit[$dependent['field']['field_name'] . "[$langcode][1]"] = '1';
}
}
elseif (in_array($dependent['widget_type'], array('file_generic', 'image_image'))) {
// TODO.
// $edit[$dependent['field']['field_name'] . "[$langcode][0][fid]"] = '1';
}
}
$this->backdropPost('node/add/conditional-fields-test', $edit, t('Save'));
$this->assertRaw(t('@type %title has been created.', array('@type' => 'Conditional Fields Test Node Type', '%title' => '')), 'Node was created with triggered dependencies.');
// Verify that the fields are visible on node view.
foreach ($this->dependents as $dependent) {
$this->assertText($dependent['instance']['label'] . ':', 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' is visible on node view');
}
// Untrigger the dependency and verify that node is updated.
$edit = array(
"dependee[$langcode]" => $this->dependee_off_value,
);
$this->backdropPost('node/1/edit', $edit, t('Save'));
$this->assertRaw(t('@type %title has been updated.', array('@type' => 'Conditional Fields Test Node Type', '%title' => '')), 'Node was updated with untriggered dependencies.');
// Verify that fields are invisible on node view.
foreach ($this->dependents as $dependent) {
$this->assertNoText($dependent['instance']['label'] . ':', 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' is invisible on node view');
}
}
}
class ConditionalFieldsUITestCase extends backdropWebTestCase {
public static function getInfo() {
return array(
'name' => 'Conditional Fields UI',
'description' => 'Test Conditional Fields UI.',
'group' => 'Conditional Fields',
);
}
public function setUp() {
parent::setUp('conditional_fields_test', 'list', 'text');
// Create and log in user.
$web_user = $this->backdropCreateUser(array('access administration pages', 'administer content types', 'administer dependencies'));
$this->backdropLogin($web_user);
// Create a field that will be used as a dependee.
$dependee = array(
'field_name' => 'dependee',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('on' => t('Dependent is visible'), 'off' => t('Dependent is invisible')),
),
);
field_create_field($dependee);
$instance = array(
'field_name' => 'dependee',
'entity_type' => 'node',
'bundle' => 'conditional_fields_test',
'label' => t('Label of the dependee field'),
);
field_create_instance($instance);
// Create a field that will be used as a dependent.
$dependent = array(
'field_name' => 'dependent',
'type' => 'text',
);
field_create_field($dependent);
$instance = array(
'field_name' => 'dependent',
'entity_type' => 'node',
'bundle' => 'conditional_fields_test',
'label' => t('Label of the dependent field'),
);
field_create_instance($instance);
}
/**
* Ensures that the administration pages are viewable.
*/
public function testDependenciesOverview() {
$dependee = field_read_instance('node', 'dependee', 'conditional_fields_test');
$this->backdropGet('admin/structure');
$this->clickLink(t('Field dependencies'));
$this->clickLink(t('Node'));
$this->backdropGet('admin/structure/types/manage/conditional-fields-test');
$this->clickLink(t('Manage dependencies'));
$this->assertFieldByName('dependee', $dependee['id'], 'The dependee selection field is in the content type dependencies page.');
$this->assertFieldByName('dependent', $dependee['id'], 'The dependent selection field is in the content type dependencies page.');
}
/**
* Tests adding, editing and deleting a dependency.
*/
public function testDependencyOperations() {
// Add dependency.
$dependee = field_read_instance('node', 'dependee', 'conditional_fields_test');
$dependent = field_read_instance('node', 'dependent', 'conditional_fields_test');
$edit = array(
'dependee' => $dependee['id'],
'dependent' => $dependent['id'],
);
$this->backdropPost('admin/structure/types/manage/conditional-fields-test/dependencies', $edit, t('Add dependency'), array(), array(), 'conditional-fields-dependency-add-form-node-conditional-fields-test');
// Edit dependency.
$edit = array(
'values_set' => CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
'values' => 'on',
);
$this->backdropPost(NULL, $edit, t('Save settings'), array('query' => array('destination' => 'admin/structure/types/manage/conditional-fields-test/dependencies')));
$this->assertRaw(t('%dependent_name is !state when %dependee_name has all the values: @values.', array(
'%dependent_name' => t('Label of the dependent field'),
'!state' => 'visible',
'%dependee_name' => t('Label of the dependee field'),
'@values' => 'on',
)), 'The dependency was updated correctly.');
// Delete dependency.
$this->clickLink(t('delete'));
$this->backdropPost(NULL, array(), t('Delete dependency'), array('query' => array('destination' => 'admin/structure/types/manage/conditional-fields-test/dependencies')));
$this->assertText(t('The dependency has been deleted.'));
}
}