function FileFieldValidateTest::testAjaxValidationMessage

Same name in other branches
  1. 9 core/modules/file/tests/src/FunctionalJavascript/FileFieldValidateTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldValidateTest::testAjaxValidationMessage()
  2. 8.9.x core/modules/file/tests/src/FunctionalJavascript/FileFieldValidateTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldValidateTest::testAjaxValidationMessage()
  3. 11.x core/modules/file/tests/src/FunctionalJavascript/FileFieldValidateTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldValidateTest::testAjaxValidationMessage()

Tests the validation message is displayed only once for ajax uploads.

File

core/modules/file/tests/src/FunctionalJavascript/FileFieldValidateTest.php, line 37

Class

FileFieldValidateTest
Tests file field validation functions.

Namespace

Drupal\Tests\file\FunctionalJavascript

Code

public function testAjaxValidationMessage() : void {
    $field_name = $this->randomMachineName();
    $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
    ]);
    $this->createFileField($field_name, 'node', 'article', [], [
        'file_extensions' => 'txt',
    ]);
    $this->drupalLogin($this->drupalCreateUser([
        'access content',
        'create article content',
    ]));
    $page = $this->getSession()
        ->getPage();
    $this->drupalGet('node/add/article');
    $image_file = current($this->getTestFiles('image'));
    $image_path = $this->container
        ->get('file_system')
        ->realpath($image_file->uri);
    $page->attachFileToField('files[' . $field_name . '_0]', $image_path);
    $elements = $page->waitFor(10, function () use ($page) {
        return $page->findAll('css', '.messages--error');
    });
    $this->assertCount(1, $elements, 'Ajax validation messages are displayed once.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.