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