3 namespace Drupal\Tests\migrate\Unit;
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
8 * Tests the SQL ID map plugin ensureTables() method.
12 class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
15 * The migration configuration, initialized to set the ID and destination IDs.
19 protected $migrationConfiguration = [
20 'id' => 'sql_idmap_test',
24 * Tests the ensureTables method when the tables do not exist.
26 public function testEnsureTablesNotExist() {
27 $fields['source_ids_hash'] = [
31 'description' => 'Hash of source ids. Used as primary key'
33 $fields['sourceid1'] = [
37 $fields['sourceid2'] = [
41 $fields['destid1'] = [
46 $fields['source_row_status'] = [
51 'default' => MigrateIdMapInterface::STATUS_IMPORTED,
52 'description' => 'Indicates current status of the source row',
54 $fields['rollback_action'] = [
59 'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
60 'description' => 'Flag indicating what to do for this item on rollback',
62 $fields['last_imported'] = [
67 'description' => 'UNIX timestamp of the last time this row was imported',
73 'description' => 'Hash of source row data, for detecting changes',
76 'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
78 'primary key' => ['source_ids_hash'],
80 'source' => ['sourceid1', 'sourceid2'],
83 $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
84 ->disableOriginalConstructor()
86 $schema->expects($this->at(0))
87 ->method('tableExists')
88 ->with('migrate_map_sql_idmap_test')
89 ->will($this->returnValue(FALSE));
90 $schema->expects($this->at(1))
91 ->method('createTable')
92 ->with('migrate_map_sql_idmap_test', $map_table_schema);
93 // Now do the message table.
100 $fields['source_ids_hash'] = [
104 'description' => 'Hash of source ids. Used as primary key'
112 $fields['message'] = [
118 'description' => 'Messages generated during a migration process',
120 'primary key' => ['msgid'],
123 $schema->expects($this->at(2))
124 ->method('tableExists')
125 ->with('migrate_message_sql_idmap_test')
126 ->will($this->returnValue(FALSE));
127 $schema->expects($this->at(3))
128 ->method('createTable')
129 ->with('migrate_message_sql_idmap_test', $table_schema);
130 $schema->expects($this->any())
131 ->method($this->anything());
132 $this->runEnsureTablesTest($schema);
136 * Tests the ensureTables method when the tables exist.
138 public function testEnsureTablesExist() {
139 $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
140 ->disableOriginalConstructor()
142 $schema->expects($this->at(0))
143 ->method('tableExists')
144 ->with('migrate_map_sql_idmap_test')
145 ->will($this->returnValue(TRUE));
146 $schema->expects($this->at(1))
147 ->method('fieldExists')
148 ->with('migrate_map_sql_idmap_test', 'rollback_action')
149 ->will($this->returnValue(FALSE));
156 'description' => 'Flag indicating what to do for this item on rollback',
158 $schema->expects($this->at(2))
160 ->with('migrate_map_sql_idmap_test', 'rollback_action', $field_schema);
161 $schema->expects($this->at(3))
162 ->method('fieldExists')
163 ->with('migrate_map_sql_idmap_test', 'hash')
164 ->will($this->returnValue(FALSE));
169 'description' => 'Hash of source row data, for detecting changes',
171 $schema->expects($this->at(4))
173 ->with('migrate_map_sql_idmap_test', 'hash', $field_schema);
174 $schema->expects($this->at(5))
175 ->method('fieldExists')
176 ->with('migrate_map_sql_idmap_test', 'source_ids_hash')
177 ->will($this->returnValue(FALSE));
182 'description' => 'Hash of source ids. Used as primary key',
184 $schema->expects($this->at(6))
186 ->with('migrate_map_sql_idmap_test', 'source_ids_hash', $field_schema);
187 $schema->expects($this->exactly(7))
188 ->method($this->anything());
189 $this->runEnsureTablesTest($schema);
193 * Actually run the test.
195 * @param array $schema
196 * The mock schema object with expectations set. The Sql constructor calls
197 * ensureTables() which in turn calls this object and the expectations on
198 * it are the actual test and there are no additional asserts added.
200 protected function runEnsureTablesTest($schema) {
201 $database = $this->getMockBuilder('Drupal\Core\Database\Connection')
202 ->disableOriginalConstructor()
204 $database->expects($this->any())
206 ->willReturn($schema);
207 $migration = $this->getMigration();
208 $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
209 $plugin->expects($this->any())
212 'source_id_property' => [
215 'source_id_property_2' => [
219 $migration->expects($this->any())
220 ->method('getSourcePlugin')
221 ->willReturn($plugin);
222 $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
223 $plugin->expects($this->any())
226 'destination_id_property' => [
230 $migration->expects($this->any())
231 ->method('getDestinationPlugin')
232 ->willReturn($plugin);
233 /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
234 $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
235 $map = new TestSqlIdMap($database, [], 'sql', [], $migration, $event_dispatcher);