function BlockFormTest::testGetUniqueMachineName

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

Tests the unique machine name generator.

See also

\Drupal\block\BlockForm::getUniqueMachineName()

File

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

Class

BlockFormTest
@coversDefaultClass \Drupal\block\BlockForm @group block

Namespace

Drupal\Tests\block\Unit

Code

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));
}

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