3 namespace Drupal\Tests\block\Unit\Plugin\migrate\process;
5 use Drupal\block\Plugin\migrate\process\BlockVisibility;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\migrate\MigrateSkipRowException;
8 use Drupal\migrate\Plugin\MigrateProcessInterface;
9 use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
12 * Tests the block_visibility process plugin.
14 * @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockVisibility
17 class BlockVisibilityTest extends MigrateProcessTestCase {
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
29 protected function setUp() {
31 $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
32 $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
33 $this->plugin = new BlockVisibility([], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
39 public function testTransformNoData() {
40 $transformed_value = $this->plugin->transform([0, '', []], $this->migrateExecutable, $this->row, 'destinationproperty');
41 $this->assertEmpty($transformed_value);
47 public function testTransformSinglePageWithFront() {
48 $visibility = $this->plugin->transform([0, '<front>', []], $this->migrateExecutable, $this->row, 'destinationproperty');
49 $this->assertSame('request_path', $visibility['request_path']['id']);
50 $this->assertTrue($visibility['request_path']['negate']);
51 $this->assertSame('<front>', $visibility['request_path']['pages']);
57 public function testTransformMultiplePagesWithFront() {
58 $visibility = $this->plugin->transform([1, "foo\n/bar\rbaz\r\n<front>", []], $this->migrateExecutable, $this->row, 'destinationproperty');
59 $this->assertSame('request_path', $visibility['request_path']['id']);
60 $this->assertFalse($visibility['request_path']['negate']);
61 $this->assertSame("/foo\n/bar\n/baz\n<front>", $visibility['request_path']['pages']);
67 public function testTransformPhpEnabled() {
68 $this->moduleHandler->moduleExists('php')->willReturn(TRUE);
69 $visibility = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
70 $this->assertSame('php', $visibility['php']['id']);
71 $this->assertFalse($visibility['php']['negate']);
72 $this->assertSame('<?php', $visibility['php']['php']);
78 public function testTransformPhpDisabled() {
79 $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
80 $transformed_value = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');
81 $this->assertEmpty($transformed_value);
87 public function testTransformException() {
88 $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
89 $migration_plugin = $this->prophesize(MigrateProcessInterface::class);
90 $this->row = $this->getMockBuilder('Drupal\migrate\Row')
91 ->disableOriginalConstructor()
92 ->setMethods(['getSourceProperty'])
94 $this->row->expects($this->exactly(2))
95 ->method('getSourceProperty')
96 ->willReturnMap([['bid', 99], ['module', 'foobar']]);
97 $this->plugin = new BlockVisibility(['skip_php' => TRUE], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migration_plugin->reveal());
98 $this->setExpectedException(MigrateSkipRowException::class, "The block with bid '99' from module 'foobar' will have no PHP or request_path visibility configuration.");
99 $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destinationproperty');