BlockFormTest.php in Drupal 8
File
core/modules/block/tests/src/Unit/BlockFormTest.php
View source
<?php
namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Tests\UnitTestCase;
class BlockFormTest extends UnitTestCase {
protected $conditionManager;
protected $storage;
protected $language;
protected $themeHandler;
protected $entityTypeManager;
protected $contextRepository;
protected $pluginFormFactory;
protected function setUp() {
parent::setUp();
$this->conditionManager = $this
->createMock('Drupal\\Core\\Executable\\ExecutableManagerInterface');
$this->language = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->contextRepository = $this
->createMock('Drupal\\Core\\Plugin\\Context\\ContextRepositoryInterface');
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->storage = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
$this->themeHandler = $this
->createMock('Drupal\\Core\\Extension\\ThemeHandlerInterface');
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValue($this->storage));
$this->pluginFormFactory = $this
->prophesize(PluginFormFactoryInterface::class);
}
protected function getBlockMockWithMachineName($machine_name) {
$plugin = $this
->getMockBuilder(BlockBase::class)
->disableOriginalConstructor()
->getMock();
$plugin
->expects($this
->any())
->method('getMachineNameSuggestion')
->will($this
->returnValue($machine_name));
$block = $this
->getMockBuilder(Block::class)
->disableOriginalConstructor()
->getMock();
$block
->expects($this
->any())
->method('getPlugin')
->will($this
->returnValue($plugin));
return $block;
}
public function testGetUniqueMachineName() {
$blocks = [];
$blocks['test'] = $this
->getBlockMockWithMachineName('test');
$blocks['other_test'] = $this
->getBlockMockWithMachineName('other_test');
$blocks['other_test_1'] = $this
->getBlockMockWithMachineName('other_test');
$blocks['other_test_2'] = $this
->getBlockMockWithMachineName('other_test');
$query = $this
->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
$query
->expects($this
->exactly(5))
->method('condition')
->will($this
->returnValue($query));
$query
->expects($this
->exactly(5))
->method('execute')
->will($this
->returnValue([
'test',
'other_test',
'other_test_1',
'other_test_2',
]));
$this->storage
->expects($this
->exactly(5))
->method('getQuery')
->will($this
->returnValue($query));
$block_form_controller = new BlockForm($this->entityTypeManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory
->reveal());
$this
->assertEquals('test_2', $block_form_controller
->getUniqueMachineName($blocks['test']));
$this
->assertEquals('other_test_3', $block_form_controller
->getUniqueMachineName($blocks['other_test']));
$this
->assertEquals('other_test_3', $block_form_controller
->getUniqueMachineName($blocks['other_test_1']));
$this
->assertEquals('other_test_3', $block_form_controller
->getUniqueMachineName($blocks['other_test_2']));
$last_block = $this
->getBlockMockWithMachineName('last_test');
$this
->assertEquals('last_test', $block_form_controller
->getUniqueMachineName($last_block));
}
}