Version 1
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / MigrateSqlIdMapEnsureTablesTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit;
4
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6
7 /**
8  * Tests the SQL ID map plugin ensureTables() method.
9  *
10  * @group migrate
11  */
12 class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
13
14   /**
15    * The migration configuration, initialized to set the ID and destination IDs.
16    *
17    * @var array
18    */
19   protected $migrationConfiguration = [
20     'id' => 'sql_idmap_test',
21   ];
22
23   /**
24    * Tests the ensureTables method when the tables do not exist.
25    */
26   public function testEnsureTablesNotExist() {
27     $fields['source_ids_hash'] = [
28       'type' => 'varchar',
29       'length' => 64,
30       'not null' => 1,
31       'description' => 'Hash of source ids. Used as primary key'
32     ];
33     $fields['sourceid1'] = [
34       'type' => 'int',
35       'not null' => TRUE,
36     ];
37     $fields['sourceid2'] = [
38       'type' => 'int',
39       'not null' => TRUE,
40     ];
41     $fields['destid1'] = [
42       'type' => 'varchar',
43       'length' => 255,
44       'not null' => FALSE,
45     ];
46     $fields['source_row_status'] = [
47       'type' => 'int',
48       'size' => 'tiny',
49       'unsigned' => TRUE,
50       'not null' => TRUE,
51       'default' => MigrateIdMapInterface::STATUS_IMPORTED,
52       'description' => 'Indicates current status of the source row',
53     ];
54     $fields['rollback_action'] = [
55       'type' => 'int',
56       'size' => 'tiny',
57       'unsigned' => TRUE,
58       'not null' => TRUE,
59       'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
60       'description' => 'Flag indicating what to do for this item on rollback',
61     ];
62     $fields['last_imported'] = [
63       'type' => 'int',
64       'unsigned' => TRUE,
65       'not null' => TRUE,
66       'default' => 0,
67       'description' => 'UNIX timestamp of the last time this row was imported',
68     ];
69     $fields['hash'] = [
70       'type' => 'varchar',
71       'length' => '64',
72       'not null' => FALSE,
73       'description' => 'Hash of source row data, for detecting changes',
74     ];
75     $map_table_schema = [
76       'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
77       'fields' => $fields,
78       'primary key' => ['source_ids_hash'],
79       'indexes' => [
80         'source' => ['sourceid1', 'sourceid2'],
81       ],
82     ];
83     $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
84       ->disableOriginalConstructor()
85       ->getMock();
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.
94     $fields = [];
95     $fields['msgid'] = [
96       'type' => 'serial',
97       'unsigned' => TRUE,
98       'not null' => TRUE,
99     ];
100     $fields['source_ids_hash'] = [
101       'type' => 'varchar',
102       'length' => 64,
103       'not null' => 1,
104       'description' => 'Hash of source ids. Used as primary key'
105     ];
106     $fields['level'] = [
107       'type' => 'int',
108       'unsigned' => TRUE,
109       'not null' => TRUE,
110       'default' => 1,
111     ];
112     $fields['message'] = [
113       'type' => 'text',
114       'size' => 'medium',
115       'not null' => TRUE,
116     ];
117     $table_schema = [
118       'description' => 'Messages generated during a migration process',
119       'fields' => $fields,
120       'primary key' => ['msgid'],
121     ];
122
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);
133   }
134
135   /**
136    * Tests the ensureTables method when the tables exist.
137    */
138   public function testEnsureTablesExist() {
139     $schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
140       ->disableOriginalConstructor()
141       ->getMock();
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));
150     $field_schema = [
151       'type' => 'int',
152       'size' => 'tiny',
153       'unsigned' => TRUE,
154       'not null' => TRUE,
155       'default' => 0,
156       'description' => 'Flag indicating what to do for this item on rollback',
157     ];
158     $schema->expects($this->at(2))
159       ->method('addField')
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));
165     $field_schema = [
166       'type' => 'varchar',
167       'length' => '64',
168       'not null' => FALSE,
169       'description' => 'Hash of source row data, for detecting changes',
170     ];
171     $schema->expects($this->at(4))
172       ->method('addField')
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));
178     $field_schema = [
179       'type' => 'varchar',
180       'length' => '64',
181       'not null' => TRUE,
182       'description' => 'Hash of source ids. Used as primary key',
183     ];
184     $schema->expects($this->at(6))
185       ->method('addField')
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);
190   }
191
192   /**
193    * Actually run the test.
194    *
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.
199    */
200   protected function runEnsureTablesTest($schema) {
201     $database = $this->getMockBuilder('Drupal\Core\Database\Connection')
202       ->disableOriginalConstructor()
203       ->getMock();
204     $database->expects($this->any())
205       ->method('schema')
206       ->willReturn($schema);
207     $migration = $this->getMigration();
208     $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
209     $plugin->expects($this->any())
210       ->method('getIds')
211       ->willReturn([
212       'source_id_property' => [
213         'type' => 'integer',
214       ],
215       'source_id_property_2' => [
216         'type' => 'integer',
217       ],
218     ]);
219     $migration->expects($this->any())
220       ->method('getSourcePlugin')
221       ->willReturn($plugin);
222     $plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
223     $plugin->expects($this->any())
224       ->method('getIds')
225       ->willReturn([
226       'destination_id_property' => [
227         'type' => 'string',
228       ],
229     ]);
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);
236     $map->getDatabase();
237   }
238
239 }