getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $state = $this->getMock('Drupal\Core\State\StateInterface'); $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); // Mock a key-value store to return high-water values. $key_value = $this->getMock(KeyValueStoreInterface::class); // SourcePluginBase does not yet support full dependency injection so we // need to make sure that \Drupal::keyValue() works as expected by mocking // the keyvalue service. $key_value_factory = $this->getMock(KeyValueFactoryInterface::class); $key_value_factory ->method('get') ->with('migrate:high_water') ->willReturn($key_value); try { $container = \Drupal::getContainer(); } catch (ContainerNotInitializedException $e) { $container = new ContainerBuilder(); } $container->set('keyvalue', $key_value_factory); \Drupal::setContainer($container); $migration = $this->getMigration(); // Set the high water value. \Drupal::keyValue('migrate:high_water') ->expects($this->any()) ->method('get') ->willReturn(static::ORIGINAL_HIGH_WATER); // Setup the plugin. $plugin_class = static::PLUGIN_CLASS; $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], [], $migration, $state, $entity_manager); // Do some reflection to set the database and moduleHandler. $plugin_reflection = new \ReflectionClass($plugin); $database_property = $plugin_reflection->getProperty('database'); $database_property->setAccessible(TRUE); $module_handler_property = $plugin_reflection->getProperty('moduleHandler'); $module_handler_property->setAccessible(TRUE); // Set the database and the module handler onto our plugin. $database_property->setValue($plugin, $this->getDatabase($this->databaseContents + ['test_map' => []])); $module_handler_property->setValue($plugin, $module_handler); $plugin->setStringTranslation($this->getStringTranslationStub()); $migration->expects($this->any()) ->method('getSourcePlugin') ->will($this->returnValue($plugin)); $this->source = $plugin; $this->expectedCount = count($this->expectedResults); } /** * Tests that the source returns the same rows as expected. */ public function testRetrieval() { $this->assertInstanceOf(SelectInterface::class, $this->source->query()); $this->queryResultTest($this->source, $this->expectedResults); } /** * Tests that the source returns the row count expected. */ public function testSourceCount() { $count = $this->source->count(); $this->assertTrue(is_numeric($count)); $this->assertEquals($this->expectedCount, $count); } /** * Tests the source defines a valid ID. */ public function testSourceId() { $this->assertNotEmpty($this->source->getIds()); } /** * Gets the value on a row for a given key. * * @param \Drupal\migrate\Row $row * The row identifier. * @param string $key * The key identifier. * * @return mixed * The value on a row for a given key. */ protected function getValue($row, $key) { return $row->getSourceProperty($key); } }