Skip to content
DedupeEntityTest.php 7.65 KiB
Newer Older
namespace Drupal\Tests\migrate\Unit\process;
@trigger_error('The ' . __NAMESPACE__ . '\DedupeEntityTest is deprecated in
Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\MakeUniqueEntityFieldTest', E_USER_DEPRECATED);

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
use Drupal\Component\Utility\Unicode;
 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\DedupeEntity
 * @group migrate
 */
class DedupeEntityTest extends MigrateProcessTestCase {

  /**
   * The mock entity query.
   *
   * @var \Drupal\Core\Entity\Query\QueryInterface
   * @var \Drupal\Core\Entity\Query\QueryFactory
   * The mocked entity type manager.
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject

  /**
   * The migration configuration, initialized to set the ID to test.
   *
   * @var array
   */
  protected $migrationConfiguration = [
    $this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);

    $storage = $this->getMock(EntityStorageInterface::class);
    $storage->expects($this->any())
      ->method('getQuery')
      ->willReturn($this->entityQuery);
    $this->entityTypeManager->expects($this->any())
      ->method('getStorage')
      ->with('test_entity_type')
      ->willReturn($storage);
   * Tests entity based deduplication based on providerTestDedupe() values.
   *
   * @dataProvider providerTestDedupe
  public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
      'entity_type' => 'test_entity_type',
      'field' => 'test_field',
    if ($postfix) {
      $configuration['postfix'] = $postfix;
    }
    $configuration['start'] = isset($start) ? $start : NULL;
    $configuration['length'] = isset($length) ? $length : NULL;
    $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
    $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
    $expected = Unicode::substr($value, $start, $length);
    $expected .= $count ? $postfix . $count : '';
    $this->assertSame($expected, $actual);
  }

  /**
   * Tests that invalid start position throws an exception.
   */
  public function testDedupeEntityInvalidStart() {
      'entity_type' => 'test_entity_type',
      'field' => 'test_field',
      'start' => 'foobar',
    ];
    $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
    $this->setExpectedException('Drupal\migrate\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
    $plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
  }

  /**
   * Tests that invalid length option throws an exception.
   */
  public function testDedupeEntityInvalidLength() {
      'entity_type' => 'test_entity_type',
      'field' => 'test_field',
      'length' => 'foobar',
    ];
    $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
    $this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
    $plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
  public function providerTestDedupe() {
      // Tests no duplication and start position.
      // Tests no duplication, start position, and length.
      // Tests duplication and start position.
      // Tests duplication, start position, and length.
      // Tests no duplication, postfix, and start position.
      // Tests no duplication, postfix, start position, and length.
      // Tests no duplication, postfix, and length.
      // Tests duplication, postfix, and start position.
      // Tests duplication, postfix, start position, and length.
      // Tests duplication, postfix, and length.
  }

  /**
   * Helper function to add expectations to the mock entity query object.
   *
   * @param int $count
   *   The number of deduplications to be set up.
   */
  protected function entityQueryExpects($count) {
    $this->entityQuery->expects($this->exactly($count + 1))
      ->method('condition')
      ->will($this->returnValue($this->entityQuery));
    $this->entityQuery->expects($this->exactly($count + 1))
      ->method('count')
      ->will($this->returnValue($this->entityQuery));
    $this->entityQuery->expects($this->exactly($count + 1))
      ->method('execute')
      ->will($this->returnCallback(function () use (&$count) { return $count--;}));
  }
  /**
   * Test deduplicating only migrated entities.
   */
  public function testDedupeMigrated() {
      'entity_type' => 'test_entity_type',
      'field' => 'test_field',
      'migrated' => TRUE,
    ];
    $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);

    // Setup the entityQuery used in DedupeEntity::exists. The map, $map, is
    // an array consisting of the four input parameters to the query condition
    // method and then the query to return. Both 'forum' and
    // 'test_vocab' are existing entities. There is no 'test_vocab1'.
    $map = [];
    foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
      $query = $this->prophesize(QueryInterface::class);
      $query->willBeConstructedWith([]);
      $query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
      $map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
    }
    $this->entityQuery
      ->method('condition')
      ->will($this->returnValueMap($map));

    // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
    $this->idMap
      ->method('lookupSourceID')
      ->will($this->returnValueMap([
        [['test_field' => 'forums'], FALSE],
        [['test_field' => 'test_vocab'], ['source_id' => 42]],
      ]));

    // Existing entity 'forums' was not migrated, it should not be deduplicated.
    $actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
    $this->assertEquals('forums', $actual, 'Pre-existing name is re-used');

    // Entity 'test_vocab' was migrated, should be deduplicated.
    $actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
    $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
  }