annotate core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\migrate\Unit;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Query\SelectInterface;
Chris@0 6 use Drupal\Core\DependencyInjection\ContainerBuilder;
Chris@0 7 use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
Chris@0 8 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
Chris@0 9 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Base class for Migrate module source unit tests.
Chris@0 13 *
Chris@0 14 * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0. Use
Chris@0 15 * \Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase instead.
Chris@0 16 */
Chris@0 17 abstract class MigrateSqlSourceTestCase extends MigrateTestCase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The tested source plugin.
Chris@0 21 *
Chris@17 22 * @var \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
Chris@0 23 */
Chris@0 24 protected $source;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The database contents.
Chris@0 28 *
Chris@0 29 * Database contents represents a mocked database. It should contain an
Chris@0 30 * associative array with the table name as key, and as many nested arrays as
Chris@0 31 * the number of mocked rows. Each of those faked rows must be another array
Chris@0 32 * with the column name as the key and the value as the cell.
Chris@0 33 *
Chris@0 34 * @var array
Chris@0 35 */
Chris@0 36 protected $databaseContents = [];
Chris@0 37
Chris@0 38 /**
Chris@0 39 * The plugin class under test.
Chris@0 40 *
Chris@0 41 * The plugin system is not working during unit testing so the source plugin
Chris@0 42 * class needs to be manually specified.
Chris@0 43 *
Chris@0 44 * @var string
Chris@0 45 */
Chris@0 46 const PLUGIN_CLASS = '';
Chris@0 47
Chris@0 48 /**
Chris@0 49 * The high water mark at the beginning of the import operation.
Chris@0 50 *
Chris@0 51 * Once the migration is run, we save a mark of the migrated sources, so the
Chris@0 52 * migration can run again and update only new sources or changed sources.
Chris@0 53 *
Chris@0 54 * @var mixed
Chris@0 55 */
Chris@0 56 const ORIGINAL_HIGH_WATER = NULL;
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Expected results after the source parsing.
Chris@0 60 *
Chris@0 61 * @var array
Chris@0 62 */
Chris@0 63 protected $expectedResults = [];
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Expected count of source rows.
Chris@0 67 *
Chris@0 68 * @var int
Chris@0 69 */
Chris@0 70 protected $expectedCount = 0;
Chris@0 71
Chris@0 72 /**
Chris@0 73 * The source plugin instance under test.
Chris@0 74 *
Chris@0 75 * @var \Drupal\migrate\Plugin\MigrateSourceInterface
Chris@0 76 */
Chris@0 77 protected $plugin;
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 protected function setUp() {
Chris@0 83 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
Chris@0 84 $state = $this->getMock('Drupal\Core\State\StateInterface');
Chris@0 85 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
Chris@0 86
Chris@0 87 // Mock a key-value store to return high-water values.
Chris@0 88 $key_value = $this->getMock(KeyValueStoreInterface::class);
Chris@0 89
Chris@0 90 // SourcePluginBase does not yet support full dependency injection so we
Chris@0 91 // need to make sure that \Drupal::keyValue() works as expected by mocking
Chris@0 92 // the keyvalue service.
Chris@0 93 $key_value_factory = $this->getMock(KeyValueFactoryInterface::class);
Chris@0 94 $key_value_factory
Chris@0 95 ->method('get')
Chris@0 96 ->with('migrate:high_water')
Chris@0 97 ->willReturn($key_value);
Chris@0 98
Chris@0 99 try {
Chris@0 100 $container = \Drupal::getContainer();
Chris@0 101 }
Chris@0 102 catch (ContainerNotInitializedException $e) {
Chris@0 103 $container = new ContainerBuilder();
Chris@0 104 }
Chris@0 105 $container->set('keyvalue', $key_value_factory);
Chris@0 106 \Drupal::setContainer($container);
Chris@0 107
Chris@0 108 $migration = $this->getMigration();
Chris@0 109
Chris@0 110 // Set the high water value.
Chris@0 111 \Drupal::keyValue('migrate:high_water')
Chris@0 112 ->expects($this->any())
Chris@0 113 ->method('get')
Chris@0 114 ->willReturn(static::ORIGINAL_HIGH_WATER);
Chris@0 115
Chris@0 116 // Setup the plugin.
Chris@0 117 $plugin_class = static::PLUGIN_CLASS;
Chris@0 118 $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], [], $migration, $state, $entity_manager);
Chris@0 119
Chris@0 120 // Do some reflection to set the database and moduleHandler.
Chris@0 121 $plugin_reflection = new \ReflectionClass($plugin);
Chris@0 122 $database_property = $plugin_reflection->getProperty('database');
Chris@0 123 $database_property->setAccessible(TRUE);
Chris@0 124 $module_handler_property = $plugin_reflection->getProperty('moduleHandler');
Chris@0 125 $module_handler_property->setAccessible(TRUE);
Chris@0 126
Chris@0 127 // Set the database and the module handler onto our plugin.
Chris@0 128 $database_property->setValue($plugin, $this->getDatabase($this->databaseContents + ['test_map' => []]));
Chris@0 129 $module_handler_property->setValue($plugin, $module_handler);
Chris@0 130
Chris@0 131 $plugin->setStringTranslation($this->getStringTranslationStub());
Chris@0 132 $migration->expects($this->any())
Chris@0 133 ->method('getSourcePlugin')
Chris@0 134 ->will($this->returnValue($plugin));
Chris@0 135 $this->source = $plugin;
Chris@0 136 $this->expectedCount = count($this->expectedResults);
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * Tests that the source returns the same rows as expected.
Chris@0 141 */
Chris@0 142 public function testRetrieval() {
Chris@0 143 $this->assertInstanceOf(SelectInterface::class, $this->source->query());
Chris@0 144 $this->queryResultTest($this->source, $this->expectedResults);
Chris@0 145 }
Chris@0 146
Chris@0 147 /**
Chris@0 148 * Tests that the source returns the row count expected.
Chris@0 149 */
Chris@0 150 public function testSourceCount() {
Chris@0 151 $count = $this->source->count();
Chris@0 152 $this->assertTrue(is_numeric($count));
Chris@0 153 $this->assertEquals($this->expectedCount, $count);
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Tests the source defines a valid ID.
Chris@0 158 */
Chris@0 159 public function testSourceId() {
Chris@0 160 $this->assertNotEmpty($this->source->getIds());
Chris@0 161 }
Chris@0 162
Chris@0 163 /**
Chris@0 164 * Gets the value on a row for a given key.
Chris@0 165 *
Chris@0 166 * @param \Drupal\migrate\Row $row
Chris@0 167 * The row identifier.
Chris@0 168 * @param string $key
Chris@0 169 * The key identifier.
Chris@0 170 *
Chris@0 171 * @return mixed
Chris@0 172 * The value on a row for a given key.
Chris@0 173 */
Chris@0 174 protected function getValue($row, $key) {
Chris@0 175 return $row->getSourceProperty($key);
Chris@0 176 }
Chris@0 177
Chris@0 178 }