BlockVisibilityTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\Tests\block\Unit\Plugin\migrate\process;
  3. use Drupal\block\Plugin\migrate\process\BlockVisibility;
  4. use Drupal\Core\Extension\ModuleHandlerInterface;
  5. use Drupal\migrate\MigrateLookupInterface;
  6. use Drupal\migrate\MigrateSkipRowException;
  7. use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
  8. /**
  9. * Tests the block_visibility process plugin.
  10. *
  11. * @coversDefaultClass \Drupal\block\Plugin\migrate\process\BlockVisibility
  12. * @group block
  13. */
  14. class BlockVisibilityTest extends MigrateProcessTestCase {
  15. /**
  16. * The module handler.
  17. *
  18. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  19. */
  20. protected $moduleHandler;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function setUp() {
  25. parent::setUp();
  26. $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
  27. $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
  28. $this->plugin = new BlockVisibility([], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migrate_lookup->reveal());
  29. }
  30. /**
  31. * @covers ::transform
  32. */
  33. public function testTransformNoData() {
  34. $transformed_value = $this->plugin->transform([0, '', []], $this->migrateExecutable, $this->row, 'destination_property');
  35. $this->assertEmpty($transformed_value);
  36. }
  37. /**
  38. * @covers ::transform
  39. */
  40. public function testTransformSinglePageWithFront() {
  41. $visibility = $this->plugin->transform([0, '<front>', []], $this->migrateExecutable, $this->row, 'destination_property');
  42. $this->assertSame('request_path', $visibility['request_path']['id']);
  43. $this->assertTrue($visibility['request_path']['negate']);
  44. $this->assertSame('<front>', $visibility['request_path']['pages']);
  45. }
  46. /**
  47. * @covers ::transform
  48. */
  49. public function testTransformMultiplePagesWithFront() {
  50. $visibility = $this->plugin->transform([1, "foo\n/bar\rbaz\r\n<front>", []], $this->migrateExecutable, $this->row, 'destination_property');
  51. $this->assertSame('request_path', $visibility['request_path']['id']);
  52. $this->assertFalse($visibility['request_path']['negate']);
  53. $this->assertSame("/foo\n/bar\n/baz\n<front>", $visibility['request_path']['pages']);
  54. }
  55. /**
  56. * @covers ::transform
  57. */
  58. public function testTransformPhpEnabled() {
  59. $this->moduleHandler->moduleExists('php')->willReturn(TRUE);
  60. $visibility = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destination_property');
  61. $this->assertSame('php', $visibility['php']['id']);
  62. $this->assertFalse($visibility['php']['negate']);
  63. $this->assertSame('<?php', $visibility['php']['php']);
  64. }
  65. /**
  66. * @covers ::transform
  67. */
  68. public function testTransformPhpDisabled() {
  69. $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
  70. $transformed_value = $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destination_property');
  71. $this->assertEmpty($transformed_value);
  72. }
  73. /**
  74. * @covers ::transform
  75. */
  76. public function testTransformException() {
  77. $this->moduleHandler->moduleExists('php')->willReturn(FALSE);
  78. $migrate_lookup = $this->prophesize(MigrateLookupInterface::class);
  79. $this->row = $this->getMockBuilder('Drupal\migrate\Row')
  80. ->disableOriginalConstructor()
  81. ->setMethods(['getSourceProperty'])
  82. ->getMock();
  83. $this->row->expects($this->exactly(2))
  84. ->method('getSourceProperty')
  85. ->willReturnMap([['bid', 99], ['module', 'foobar']]);
  86. $this->plugin = new BlockVisibility(['skip_php' => TRUE], 'block_visibility_pages', [], $this->moduleHandler->reveal(), $migrate_lookup->reveal());
  87. $this->expectException(MigrateSkipRowException::class);
  88. $this->expectExceptionMessage("The block with bid '99' from module 'foobar' will have no PHP or request_path visibility configuration.");
  89. $this->plugin->transform([2, '<?php', []], $this->migrateExecutable, $this->row, 'destination_property');
  90. }
  91. }