function FileFieldWidgetTest::testMultiValuedWidget in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/file/src/Tests/FileFieldWidgetTest.php \Drupal\file\Tests\FileFieldWidgetTest::testMultiValuedWidget()
Tests upload and remove buttons for multiple multi-valued File fields.
File
- core/
modules/ file/ src/ Tests/ FileFieldWidgetTest.php, line 106 - Contains \Drupal\file\Tests\FileFieldWidgetTest.
Class
- FileFieldWidgetTest
- Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.
Namespace
Drupal\file\TestsCode
function testMultiValuedWidget() {
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$type_name = 'article';
// Use explicit names instead of random names for those fields, because of a
// bug in drupalPostForm() with multiple file uploads in one form, where the
// order of uploads depends on the order in which the upload elements are
// added to the $form (which, in the current implementation of
// FileStorage::listAll(), comes down to the alphabetical order on field
// names).
$field_name = 'test_file_field_1';
$field_name2 = 'test_file_field_2';
$cardinality = 3;
$this
->createFileField($field_name, 'node', $type_name, array(
'cardinality' => $cardinality,
));
$this
->createFileField($field_name2, 'node', $type_name, array(
'cardinality' => $cardinality,
));
$test_file = $this
->getTestFile('text');
foreach (array(
'nojs',
'js',
) as $type) {
// Visit the node creation form, and upload 3 files for each field. Since
// the field has cardinality of 3, ensure the "Upload" button is displayed
// until after the 3rd file, and after that, isn't displayed. Because
// SimpleTest triggers the last button with a given name, so upload to the
// second field first.
// @todo This is only testing a non-Ajax upload, because drupalPostAjaxForm()
// does not yet emulate jQuery's file upload.
//
$this
->drupalGet("node/add/{$type_name}");
foreach (array(
$field_name2,
$field_name,
) as $each_field_name) {
for ($delta = 0; $delta < 3; $delta++) {
$edit = array(
'files[' . $each_field_name . '_' . $delta . '][]' => drupal_realpath($test_file
->getFileUri()),
);
// If the Upload button doesn't exist, drupalPostForm() will automatically
// fail with an assertion message.
$this
->drupalPostForm(NULL, $edit, t('Upload'));
}
}
$this
->assertNoFieldByXpath('//input[@type="submit"]', t('Upload'), 'After uploading 3 files for each field, the "Upload" button is no longer displayed.');
$num_expected_remove_buttons = 6;
foreach (array(
$field_name,
$field_name2,
) as $current_field_name) {
// How many uploaded files for the current field are remaining.
$remaining = 3;
// Test clicking each "Remove" button. For extra robustness, test them out
// of sequential order. They are 0-indexed, and get renumbered after each
// iteration, so array(1, 1, 0) means:
// - First remove the 2nd file.
// - Then remove what is then the 2nd file (was originally the 3rd file).
// - Then remove the first file.
foreach (array(
1,
1,
0,
) as $delta) {
// Ensure we have the expected number of Remove buttons, and that they
// are numbered sequentially.
$buttons = $this
->xpath('//input[@type="submit" and @value="Remove"]');
$this
->assertTrue(is_array($buttons) && count($buttons) === $num_expected_remove_buttons, format_string('There are %n "Remove" buttons displayed (JSMode=%type).', array(
'%n' => $num_expected_remove_buttons,
'%type' => $type,
)));
foreach ($buttons as $i => $button) {
$key = $i >= $remaining ? $i - $remaining : $i;
$check_field_name = $field_name2;
if ($current_field_name == $field_name && $i < $remaining) {
$check_field_name = $field_name;
}
$this
->assertIdentical((string) $button['name'], $check_field_name . '_' . $key . '_remove_button');
}
// "Click" the remove button (emulating either a nojs or js submission).
$button_name = $current_field_name . '_' . $delta . '_remove_button';
switch ($type) {
case 'nojs':
// drupalPostForm() takes a $submit parameter that is the value of the
// button whose click we want to emulate. Since we have multiple
// buttons with the value "Remove", and want to control which one we
// use, we change the value of the other ones to something else.
// Since non-clicked buttons aren't included in the submitted POST
// data, and since drupalPostForm() will result in $this being updated
// with a newly rebuilt form, this doesn't cause problems.
foreach ($buttons as $button) {
if ($button['name'] != $button_name) {
$button['value'] = 'DUMMY';
}
}
$this
->drupalPostForm(NULL, array(), t('Remove'));
break;
case 'js':
// drupalPostAjaxForm() lets us target the button precisely, so we don't
// require the workaround used above for nojs.
$this
->drupalPostAjaxForm(NULL, array(), array(
$button_name => t('Remove'),
));
break;
}
$num_expected_remove_buttons--;
$remaining--;
// Ensure an "Upload" button for the current field is displayed with the
// correct name.
$upload_button_name = $current_field_name . '_' . $remaining . '_upload_button';
$buttons = $this
->xpath('//input[@type="submit" and @value="Upload" and @name=:name]', array(
':name' => $upload_button_name,
));
$this
->assertTrue(is_array($buttons) && count($buttons) == 1, format_string('The upload button is displayed with the correct name (JSMode=%type).', array(
'%type' => $type,
)));
// Ensure only at most one button per field is displayed.
$buttons = $this
->xpath('//input[@type="submit" and @value="Upload"]');
$expected = $current_field_name == $field_name ? 1 : 2;
$this
->assertTrue(is_array($buttons) && count($buttons) == $expected, format_string('After removing a file, only one "Upload" button for each possible field is displayed (JSMode=%type).', array(
'%type' => $type,
)));
}
}
// Ensure the page now has no Remove buttons.
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), format_string('After removing all files, there is no "Remove" button displayed (JSMode=%type).', array(
'%type' => $type,
)));
// Save the node and ensure it does not have any files.
$this
->drupalPostForm(NULL, array(
'title[0][value]' => $this
->randomMachineName(),
), t('Save and publish'));
$matches = array();
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
$node_storage
->resetCache(array(
$nid,
));
$node = $node_storage
->load($nid);
$this
->assertTrue(empty($node->{$field_name}->target_id), 'Node was successfully saved without any files.');
}
$upload_files = array(
$test_file,
$test_file,
);
// Try to upload multiple files, but fewer than the maximum.
$nid = $this
->uploadNodeFiles($upload_files, $field_name, $type_name);
$node_storage
->resetCache(array(
$nid,
));
$node = $node_storage
->load($nid);
$this
->assertEqual(count($node->{$field_name}), count($upload_files), 'Node was successfully saved with mulitple files.');
// Try to upload more files than allowed on revision.
$this
->uploadNodeFiles($upload_files, $field_name, $nid, 1);
$args = array(
'%field' => $field_name,
'@count' => $cardinality,
);
$this
->assertRaw(t('%field: this field cannot hold more than @count values.', $args));
$node_storage
->resetCache(array(
$nid,
));
$node = $node_storage
->load($nid);
$this
->assertEqual(count($node->{$field_name}), count($upload_files), 'More files than allowed could not be saved to node.');
// Try to upload exactly the allowed number of files on revision.
$this
->uploadNodeFile($test_file, $field_name, $nid, 1);
$node_storage
->resetCache(array(
$nid,
));
$node = $node_storage
->load($nid);
$this
->assertEqual(count($node->{$field_name}), $cardinality, 'Node was successfully revised to maximum number of files.');
// Try to upload exactly the allowed number of files, new node.
$upload_files[] = $test_file;
$nid = $this
->uploadNodeFiles($upload_files, $field_name, $type_name);
$node_storage
->resetCache(array(
$nid,
));
$node = $node_storage
->load($nid);
$this
->assertEqual(count($node->{$field_name}), $cardinality, 'Node was successfully saved with maximum number of files.');
// Try to upload more files than allowed, new node.
$upload_files[] = $test_file;
$this
->uploadNodeFiles($upload_files, $field_name, $type_name);
$args = [
'%field' => $field_name,
'@max' => $cardinality,
'@count' => count($upload_files),
'%list' => $test_file
->getFileName(),
];
$this
->assertRaw(t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args));
}