annotate core/modules/migrate/tests/src/Kernel/SqlBaseTest.php @ 9:1fc0ff908d1f

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