comparison core/modules/migrate/tests/src/Kernel/SqlBaseTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /**
4 * @file
5 * Contains \Drupal\Tests\migrate\Kernel\SqlBaseTest.
6 */
7
8 namespace Drupal\Tests\migrate\Kernel;
9
10 use Drupal\Core\Database\Query\ConditionInterface;
11 use Drupal\Core\Database\Query\SelectInterface;
12 use Drupal\migrate\Exception\RequirementsException;
13 use Drupal\Core\Database\Database;
14 use Drupal\migrate\Plugin\migrate\source\SqlBase;
15 use Drupal\migrate\Plugin\MigrationInterface;
16
17 /**
18 * Tests the functionality of SqlBase.
19 *
20 * @group migrate
21 */
22 class SqlBaseTest extends MigrateTestBase {
23
24 /**
25 * The (probably mocked) migration under test.
26 *
27 * @var \Drupal\migrate\Plugin\MigrationInterface
28 */
29 protected $migration;
30
31 /**
32 * {@inheritdoc}
33 */
34 protected function setUp() {
35 parent::setUp();
36
37 $this->migration = $this->getMock(MigrationInterface::class);
38 $this->migration->method('id')->willReturn('fubar');
39 }
40
41 /**
42 * Tests different connection types.
43 */
44 public function testConnectionTypes() {
45 $sql_base = new TestSqlBase([], $this->migration);
46
47 // Verify that falling back to the default 'migrate' connection (defined in
48 // the base class) works.
49 $this->assertSame($sql_base->getDatabase()->getTarget(), 'default');
50 $this->assertSame($sql_base->getDatabase()->getKey(), 'migrate');
51
52 // Verify the fallback state key overrides the 'migrate' connection.
53 $target = 'test_fallback_target';
54 $key = 'test_fallback_key';
55 $config = ['target' => $target, 'key' => $key];
56 $database_state_key = 'test_fallback_state';
57 \Drupal::state()->set($database_state_key, $config);
58 \Drupal::state()->set('migrate.fallback_state_key', $database_state_key);
59 // Create a test connection using the default database configuration.
60 Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
61 $this->assertSame($sql_base->getDatabase()->getTarget(), $target);
62 $this->assertSame($sql_base->getDatabase()->getKey(), $key);
63
64 // Verify that setting explicit connection information overrides fallbacks.
65 $target = 'test_db_target';
66 $key = 'test_migrate_connection';
67 $config = ['target' => $target, 'key' => $key];
68 $sql_base->setConfiguration($config);
69 Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
70
71 // Validate we have injected our custom key and target.
72 $this->assertSame($sql_base->getDatabase()->getTarget(), $target);
73 $this->assertSame($sql_base->getDatabase()->getKey(), $key);
74
75 // Now test we can have SqlBase create the connection from an info array.
76 $sql_base = new TestSqlBase([], $this->migration);
77
78 $target = 'test_db_target2';
79 $key = 'test_migrate_connection2';
80 $database = Database::getConnectionInfo('default')['default'];
81 $config = ['target' => $target, 'key' => $key, 'database' => $database];
82 $sql_base->setConfiguration($config);
83
84 // Call getDatabase() to get the connection defined.
85 $sql_base->getDatabase();
86
87 // Validate the connection has been created with the right values.
88 $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
89
90 // Now, test this all works when using state to store db info.
91 $target = 'test_state_db_target';
92 $key = 'test_state_migrate_connection';
93 $config = ['target' => $target, 'key' => $key];
94 $database_state_key = 'migrate_sql_base_test';
95 \Drupal::state()->set($database_state_key, $config);
96 $sql_base->setConfiguration(['database_state_key' => $database_state_key]);
97 Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
98
99 // Validate we have injected our custom key and target.
100 $this->assertSame($sql_base->getDatabase()->getTarget(), $target);
101 $this->assertSame($sql_base->getDatabase()->getKey(), $key);
102
103 // Now test we can have SqlBase create the connection from an info array.
104 $sql_base = new TestSqlBase([], $this->migration);
105
106 $target = 'test_state_db_target2';
107 $key = 'test_state_migrate_connection2';
108 $database = Database::getConnectionInfo('default')['default'];
109 $config = ['target' => $target, 'key' => $key, 'database' => $database];
110 $database_state_key = 'migrate_sql_base_test2';
111 \Drupal::state()->set($database_state_key, $config);
112 $sql_base->setConfiguration(['database_state_key' => $database_state_key]);
113
114 // Call getDatabase() to get the connection defined.
115 $sql_base->getDatabase();
116
117 // Validate the connection has been created with the right values.
118 $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
119
120 // Verify that falling back to 'migrate' when the connection is not defined
121 // throws a RequirementsException.
122 \Drupal::state()->delete('migrate.fallback_state_key');
123 $sql_base->setConfiguration([]);
124 Database::renameConnection('migrate', 'fallback_connection');
125 $this->setExpectedException(RequirementsException::class,
126 'No database connection configured for source plugin');
127 $sql_base->getDatabase();
128 }
129
130 /**
131 * Tests that SqlBase respects high-water values.
132 *
133 * @param mixed $high_water
134 * (optional) The high-water value to set.
135 * @param array $query_result
136 * (optional) The expected query results.
137 *
138 * @dataProvider highWaterDataProvider
139 */
140 public function testHighWater($high_water = NULL, array $query_result = []) {
141 $configuration = [
142 'high_water_property' => [
143 'name' => 'order',
144 ],
145 ];
146 $source = new TestSqlBase($configuration, $this->migration);
147
148 if ($high_water) {
149 $source->getHighWaterStorage()->set($this->migration->id(), $high_water);
150 }
151
152 $query_result = new \ArrayIterator($query_result);
153
154 $query = $this->getMock(SelectInterface::class);
155 $query->method('execute')->willReturn($query_result);
156 $query->expects($this->atLeastOnce())->method('orderBy')->with('order', 'ASC');
157
158 $condition_group = $this->getMock(ConditionInterface::class);
159 $query->method('orConditionGroup')->willReturn($condition_group);
160
161 $source->setQuery($query);
162 $source->rewind();
163 }
164
165 /**
166 * Data provider for ::testHighWater().
167 *
168 * @return array
169 * The scenarios to test.
170 */
171 public function highWaterDataProvider() {
172 return [
173 'no high-water value set' => [],
174 'high-water value set' => [33],
175 ];
176 }
177
178 }
179
180 /**
181 * A dummy source to help with testing SqlBase.
182 *
183 * @package Drupal\migrate\Plugin\migrate\source
184 */
185 class TestSqlBase extends SqlBase {
186
187 /**
188 * The query to execute.
189 *
190 * @var \Drupal\Core\Database\Query\SelectInterface
191 */
192 protected $query;
193
194 /**
195 * Overrides the constructor so we can create one easily.
196 *
197 * @param array $configuration
198 * The plugin instance configuration.
199 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
200 * (optional) The migration being run.
201 */
202 public function __construct(array $configuration = [], MigrationInterface $migration = NULL) {
203 parent::__construct($configuration, 'sql_base', [], $migration, \Drupal::state());
204 }
205
206 /**
207 * Gets the database without caching it.
208 */
209 public function getDatabase() {
210 $this->database = NULL;
211 return parent::getDatabase();
212 }
213
214 /**
215 * Allows us to set the configuration from a test.
216 *
217 * @param array $config
218 * The config array.
219 */
220 public function setConfiguration($config) {
221 $this->configuration = $config;
222 }
223
224 /**
225 * {@inheritdoc}
226 */
227 public function getIds() {}
228
229 /**
230 * {@inheritdoc}
231 */
232 public function fields() {}
233
234 /**
235 * {@inheritdoc}
236 */
237 public function query() {
238 return $this->query;
239 }
240
241 /**
242 * Sets the query to execute.
243 *
244 * @param \Drupal\Core\Database\Query\SelectInterface $query
245 * The query to execute.
246 */
247 public function setQuery(SelectInterface $query) {
248 $this->query = $query;
249 }
250
251 /**
252 * {@inheritdoc}
253 */
254 public function getHighWaterStorage() {
255 return parent::getHighWaterStorage();
256 }
257
258 }