function MigrateSqlIdMapTest::testLookupDestinationIds

Same name in other branches
  1. 8.9.x core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testLookupDestinationIds()
  2. 10 core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testLookupDestinationIds()
  3. 11.x core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testLookupDestinationIds()

Tests lookupDestinationIds().

File

core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php, line 474

Class

MigrateSqlIdMapTest
Tests the SQL ID map plugin.

Namespace

Drupal\Tests\migrate\Unit

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 migrate_map_sql_idmap_test 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 migrate_map_sql_idmap_test 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([
        TestSqlIdMap::SOURCE_IDS_HASH => uniqid(),
    ])
        ->execute();
    $this->assertNotEquals([
        [
            101,
            'en',
        ],
    ], $id_map->lookupDestinationIds([
        1,
        'en',
    ]));
}

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