BlockFormTest.php 4.31 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block\Unit;
4

5
use Drupal\block\BlockForm;
6
use Drupal\Core\Plugin\PluginFormFactoryInterface;
7 8 9
use Drupal\Tests\UnitTestCase;

/**
10 11
 * @coversDefaultClass \Drupal\block\BlockForm
 * @group block
12
 */
13
class BlockFormTest extends UnitTestCase {
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  /**
   * 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;

36 37 38 39 40 41 42 43

  /**
   * The theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $themeHandler;

44 45 46 47 48 49 50
  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

51 52 53 54 55 56 57
  /**
   * The mocked context repository.
   *
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $contextRepository;

58 59 60 61 62 63 64
  /**
   * The plugin form manager.
   *
   * @var \Drupal\Core\Plugin\PluginFormFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $pluginFormFactory;

65 66 67 68 69 70 71 72
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
    $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
73
    $this->contextRepository = $this->getMock('Drupal\Core\Plugin\Context\ContextRepositoryInterface');
74 75 76

    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
    $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
77
    $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
78 79 80 81
    $this->entityManager->expects($this->any())
      ->method('getStorage')
      ->will($this->returnValue($this->storage));

82
    $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
83 84
  }

85 86 87
  /**
   * Tests the unique machine name generator.
   *
88
   * @see \Drupal\block\BlockForm::getUniqueMachineName()
89 90 91 92 93 94 95 96 97
   */
  public function testGetUniqueMachineName() {
    $blocks = array();

    $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');

98
    $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
99 100 101 102 103 104 105 106
    $query->expects($this->exactly(5))
      ->method('condition')
      ->will($this->returnValue($query));

    $query->expects($this->exactly(5))
      ->method('execute')
      ->will($this->returnValue(array('test', 'other_test', 'other_test_1', 'other_test_2')));

107
    $this->storage->expects($this->exactly(5))
108
      ->method('getQuery')
109 110
      ->will($this->returnValue($query));

111
    $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory->reveal());
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

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

}