only in patch2: unchanged: --- /dev/null +++ b/core/modules/filter/tests/src/Unit/Plugin/migrate/process/FilterIdTest.php @@ -0,0 +1,146 @@ +filterManager = $this->prophesize(FilterPluginManager::class); + $this->executable = $this->prophesize(MigrateExecutableInterface::class); + + $this->filterManager + ->hasDefinition('filter_html') + ->willReturn(TRUE); + + $this->filterManager + ->hasDefinition('php_code') + ->willReturn(FALSE); + + $this->filterManager + ->getFallbackPluginId(Argument::type('string')) + ->willReturn('filter_null'); + + // Mock the string translation service, since the plugin will translate + // any message it saves. + $translator = $this->prophesize(TranslationInterface::class); + + $translator + ->translate( + Argument::type('string') + ) + ->willReturnArgument(0); + + $translator + ->translate( + Argument::type('string'), + Argument::type('array') + ) + ->willReturnArgument(0); + + $container = new ContainerBuilder(); + $container->set('string_translation', $translator->reveal()); + + \Drupal::setContainer($container); + } + + /** + * Tests the filter_id plugin. + * + * @param mixed $value + * The input value to the plugin. + * @param string $expected_value + * The output value expected from the plugin. + * @param PredictionInterface $message_saved + * A prediction for if and how the MigrateExecutable's saveMessage() method + * will be called. + * + * @dataProvider testProvider + * + * @covers ::transform + */ + public function test($value, $expected_value, PredictionInterface $message_saved) { + $plugin = new FilterID( + [ + 'bypass' => TRUE, + 'map' => [ + 'foo' => 'filter_html', + 'baz' => 'php_code', + ], + ], + 'filter_id', + [], + $this->filterManager->reveal() + ); + + $this->executable + ->saveMessage( + Argument::any(), + Argument::type('integer') + ) + ->should($message_saved); + + $row = new Row([], []); + $output_value = $plugin->transform($value, $this->executable->reveal(), $row, 'foo'); + + $this->assertSame($expected_value, $output_value); + $this->executable->checkProphecyMethodsPredictions(); + } + + /** + * The test data provider. + * + * @return array + */ + public function testProvider() { + return [ + // The filter ID is mapped, and the plugin exists. + ['foo', 'filter_html', new NoCallsPrediction], + // The filter ID isn't mapped, but it's unchanged from the source (i.e., + // it bypasses the static map) and the plugin exists. + ['filter_html', 'filter_html', new NoCallsPrediction], + // The filter ID is mapped, but the plugin does not exist. + ['baz', 'filter_null', new CallPrediction], + // The filter ID isn't mapped, but it's unchanged from the source (i.e., + // it bypasses the static map) but the plugin does not exist. + ['php_code', 'filter_null', new CallPrediction], + ]; + } + +}