Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block / tests / src / Unit / Plugin / migrate / process / BlockVisibilityTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Unit\Plugin\migrate\process;
4
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;
10
11 /**
12  * Tests the block_visibility process plugin.
13  *
14  * @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockVisibility
15  * @group block
16  */
17 class BlockVisibilityTest extends MigrateProcessTestCase {
18
19   /**
20    * The module handler.
21    *
22    * @var \Drupal\Core\Extension\ModuleHandlerInterface
23    */
24   protected $moduleHandler;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::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());
34   }
35
36   /**
37    * @covers ::transform
38    */
39   public function testTransformNoData() {
40     $transformed_value = $this->plugin->transform([0, '', []], $this->migrateExecutable, $this->row, 'destinationproperty');
41     $this->assertEmpty($transformed_value);
42   }
43
44   /**
45    * @covers ::transform
46    */
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']);
52   }
53
54   /**
55    * @covers ::transform
56    */
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']);
62   }
63
64   /**
65    * @covers ::transform
66    */
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']);
73   }
74
75   /**
76    * @covers ::transform
77    */
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);
82   }
83
84   /**
85    * @covers ::transform
86    */
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'])
93       ->getMock();
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');
100   }
101
102 }