‪TYPO3CMS  10.4
WidgetRegistryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
21 use Prophecy\Prophecy\ObjectProphecy;
22 use Psr\Container\ContainerInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 class ‪WidgetRegistryTest extends UnitTestCase
29 {
33  protected ‪$resetSingletonInstances = true;
34 
38  protected ‪$subject;
39 
43  protected ‪$beUserProphecy;
44 
48  protected ‪$containerProphecy;
49 
50  public function ‪setUp(): void
51  {
52  $this->beUserProphecy = $this->prophesize(BackendUserAuthentication::class);
53  $this->containerProphecy = $this->prophesize(ContainerInterface::class);
54 
55  ‪$GLOBALS['BE_USER'] = $this->beUserProphecy->reveal();
56  $this->subject = new ‪WidgetRegistry($this->containerProphecy->reveal());
57  }
58 
62  public function ‪initiallyZeroWidgetAreRegistered(): void
63  {
64  self::assertCount(0, $this->subject->getAllWidgets());
65  }
66 
74  public function ‪getAllWidgetReturnsAllRegisteredWidgets(array $expectedValues, array $widgetsToRegister): void
75  {
76  $this->‪registerWidgets($widgetsToRegister);
77 
78  self::assertCount((int)$expectedValues['count'], $this->subject->getAllWidgets());
79  }
80 
88  public function ‪returnsWidgetsForGroup(
89  array $expectedValues,
90  array $widgetsToRegister
91  ): void {
92  $this->‪registerWidgets($widgetsToRegister);
93  $this->beUserProphecy->check('available_widgets', Argument::any())->willReturn(true);
94 
95  self::assertCount(
96  (int)$expectedValues['group1Count'],
97  $this->subject->getAvailableWidgetsForWidgetGroup('group1')
98  );
99  }
100 
109  array $expectedValues,
110  array $widgetsToRegister
111  ): void {
112  foreach ($widgetsToRegister as $widget) {
113  $this->‪registerWidget($widget);
114 
115  $this->beUserProphecy
116  ->check(
117  'available_widgets',
118  $widget['identifier']
119  )
120  ->willReturn(true);
121  }
122 
123  self::assertCount((int)$expectedValues['adminCount'], $this->subject->getAvailableWidgets());
124  }
125 
134  array $expectedValues,
135  array $widgetsToRegister
136  ): void {
137  foreach ($widgetsToRegister as $widget) {
138  $this->‪registerWidget($widget);
139 
140  $this->beUserProphecy
141  ->check(
142  'available_widgets',
143  $widget['identifier']
144  )
145  ->shouldBeCalled()
146  ->willReturn($widget['availableForUser']);
147  }
148 
149  self::assertCount((int)$expectedValues['userCount'], $this->subject->getAvailableWidgets());
150  }
151 
155  public function ‪addWidgetsInItemsProcFunc(): void
156  {
157  $this->‪registerWidgets([
158  [
159  'serviceName' => 'dashboard.widget.t3news',
160  'identifier' => 't3orgnews',
161  'groups' => ['typo3'],
162  'iconIdentifier' => 'content-widget-rss',
163  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
164  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
165  'height' => 4,
166  'width' => 4,
167  'additionalCssClasses' => [],
168  ],
169  [
170  'serviceName' => 'dashboard.widget.t3comnews',
171  'identifier' => '2ndWidget',
172  'groups' => ['typo3'],
173  'iconIdentifier' => 'content-widget-2nd',
174  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:2ndWidget.title',
175  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:2ndWidget.description',
176  'height' => 4,
177  'width' => 4,
178  'additionalCssClasses' => [],
179  ],
180  ]);
181 
182  $parameters = [];
183  $this->subject->widgetItemsProcFunc($parameters);
184 
185  self::assertEquals(
186  [
187  'items' => [
188  [
189  'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
190  't3orgnews',
191  'content-widget-rss',
192  null,
193  'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description'
194  ],
195  [
196  'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:2ndWidget.title',
197  '2ndWidget',
198  'content-widget-2nd',
199  null,
200  'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:2ndWidget.description'
201  ],
202  ]
203  ],
204  $parameters
205  );
206  }
207 
208  private function ‪registerWidgets(array $widgetsToRegister): void
209  {
210  foreach ($widgetsToRegister as $widget) {
211  $this->‪registerWidget($widget);
212  }
213  }
214 
215  private function ‪registerWidget(array $widget)
216  {
217  $widgetConfiguration = $this->prophesize(WidgetConfigurationInterface::class);
218  $widgetConfiguration->getTitle()->willReturn($widget['title']);
219  $widgetConfiguration->getIdentifier()->willReturn($widget['identifier']);
220  $widgetConfiguration->getIconIdentifier()->willReturn($widget['iconIdentifier']);
221  $widgetConfiguration->getDescription()->willReturn($widget['description']);
222  $widgetConfiguration->getGroupNames()->willReturn($widget['groups']);
223  $widgetConfiguration->getHeight()->willReturn($widget['height']);
224  $widgetConfiguration->getWidth()->willReturn($widget['width']);
225  $widgetConfiguration->getAdditionalCssClasses()->willReturn($widget['additionalCssClasses']);
226 
227  $this->containerProphecy->get($widget['serviceName'])->willReturn($widgetConfiguration->reveal());
228  $this->subject->registerWidget($widget['serviceName']);
229  }
230 
231  public function ‪widgetsToRegister(): array
232  {
233  return [
234  'Single widget' => [
235  [
236  'count' => 1,
237  'adminCount' => 1,
238  'userCount' => 1,
239  'group1Count' => 1,
240  ],
241  [
242  [
243  'identifier' => 'test-widget1',
244  'serviceName' => 'dashboard.widget.t3news',
245  'groups' => ['group1'],
246  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
247  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
248  'iconIdentifier' => 'content-widget-rss',
249  'height' => 2,
250  'width' => 4,
251  'additionalCssClasses' => [
252  'custom-widget',
253  'rss-condensed',
254  ],
255  'availableForUser' => true
256  ]
257  ]
258  ],
259  'Two widgets' => [
260  [
261  'count' => 2,
262  'adminCount' => 2,
263  'userCount' => 1,
264  'group1Count' => 2,
265  ],
266  [
267  [
268  'identifier' => 'test-widget1',
269  'serviceName' => 'dashboard.widget.t3news',
270  'groups' => ['group1'],
271  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
272  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
273  'iconIdentifier' => 'content-widget-rss',
274  'height' => 2,
275  'width' => 4,
276  'additionalCssClasses' => [
277  'custom-widget',
278  'rss-condensed',
279  ],
280  'availableForUser' => true,
281  ],
282  [
283  'identifier' => 'test-widget2',
284  'serviceName' => 'dashboard.widget.t3comnews',
285  'groups' => ['group1'],
286  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
287  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
288  'iconIdentifier' => 'content-widget-rss',
289  'height' => 2,
290  'width' => 4,
291  'additionalCssClasses' => [
292  'custom-widget',
293  'rss-condensed',
294  ],
295  'availableForUser' => false
296  ],
297  ]
298  ],
299  'Three widgets, two having same identifier' => [
300  [
301  'count' => 2,
302  'adminCount' => 2,
303  'userCount' => 2,
304  'group1Count' => 1,
305  ],
306  [
307  [
308  'identifier' => 'test-widget1',
309  'serviceName' => 'dashboard.widget.t3news',
310  'groups' => ['group1'],
311  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
312  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
313  'iconIdentifier' => 'content-widget-rss',
314  'height' => 2,
315  'width' => 4,
316  'additionalCssClasses' => [
317  'custom-widget',
318  'rss-condensed',
319  ],
320  'availableForUser' => true
321  ],
322  [
323  'identifier' => 'test-widget1',
324  'serviceName' => 'dashboard.widget.t3news',
325  'groups' => ['group1'],
326  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
327  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
328  'iconIdentifier' => 'content-widget-rss',
329  'height' => 2,
330  'width' => 4,
331  'additionalCssClasses' => [
332  'custom-widget',
333  'rss-condensed',
334  ],
335  'availableForUser' => true
336  ],
337  [
338  'identifier' => 'test-widget2',
339  'serviceName' => 'dashboard.widget.t3orgnews',
340  'groups' => ['group2'],
341  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
342  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
343  'iconIdentifier' => 'content-widget-rss',
344  'height' => 2,
345  'width' => 4,
346  'additionalCssClasses' => [
347  'custom-widget',
348  'rss-condensed',
349  ],
350  'availableForUser' => true
351  ],
352  ]
353  ],
354  'Two widgets, one not available for user' => [
355  [
356  'count' => 2,
357  'adminCount' => 2,
358  'userCount' => 1,
359  'group1Count' => 1,
360  ],
361  [
362  [
363  'identifier' => 'test-widget1',
364  'serviceName' => 'dashboard.widget.t3news',
365  'groups' => ['group1'],
366  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
367  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
368  'iconIdentifier' => 'content-widget-rss',
369  'height' => 2,
370  'width' => 4,
371  'additionalCssClasses' => [
372  'custom-widget',
373  'rss-condensed',
374  ],
375  'availableForUser' => true
376  ],
377  [
378  'identifier' => 'test-widget2',
379  'serviceName' => 'dashboard.widget.t3comnews',
380  'groups' => ['group2'],
381  'title' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.title',
382  'description' => 'LLL:EXT:dashboard/Resources/Private/Language/Widgets.xlf:T3OrgNews.description',
383  'iconIdentifier' => 'content-widget-rss',
384  'height' => 2,
385  'width' => 4,
386  'additionalCssClasses' => [
387  'custom-widget',
388  'rss-condensed',
389  ],
390  'availableForUser' => false
391  ],
392  ]
393  ],
394  ];
395  }
396 }
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: WidgetRegistryTest.php:32
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\initiallyZeroWidgetAreRegistered
‪initiallyZeroWidgetAreRegistered()
Definition: WidgetRegistryTest.php:58
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\$containerProphecy
‪ContainerInterface ObjectProphecy $containerProphecy
Definition: WidgetRegistryTest.php:44
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\addWidgetsInItemsProcFunc
‪addWidgetsInItemsProcFunc()
Definition: WidgetRegistryTest.php:151
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\returnsWidgetsForGroup
‪returnsWidgetsForGroup(array $expectedValues, array $widgetsToRegister)
Definition: WidgetRegistryTest.php:84
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\registerWidget
‪registerWidget(array $widget)
Definition: WidgetRegistryTest.php:211
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\registerWidgets
‪registerWidgets(array $widgetsToRegister)
Definition: WidgetRegistryTest.php:204
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:31
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\setUp
‪setUp()
Definition: WidgetRegistryTest.php:46
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\getAvailableWidgetsOnlyReturnWidgetsAccessibleByUser
‪getAvailableWidgetsOnlyReturnWidgetsAccessibleByUser(array $expectedValues, array $widgetsToRegister)
Definition: WidgetRegistryTest.php:129
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\getAvailableWidgetsOnlyReturnWidgetsAccessibleByAdmin
‪getAvailableWidgetsOnlyReturnWidgetsAccessibleByAdmin(array $expectedValues, array $widgetsToRegister)
Definition: WidgetRegistryTest.php:104
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\$beUserProphecy
‪BackendUserAuthentication ObjectProphecy $beUserProphecy
Definition: WidgetRegistryTest.php:40
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\widgetsToRegister
‪widgetsToRegister()
Definition: WidgetRegistryTest.php:227
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\getAllWidgetReturnsAllRegisteredWidgets
‪getAllWidgetReturnsAllRegisteredWidgets(array $expectedValues, array $widgetsToRegister)
Definition: WidgetRegistryTest.php:70
‪TYPO3\CMS\Dashboard\Tests\Unit
Definition: DashboardPresetRegistryTest.php:18
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest\$subject
‪WidgetRegistry $subject
Definition: WidgetRegistryTest.php:36
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface
Definition: WidgetConfigurationInterface.php:26
‪TYPO3\CMS\Dashboard\Tests\Unit\WidgetRegistryTest
Definition: WidgetRegistryTest.php:29