You are here

public function AcquiadamMediaTest::testFailedFileSave in Media: Acquia DAM 8

Tests that version is only updated when file is saved correctly.

File

tests/src/Kernel/AcquiadamMediaTest.php, line 103

Class

AcquiadamMediaTest
Tests Media entities with Media: Acquia DAM source.

Namespace

Drupal\Tests\media_acquiadam\Kernel

Code

public function testFailedFileSave() {

  /** @var \Drupal\media_acquiadam\AssetData $asset_data */
  $asset_data = $this->container
    ->get('media_acquiadam.asset_data');

  /** @var \Drupal\media_acquiadam\Service\AssetFileEntityHelper $asset_file_helper */
  $asset_file_helper = $this->container
    ->get('media_acquiadam.asset_file.helper');

  /** @var \Drupal\Core\File\FileSystem $file_system */
  $file_system = $this->container
    ->get('file_system');

  // Store old version to test if version remains unchanged.
  $old_version = $asset_data
    ->get($this->asset->id, 'version');

  // Makes directory read only so file save fails.
  $directory = $asset_file_helper
    ->getDestinationFromEntity($this->media, 'field_acquiadam_asset_file');
  $file_system
    ->chmod($directory, 00);

  // Attempts to save new version of asset while directory isn't accessible.
  $this
    ->saveNewVersion();
  $new_version = $asset_data
    ->get($this->asset->id, 'version');
  $this
    ->assertEqual($old_version, $new_version, 'Asset version unchanged as expected.');

  // Restore permissions to directory and resave entity.
  $file_system
    ->chmod($directory, FileSystem::CHMOD_DIRECTORY);
  $this
    ->reSaveMedia();
  $new_version = $asset_data
    ->get($this->asset->id, 'version');
  $this
    ->assertNotEqual($old_version, $new_version, 'New version different from old version.');
  $this
    ->assertEqual($this->asset->version, $new_version, 'Asset version updated as expected.');
}