diff --git a/core/modules/file/tests/src/FunctionalJavascript/ClientValidationTest.php b/core/modules/file/tests/src/FunctionalJavascript/ClientValidationTest.php index 82ee052..e2bb136 100644 --- a/core/modules/file/tests/src/FunctionalJavascript/ClientValidationTest.php +++ b/core/modules/file/tests/src/FunctionalJavascript/ClientValidationTest.php @@ -3,6 +3,8 @@ namespace Drupal\Tests\file\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\image\Tests\ImageFieldCreationTrait; + /** * Tests the client side JavaScript validation. @@ -11,10 +13,12 @@ */ class ClientValidationTest extends JavascriptTestBase { + use ImageFieldCreationTrait; + /** * {@inheritdoc} */ - public static $modules = ['file', 'node']; + public static $modules = ['node', 'image', 'field_ui']; /** @@ -29,6 +33,30 @@ class ClientValidationTest extends JavascriptTestBase { */ protected function setUp() { parent::setUp(); + + // Create the Article node type. + $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); + + // Create a field with a basic filetype restriction. + $field_name = strtolower($this->randomMachineName()); + $field_settings = [ + 'file_extensions' => 'png', + ]; + $formatter_settings = [ + 'image_style' => 'large', + 'image_link' => '', + ]; + $this->createImageField($field_name, 'article', [], $field_settings, [], $formatter_settings); + + // Log in as a content author who can create Articles. + $user = $this->drupalCreateUser([ + 'access content', + 'create article content', + 'edit any article content', + 'delete any article content', + ]); + $this->drupalLogin($user); + $uri = 'public://file.txt'; file_unmanaged_save_data('Drupal rocks!', $uri, FILE_EXISTS_REPLACE); $this->file_path = \Drupal::service('file_system')->realpath($uri); @@ -38,27 +66,28 @@ protected function setUp() { * Tests the client side validation on file fields. */ public function testDisallowedExtensionErrorMessage() { - $admin_user = $this->drupalCreateUser([ - 'administer site configuration', - ]); - $this->drupalLogin($admin_user); - $this->drupalGet('node/add/article'); + $assert = $this->assertSession(); // Test uploading a file with a file extension not allowed. // Try to upload a file with an unallowed extension - $file_path = $this->file_path; - $session = $this->getSession(); - $session->executeScript("page.uploadFile('input[name=files[field_image_0]]', $file_path);"); + $input_selector = 'input.form-file'; + $input = $assert->elementExists('css', $input_selector); + $input->attachFile($this->file_path); + + // Trigger the upload logic with a mock "drop" event. + $script = 'var e = jQuery.Event("drop");' + . 'e.originalEvent = {dataTransfer: {files: jQuery("input.form-file").get(0).files}};' + . 'e.preventDefault = e.stopPropagation = function () {};' + . 'jQuery("input.form-file").trigger(e);'; + $this->getSession()->executeScript($script); // Verify that the error message is there. $error_selector = '.file-upload-js-error'; - $assert = $this->assertSession(); + // Verify that there is one and only one error message on the page. + $condition = "jQuery('$error_selector').length == 1"; + $this->assertJsCondition($condition, 10000); $assert->elementExists('css', $error_selector); - $assert->elementContains('css', $error_selector, 'The selected file robots.txt cannot be uploaded. Only files with the following extensions are allowed: png, gif, jpg, jpeg.'); - // Verify that there is only one error message on the page. - $condition = "(jQuery('$error_selector').length == 1)"; - $assert->assertJsCondition($condition); } }