f633debad867894db63f4ab827d3d4340ad5419a
[yaffs-website] / web / core / modules / filter / tests / src / Kernel / Plugin / migrate / process / FilterIdTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Kernel\Plugin\migrate\process;
4
5 use Drupal\filter\Plugin\migrate\process\FilterID;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\Row;
10
11 /**
12  * Unit tests of the filter_id plugin.
13  *
14  * @coversDefaultClass \Drupal\filter\Plugin\migrate\process\FilterID
15  * @group filter
16  */
17 class FilterIdTest extends KernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['filter'];
23
24   /**
25    * The mocked MigrateExecutable.
26    *
27    * @var \Drupal\migrate\MigrateExecutableInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $executable;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36     $this->executable = $this->getMock(MigrateExecutableInterface::class);
37   }
38
39   /**
40    * Tests transformation of filter_id plugin.
41    *
42    * @param mixed $value
43    *   The input value to the plugin.
44    * @param string $expected_value
45    *   The output value expected from the plugin.
46    * @param string $invalid_id
47    *   (optional) The invalid plugin ID which is expected to be logged by the
48    *   MigrateExecutable object.
49    *
50    * @dataProvider provideFilters
51    *
52    * @covers ::transform
53    */
54   public function testTransform($value, $expected_value, $invalid_id = NULL) {
55     $configuration = [
56       'bypass' => TRUE,
57       'map' => [
58         'foo' => 'filter_html',
59         'baz' => 'php_code',
60       ],
61     ];
62     $plugin = FilterID::create($this->container, $configuration, 'filter_id', []);
63
64     if (isset($invalid_id)) {
65       $this->executable
66         ->expects($this->exactly(1))
67         ->method('saveMessage')
68         ->with(
69           'Filter ' . $invalid_id . ' could not be mapped to an existing filter plugin; defaulting to filter_null.',
70           MigrationInterface::MESSAGE_WARNING
71         );
72     }
73
74     $row = new Row();
75     $output_value = $plugin->transform($value, $this->executable, $row, 'foo');
76
77     $this->assertSame($expected_value, $output_value);
78   }
79
80   /**
81    * Provides filter ids for testing transformations.
82    *
83    * @return array
84    *   Formatted as $source_id, $tranformed_id, $invalid_id.
85    *   When $invalid_id is provided the transformation should fail with the
86    *   supplied id.
87    */
88   public function provideFilters() {
89     return [
90       'filter ID mapped to plugin that exists' => [
91         'foo',
92         'filter_html',
93       ],
94       'filter ID not mapped but unchanged from the source and the plugin exists' => [
95         'filter_html',
96         'filter_html',
97       ],
98       'filter ID mapped to plugin that does not exist' => [
99         'baz',
100         'filter_null',
101         'php_code',
102       ],
103       'filter ID not mapped but unchanged from the source and the plugin does not exist' => [
104         'php_code',
105         'filter_null',
106         'php_code',
107       ],
108       'filter ID set and the plugin does not exist' => [
109         ['filter', 1],
110         'filter_null',
111         'filter:1',
112       ],
113     ];
114   }
115
116 }