‪TYPO3CMS  ‪main
ShortcutRepositoryTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 final class ‪ShortcutRepositoryTest extends FunctionalTestCase
34 {
36 
37  protected function ‪setUp(): void
38  {
39  parent::setUp();
40  $this->importCSVDataSet(__DIR__ . '/../Fixtures/ShortcutsBase.csv');
41  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv');
42  $backendUser = $this->setUpBackendUser(1);
43  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
44 
45  $request = new ‪ServerRequest('https://localhost/typo3/');
46  $requestContextFactory = $this->get(RequestContextFactory::class);
47  $uriBuilder = $this->get(UriBuilder::class);
48  $uriBuilder->setRequestContext($requestContextFactory->fromBackendRequest($request));
49  $this->subject = new ‪ShortcutRepository(
50  $this->get(ConnectionPool::class),
51  $this->get(IconFactory::class),
52  $this->get(ModuleProvider::class),
53  $this->get(Router::class),
54  $this->get(UriBuilder::class),
55  );
56  }
57 
58  #[DataProvider('shortcutExistsTestDataProvider')]
59  #[Test]
60  public function ‪shortcutExistsTest(string $routeIdentifier, array $arguments, int $userid, bool $exists): void
61  {
62  ‪$GLOBALS['BE_USER']->user['uid'] = $userid;
63  self::assertEquals($exists, $this->subject->shortcutExists($routeIdentifier, json_encode($arguments)));
64  }
65 
66  public static function ‪shortcutExistsTestDataProvider(): \Generator
67  {
68  yield 'Shortcut exists' => [
69  'web_list',
70  ['id' => 123, 'GET' => ['clipBoard' => 1]],
71  1,
72  true,
73  ];
74  yield 'Not this user' => [
75  'web_list',
76  ['id' => 123, 'GET' => ['clipBoard' => 1]],
77  2,
78  false,
79  ];
80  yield 'Wrong route identifer' => [
81  'web_layout',
82  ['id' => 123, 'GET' => ['clipBoard' => 1]],
83  1,
84  false,
85  ];
86  yield 'Wrong arguments' => [
87  'web_list',
88  ['id' => 321, 'GET' => ['clipBoard' => 1]],
89  1,
90  false,
91  ];
92  }
93 
94  #[Test]
95  public function ‪addShortcutTest(): void
96  {
97  foreach ($this->‪getShortcutsToAdd() as $shortcut) {
98  $this->subject->addShortcut(
99  $shortcut['routeIdentifier'],
100  json_encode($shortcut['arguments']),
101  $shortcut['title']
102  );
103  }
104 
105  $this->assertCSVDataSet(__DIR__ . '/../Fixtures/ShortcutsAddedResult.csv');
106  }
107 
108  public function ‪getShortcutsToAdd(): array
109  {
110  return [
111  'Basic shortcut with all information' => [
112  'routeIdentifier' => 'web_list',
113  'arguments' => ['id' => 111, 'GET' => ['clipBoard' => 1]],
114  'title' => 'Recordlist of id 111',
115  ],
116  'Shortcut with empty title' => [
117  'routeIdentifier' => 'record_edit',
118  'arguments' => ['edit' => ['pages' => [112 => 'edit']]],
119  'title' => '',
120  ],
121  'Shortcut with invalid route' => [
122  'routeIdentifier' => 'invalid_route',
123  'arguments' => ['edit' => ['pages' => [112 => 'edit']]],
124  'title' => 'Some title',
125  ],
126  ];
127  }
128 
132  #[Test]
133  public function ‪getShortcutsByGroupTest(): void
134  {
135  $expected = [
136  1 => [
137  'table' => null,
138  'recordid' => null,
139  'groupLabel' => 'Pages',
140  'type' => 'other',
141  'icon' => 'data-identifier="module-list"',
142  'label' => 'Recordlist',
143  'href' => '/typo3/module/web/list?token=%s&id=123&GET%5BclipBoard%5D=1',
144  ],
145  2 => [
146  'table' => 'tt_content',
147  'recordid' => 113,
148  'groupLabel' => null,
149  'type' => 'edit',
150  'label' => 'Edit Content',
151  'icon' => 'data-identifier="mimetypes-x-content-text"',
152  'href' => '/typo3/record/edit?token=%s&edit%5Btt_content%5D%5B113%5D=edit',
153  ],
154  3 => [
155  'table' => 'tt_content',
156  'recordid' => 117,
157  'groupLabel' => null,
158  'type' => 'new',
159  'label' => 'Create Content',
160  'icon' => 'data-identifier="mimetypes-x-content-text"',
161  'href' => '/typo3/record/edit?token=%s&edit%5Btt_content%5D%5B117%5D=new',
162  ],
163  7 => [
164  'table' => null,
165  'recordid' => null,
166  'groupLabel' => null,
167  'type' => 'other',
168  'label' => 'Shortcut', // This is a fallback to not display shortcuts without title
169  'icon' => 'data-identifier="module-page"',
170  'href' => '/typo3/module/web/layout?token=%s&id=123',
171  ],
172  ];
173 
174  $shortcuts = $this->subject->getShortcutsByGroup(1);
175  self::assertCount(count($expected), $shortcuts);
176 
177  foreach ($shortcuts as $shortcut) {
178  $id = (int)$shortcut['raw']['uid'];
179  self::assertEquals(1, $shortcut['group']);
180  self::assertEquals($expected[$id]['table'], $shortcut['table'] ?? null);
181  self::assertEquals($expected[$id]['recordid'], $shortcut['recordid'] ?? null);
182  self::assertEquals($expected[$id]['groupLabel'], $shortcut['groupLabel'] ?? null);
183  self::assertEquals($expected[$id]['type'], $shortcut['type']);
184  self::assertEquals($expected[$id]['label'], $shortcut['label']);
185  self::assertStringContainsString($expected[$id]['icon'], $shortcut['icon']);
186  self::assertStringMatchesFormat($expected[$id]['href'], $shortcut['href']);
187  }
188  }
189 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\getShortcutsByGroupTest
‪getShortcutsByGroupTest()
Definition: ShortcutRepositoryTest.php:133
‪TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository
Definition: ShortcutRepository.php:42
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut
Definition: ShortcutRepositoryTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\getShortcutsToAdd
‪getShortcutsToAdd()
Definition: ShortcutRepositoryTest.php:108
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\addShortcutTest
‪addShortcutTest()
Definition: ShortcutRepositoryTest.php:95
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest
Definition: ShortcutRepositoryTest.php:34
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Module\ModuleProvider
Definition: ModuleProvider.php:29
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\shortcutExistsTestDataProvider
‪static shortcutExistsTestDataProvider()
Definition: ShortcutRepositoryTest.php:66
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\setUp
‪setUp()
Definition: ShortcutRepositoryTest.php:37
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\$subject
‪ShortcutRepository $subject
Definition: ShortcutRepositoryTest.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Backend\Tests\Functional\Backend\Shortcut\ShortcutRepositoryTest\shortcutExistsTest
‪shortcutExistsTest(string $routeIdentifier, array $arguments, int $userid, bool $exists)
Definition: ShortcutRepositoryTest.php:60
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30