5 * Contains \Drupal\Tests\migrate\Kernel\SqlBaseTest.
8 namespace Drupal\Tests\migrate\Kernel;
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;
19 * Tests the functionality of SqlBase.
23 class SqlBaseTest extends MigrateTestBase {
26 * The (probably mocked) migration under test.
28 * @var \Drupal\migrate\Plugin\MigrationInterface
35 protected function setUp() {
38 $this->migration = $this->getMock(MigrationInterface::class);
39 $this->migration->method('id')->willReturn('fubar');
43 * Tests different connection types.
45 public function testConnectionTypes() {
46 $sql_base = new TestSqlBase([], $this->migration);
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());
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);
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']);
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);
76 // Now test we can have SqlBase create the connection from an info array.
77 $sql_base = new TestSqlBase([], $this->migration);
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);
85 // Call getDatabase() to get the connection defined.
86 $sql_base->getDatabase();
88 // Validate the connection has been created with the right values.
89 $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
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']);
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);
104 // Now test we can have SqlBase create the connection from an info array.
105 $sql_base = new TestSqlBase([], $this->migration);
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]);
115 // Call getDatabase() to get the connection defined.
116 $sql_base->getDatabase();
118 // Validate the connection has been created with the right values.
119 $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
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();
132 * Tests that SqlBase respects high-water values.
134 * @param mixed $high_water
135 * (optional) The high-water value to set.
136 * @param array $query_result
137 * (optional) The expected query results.
139 * @dataProvider highWaterDataProvider
141 public function testHighWater($high_water = NULL, array $query_result = []) {
143 'high_water_property' => [
147 $source = new TestSqlBase($configuration, $this->migration);
150 $source->getHighWaterStorage()->set($this->migration->id(), $high_water);
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');
159 $condition_group = $this->getMock(ConditionInterface::class);
160 $query->method('orConditionGroup')->willReturn($condition_group);
162 $source->setQuery($query);
167 * Data provider for ::testHighWater().
170 * The scenarios to test.
172 public function highWaterDataProvider() {
174 'no high-water value set' => [],
175 'high-water value set' => [33],
182 * A dummy source to help with testing SqlBase.
184 * @package Drupal\migrate\Plugin\migrate\source
186 class TestSqlBase extends SqlBase {
189 * The query to execute.
191 * @var \Drupal\Core\Database\Query\SelectInterface
196 * Overrides the constructor so we can create one easily.
198 * @param array $configuration
199 * The plugin instance configuration.
200 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
201 * (optional) The migration being run.
203 public function __construct(array $configuration = [], MigrationInterface $migration = NULL) {
204 parent::__construct($configuration, 'sql_base', [], $migration, \Drupal::state());
208 * Gets the database without caching it.
210 public function getDatabase() {
211 $this->database = NULL;
212 return parent::getDatabase();
216 * Allows us to set the configuration from a test.
218 * @param array $config
221 public function setConfiguration($config) {
222 $this->configuration = $config;
228 public function getIds() {}
233 public function fields() {}
238 public function query() {
243 * Sets the query to execute.
245 * @param \Drupal\Core\Database\Query\SelectInterface $query
246 * The query to execute.
248 public function setQuery(SelectInterface $query) {
249 $this->query = $query;
255 public function getHighWaterStorage() {
256 return parent::getHighWaterStorage();