1. 8.3.x core/modules/migrate_drupal/tests/src/Kernel/d6/EntityContentBaseTest.php
  2. 8.3.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
  3. 8.0.x core/modules/migrate_drupal/src/Tests/d6/EntityContentBaseTest.php
  4. 8.0.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
  5. 8.1.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
  6. 8.1.x core/modules/migrate_drupal/tests/src/Kernel/d6/EntityContentBaseTest.php
  7. 8.2.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
  8. 8.2.x core/modules/migrate_drupal/tests/src/Kernel/d6/EntityContentBaseTest.php
  9. 8.4.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
  10. 8.4.x core/modules/migrate_drupal/tests/src/Kernel/d6/EntityContentBaseTest.php

Contains \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityContentBaseTest

Namespace

Drupal\Tests\migrate\Unit\Plugin\migrate\destination

File

core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityContentBaseTest.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityContentBaseTest
  5. */
  6. namespace Drupal\Tests\migrate\Unit\Plugin\migrate\destination;
  7. use Drupal\Core\Entity\ContentEntityInterface;
  8. use Drupal\Core\Entity\ContentEntityType;
  9. use Drupal\Core\Entity\EntityManagerInterface;
  10. use Drupal\Core\Entity\EntityStorageInterface;
  11. use Drupal\Core\Field\BaseFieldDefinition;
  12. use Drupal\Core\Field\FieldTypePluginManagerInterface;
  13. use Drupal\migrate\Plugin\MigrationInterface;
  14. use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
  15. use Drupal\migrate\Plugin\MigrateIdMapInterface;
  16. use Drupal\migrate\Row;
  17. use Drupal\Tests\UnitTestCase;
  18. /**
  19. * Tests base entity migration destination functionality.
  20. *
  21. * @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
  22. * @group migrate
  23. */
  24. class EntityContentBaseTest extends UnitTestCase {
  25. /**
  26. * @var \Drupal\migrate\Plugin\MigrationInterface
  27. */
  28. protected $migration;
  29. /**
  30. * @var \Drupal\Core\Entity\EntityStorageInterface
  31. */
  32. protected $storage;
  33. /**
  34. * @var \Drupal\Core\Entity\EntityManagerInterface
  35. */
  36. protected $entityManager;
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->migration = $this->prophesize(MigrationInterface::class);
  43. $this->storage = $this->prophesize(EntityStorageInterface::class);
  44. $this->entityManager = $this->prophesize(EntityManagerInterface::class);
  45. }
  46. /**
  47. * Test basic entity save.
  48. *
  49. * @covers ::import
  50. */
  51. public function testImport() {
  52. $bundles = [];
  53. $destination = new EntityTestDestination([], '', [],
  54. $this->migration->reveal(),
  55. $this->storage->reveal(),
  56. $bundles,
  57. $this->entityManager->reveal(),
  58. $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
  59. $entity = $this->prophesize(ContentEntityInterface::class);
  60. // Assert that save is called.
  61. $entity->save()
  62. ->shouldBeCalledTimes(1);
  63. // Set an id for the entity
  64. $entity->id()
  65. ->willReturn(5);
  66. $destination->setEntity($entity->reveal());
  67. // Ensure the id is saved entity id is returned from import.
  68. $this->assertEquals([5], $destination->import(new Row()));
  69. // Assert that import set the rollback action.
  70. $this->assertEquals(MigrateIdMapInterface::ROLLBACK_DELETE, $destination->rollbackAction());
  71. }
  72. /**
  73. * Test row skipping when we can't get an entity to save.
  74. *
  75. * @covers ::import
  76. * @expectedException \Drupal\migrate\MigrateException
  77. * @expectedExceptionMessage Unable to get entity
  78. */
  79. public function testImportEntityLoadFailure() {
  80. $bundles = [];
  81. $destination = new EntityTestDestination([], '', [],
  82. $this->migration->reveal(),
  83. $this->storage->reveal(),
  84. $bundles,
  85. $this->entityManager->reveal(),
  86. $this->prophesize(FieldTypePluginManagerInterface::class)->reveal());
  87. $destination->setEntity(FALSE);
  88. $destination->import(new Row());
  89. }
  90. /**
  91. * Test that translation destination fails for untranslatable entities.
  92. *
  93. * @expectedException \Drupal\migrate\MigrateException
  94. * @expectedExceptionMessage This entity type does not support translation
  95. */
  96. public function testUntranslatable() {
  97. // An entity type without a language.
  98. $entity_type = $this->prophesize(ContentEntityType::class);
  99. $entity_type->getKey('langcode')->willReturn('');
  100. $entity_type->getKey('id')->willReturn('id');
  101. $this->entityManager->getBaseFieldDefinitions('foo')
  102. ->willReturn(['id' => BaseFieldDefinitionTest::create('integer')]);
  103. $this->storage->getEntityType()->willReturn($entity_type->reveal());
  104. $destination = new EntityTestDestination(
  105. [ 'translations' => TRUE ],
  106. '',
  107. [],
  108. $this->migration->reveal(),
  109. $this->storage->reveal(),
  110. [],
  111. $this->entityManager->reveal(),
  112. $this->prophesize(FieldTypePluginManagerInterface::class)->reveal()
  113. );
  114. $destination->getIds();
  115. }
  116. }
  117. /**
  118. * Stub class for testing EntityContentBase methods.
  119. *
  120. * We want to test things without testing the base class implementations.
  121. */
  122. class EntityTestDestination extends EntityContentBase {
  123. private $entity = NULL;
  124. public function setEntity($entity) {
  125. $this->entity = $entity;
  126. }
  127. protected function getEntity(Row $row, array $old_destination_id_values) {
  128. return $this->entity;
  129. }
  130. public static function getEntityTypeId($plugin_id) {
  131. return 'foo';
  132. }
  133. }
  134. /**
  135. * Stub class for BaseFieldDefinition.
  136. */
  137. class BaseFieldDefinitionTest extends BaseFieldDefinition {
  138. public static function create($type) {
  139. return new static([]);
  140. }
  141. public function getSettings() {
  142. return [];
  143. }
  144. public function getType() {
  145. return 'integer';
  146. }
  147. }

Classes

Namesort descending Description
BaseFieldDefinitionTest Stub class for BaseFieldDefinition.
EntityContentBaseTest Tests base entity migration destination functionality.
EntityTestDestination Stub class for testing EntityContentBase methods.