class EntityRevisionTest

Same name in this branch
  1. 9 core/modules/migrate/tests/src/Unit/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest
  2. 9 core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityRevisionTest
Same name and namespace in other branches
  1. 11.x core/modules/migrate/tests/src/Unit/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest
  2. 11.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityRevisionTest
  3. 11.x core/modules/migrate/tests/src/Kernel/Plugin/EntityRevisionTest.php \Drupal\Tests\migrate\Kernel\Plugin\EntityRevisionTest
  4. 10 core/modules/migrate/tests/src/Unit/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest
  5. 10 core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityRevisionTest
  6. 10 core/modules/migrate/tests/src/Kernel/Plugin/EntityRevisionTest.php \Drupal\Tests\migrate\Kernel\Plugin\EntityRevisionTest
  7. 8.9.x core/modules/migrate/tests/src/Unit/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\destination\EntityRevisionTest
  8. 8.9.x core/modules/migrate/tests/src/Unit/Plugin/migrate/destination/EntityRevisionTest.php \Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityRevisionTest
  9. 8.9.x core/modules/migrate/tests/src/Kernel/Plugin/EntityRevisionTest.php \Drupal\Tests\migrate\Kernel\Plugin\EntityRevisionTest

Tests the EntityRevision destination plugin.

@group migrate

Hierarchy

Expanded class hierarchy of EntityRevisionTest

File

core/modules/migrate/tests/src/Kernel/Plugin/EntityRevisionTest.php, line 15

Namespace

Drupal\Tests\migrate\Kernel\Plugin
View source
class EntityRevisionTest extends MigrateTestBase {
  use ContentTypeCreationTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'content_translation',
    'field',
    'filter',
    'language',
    'node',
    'system',
    'text',
    'user',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installConfig('node');
    $this->installSchema('node', [
      'node_access',
    ]);
  }
  
  /**
   * Tests that EntityRevision correctly handles revision translations.
   */
  public function testRevisionTranslation() {
    ConfigurableLanguage::createFromLangcode('fr')->save();
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
      'type' => $this->createContentType()
        ->id(),
      'title' => 'Default 1',
    ]);
    $node->addTranslation('fr', [
      'title' => 'French 1',
    ]);
    $node->save();
    $node->setNewRevision();
    $node->setTitle('Default 2');
    $node->getTranslation('fr')
      ->setTitle('French 2');
    $node->save();
    $migration = [
      'source' => [
        'plugin' => 'embedded_data',
        'data_rows' => [
          [
            'nid' => $node->id(),
            'vid' => $node->getRevisionId(),
            'langcode' => 'fr',
            'title' => 'Titre nouveau, tabarnak!',
          ],
        ],
        'ids' => [
          'nid' => [
            'type' => 'integer',
          ],
          'vid' => [
            'type' => 'integer',
          ],
          'langcode' => [
            'type' => 'string',
          ],
        ],
      ],
      'process' => [
        'nid' => 'nid',
        'vid' => 'vid',
        'langcode' => 'langcode',
        'title' => 'title',
      ],
      'destination' => [
        'plugin' => 'entity_revision:node',
        'translations' => TRUE,
      ],
    ];
    /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
    $migration = $this->container
      ->get('plugin.manager.migration')
      ->createStubMigration($migration);
    $this->executeMigration($migration);
    // The entity_revision destination uses the revision ID and langcode as its
    // keys (the langcode is only used if the destination is configured for
    // translation), so we should be able to look up the source IDs by revision
    // ID and langcode.
    $source_ids = $migration->getIdMap()
      ->lookupSourceID([
      'vid' => $node->getRevisionId(),
      'langcode' => 'fr',
    ]);
    $this->assertNotEmpty($source_ids);
    $this->assertSame($node->id(), $source_ids['nid']);
    $this->assertSame($node->getRevisionId(), $source_ids['vid']);
    $this->assertSame('fr', $source_ids['langcode']);
    // Confirm the french revision was used in the migration, instead of the
    // default revision.
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = \Drupal::entityTypeManager();
    $revision = $entity_type_manager->getStorage('node')
      ->loadRevision(1);
    $this->assertSame('Default 1', $revision->label());
    $this->assertSame('French 1', $revision->getTranslation('fr')
      ->label());
    $revision = $entity_type_manager->getStorage('node')
      ->loadRevision(2);
    $this->assertSame('Default 2', $revision->label());
    $this->assertSame('Titre nouveau, tabarnak!', $revision->getTranslation('fr')
      ->label());
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.