View source
<?php
namespace Drupal\Tests\file\FunctionalJavascript;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\file\Entity\File;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\file\Functional\FileFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class FileFieldWidgetTest extends WebDriverTestBase {
use FieldUiTestTrait;
use FileFieldCreationTrait;
use TestFileCreationTrait;
protected $adminUser;
protected static $modules = [
'node',
'file',
'file_module_test',
'field_ui',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access content',
'access administration pages',
'administer site configuration',
'administer users',
'administer permissions',
'administer content types',
'administer node fields',
'administer node display',
'administer nodes',
'bypass node access',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
public function testMultiValuedWidget() {
$type_name = 'article';
$field_name = 'test_file_field_1';
$field_name2 = 'test_file_field_2';
$cardinality = 3;
$this
->createFileField($field_name, 'node', $type_name, [
'cardinality' => $cardinality,
]);
$this
->createFileField($field_name2, 'node', $type_name, [
'cardinality' => $cardinality,
]);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$test_file = current($this
->getTestFiles('text'));
$test_file_path = \Drupal::service('file_system')
->realpath($test_file->uri);
$this
->drupalGet("node/add/{$type_name}");
foreach ([
$field_name2,
$field_name,
] as $each_field_name) {
for ($delta = 0; $delta < 3; $delta++) {
$page
->attachFileToField('files[' . $each_field_name . '_' . $delta . '][]', $test_file_path);
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '[name="' . $each_field_name . '_' . $delta . '_remove_button"]'));
$this
->assertNull($assert_session
->waitForButton($each_field_name . '_' . $delta . '_upload_button'));
}
}
$num_expected_remove_buttons = 6;
foreach ([
$field_name,
$field_name2,
] as $current_field_name) {
$remaining = 3;
foreach ([
1,
1,
0,
] as $delta) {
$buttons = $this
->xpath('//input[@type="submit" and @value="Remove"]');
$this
->assertCount($num_expected_remove_buttons, $buttons, new FormattableMarkup('There are %n "Remove" buttons displayed.', [
'%n' => $num_expected_remove_buttons,
]));
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
->assertSame($check_field_name . '_' . $key . '_remove_button', $button
->getAttribute('name'));
}
$button_name = $current_field_name . '_' . $delta . '_remove_button';
$remove_button = $assert_session
->waitForButton($button_name);
$remove_button
->click();
$num_expected_remove_buttons--;
$remaining--;
$upload_button_name = $current_field_name . '_' . $remaining . '_upload_button';
$this
->assertNotNull($assert_session
->waitForButton($upload_button_name));
$button = $this
->assertSession()
->buttonExists($upload_button_name);
$this
->assertSame('Upload', $button
->getValue());
$expected = $current_field_name == $field_name ? 1 : 2;
$this
->assertSession()
->elementsCount('xpath', '//input[@type="submit" and @value="Upload"]', $expected);
}
}
}
public function testSingleValuedWidget() {
$type_name = 'article';
$field_name = 'test_file_field_1';
$cardinality = 1;
$this
->createFileField($field_name, 'node', $type_name, [
'cardinality' => $cardinality,
]);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$test_file = current($this
->getTestFiles('text'));
$test_file_path = \Drupal::service('file_system')
->realpath($test_file->uri);
$this
->drupalGet("node/add/{$type_name}");
$page
->findField('title[0][value]')
->setValue($this
->randomString());
$page
->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
$remove_button = $assert_session
->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
$this
->assertNotNull($remove_button);
$remove_button
->click();
$upload_field = $assert_session
->waitForElementVisible('css', 'input[type="file"]');
$this
->assertNotEmpty($upload_field);
$page
->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
$remove_button = $assert_session
->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
$this
->assertNotNull($remove_button);
$page
->pressButton('Save');
$page
->hasContent($test_file->name);
$test_image = current($this
->getTestFiles('image'));
$test_image_path = \Drupal::service('file_system')
->realpath($test_image->uri);
$this
->drupalGet("node/add/{$type_name}");
$page
->findField('title[0][value]')
->setValue($this
->randomString());
$page
->attachFileToField('files[' . $field_name . '_0]', $test_image_path);
$messages = $assert_session
->waitForElementVisible('css', '.file-upload-js-error');
$this
->assertEquals('The selected file image-test.png cannot be uploaded. Only files with the following extensions are allowed: txt.', $messages
->getText());
$page
->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
$remove_button = $assert_session
->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
$this
->assertNotEmpty($remove_button);
$this
->assertEmpty($this
->cssSelect('.file-upload-js-error'));
}
public function testUploadingMoreFilesThanAllowed() {
$type_name = 'article';
$field_name = 'test_file_field_1';
$cardinality = 2;
$this
->createFileField($field_name, 'node', $type_name, [
'cardinality' => $cardinality,
]);
$web_driver = $this
->getSession()
->getDriver();
$file_system = \Drupal::service('file_system');
$files = array_slice($this
->getTestFiles('text'), 0, 3);
$real_paths = [];
foreach ($files as $file) {
$real_paths[] = $file_system
->realpath($file->uri);
}
$remote_paths = [];
foreach ($real_paths as $path) {
$remote_paths[] = $web_driver
->uploadFileAndGetRemoteFilePath($path);
}
$this
->drupalGet("node/add/{$type_name}");
$multiple_field = $this
->getSession()
->getPage()
->findField('files[test_file_field_1_0][]');
$multiple_field
->setValue(implode("\n", $remote_paths));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains("Field {$field_name} can only hold {$cardinality} values but there were 3 uploaded. The following files have been omitted as a result: text-2.txt.");
}
public function getTestFile($type_name, $size = NULL) {
$file = current($this
->getTestFiles($type_name, $size));
$file->filesize = filesize($file->uri);
return File::create((array) $file);
}
}