‪TYPO3CMS  ‪main
ShortcutButtonTest.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;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪ShortcutButtonTest extends FunctionalTestCase
30 {
31  private const ‪FIXTURES_PATH_PATTERN = __DIR__ . '/../../../Fixtures/%s.html';
32 
33  #[Test]
34  public function ‪isButtonValid(): void
35  {
36  self::assertFalse((new ‪ShortcutButton())->isValid());
37  self::assertFalse((new ‪ShortcutButton())->setRouteIdentifier('web_list')->isValid());
38  self::assertFalse((new ‪ShortcutButton())->setDisplayName('Some module anme')->isValid());
39  self::assertTrue((new ‪ShortcutButton())->setRouteIdentifier('web_list')->setDisplayName('Some module anme')->isValid());
40  }
41 
42  #[Test]
44  {
45  $this->importCSVDataSet(__DIR__ . '/../../../../Fixtures/be_users_no_bookmarks.csv');
46  $backendUser = $this->setUpBackendUser(1);
47  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
48  self::assertEmpty(
49  (new ‪ShortcutButton())->setRouteIdentifier('web_list')->setDisplayName('Some module anme')->render()
50  );
51  }
52 
53  #[DataProvider('rendersCorrectMarkupDataProvider')]
54  #[Test]
55  public function ‪rendersCorrectMarkup(‪ShortcutButton $button, string $expectedMarkupFile): void
56  {
57  $this->importCSVDataSet(__DIR__ . '/../../../../Fixtures/be_users.csv');
58  $backendUser = $this->setUpBackendUser(1);
59  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
60  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'SCRIPT_NAME' => '/index.php']);
61  $request = new ‪ServerRequest('http://example.com/typo3/index.php', 'GET', null, $serverParams);
62  ‪$GLOBALS['TYPO3_REQUEST'] = $request
63  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE)
64  ->withAttribute('normalizedParams', ‪NormalizedParams::createFromServerParams($serverParams));
65 
66  self::assertEquals(
67  $this->‪normalizeSpaces(file_get_contents(sprintf(self::FIXTURES_PATH_PATTERN, $expectedMarkupFile))),
68  $this->‪normalizeSpaces($button->‪render())
69  );
70  }
71 
72  public static function ‪rendersCorrectMarkupDataProvider(): \Generator
73  {
74  yield 'Recordlist' => [
75  (new ‪ShortcutButton())
76  ->setRouteIdentifier('web_list')
77  ->setDisplayName('Recordlist')
78  ->setCopyUrlToClipboard(false),
79  'RecordList',
80  ];
81  yield 'Recordlist with copyToClipboard action' => [
82  (new ‪ShortcutButton())
83  ->setRouteIdentifier('web_list')
84  ->setDisplayName('Recordlist'),
85  'RecordListCopyToClipboard',
86  ];
87  yield 'Recordlist - single table view' => [
88  (new ‪ShortcutButton())
89  ->setRouteIdentifier('web_list')
90  ->setDisplayName('Recordlist - single table view')
91  ->setCopyUrlToClipboard(false)
92  ->setArguments([
93  'id' => 123,
94  'table' => 'some_table',
95  'GET' => [
96  'clipBoard' => 1,
97  ],
98  ]),
99  'RecordListSingleTable',
100  ];
101  yield 'Recordlist - single table view with copyToClipboard action' => [
102  (new ‪ShortcutButton())
103  ->setRouteIdentifier('web_list')
104  ->setDisplayName('Recordlist - single table view')
105  ->setArguments([
106  'id' => 123,
107  'table' => 'some_table',
108  'GET' => [
109  'clipBoard' => 1,
110  ],
111  ]),
112  'RecordListSingleTableCopyToClipboard',
113  ];
114  yield 'With special route identifier' => [
115  (new ‪ShortcutButton())
116  ->setRouteIdentifier('record_edit')
117  ->setDisplayName('Edit record')
118  ->setCopyUrlToClipboard(false),
119  'SpecialRouteIdentifier',
120  ];
121  yield 'With special route identifier and arguments' => [
122  (new ‪ShortcutButton())
123  ->setRouteIdentifier('record_edit')
124  ->setDisplayName('Edit record')
125  ->setCopyUrlToClipboard(false)
126  ->setArguments([
127  'id' => 123,
128  'edit' => [
129  'pages' => [
130  123 => 'edit',
131  ],
132  'overrideVals' => [
133  'pages' => [
134  'sys_language_uid' => 1,
135  ],
136  ],
137  ],
138  'returnUrl' => 'some/url',
139  ]),
140  'SpecialRouteIdentifierWithArguments',
141  ];
142  yield 'With special route identifier and arguments - copyToClipboard' => [
143  (new ‪ShortcutButton())
144  ->setRouteIdentifier('record_edit')
145  ->setDisplayName('Edit record')
146  ->setArguments([
147  'id' => 123,
148  'edit' => [
149  'pages' => [
150  123 => 'edit',
151  ],
152  'overrideVals' => [
153  'pages' => [
154  'sys_language_uid' => 1,
155  ],
156  ],
157  ],
158  'returnUrl' => 'some/url',
159  ]),
160  'SpecialRouteIdentifierWithArgumentsCopyToClipboard',
161  ];
162  }
163 
170  private function ‪normalizeSpaces(string $html): string
171  {
172  return preg_replace(
173  ['/^\s+(?=<)/m', '/^\s+(?!<)/m', '/\v+/'],
174  ['', ' ', ''],
175  $html
176  );
177  }
178 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\rendersCorrectMarkupDataProvider
‪static rendersCorrectMarkupDataProvider()
Definition: ShortcutButtonTest.php:72
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest
Definition: ShortcutButtonTest.php:30
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\normalizeSpaces
‪normalizeSpaces(string $html)
Definition: ShortcutButtonTest.php:170
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton
Definition: ShortcutButton.php:53
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\buttonIsNotRenderedForUserWithInsufficientPermissions
‪buttonIsNotRenderedForUserWithInsufficientPermissions()
Definition: ShortcutButtonTest.php:43
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\FIXTURES_PATH_PATTERN
‪const FIXTURES_PATH_PATTERN
Definition: ShortcutButtonTest.php:31
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\rendersCorrectMarkup
‪rendersCorrectMarkup(ShortcutButton $button, string $expectedMarkupFile)
Definition: ShortcutButtonTest.php:55
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, array $systemConfiguration=null)
Definition: NormalizedParams.php:824
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action\ShortcutButtonTest\isButtonValid
‪isButtonValid()
Definition: ShortcutButtonTest.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Backend\Tests\Functional\Template\Components\Buttons\Action
Definition: ShortcutButtonTest.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\render
‪string render()
Definition: ShortcutButton.php:183
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38