You are here

public function ImageBrowserUploadValidationTest::testValidation in Lightning Media 8.4

Same name and namespace in other branches
  1. 8.3 modules/lightning_media_image/tests/src/Functional/ImageBrowserUploadValidationTest.php \Drupal\Tests\lightning_media_image\Functional\ImageBrowserUploadValidationTest::testValidation()

Tests that the upload widget validates input correctly.

@dataProvider providerValidation

Parameters

string $file: The name of the file to upload (in ../../files).

string $expected_error: The expected error message.

File

modules/lightning_media_image/tests/src/Functional/ImageBrowserUploadValidationTest.php, line 61

Class

ImageBrowserUploadValidationTest
Tests validation when uploading files into the image browser.

Namespace

Drupal\Tests\lightning_media_image\Functional

Code

public function testValidation($file, $expected_error) {
  $assert_session = $this
    ->assertSession();
  $node_type = $this
    ->createContentType();

  /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_lightweight_image',
    'entity_type' => 'node',
    'type' => 'image',
    'cardinality' => 1,
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $node_type
      ->id(),
    'label' => 'Lightweight Image',
    'settings' => [
      'max_filesize' => '5 KB',
    ],
  ])
    ->save();
  $this->container
    ->get('entity_display.repository')
    ->getFormDisplay('node', $node_type
    ->id())
    ->setComponent('field_lightweight_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->save();
  $account = $this
    ->createUser([
    'create media',
    'create ' . $node_type
      ->id() . ' content',
    'access image_browser entity browser pages',
  ]);
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('/node/add/' . $node_type
    ->id());
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->visitImageBrowser();
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Upload');
  $file_field = $assert_session
    ->elementExists('css', '.js-form-managed-file');
  $file_field
    ->attachFileToField('File', __DIR__ . "/../../files/{$file}");
  $file_field
    ->pressButton('Upload');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->elementExists('css', '[role="alert"]');
  $assert_session
    ->pageTextContains($expected_error);

  // The error message should not be double-escaped.
  $assert_session
    ->responseNotContains('<em class="placeholder">');
  $assert_session
    ->elementExists('css', 'input.form-file.error');
}