Same filename and directory in other branches
- 8.9.x core/modules/block/tests/src/Unit/BlockFormTest.php
- 9 core/modules/block/tests/src/Unit/BlockFormTest.php
Namespace
Drupal\Tests\block\Unit
File
core/modules/block/tests/src/Unit/BlockFormTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockForm;
use Drupal\block\BlockRepository;
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 $themeManager;
protected $entityTypeManager;
protected $contextHandler;
protected $contextRepository;
protected $pluginFormFactory;
protected $blockRepository;
protected function setUp() : void {
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')
->willReturn($this->storage);
$this->pluginFormFactory = $this
->prophesize(PluginFormFactoryInterface::class);
$this->themeManager = $this
->createMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
$this->contextHandler = $this
->createMock('Drupal\\Core\\Plugin\\Context\\ContextHandlerInterface');
$this->blockRepository = new BlockRepository($this->entityTypeManager, $this->themeManager, $this->contextHandler);
}
protected function getBlockMockWithMachineName($machine_name) {
$plugin = $this
->getMockBuilder(BlockBase::class)
->disableOriginalConstructor()
->getMock();
$plugin
->expects($this
->any())
->method('getMachineNameSuggestion')
->willReturn($machine_name);
$block = $this
->getMockBuilder(Block::class)
->disableOriginalConstructor()
->getMock();
$block
->expects($this
->any())
->method('getPlugin')
->willReturn($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')
->willReturn($query);
$query
->expects($this
->exactly(5))
->method('execute')
->willReturn([
'test',
'other_test',
'other_test_1',
'other_test_2',
]);
$this->storage
->expects($this
->exactly(5))
->method('getQuery')
->willReturn($query);
$block_form_controller = new BlockForm($this->entityTypeManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory
->reveal(), $this->blockRepository);
$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));
}
}
Classes