‪TYPO3CMS  ‪main
CommandUtilityTest.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;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪CommandUtilityTest extends UnitTestCase
26 {
30  public static function ‪getConfiguredAppsDataProvider(): array
31  {
32  $defaultExpected = [
33  'perl' => [
34  'app' => 'perl',
35  'path' => '/usr/bin/',
36  'valid' => true,
37  ],
38  'unzip' => [
39  'app' => 'unzip',
40  'path' => '/usr/local/bin/',
41  'valid' => true,
42  ],
43  ];
44  return [
45  'returns empty array for empty string' => [
46  '',
47  [],
48  ],
49  'separated by comma' => [
50  'perl=/usr/bin/perl,unzip=/usr/local/bin/unzip',
51  $defaultExpected,
52  ],
53  'separated by new line' => [
54  'perl=/usr/bin/perl ' . LF . ' unzip=/usr/local/bin/unzip',
55  $defaultExpected,
56  ],
57  'separated by new line with spaces' => [
58  'perl = /usr/bin/perl ' . LF . ' unzip = /usr/local/bin/unzip',
59  $defaultExpected,
60  ],
61  'separated by new line with spaces and empty rows' => [
62  LF . 'perl = /usr/bin/perl ' . LF . LF . ' unzip = /usr/local/bin/unzip' . LF,
63  $defaultExpected,
64  ],
65  'separated by char(10)' => [
66  'perl=/usr/bin/perl\'.chr(10).\'unzip=/usr/local/bin/unzip',
67  $defaultExpected,
68  ],
69  'separated by LF as string' => [
70  'perl=/usr/bin/perl\' . LF . \'unzip=/usr/local/bin/unzip',
71  $defaultExpected,
72  ],
73  ];
74  }
75 
81  #[DataProvider('getConfiguredAppsDataProvider')]
82  #[Test]
83  public function ‪getConfiguredApps($globalsBinSetup, $expected): void
84  {
85  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup'] = $globalsBinSetup;
86  $commandUtilityMock = $this->getAccessibleMock(CommandUtility::class, null);
87  $result = $commandUtilityMock->_call('getConfiguredApps');
88  self::assertSame($expected, $result);
89  }
90 
94  public static function ‪unQuoteFilenameUnquotesCorrectlyDataProvider(): array
95  {
96  return [
97  // Some theoretical tests first
98  [
99  '',
100  [],
101  ],
102  [
103  'aa bb "cc" "dd"',
104  ['aa', 'bb', '"cc"', '"dd"'],
105  ],
106  [
107  'aa bb "cc dd"',
108  ['aa', 'bb', '"cc dd"'],
109  ],
110  [
111  '\'aa bb\' "cc dd"',
112  ['\'aa bb\'', '"cc dd"'],
113  ],
114  [
115  '\'aa bb\' cc "dd"',
116  ['\'aa bb\'', 'cc', '"dd"'],
117  ],
118  // Now test against some real world examples
119  [
120  '/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
121  [
122  '/opt/local/bin/gm.exe',
123  'convert',
124  '+profile',
125  '\'*\'',
126  '-geometry',
127  '170x136!',
128  '-negate',
129  '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
130  '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
131  ],
132  ],
133  [
134  'C:/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
135  [
136  'C:/opt/local/bin/gm.exe',
137  'convert',
138  '+profile',
139  '\'*\'',
140  '-geometry',
141  '170x136!',
142  '-negate',
143  '"C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
144  '"C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
145  ],
146  ],
147  [
148  '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
149  [
150  '/usr/bin/gm',
151  'convert',
152  '+profile',
153  '\'*\'',
154  '-geometry',
155  '170x136!',
156  '-negate',
157  '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
158  '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
159  ],
160  ],
161  [
162  '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
163  [
164  '/usr/bin/gm',
165  'convert',
166  '+profile',
167  '\'*\'',
168  '-geometry',
169  '170x136!',
170  '-negate',
171  '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
172  '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
173  ],
174  ],
175  [
176  '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate \'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\' \'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\'',
177  [
178  '/usr/bin/gm',
179  'convert',
180  '+profile',
181  '\'*\'',
182  '-geometry',
183  '170x136!',
184  '-negate',
185  '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\'',
186  '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\'',
187  ],
188  ],
189  ];
190  }
191 
195  #[DataProvider('unQuoteFilenameUnquotesCorrectlyDataProvider')]
196  #[Test]
197  public function ‪unQuoteFilenameUnquotesCorrectly(string $source, array $expectedQuoted): void
198  {
199  $commandUtilityMock = $this->getAccessibleMock(CommandUtility::class, null);
200  $actualQuoted = $commandUtilityMock->_call('unQuoteFilenames', $source);
201  self::assertEquals($expectedQuoted, $actualQuoted);
202  }
203 
209  #[Test]
211  {
212  $commandUtilityMock = $this->getAccessibleMock(CommandUtility::class, null);
213  $command = $commandUtilityMock->_call('getCommand', 'php');
214 
215  self::assertIsString($command);
216  self::assertNotEmpty($command);
217  self::assertStringContainsString('php', $command);
218  }
219 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest\unQuoteFilenameUnquotesCorrectly
‪unQuoteFilenameUnquotesCorrectly(string $source, array $expectedQuoted)
Definition: CommandUtilityTest.php:197
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest
Definition: CommandUtilityTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest\getConfiguredAppsDataProvider
‪static getConfiguredAppsDataProvider()
Definition: CommandUtilityTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest\getCommandWithPhpReturnsPathToPhpExecutable
‪getCommandWithPhpReturnsPathToPhpExecutable()
Definition: CommandUtilityTest.php:210
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest\getConfiguredApps
‪getConfiguredApps($globalsBinSetup, $expected)
Definition: CommandUtilityTest.php:83
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:54
‪TYPO3\CMS\Core\Tests\Unit\Utility\CommandUtilityTest\unQuoteFilenameUnquotesCorrectlyDataProvider
‪static unQuoteFilenameUnquotesCorrectlyDataProvider()
Definition: CommandUtilityTest.php:94