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