class BlockFormTest

Same name in other branches
  1. 9 core/modules/block/tests/src/Unit/BlockFormTest.php \Drupal\Tests\block\Unit\BlockFormTest
  2. 8.9.x core/modules/block/tests/src/Unit/BlockFormTest.php \Drupal\Tests\block\Unit\BlockFormTest
  3. 10 core/modules/block/tests/src/Unit/BlockFormTest.php \Drupal\Tests\block\Unit\BlockFormTest

@coversDefaultClass \Drupal\block\BlockForm @group block

Hierarchy

Expanded class hierarchy of BlockFormTest

File

core/modules/block/tests/src/Unit/BlockFormTest.php, line 18

Namespace

Drupal\Tests\block\Unit
View source
class BlockFormTest extends UnitTestCase {
    
    /**
     * The condition plugin manager.
     *
     * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $conditionManager;
    
    /**
     * The block storage.
     *
     * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $storage;
    
    /**
     * The language manager service.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $language;
    
    /**
     * The theme handler.
     *
     * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $themeHandler;
    
    /**
     * The theme manager service.
     *
     * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $themeManager;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The mocked context handler.
     *
     * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $contextHandler;
    
    /**
     * The mocked context repository.
     *
     * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $contextRepository;
    
    /**
     * The plugin form manager.
     *
     * @var \Drupal\Core\Plugin\PluginFormFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $pluginFormFactory;
    
    /**
     * The block repository.
     *
     * @var \Drupal\block\BlockRepositoryInterface
     */
    protected $blockRepository;
    
    /**
     * {@inheritdoc}
     */
    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);
    }
    
    /**
     * Mocks a block with a block plugin.
     *
     * @param string $machine_name
     *   The machine name of the block plugin.
     *
     * @return \Drupal\block\BlockInterface|\PHPUnit\Framework\MockObject\MockObject
     *   The mocked block.
     */
    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;
    }
    
    /**
     * Tests the unique machine name generator.
     *
     * @see \Drupal\block\BlockForm::getUniqueMachineName()
     */
    public function testGetUniqueMachineName() : void {
        $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);
        // Ensure that the block with just one other instance gets
        // the next available name suggestion.
        $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
        // Ensure that the block with already three instances (_0, _1, _2) gets the
        // 4th available name.
        $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']));
        // Ensure that a block without an instance yet gets the suggestion as
        // unique machine name.
        $last_block = $this->getBlockMockWithMachineName('last_test');
        $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BlockFormTest::$blockRepository protected property The block repository.
BlockFormTest::$conditionManager protected property The condition plugin manager.
BlockFormTest::$contextHandler protected property The mocked context handler.
BlockFormTest::$contextRepository protected property The mocked context repository.
BlockFormTest::$entityTypeManager protected property The entity type manager.
BlockFormTest::$language protected property The language manager service.
BlockFormTest::$pluginFormFactory protected property The plugin form manager.
BlockFormTest::$storage protected property The block storage.
BlockFormTest::$themeHandler protected property The theme handler.
BlockFormTest::$themeManager protected property The theme manager service.
BlockFormTest::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
BlockFormTest::setUp protected function Overrides UnitTestCase::setUp
BlockFormTest::testGetUniqueMachineName public function Tests the unique machine name generator.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.