You are here

public function EntityFileTest::testGetOverwriteMode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/Migrate/EntityFileTest.php \Drupal\file\Tests\Migrate\EntityFileTest::testGetOverwriteMode()

Tests various invocations of the getOverwriteMode() method.

File

core/modules/file/src/Tests/Migrate/EntityFileTest.php, line 134
Contains \Drupal\file\Tests\Migrate\EntityFileTest.

Class

EntityFileTest
Tests the entity file destination plugin.

Namespace

Drupal\file\Tests\Migrate

Code

public function testGetOverwriteMode() {
  $plugin = $this->destination;
  $method = new \ReflectionMethod($plugin, 'getOverwriteMode');
  $method
    ->setAccessible(TRUE);
  $row = new Row([], []);

  // If the plugin is not configured to rename the destination file, we should
  // always get FILE_EXISTS_REPLACE.
  $this
    ->assertIdentical(FILE_EXISTS_REPLACE, $method
    ->invoke($plugin, $row));

  // When the plugin IS configured to rename the destination file, it should
  // return FILE_EXISTS_RENAME if the destination entity already exists,
  // and FILE_EXISTS_REPLACE otherwise.
  $plugin->configuration['rename'] = TRUE;
  $plugin->storage = \Drupal::entityManager()
    ->getStorage('file');

  /** @var \Drupal\file\FileInterface $file */
  $file = $plugin->storage
    ->create();
  touch('public://foo.txt');
  $file
    ->setFileUri('public://foo.txt');
  $file
    ->save();
  $row
    ->setDestinationProperty($plugin->storage
    ->getEntityType()
    ->getKey('id'), $file
    ->id());
  $this
    ->assertIdentical(FILE_EXISTS_RENAME, $method
    ->invoke($plugin, $row));
  unlink('public://foo.txt');
}