3 namespace Drupal\Tests\block\Unit;
5 use Drupal\block\BlockForm;
6 use Drupal\block\Entity\Block;
7 use Drupal\Core\Block\BlockBase;
8 use Drupal\Core\Plugin\PluginFormFactoryInterface;
9 use Drupal\Tests\UnitTestCase;
12 * @coversDefaultClass \Drupal\block\BlockForm
15 class BlockFormTest extends UnitTestCase {
18 * The condition plugin manager.
20 * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit_Framework_MockObject_MockObject
22 protected $conditionManager;
27 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
32 * The language manager service.
34 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
42 * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected $themeHandler;
49 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
51 protected $entityManager;
54 * The mocked context repository.
56 * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $contextRepository;
61 * The plugin form manager.
63 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
65 protected $pluginFormFactory;
70 protected function setUp() {
73 $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
74 $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
75 $this->contextRepository = $this->getMock('Drupal\Core\Plugin\Context\ContextRepositoryInterface');
77 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
78 $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
79 $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
80 $this->entityManager->expects($this->any())
81 ->method('getStorage')
82 ->will($this->returnValue($this->storage));
84 $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
88 * Mocks a block with a block plugin.
90 * @param string $machine_name
91 * The machine name of the block plugin.
93 * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
96 protected function getBlockMockWithMachineName($machine_name) {
97 $plugin = $this->getMockBuilder(BlockBase::class)
98 ->disableOriginalConstructor()
100 $plugin->expects($this->any())
101 ->method('getMachineNameSuggestion')
102 ->will($this->returnValue($machine_name));
104 $block = $this->getMockBuilder(Block::class)
105 ->disableOriginalConstructor()
107 $block->expects($this->any())
108 ->method('getPlugin')
109 ->will($this->returnValue($plugin));
114 * Tests the unique machine name generator.
116 * @see \Drupal\block\BlockForm::getUniqueMachineName()
118 public function testGetUniqueMachineName() {
121 $blocks['test'] = $this->getBlockMockWithMachineName('test');
122 $blocks['other_test'] = $this->getBlockMockWithMachineName('other_test');
123 $blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test');
124 $blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test');
126 $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
127 $query->expects($this->exactly(5))
128 ->method('condition')
129 ->will($this->returnValue($query));
131 $query->expects($this->exactly(5))
133 ->will($this->returnValue(['test', 'other_test', 'other_test_1', 'other_test_2']));
135 $this->storage->expects($this->exactly(5))
137 ->will($this->returnValue($query));
139 $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory->reveal());
141 // Ensure that the block with just one other instance gets the next available
143 $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
145 // Ensure that the block with already three instances (_0, _1, _2) gets the
146 // 4th available name.
147 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
148 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
149 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
151 // Ensure that a block without an instance yet gets the suggestion as
152 // unique machine name.
153 $last_block = $this->getBlockMockWithMachineName('last_test');
154 $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));