You are here

public function EntityExistsTest::testEntityExists in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Kernel/Plugin/EntityExistsTest.php \Drupal\Tests\migrate\Kernel\Plugin\EntityExistsTest::testEntityExists()
  2. 10 core/modules/migrate/tests/src/Kernel/Plugin/EntityExistsTest.php \Drupal\Tests\migrate\Kernel\Plugin\EntityExistsTest::testEntityExists()

Tests the EntityExists plugin.

File

core/modules/migrate/tests/src/Kernel/Plugin/EntityExistsTest.php, line 34

Class

EntityExistsTest
Tests the EntityExists process plugin.

Namespace

Drupal\Tests\migrate\Kernel\Plugin

Code

public function testEntityExists() {
  $user = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $user
    ->save();
  $uid = $user
    ->id();
  $plugin = \Drupal::service('plugin.manager.migrate.process')
    ->createInstance('entity_exists', [
    'entity_type' => 'user',
  ]);
  $executable = $this
    ->prophesize(MigrateExecutableInterface::class)
    ->reveal();
  $row = new Row();

  // Ensure that the entity ID is returned if it really exists.
  $value = $plugin
    ->transform($uid, $executable, $row, 'buffalo');
  $this
    ->assertSame($uid, $value);

  // Ensure that the plugin returns FALSE if the entity doesn't exist.
  $value = $plugin
    ->transform(420, $executable, $row, 'buffalo');
  $this
    ->assertFalse($value);

  // Make sure the plugin can gracefully handle an array as input.
  $value = $plugin
    ->transform([
    $uid,
    420,
  ], $executable, $row, 'buffalo');
  $this
    ->assertSame($uid, $value);
}