FileFieldValidateTest.php 1.5 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\file\FunctionalJavascript;

5
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6 7 8 9 10 11 12 13 14
use Drupal\Tests\file\Functional\FileFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests validation functions such as file type, max file size, max size per
 * node, and required.
 *
 * @group file
 */
15
class FileFieldValidateTest extends WebDriverTestBase {
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

  use FileFieldCreationTrait;
  use TestFileCreationTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = ['node', 'file'];

  /**
   * Test the validation message is displayed only once for ajax uploads.
   */
  public function testAjaxValidationMessage() {
    $field_name = strtolower($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.');
  }

}