You are here

public function BulkUploadTest::testBulkUpload in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 modules/lightning_media_bulk_upload/tests/src/FunctionalJavascript/BulkUploadTest.php \Drupal\Tests\lightning_media_bulk_upload\FunctionalJavascript\BulkUploadTest::testBulkUpload()

Tests bulk uploading files into the media library with a dropzone.

File

modules/lightning_media_bulk_upload/tests/src/FunctionalJavascript/BulkUploadTest.php, line 43

Class

BulkUploadTest
Tests bulk upload of files into the media library.

Namespace

Drupal\Tests\lightning_media_bulk_upload\FunctionalJavascript

Code

public function testBulkUpload() {
  $page = $this
    ->getSession()
    ->getPage();
  $account = $this
    ->drupalCreateUser([
    'access media overview',
    'create media',
    'update media',
    'dropzone upload files',
    'view the administration theme',
  ]);
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('/admin/content/media');
  $page
    ->clickLink('Bulk upload');

  // Wait for the dropzone to be initialized.
  sleep(3);
  $files = [
    'test.jpg',
    'test.mp3',
    'test.mp4',
    'test.pdf',
  ];
  $dir = $this->root . '/' . $this->container
    ->get('extension.list.module')
    ->get('lightning_media')
    ->getPath();
  foreach ($files as $file) {
    $file = "{$dir}/tests/files/{$file}";
    $this
      ->assertFileExists($file);
    $this
      ->getSession()
      ->executeScript('Dropzone.instances[0].hiddenFileInput.name = "file"');
    $page
      ->attachFileToField('file', $file);

    // @todo React when the upload actually completes.
    sleep(3);
  }
  $page
    ->pressButton('Continue');
  for ($i = 0; $i < count($files); $i++) {
    $page
      ->pressButton('Save');
  }

  // Ensure all the files were actually saved, and have the current user as
  // their owner.
  $saved_files = $this->container
    ->get('entity_type.manager')
    ->getStorage('file')
    ->loadByProperties([
    'filename' => $files,
    'uid' => $account
      ->id(),
  ]);
  $this
    ->assertSame(count($saved_files), count($files));
  $this
    ->drupalGet('/admin/content/media');

  // @todo Make this linkExists. For whatever reason, that assertion fails and
  // I don't really feel like debugging it.
  array_walk($files, [
    $this
      ->assertSession(),
    'pageTextContains',
  ]);
}