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