Pull merge.
[yaffs-website] / web / core / modules / block / tests / src / Unit / BlockFormTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Unit;
4
5 use Drupal\block\BlockForm;
6 use Drupal\block\Entity\Block;
7 use Drupal\Core\Block\BlockBase;
8 use Drupal\Core\Plugin\PluginFormFactoryInterface;
9 use Drupal\Tests\UnitTestCase;
10
11 /**
12  * @coversDefaultClass \Drupal\block\BlockForm
13  * @group block
14  */
15 class BlockFormTest extends UnitTestCase {
16
17   /**
18    * The condition plugin manager.
19    *
20    * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $conditionManager;
23
24   /**
25    * The block storage.
26    *
27    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $storage;
30
31   /**
32    * The language manager service.
33    *
34    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
35    */
36   protected $language;
37
38
39   /**
40    * The theme handler.
41    *
42    * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
43    */
44   protected $themeHandler;
45
46   /**
47    * The entity manager.
48    *
49    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50    */
51   protected $entityManager;
52
53   /**
54    * The mocked context repository.
55    *
56    * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
57    */
58   protected $contextRepository;
59
60   /**
61    * The plugin form manager.
62    *
63    * @var \Drupal\Core\Plugin\PluginFormFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
64    */
65   protected $pluginFormFactory;
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function setUp() {
71     parent::setUp();
72
73     $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
74     $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
75     $this->contextRepository = $this->getMock('Drupal\Core\Plugin\Context\ContextRepositoryInterface');
76
77     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
78     $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
79     $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
80     $this->entityManager->expects($this->any())
81       ->method('getStorage')
82       ->will($this->returnValue($this->storage));
83
84     $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
85   }
86
87   /**
88    * Mocks a block with a block plugin.
89    *
90    * @param string $machine_name
91    *   The machine name of the block plugin.
92    *
93    * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
94    *   The mocked block.
95    */
96   protected function getBlockMockWithMachineName($machine_name) {
97     $plugin = $this->getMockBuilder(BlockBase::class)
98       ->disableOriginalConstructor()
99       ->getMock();
100     $plugin->expects($this->any())
101       ->method('getMachineNameSuggestion')
102       ->will($this->returnValue($machine_name));
103
104     $block = $this->getMockBuilder(Block::class)
105       ->disableOriginalConstructor()
106       ->getMock();
107     $block->expects($this->any())
108       ->method('getPlugin')
109       ->will($this->returnValue($plugin));
110     return $block;
111   }
112
113   /**
114    * Tests the unique machine name generator.
115    *
116    * @see \Drupal\block\BlockForm::getUniqueMachineName()
117    */
118   public function testGetUniqueMachineName() {
119     $blocks = [];
120
121     $blocks['test'] = $this->getBlockMockWithMachineName('test');
122     $blocks['other_test'] = $this->getBlockMockWithMachineName('other_test');
123     $blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test');
124     $blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test');
125
126     $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
127     $query->expects($this->exactly(5))
128       ->method('condition')
129       ->will($this->returnValue($query));
130
131     $query->expects($this->exactly(5))
132       ->method('execute')
133       ->will($this->returnValue(['test', 'other_test', 'other_test_1', 'other_test_2']));
134
135     $this->storage->expects($this->exactly(5))
136       ->method('getQuery')
137       ->will($this->returnValue($query));
138
139     $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory->reveal());
140
141     // Ensure that the block with just one other instance gets the next available
142     // name suggestion.
143     $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
144
145     // Ensure that the block with already three instances (_0, _1, _2) gets the
146     // 4th available name.
147     $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
148     $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
149     $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
150
151     // Ensure that a block without an instance yet gets the suggestion as
152     // unique machine name.
153     $last_block = $this->getBlockMockWithMachineName('last_test');
154     $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));
155   }
156
157 }