You are here

public function ImageBrowserUploadValidationTest::testValidation in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 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 62

Class

ImageBrowserUploadValidationTest
@group lightning_media @group lightning_media_image

Namespace

Drupal\Tests\lightning_media_image\Functional

Code

public function testValidation($file, $expected_error) {
  $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,
  ]);
  $this
    ->assertSame(SAVED_NEW, $field_storage
    ->save());
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $node_type
      ->id(),
    'label' => 'Lightweight Image',
    'settings' => [
      'max_filesize' => '5 KB',
    ],
  ])
    ->save();
  lightning_media_entity_get_form_display('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());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $settings = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="drupal-settings-json"]')
    ->getText();
  $settings = Json::decode($settings);
  $this
    ->assertArrayHasKey('entity_browser', $settings);
  $settings = reset($settings['entity_browser']['modal']);
  $url = $this
    ->buildUrl('/entity-browser/modal/image_browser', [
    'query' => [
      'uuid' => $settings['uuid'],
      'original_path' => $settings['original_path'],
    ],
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $file_field = $this
    ->assertSession()
    ->elementExists('css', '.js-form-managed-file');
  $file_field
    ->attachFileToField('File', __DIR__ . "/../../files/{$file}");
  $file_field
    ->pressButton('Upload');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->elementExists('css', '[role="alert"]');
  $this
    ->assertSession()
    ->pageTextContains($expected_error);

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