You are here

public function SmartSqlTest::testLookupDestinationIds in Smart SQL ID Map 1.1.x

Same name and namespace in other branches
  1. 1.0.x tests/src/Unit/Plugin/migrate/id_map/SmartSqlTest.php \Drupal\Tests\smart_sql_idmap\Unit\Plugin\migrate\id_map\SmartSqlTest::testLookupDestinationIds()

Tests lookupDestinationIds().

Overrides MigrateSqlIdMapTest::testLookupDestinationIds

File

tests/src/Unit/Plugin/migrate/id_map/SmartSqlTest.php, line 217

Class

SmartSqlTest
Tests the Smart SQL ID map plugin.

Namespace

Drupal\Tests\smart_sql_idmap\Unit\Plugin\migrate\id_map

Code

public function testLookupDestinationIds() {

  // Simple map with one source and one destination ID.
  $id_map = $this
    ->setupRows([
    'nid',
  ], [
    'nid',
  ], [
    [
      1,
      101,
    ],
    [
      2,
      102,
    ],
    [
      3,
      103,
    ],
  ]);

  // Lookup nothing, gives nothing.
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([]));

  // Lookup by complete non-associative list.
  $this
    ->assertEquals([
    [
      101,
    ],
  ], $id_map
    ->lookupDestinationIds([
    1,
  ]));
  $this
    ->assertEquals([
    [
      102,
    ],
  ], $id_map
    ->lookupDestinationIds([
    2,
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    99,
  ]));

  // Lookup by complete associative list.
  $this
    ->assertEquals([
    [
      101,
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 1,
  ]));
  $this
    ->assertEquals([
    [
      102,
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 2,
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    'nid' => 99,
  ]));

  // Map with multiple source and destination IDs.
  $id_map = $this
    ->setupRows([
    'nid',
    'language',
  ], [
    'nid',
    'langcode',
  ], [
    [
      1,
      'en',
      101,
      'en',
    ],
    [
      1,
      'fr',
      101,
      'fr',
    ],
    [
      1,
      'de',
      101,
      'de',
    ],
    [
      2,
      'en',
      102,
      'en',
    ],
  ]);

  // Lookup nothing, gives nothing.
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([]));

  // Lookup by complete non-associative list.
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    1,
    'en',
  ]));
  $this
    ->assertEquals([
    [
      101,
      'fr',
    ],
  ], $id_map
    ->lookupDestinationIds([
    1,
    'fr',
  ]));
  $this
    ->assertEquals([
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    2,
    'en',
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    2,
    'fr',
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    99,
    'en',
  ]));

  // Lookup by complete associative list.
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 1,
    'language' => 'en',
  ]));
  $this
    ->assertEquals([
    [
      101,
      'fr',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 1,
    'language' => 'fr',
  ]));
  $this
    ->assertEquals([
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 2,
    'language' => 'en',
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    'nid' => 2,
    'language' => 'fr',
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    'nid' => 99,
    'language' => 'en',
  ]));

  // Lookup by partial non-associative list.
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
    [
      101,
      'fr',
    ],
    [
      101,
      'de',
    ],
  ], $id_map
    ->lookupDestinationIds([
    1,
  ]));
  $this
    ->assertEquals([
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    2,
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    99,
  ]));

  // Lookup by partial associative list.
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
    [
      101,
      'fr',
    ],
    [
      101,
      'de',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 1,
  ]));
  $this
    ->assertEquals([
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 2,
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    'nid' => 99,
  ]));
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
    [
      101,
      'fr',
    ],
    [
      101,
      'de',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 1,
    'language' => NULL,
  ]));
  $this
    ->assertEquals([
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'nid' => 2,
    'language' => NULL,
  ]));

  // Out-of-order partial associative list.
  $this
    ->assertEquals([
    [
      101,
      'en',
    ],
    [
      102,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'language' => 'en',
  ]));
  $this
    ->assertEquals([
    [
      101,
      'fr',
    ],
  ], $id_map
    ->lookupDestinationIds([
    'language' => 'fr',
  ]));
  $this
    ->assertEquals([], $id_map
    ->lookupDestinationIds([
    'language' => 'zh',
  ]));

  // Error conditions.
  try {
    $id_map
      ->lookupDestinationIds([
      1,
      2,
      3,
    ]);
    $this
      ->fail('Too many source IDs should throw');
  } catch (MigrateException $e) {
    $this
      ->assertEquals("Extra unknown items for map {$this->expectedMapTableName} in source IDs: array (\n  0 => 3,\n)", $e
      ->getMessage());
  }
  try {
    $id_map
      ->lookupDestinationIds([
      'nid' => 1,
      'aaa' => '2',
    ]);
    $this
      ->fail('Unknown source ID key should throw');
  } catch (MigrateException $e) {
    $this
      ->assertEquals("Extra unknown items for map {$this->expectedMapTableName} in source IDs: array (\n  'aaa' => '2',\n)", $e
      ->getMessage());
  }

  // Verify that we are looking up by source_id_hash when all source IDs are
  // passed in.
  $id_map
    ->getDatabase()
    ->update($id_map
    ->mapTableName())
    ->condition('sourceid1', 1)
    ->condition('sourceid2', 'en')
    ->fields([
    TestSmartSqlIdMap::SOURCE_IDS_HASH => uniqid(),
  ])
    ->execute();
  $this
    ->assertNotEquals([
    [
      101,
      'en',
    ],
  ], $id_map
    ->lookupDestinationIds([
    1,
    'en',
  ]));
}