Replace text_dir global var with LanguageManager::$textDir static
[phpmyadmin.git] / tests / classes / TransformationsTest.php
blob42b43c992a89ae01d42c0ca8bf2051b83847fe34
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\ConfigStorage\Relation;
9 use PhpMyAdmin\ConfigStorage\RelationParameters;
10 use PhpMyAdmin\Current;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Tests\Stubs\DummyResult;
13 use PhpMyAdmin\Transformations;
14 use PHPUnit\Framework\Attributes\CoversClass;
15 use PHPUnit\Framework\Attributes\DataProvider;
16 use PHPUnit\Framework\Attributes\PreserveGlobalState;
17 use PHPUnit\Framework\Attributes\RunInSeparateProcess;
18 use ReflectionProperty;
20 #[CoversClass(Transformations::class)]
21 class TransformationsTest extends AbstractTestCase
23 private Transformations $transformations;
25 /**
26 * Set up global environment.
28 protected function setUp(): void
30 parent::setUp();
32 DatabaseInterface::$instance = $this->createDatabaseInterface();
33 Current::$table = 'table';
34 Current::$database = 'db';
35 $config = Config::getInstance();
36 $config->settings = ['ServerDefault' => 1, 'ActionLinksMode' => 'icons'];
37 $config->selectedServer['pmadb'] = 'pmadb';
38 $config->selectedServer['user'] = 'user';
39 $config->selectedServer['bookmarktable'] = '';
40 $config->selectedServer['relation'] = '';
41 $config->selectedServer['table_info'] = '';
42 $config->selectedServer['table_coords'] = '';
43 $config->selectedServer['column_info'] = 'column_info';
44 $config->settings['DBG']['sql'] = false;
46 $this->transformations = new Transformations();
49 /**
50 * Test for parsing options.
52 * @param string $input String to parse
53 * @param mixed[] $expected Expected result
55 #[DataProvider('getOptionsData')]
56 public function testGetOptions(string $input, array $expected): void
58 $this->assertEquals(
59 $expected,
60 $this->transformations->getOptions($input),
64 /**
65 * Data provided for parsing options
67 * @return mixed[][]
69 public static function getOptionsData(): array
71 return [
72 ['option1 , option2 ', ['option1 ', ' option2 ']],
73 ["'option1' ,' option2' ", ['option1', ' option2']],
74 ["'2,3' ,' ,, option ,,' ", ['2,3', ' ,, option ,,']],
75 ["'',,", ['', '', '']],
76 ['', []],
80 #[PreserveGlobalState(false)]
81 #[RunInSeparateProcess]
82 public function testGetTypes(): void
84 $this->assertEquals(
86 'mimetype' => [
87 'Application/Octetstream' => 'Application/Octetstream',
88 'Image/JPEG' => 'Image/JPEG',
89 'Image/PNG' => 'Image/PNG',
90 'Text/Plain' => 'Text/Plain',
91 'Text/Octetstream' => 'Text/Octetstream',
93 'transformation' => [
94 0 => 'Application/Octetstream: Download',
95 1 => 'Application/Octetstream: Hex',
96 2 => 'Image/JPEG: Inline',
97 3 => 'Image/JPEG: Link',
98 4 => 'Image/PNG: Inline',
99 5 => 'Text/Octetstream: Sql',
100 6 => 'Text/Plain: Binarytoip',
101 7 => 'Text/Plain: Bool2Text',
102 8 => 'Text/Plain: Dateformat',
103 9 => 'Text/Plain: External',
104 10 => 'Text/Plain: Formatted',
105 11 => 'Text/Plain: Imagelink',
106 12 => 'Text/Plain: Json',
107 13 => 'Text/Plain: Sql',
108 14 => 'Text/Plain: Xml',
109 15 => 'Text/Plain: Link',
110 16 => 'Text/Plain: Longtoipv4',
111 17 => 'Text/Plain: PreApPend',
112 18 => 'Text/Plain: Substring',
114 'transformation_file' => [
115 0 => 'Output/Application_Octetstream_Download.php',
116 1 => 'Output/Application_Octetstream_Hex.php',
117 2 => 'Output/Image_JPEG_Inline.php',
118 3 => 'Output/Image_JPEG_Link.php',
119 4 => 'Output/Image_PNG_Inline.php',
120 5 => 'Output/Text_Octetstream_Sql.php',
121 6 => 'Output/Text_Plain_Binarytoip.php',
122 7 => 'Output/Text_Plain_Bool2Text.php',
123 8 => 'Output/Text_Plain_Dateformat.php',
124 9 => 'Output/Text_Plain_External.php',
125 10 => 'Output/Text_Plain_Formatted.php',
126 11 => 'Output/Text_Plain_Imagelink.php',
127 12 => 'Output/Text_Plain_Json.php',
128 13 => 'Output/Text_Plain_Sql.php',
129 14 => 'Output/Text_Plain_Xml.php',
130 15 => 'Text_Plain_Link.php',
131 16 => 'Text_Plain_Longtoipv4.php',
132 17 => 'Text_Plain_PreApPend.php',
133 18 => 'Text_Plain_Substring.php',
135 'input_transformation' => [
136 'Image/JPEG: Upload',
137 'Text/Plain: FileUpload',
138 'Text/Plain: Iptobinary',
139 'Text/Plain: Iptolong',
140 'Text/Plain: JsonEditor',
141 'Text/Plain: RegexValidation',
142 'Text/Plain: SqlEditor',
143 'Text/Plain: XmlEditor',
144 'Text/Plain: Link',
145 'Text/Plain: Longtoipv4',
146 'Text/Plain: PreApPend',
147 'Text/Plain: Substring',
149 'input_transformation_file' => [
150 'Input/Image_JPEG_Upload.php',
151 'Input/Text_Plain_FileUpload.php',
152 'Input/Text_Plain_Iptobinary.php',
153 'Input/Text_Plain_Iptolong.php',
154 'Input/Text_Plain_JsonEditor.php',
155 'Input/Text_Plain_RegexValidation.php',
156 'Input/Text_Plain_SqlEditor.php',
157 'Input/Text_Plain_XmlEditor.php',
158 'Text_Plain_Link.php',
159 'Text_Plain_Longtoipv4.php',
160 'Text_Plain_PreApPend.php',
161 'Text_Plain_Substring.php',
164 $this->transformations->getAvailableMimeTypes(),
169 * Tests getting mime types for table
171 public function testGetMime(): void
173 $relationParameters = RelationParameters::fromArray([
174 'db' => 'pmadb',
175 'mimework' => true,
176 'trackingwork' => true,
177 'column_info' => 'column_info',
179 (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters);
180 $this->assertEquals(
182 'o' => [
183 'column_name' => 'o',
184 'mimetype' => 'Text/plain',
185 'transformation' => 'Sql',
186 'transformation_options' => '',
187 'input_transformation' => 'regex',
188 'input_transformation_options' => '/pma/i',
190 'col' => [
191 'column_name' => 'col',
192 'mimetype' => 'T',
193 'transformation' => 'O/P',
194 'transformation_options' => '',
195 'input_transformation' => 'i/p',
196 'input_transformation_options' => '',
199 $this->transformations->getMime('pma_test', 'table1'),
204 * Test for clear
206 public function testClear(): void
208 // Mock dbi
209 $dbi = $this->getMockBuilder(DatabaseInterface::class)
210 ->disableOriginalConstructor()
211 ->getMock();
212 $dbi->expects($this->any())
213 ->method('tryQuery')
214 ->willReturn($this->createStub(DummyResult::class));
215 DatabaseInterface::$instance = $dbi;
217 (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, null);
219 // Case 1 : no configuration storage
220 $actual = $this->transformations->clear('db');
221 $this->assertFalse($actual);
223 $relationParameters = RelationParameters::fromArray([
224 'db' => 'pmadb',
225 'mimework' => true,
226 'column_info' => 'column_info',
228 (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters);
230 // Case 2 : database delete
231 $actual = $this->transformations->clear('db');
232 $this->assertTrue($actual);
234 // Case 3 : table delete
235 $actual = $this->transformations->clear('db', 'table');
236 $this->assertTrue($actual);
238 // Case 4 : column delete
239 $actual = $this->transformations->clear('db', 'table', 'col');
240 $this->assertTrue($actual);
244 * @param string $value value
245 * @param string $expected expected result
247 #[DataProvider('fixupData')]
248 public function testFixup(string $value, string $expected): void
250 $this->assertEquals(
251 $expected,
252 $this->transformations->fixUpMime($value),
256 /** @return mixed[][] */
257 public static function fixupData(): array
259 return [
260 ['text_plain_bool2text.php', 'Text_Plain_Bool2Text.php'],
261 ['application_octetstream_download.php', 'Application_Octetstream_Download.php'],
262 ['text_plain_json.php', 'Text_Plain_Json.php'],
263 ['image_jpeg_link.php', 'Image_JPEG_Link.php'],
264 ['text_plain_dateformat.php', 'Text_Plain_Dateformat.php'],
269 * Test for getDescription
271 * @param string $file transformation file
272 * @param string $expectedDescription expected description
274 #[DataProvider('providerGetDescription')]
275 public function testGetDescription(string $file, string $expectedDescription): void
277 $this->assertEquals(
278 $expectedDescription,
279 $this->transformations->getDescription($file),
283 /** @return mixed[][] */
284 public static function providerGetDescription(): array
286 return [
287 ['../../../../test', ''],
288 ['Input/Text_Plain_SqlEditor', 'Syntax highlighted CodeMirror editor for SQL.'],
289 ['Output/Text_Plain_Sql', 'Formats text as SQL query with syntax highlighting.'],
294 * Test for getName
296 * @param string $file transformation file
297 * @param string $expectedName expected name
299 #[DataProvider('providerGetName')]
300 public function testGetName(string $file, string $expectedName): void
302 $this->assertEquals(
303 $expectedName,
304 $this->transformations->getName($file),
308 /** @return mixed[][] */
309 public static function providerGetName(): array
311 return [['../../../../test', ''], ['Input/Text_Plain_SqlEditor', 'SQL'], ['Output/Text_Plain_Sql', 'SQL']];