/** * Retrieves a mocked migration. * * @return \Drupal\migrate\Entity\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject * The mocked migration. */ protected function getMigration() { $this->migrationConfiguration += ['migrationClass' => 'Drupal\\migrate\\Entity\\Migration']; $this->idMap = $this->getMock('Drupal\\migrate\\Plugin\\MigrateIdMapInterface'); $this->idMap->method('getQualifiedMapTableName')->willReturn('test_map'); $migration = $this->getMockBuilder($this->migrationConfiguration['migrationClass'])->disableOriginalConstructor()->getMock(); $migration->method('checkRequirements')->willReturn(TRUE); $migration->method('getIdMap')->willReturn($this->idMap); // We need the state to be toggled throughout the test so we store the value // on the test class and use a return callback. $migration->expects($this->any())->method('getStatus')->willReturnCallback(function () { return $this->migrationStatus; }); $migration->expects($this->any())->method('setStatus')->willReturnCallback(function ($status) { $this->migrationStatus = $status; }); $migration->method('getMigrationDependencies')->willReturn(['required' => [], 'optional' => []]); $configuration =& $this->migrationConfiguration; $migration->method('get')->willReturnCallback(function ($argument) use(&$configuration) { return isset($configuration[$argument]) ? $configuration[$argument] : ''; }); $migration->method('set')->willReturnCallback(function ($argument, $value) use(&$configuration) { $configuration[$argument] = $value; }); $migration->method('id')->willReturn($configuration['id']); return $migration; }
/** * Helper function to perform assertions on a user role. * * @param string $id * The role ID. * @param string[] $permissions * An array of user permissions. * @param int $lookupId * The original numeric ID of the role in the source database. * @param \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map * The map table plugin. */ protected function assertRole($id, array $permissions, $lookupId, MigrateIdMapInterface $id_map) { /** @var \Drupal\user\RoleInterface $role */ $role = Role::load($id); $this->assertInstanceOf(RoleInterface::class, $role); $this->assertSame($permissions, $role->getPermissions()); $this->assertSame([[$id]], $id_map->lookupDestinationIds(['rid' => $lookupId])); }
/** * {@inheritdoc} * * The migration iterates over rows returned by the source plugin. This * method determines the next row which will be processed and imported into * the system. * * The method tracks the source and destination IDs using the ID map plugin. * * This also takes care about highwater support. Highwater allows to reimport * rows from a previous migration run, which got changed in the meantime. * This is done by specifying a highwater field, which is compared with the * last time, the migration got executed (originalHighWater). */ public function next() { $this->currentSourceIds = NULL; $this->currentRow = NULL; // In order to find the next row we want to process, we ask the source // plugin for the next possible row. while (!isset($this->currentRow) && $this->getIterator()->valid()) { $row_data = $this->getIterator()->current() + $this->configuration; $this->getIterator()->next(); $row = new Row($row_data, $this->migration->getSourcePlugin()->getIds(), $this->migration->get('destinationIds')); // Populate the source key for this row. $this->currentSourceIds = $row->getSourceIdValues(); // Pick up the existing map row, if any, unless getNextRow() did it. if (!$this->mapRowAdded && ($id_map = $this->idMap->getRowBySource($this->currentSourceIds))) { $row->setIdMap($id_map); } // Clear any previous messages for this row before potentially adding // new ones. if (!empty($this->currentSourceIds)) { $this->idMap->delete($this->currentSourceIds, TRUE); } // Preparing the row gives source plugins the chance to skip. if ($this->prepareRow($row) === FALSE) { continue; } // Check whether the row needs processing. // 1. This row has not been imported yet. // 2. Explicitly set to update. // 3. The row is newer than the current highwater mark. // 4. If no such property exists then try by checking the hash of the row. if (!$row->getIdMap() || $row->needsUpdate() || $this->aboveHighwater($row) || $this->rowChanged($row)) { $this->currentRow = $row->freezeSource(); } } }
/** * {@inheritdoc} * * The migration iterates over rows returned by the source plugin. This * method determines the next row which will be processed and imported into * the system. * * The method tracks the source and destination IDs using the ID map plugin. * * This also takes care about highwater support. Highwater allows to reimport * rows from a previous migration run, which got changed in the meantime. * This is done by specifying a highwater field, which is compared with the * last time, the migration got executed (originalHighWater). */ public function next() { $this->currentSourceIds = NULL; $this->currentRow = NULL; // In order to find the next row we want to process, we ask the source // plugin for the next possible row. while (!isset($this->currentRow) && $this->getIterator()->valid()) { $row_data = $this->getIterator()->current() + $this->configuration; $this->getIterator()->next(); $row = new Row($row_data, $this->migration->getSourcePlugin()->getIds(), $this->migration->get('destinationIds')); // Populate the source key for this row. $this->currentSourceIds = $row->getSourceIdValues(); // Pick up the existing map row, if any, unless getNextRow() did it. if (!$this->mapRowAdded && ($id_map = $this->idMap->getRowBySource($this->currentSourceIds))) { $row->setIdMap($id_map); } // In case we have specified an ID list, but the ID given by the source is // not in there, we skip the row. $id_in_the_list = $this->idList && in_array(reset($this->currentSourceIds), $this->idList); if ($this->idList && !$id_in_the_list) { continue; } // Preparing the row gives source plugins the chance to skip. if ($this->prepareRow($row) === FALSE) { continue; } // Check whether the row needs processing. // 1. Explicitly specified IDs. // 2. This row has not been imported yet. // 3. Explicitly set to update. // 4. The row is newer than the current highwater mark. // 5. If no such property exists then try by checking the hash of the row. if ($id_in_the_list || !$row->getIdMap() || $row->needsUpdate() || $this->aboveHighwater($row) || $this->rowChanged($row)) { $this->currentRow = $row->freezeSource(); } } }
/** * {@inheritdoc} */ public function next() { $this->currentIds = NULL; $this->currentRow = NULL; $source_configuration = $this->migration->get('source'); while ($this->getIterator()->valid()) { $row_data = $this->getIterator()->current() + $source_configuration; $this->getIterator()->next(); $row = new Row($row_data, $this->migration->getSourcePlugin()->getIds(), $this->migration->get('destinationIds')); // Populate the source key for this row. $this->currentIds = $row->getSourceIdValues(); // Pick up the existing map row, if any, unless getNextRow() did it. if (!$this->mapRowAdded && ($id_map = $this->idMap->getRowBySource($this->currentIds))) { $row->setIdMap($id_map); } // First, determine if this row should be passed to prepareRow(), or // skipped entirely. The rules are: // 1. If there's an explicit idlist, that's all we care about (ignore // high waters and map rows). $prepared = FALSE; if (!empty($this->idList)) { if (in_array(reset($this->currentIds), $this->idList)) { // In the list, fall through. } else { // Not in the list, skip it. continue; } } elseif (!$row->getIdMap()) { // Fall through } elseif ($row->needsUpdate()) { // Fall through. } elseif (!empty($this->highWaterProperty['field'])) { if ($this->trackChanges) { if ($this->prepareRow($row) !== FALSE) { if ($row->changed()) { // This is a keeper $this->currentRow = $row; break; } else { // No change, skip it. continue; } } else { // prepareRow() told us to skip it. continue; } } else { // No high water and not tracking changes, skip. continue; } } elseif ($this->originalHighWater === '') { // Fall through } else { // Call prepareRow() here, in case the highWaterField needs preparation. if ($this->prepareRow($row) !== FALSE) { if ($row->getSourceProperty($this->highWaterProperty['name']) > $this->originalHighWater) { $this->currentRow = $row; break; } else { // Skip. continue; } } $prepared = TRUE; } // Allow the Migration to prepare this row. prepareRow() can return // boolean FALSE to ignore this row. if (!$prepared) { if ($this->prepareRow($row) !== FALSE) { // Finally, we've got a keeper. $this->currentRow = $row; break; } else { $this->currentRow = NULL; } } } if ($this->currentRow) { $this->currentRow->freezeSource(); } else { $this->currentIds = NULL; } }