3 namespace Drupal\Tests\block\Unit;
5 use Drupal\block\BlockForm;
6 use Drupal\Core\Plugin\PluginFormFactoryInterface;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\block\BlockForm
13 class BlockFormTest extends UnitTestCase {
16 * The condition plugin manager.
18 * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $conditionManager;
25 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
30 * The language manager service.
32 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
40 * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
42 protected $themeHandler;
47 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
49 protected $entityManager;
52 * The mocked context repository.
54 * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
56 protected $contextRepository;
59 * The plugin form manager.
61 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
63 protected $pluginFormFactory;
68 protected function setUp() {
71 $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
72 $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
73 $this->contextRepository = $this->getMock('Drupal\Core\Plugin\Context\ContextRepositoryInterface');
75 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
76 $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
77 $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
78 $this->entityManager->expects($this->any())
79 ->method('getStorage')
80 ->will($this->returnValue($this->storage));
82 $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
86 * Tests the unique machine name generator.
88 * @see \Drupal\block\BlockForm::getUniqueMachineName()
90 public function testGetUniqueMachineName() {
93 $blocks['test'] = $this->getBlockMockWithMachineName('test');
94 $blocks['other_test'] = $this->getBlockMockWithMachineName('other_test');
95 $blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test');
96 $blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test');
98 $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
99 $query->expects($this->exactly(5))
100 ->method('condition')
101 ->will($this->returnValue($query));
103 $query->expects($this->exactly(5))
105 ->will($this->returnValue(['test', 'other_test', 'other_test_1', 'other_test_2']));
107 $this->storage->expects($this->exactly(5))
109 ->will($this->returnValue($query));
111 $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory->reveal());
113 // Ensure that the block with just one other instance gets the next available
115 $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
117 // Ensure that the block with already three instances (_0, _1, _2) gets the
118 // 4th available name.
119 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
120 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
121 $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
123 // Ensure that a block without an instance yet gets the suggestion as
124 // unique machine name.
125 $last_block = $this->getBlockMockWithMachineName('last_test');
126 $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));