class EntityRevisionTest
Tests entity revision destination.
Attributes
#[CoversClass(RealEntityRevision::class)]
#[Group('migrate')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest extends \Drupal\Tests\UnitTestCase
 
 
Expanded class hierarchy of EntityRevisionTest
File
- 
              core/
modules/ migrate/ tests/ src/ Unit/ destination/ EntityRevisionTest.php, line 28  
Namespace
Drupal\Tests\migrate\Unit\destinationView source
class EntityRevisionTest extends UnitTestCase {
  
  /**
   * The migration.
   *
   * @var \Drupal\migrate\Plugin\MigrationInterface
   */
  protected MigrationInterface $migration;
  
  /**
   * The destination storage.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected ObjectProphecy $storage;
  
  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected EntityFieldManagerInterface $entityFieldManager;
  
  /**
   * The field type manager.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
   */
  protected FieldTypePluginManagerInterface $fieldTypeManager;
  
  /**
   * The account switcher.
   *
   * @var \Drupal\Core\Session\AccountSwitcherInterface
   */
  protected AccountSwitcherInterface $accountSwitcher;
  
  /**
   * The entity type bundle information.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected EntityTypeBundleInfoInterface $entityTypeBundle;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    // Setup mocks to be used when creating a revision destination.
    $this->migration = $this->prophesize(MigrationInterface::class)
      ->reveal();
    $this->storage = $this->prophesize(RevisionableStorageInterface::class);
    $entity_type = $this->prophesize(EntityTypeInterface::class);
    $entity_type->getSingularLabel()
      ->willReturn('crazy');
    $entity_type->getPluralLabel()
      ->willReturn('craziness');
    $entity_type->getKey('id')
      ->willReturn('nid');
    $entity_type->getKey('revision')
      ->willReturn('vid');
    $this->storage
      ->getEntityType()
      ->willReturn($entity_type->reveal());
    $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class)
      ->reveal();
    $this->fieldTypeManager = $this->prophesize(FieldTypePluginManagerInterface::class)
      ->reveal();
    $this->accountSwitcher = $this->prophesize(AccountSwitcherInterface::class)
      ->reveal();
    $this->entityTypeBundle = $this->prophesize(EntityTypeBundleInfoInterface::class)
      ->reveal();
  }
  
  /**
   * Tests that passed old destination values are used by default.
   *
   * @legacy-covers ::getEntity
   */
  public function testGetEntityDestinationValues() : void {
    $destination = $this->getEntityRevisionDestination([]);
    // Return a dummy because we don't care what gets called.
    $entity = $this->prophesize(RevisionableInterface::class);
    // Assert that the first ID from the destination values is used to load the
    // entity.
    $this->storage
      ->loadRevision(12)
      ->shouldBeCalled()
      ->willReturn($entity->reveal());
    $row = new Row();
    $this->assertEquals($entity->reveal(), $destination->getEntity($row, [
      12,
      13,
    ]));
  }
  
  /**
   * Tests that revision updates update.
   *
   * @legacy-covers ::getEntity
   */
  public function testGetEntityUpdateRevision() : void {
    $destination = $this->getEntityRevisionDestination([]);
    $entity = $this->prophesize(RevisionableInterface::class);
    // Assert we load the correct revision.
    $this->storage
      ->loadRevision(2)
      ->shouldBeCalled()
      ->willReturn($entity->reveal());
    // Make sure its set as an update and not the default revision.
    $entity->setNewRevision(FALSE)
      ->shouldBeCalled();
    $entity->isDefaultRevision()
      ->shouldNotBeCalled();
    $row = new Row([
      'nid' => 1,
      'vid' => 2,
    ], [
      'nid' => 1,
      'vid' => 2,
    ]);
    $row->setDestinationProperty('vid', 2);
    $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
  }
  
