annotate core/modules/migrate/tests/src/Unit/MigrateSqlIdMapEnsureTablesTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\migrate\Unit;
Chris@0 4
Chris@0 5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the SQL ID map plugin ensureTables() method.
Chris@0 9 *
Chris@0 10 * @group migrate
Chris@0 11 */
Chris@0 12 class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * The migration configuration, initialized to set the ID and destination IDs.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 protected $migrationConfiguration = [
Chris@0 20 'id' => 'sql_idmap_test',
Chris@0 21 ];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Tests the ensureTables method when the tables do not exist.
Chris@0 25 */
Chris@0 26 public function testEnsureTablesNotExist() {
Chris@0 27 $fields['source_ids_hash'] = [
Chris@0 28 'type' => 'varchar',
Chris@0 29 'length' => 64,
Chris@0 30 'not null' => 1,
Chris@17 31 'description' => 'Hash of source ids. Used as primary key',
Chris@0 32 ];
Chris@0 33 $fields['sourceid1'] = [
Chris@0 34 'type' => 'int',
Chris@0 35 'not null' => TRUE,
Chris@0 36 ];
Chris@0 37 $fields['sourceid2'] = [
Chris@0 38 'type' => 'int',
Chris@0 39 'not null' => TRUE,
Chris@0 40 ];
Chris@0 41 $fields['destid1'] = [
Chris@0 42 'type' => 'varchar',
Chris@0 43 'length' => 255,
Chris@0 44 'not null' => FALSE,
Chris@0 45 ];
Chris@0 46 $fields['source_row_status'] = [
Chris@0 47 'type' => 'int',
Chris@0 48 'size' => 'tiny',
Chris@0 49 'unsigned' => TRUE,
Chris@0 50 'not null' => TRUE,
Chris@0 51 'default' => MigrateIdMapInterface::STATUS_IMPORTED,
Chris@0 52 'description' => 'Indicates current status of the source row',
Chris@0 53 ];
Chris@0 54 $fields['rollback_action'] = [
Chris@0 55 'type' => 'int',
Chris@0 56 'size' => 'tiny',
Chris@0 57 'unsigned' => TRUE,
Chris@0 58 'not null' => TRUE,
Chris@0 59 'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
Chris@0 60 'description' => 'Flag indicating what to do for this item on rollback',
Chris@0 61 ];
Chris@0 62 $fields['last_imported'] = [
Chris@0 63 'type' => 'int',
Chris@0 64 'unsigned' => TRUE,
Chris@0 65 'not null' => TRUE,
Chris@0 66 'default' => 0,
Chris@0 67 'description' => 'UNIX timestamp of the last time this row was imported',
Chris@0 68 ];
Chris@0 69 $fields['hash'] = [
Chris@0 70 'type' => 'varchar',
Chris@0 71 'length' => '64',
Chris@0 72 'not null' => FALSE,
Chris@0 73 'description' => 'Hash of source row data, for detecting changes',
Chris@0 74 ];
Chris@0 75 $map_table_schema = [
Chris@0 76 'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
Chris@0 77 'fields' => $fields,
Chris@0 78 'primary key' => ['source_ids_hash'],
Chris@0 79 'indexes' => [
Chris@0 80 'source' => ['sourceid1', 'sourceid2'],
Chris@0 81 ],
Chris@0 82 ];
Chris@0 83 $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
Chris@0 84 ->disableOriginalConstructor()
Chris@0 85 ->getMock();
Chris@0 86 $schema->expects($this->at(0))
Chris@0 87 ->method('tableExists')
Chris@0 88 ->with('migrate_map_sql_idmap_test')
Chris@0 89 ->will($this->returnValue(FALSE));
Chris@0 90 $schema->expects($this->at(1))
Chris@0 91 ->method('createTable')
Chris@0 92 ->with('migrate_map_sql_idmap_test', $map_table_schema);
Chris@0 93 // Now do the message table.
Chris@0 94 $fields = [];
Chris@0 95 $fields['msgid'] = [
Chris@0 96 'type' => 'serial',
Chris@0 97 'unsigned' => TRUE,
Chris@0 98 'not null' => TRUE,
Chris@0 99 ];
Chris@0 100 $fields['source_ids_hash'] = [
Chris@0 101 'type' => 'varchar',
Chris@0 102 'length' => 64,
Chris@0 103 'not null' => 1,
Chris@17 104 'description' => 'Hash of source ids. Used as primary key',
Chris@0 105 ];
Chris@0 106 $fields['level'] = [
Chris@0 107 'type' => 'int',
Chris@0 108 'unsigned' => TRUE,
Chris@0 109 'not null' => TRUE,
Chris@0 110 'default' => 1,
Chris@0 111 ];
Chris@0 112 $fields['message'] = [
Chris@0 113 'type' => 'text',
Chris@0 114 'size' => 'medium',
Chris@0 115 'not null' => TRUE,
Chris@0 116 ];
Chris@0 117 $table_schema = [
Chris@0 118 'description' => 'Messages generated during a migration process',
Chris@0 119 'fields' => $fields,
Chris@0 120 'primary key' => ['msgid'],
Chris@0 121 ];
Chris@0 122
Chris@0 123 $schema->expects($this->at(2))
Chris@0 124 ->method('tableExists')
Chris@0 125 ->with('migrate_message_sql_idmap_test')
Chris@0 126 ->will($this->returnValue(FALSE));
Chris@0 127 $schema->expects($this->at(3))
Chris@0 128 ->method('createTable')
Chris@0 129 ->with('migrate_message_sql_idmap_test', $table_schema);
Chris@0 130 $schema->expects($this->any())
Chris@0 131 ->method($this->anything());
Chris@0 132 $this->runEnsureTablesTest($schema);
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Tests the ensureTables method when the tables exist.
Chris@0 137 */
Chris@0 138 public function testEnsureTablesExist() {
Chris@0 139 $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
Chris@0 140 ->disableOriginalConstructor()
Chris@0 141 ->getMock();
Chris@0 142 $schema->expects($this->at(0))
Chris@0 143 ->method('tableExists')
Chris@0 144 ->with('migrate_map_sql_idmap_test')
Chris@0 145 ->will($this->returnValue(TRUE));
Chris@0 146 $schema->expects($this->at(1))
Chris@0 147 ->method('fieldExists')
Chris@0 148 ->with('migrate_map_sql_idmap_test', 'rollback_action')
Chris@0 149 ->will($this->returnValue(FALSE));
Chris@0 150 $field_schema = [
Chris@0 151 'type' => 'int',
Chris@0 152 'size' => 'tiny',
Chris@0 153 'unsigned' => TRUE,
Chris@0 154 'not null' => TRUE,
Chris@0 155 'default' => 0,
Chris@0 156 'description' => 'Flag indicating what to do for this item on rollback',
Chris@0 157 ];
Chris@0 158 $schema->expects($this->at(2))
Chris@0 159 ->method('addField')
Chris@0 160 ->with('migrate_map_sql_idmap_test', 'rollback_action', $field_schema);
Chris@0 161 $schema->expects($this->at(3))
Chris@0 162 ->method('fieldExists')
Chris@0 163 ->with('migrate_map_sql_idmap_test', 'hash')
Chris@0 164 ->will($this->returnValue(FALSE));
Chris@0 165 $field_schema = [
Chris@0 166 'type' => 'varchar',
Chris@0 167 'length' => '64',
Chris@0 168 'not null' => FALSE,
Chris@0 169 'description' => 'Hash of source row data, for detecting changes',
Chris@0 170 ];
Chris@0 171 $schema->expects($this->at(4))
Chris@0 172 ->method('addField')
Chris@0 173 ->with('migrate_map_sql_idmap_test', 'hash', $field_schema);
Chris@0 174 $schema->expects($this->at(5))
Chris@0 175 ->method('fieldExists')
Chris@0 176 ->with('migrate_map_sql_idmap_test', 'source_ids_hash')
Chris@0 177 ->will($this->returnValue(FALSE));
Chris@0 178 $field_schema = [
Chris@0 179 'type' => 'varchar',
Chris@0 180 'length' => '64',
Chris@0 181 'not null' => TRUE,
Chris@0 182 'description' => 'Hash of source ids. Used as primary key',
Chris@0 183 ];
Chris@0 184 $schema->expects($this->at(6))
Chris@0 185 ->method('addField')
Chris@0 186 ->with('migrate_map_sql_idmap_test', 'source_ids_hash', $field_schema);
Chris@0 187 $schema->expects($this->exactly(7))
Chris@0 188 ->method($this->anything());
Chris@0 189 $this->runEnsureTablesTest($schema);
Chris@0 190 }
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Actually run the test.
Chris@0 194 *
Chris@0 195 * @param array $schema
Chris@0 196 * The mock schema object with expectations set. The Sql constructor calls
Chris@0 197 * ensureTables() which in turn calls this object and the expectations on
Chris@0 198 * it are the actual test and there are no additional asserts added.
Chris@0 199 */
Chris@0 200 protected function runEnsureTablesTest($schema) {
Chris@0 201 $database = $this->getMockBuilder('Drupal\Core\Database\Connection')
Chris@0 202 ->disableOriginalConstructor()
Chris@0 203 ->getMock();
Chris@0 204 $database->expects($this->any())
Chris@0 205 ->method('schema')
Chris@0 206 ->willReturn($schema);
Chris@0 207 $migration = $this->getMigration();
Chris@0 208 $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
Chris@0 209 $plugin->expects($this->any())
Chris@0 210 ->method('getIds')
Chris@0 211 ->willReturn([
Chris@0 212 'source_id_property' => [
Chris@0 213 'type' => 'integer',
Chris@0 214 ],
Chris@0 215 'source_id_property_2' => [
Chris@0 216 'type' => 'integer',
Chris@0 217 ],
Chris@0 218 ]);
Chris@0 219 $migration->expects($this->any())
Chris@0 220 ->method('getSourcePlugin')
Chris@0 221 ->willReturn($plugin);
Chris@0 222 $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
Chris@0 223 $plugin->expects($this->any())
Chris@0 224 ->method('getIds')
Chris@0 225 ->willReturn([
Chris@0 226 'destination_id_property' => [
Chris@0 227 'type' => 'string',
Chris@0 228 ],
Chris@0 229 ]);
Chris@0 230 $migration->expects($this->any())
Chris@0 231 ->method('getDestinationPlugin')
Chris@0 232 ->willReturn($plugin);
Chris@0 233 /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
Chris@0 234 $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
Chris@0 235 $map = new TestSqlIdMap($database, [], 'sql', [], $migration, $event_dispatcher);
Chris@0 236 $map->getDatabase();
Chris@0 237 }
Chris@0 238
Chris@0 239 }