comparison core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php @ 0:4c8ae668cc8c

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