  /**
   * Tests that new revisions are flagged to be written as new.
   *
   * @legacy-covers ::getEntity
   */
  public function testGetEntityNewRevision() : void {
    $destination = $this->getEntityRevisionDestination([]);
    $entity = $this->prophesize(RevisionableInterface::class);
    // Enforce is new should be disabled.
    $entity->enforceIsNew(FALSE)
      ->shouldBeCalled();
    // And toggle this as new revision but not the default revision.
    $entity->setNewRevision(TRUE)
      ->shouldBeCalled();
    $entity->isDefaultRevision(FALSE)
      ->shouldBeCalled();
    // Assert we load the correct revision.
    $this->storage
      ->load(1)
      ->shouldBeCalled()
      ->willReturn($entity->reveal());
    $row = new Row([
      'nid' => 1,
      'vid' => 2,
    ], [
      'nid' => 1,
      'vid' => 2,
    ]);
    $row->setDestinationProperty('nid', 1);
    $this->assertEquals($entity->reveal(), $destination->getEntity($row, []));
  }
  
  /**
   * Tests entity load failure.
   *
   * @legacy-covers ::getEntity
   */
  public function testGetEntityLoadFailure() : void {
    $destination = $this->getEntityRevisionDestination([]);
    // Return a failed load and make sure we don't fail and we return FALSE.
    $this->storage
      ->load(1)
      ->shouldBeCalled()
      ->willReturn(FALSE);
    $row = new Row([
      'nid' => 1,
      'vid' => 2,
    ], [
      'nid' => 1,
      'vid' => 2,
    ]);
    $row->setDestinationProperty('nid', 1);
    $this->assertFalse($destination->getEntity($row, []));
  }
  
  /**
   * Tests entity revision save.
   *
   * @legacy-covers ::save
   */
  public function testSave() : void {
    $entity = $this->prophesize(ContentEntityInterface::class);
    $entity->save()
      ->shouldBeCalled();
    // Syncing should be set once.
    $entity->setSyncing(Argument::exact(TRUE))
      ->shouldBeCalledTimes(1);
    $entity->getRevisionId()
      ->shouldBeCalled()
      ->willReturn(1234);
    $destination = $this->getEntityRevisionDestination();
    $this->assertEquals([
      1234,
    ], $destination->save($entity->reveal(), []));
  }
  
  /**
   * Helper method to create an entity revision destination with mock services.
   *
   * @param array $configuration
   *   Configuration for the destination.
   * @param string $plugin_id
   *   The plugin id.
   * @param array $plugin_definition
   *   The plugin definition.
   *
   * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision
   *   Mocked destination.
   *
   * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision
   */
  protected function getEntityRevisionDestination(array $configuration = [], $plugin_id = 'entity_revision', array $plugin_definition = []) {
    return new EntityRevision($configuration, $plugin_id, $plugin_definition, $this->migration, $this->storage
      ->reveal(), [], $this->entityFieldManager, $this->fieldTypeManager, $this->accountSwitcher, $this->entityTypeBundle);
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| EntityRevisionTest::$accountSwitcher | protected | property | The account switcher. | |
| EntityRevisionTest::$entityFieldManager | protected | property | The entity field manager. | |
| EntityRevisionTest::$entityTypeBundle | protected | property | The entity type bundle information. | |
| EntityRevisionTest::$fieldTypeManager | protected | property | The field type manager. | |
| EntityRevisionTest::$migration | protected | property | The migration. | |
| EntityRevisionTest::$storage | protected | property | The destination storage. | |
| EntityRevisionTest::getEntityRevisionDestination | protected | function | Helper method to create an entity revision destination with mock services. | |
| EntityRevisionTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| EntityRevisionTest::testGetEntityDestinationValues | public | function | Tests that passed old destination values are used by default. | |
| EntityRevisionTest::testGetEntityLoadFailure | public | function | Tests entity load failure. | |
| EntityRevisionTest::testGetEntityNewRevision | public | function | Tests that new revisions are flagged to be written as new. | |
| EntityRevisionTest::testGetEntityUpdateRevision | public | function | Tests that revision updates update. | |
| EntityRevisionTest::testSave | public | function | Tests entity revision save. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.