3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
8 use PhpMyAdmin\ConfigStorage\Relation
;
9 use PhpMyAdmin\ConfigStorage\RelationParameters
;
10 use PhpMyAdmin\Current
;
11 use PhpMyAdmin\Dbal\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 ReflectionProperty
;
18 #[CoversClass(Transformations::class)]
19 class TransformationsTest
extends AbstractTestCase
21 private Transformations
$transformations;
24 * Set up global environment.
26 protected function setUp(): void
30 DatabaseInterface
::$instance = $this->createDatabaseInterface();
31 Current
::$table = 'table';
32 Current
::$database = 'db';
33 $config = Config
::getInstance();
34 $config->settings
= ['ServerDefault' => 1, 'ActionLinksMode' => 'icons'];
35 $config->selectedServer
['pmadb'] = 'pmadb';
36 $config->selectedServer
['user'] = 'user';
37 $config->selectedServer
['bookmarktable'] = '';
38 $config->selectedServer
['relation'] = '';
39 $config->selectedServer
['table_info'] = '';
40 $config->selectedServer
['table_coords'] = '';
41 $config->selectedServer
['column_info'] = 'column_info';
43 $this->transformations
= new Transformations();
47 * Test for parsing options.
49 * @param string $input String to parse
50 * @param mixed[] $expected Expected result
52 #[DataProvider('getOptionsData')]
53 public function testGetOptions(string $input, array $expected): void
57 $this->transformations
->getOptions($input),
62 * Data provided for parsing options
66 public static function getOptionsData(): array
69 ['option1 , option2 ', ['option1 ', ' option2 ']],
70 ["'option1' ,' option2' ", ['option1', ' option2']],
71 ["'2,3' ,' ,, option ,,' ", ['2,3', ' ,, option ,,']],
72 ["'',,", ['', '', '']],
77 public function testGetTypes(): void
82 'Application/Octetstream' => 'Application/Octetstream',
83 'Image/JPEG' => 'Image/JPEG',
84 'Image/PNG' => 'Image/PNG',
85 'Text/Plain' => 'Text/Plain',
86 'Text/Octetstream' => 'Text/Octetstream',
89 'Application/Octetstream: Download',
90 'Application/Octetstream: Hex',
94 'Text/Octetstream: Sql',
95 'Text/Plain: Binarytoip',
96 'Text/Plain: Bool2Text',
97 'Text/Plain: Dateformat',
98 'Text/Plain: External',
99 'Text/Plain: Formatted',
100 'Text/Plain: Imagelink',
105 'Text/Plain: Longtoipv4',
106 'Text/Plain: PreApPend',
107 'Text/Plain: Substring',
109 'transformation_file' => [
110 'Output/Application_Octetstream_Download.php',
111 'Output/Application_Octetstream_Hex.php',
112 'Output/Image_JPEG_Inline.php',
113 'Output/Image_JPEG_Link.php',
114 'Output/Image_PNG_Inline.php',
115 'Output/Text_Octetstream_Sql.php',
116 'Output/Text_Plain_Binarytoip.php',
117 'Output/Text_Plain_Bool2Text.php',
118 'Output/Text_Plain_Dateformat.php',
119 'Output/Text_Plain_External.php',
120 'Output/Text_Plain_Formatted.php',
121 'Output/Text_Plain_Imagelink.php',
122 'Output/Text_Plain_Json.php',
123 'Output/Text_Plain_Sql.php',
124 'Output/Text_Plain_Xml.php',
125 'Text_Plain_Link.php',
126 'Text_Plain_Longtoipv4.php',
127 'Text_Plain_PreApPend.php',
128 'Text_Plain_Substring.php',
130 'input_transformation' => [
131 'Image/JPEG: Upload',
132 'Text/Plain: FileUpload',
133 'Text/Plain: Iptobinary',
134 'Text/Plain: Iptolong',
135 'Text/Plain: JsonEditor',
136 'Text/Plain: RegexValidation',
137 'Text/Plain: SqlEditor',
138 'Text/Plain: XmlEditor',
140 'Text/Plain: Longtoipv4',
141 'Text/Plain: PreApPend',
142 'Text/Plain: Substring',
144 'input_transformation_file' => [
145 'Input/Image_JPEG_Upload.php',
146 'Input/Text_Plain_FileUpload.php',
147 'Input/Text_Plain_Iptobinary.php',
148 'Input/Text_Plain_Iptolong.php',
149 'Input/Text_Plain_JsonEditor.php',
150 'Input/Text_Plain_RegexValidation.php',
151 'Input/Text_Plain_SqlEditor.php',
152 'Input/Text_Plain_XmlEditor.php',
153 'Text_Plain_Link.php',
154 'Text_Plain_Longtoipv4.php',
155 'Text_Plain_PreApPend.php',
156 'Text_Plain_Substring.php',
159 $this->transformations
->getAvailableMimeTypes(),
164 * Tests getting mime types for table
166 public function testGetMime(): void
168 $relationParameters = RelationParameters
::fromArray([
171 'trackingwork' => true,
172 'column_info' => 'column_info',
174 (new ReflectionProperty(Relation
::class, 'cache'))->setValue(null, $relationParameters);
178 'column_name' => 'o',
179 'mimetype' => 'Text/plain',
180 'transformation' => 'Sql',
181 'transformation_options' => '',
182 'input_transformation' => 'regex',
183 'input_transformation_options' => '/pma/i',
186 'column_name' => 'col',
188 'transformation' => 'O/P',
189 'transformation_options' => '',
190 'input_transformation' => 'i/p',
191 'input_transformation_options' => '',
194 $this->transformations
->getMime('pma_test', 'table1'),
201 public function testClear(): void
204 $dbi = $this->getMockBuilder(DatabaseInterface
::class)
205 ->disableOriginalConstructor()
207 $dbi->expects(self
::any())
209 ->willReturn(self
::createStub(DummyResult
::class));
210 DatabaseInterface
::$instance = $dbi;
212 (new ReflectionProperty(Relation
::class, 'cache'))->setValue(null, null);
214 // Case 1 : no configuration storage
215 $actual = $this->transformations
->clear('db');
216 self
::assertFalse($actual);
218 $relationParameters = RelationParameters
::fromArray([
221 'column_info' => 'column_info',
223 (new ReflectionProperty(Relation
::class, 'cache'))->setValue(null, $relationParameters);
225 // Case 2 : database delete
226 $actual = $this->transformations
->clear('db');
227 self
::assertTrue($actual);
229 // Case 3 : table delete
230 $actual = $this->transformations
->clear('db', 'table');
231 self
::assertTrue($actual);
233 // Case 4 : column delete
234 $actual = $this->transformations
->clear('db', 'table', 'col');
235 self
::assertTrue($actual);
239 * @param string $value value
240 * @param string $expected expected result
242 #[DataProvider('fixupData')]
243 public function testFixup(string $value, string $expected): void
247 $this->transformations
->fixUpMime($value),
251 /** @return mixed[][] */
252 public static function fixupData(): array
255 ['text_plain_bool2text.php', 'Text_Plain_Bool2Text.php'],
256 ['application_octetstream_download.php', 'Application_Octetstream_Download.php'],
257 ['text_plain_json.php', 'Text_Plain_Json.php'],
258 ['image_jpeg_link.php', 'Image_JPEG_Link.php'],
259 ['text_plain_dateformat.php', 'Text_Plain_Dateformat.php'],
264 * Test for getDescription
266 * @param string $file transformation file
267 * @param string $expectedDescription expected description
269 #[DataProvider('providerGetDescription')]
270 public function testGetDescription(string $file, string $expectedDescription): void
273 $expectedDescription,
274 $this->transformations
->getDescription($file),
278 /** @return mixed[][] */
279 public static function providerGetDescription(): array
282 ['../../../../test', ''],
283 ['Input/Text_Plain_SqlEditor', 'Syntax highlighted CodeMirror editor for SQL.'],
284 ['Output/Text_Plain_Sql', 'Formats text as SQL query with syntax highlighting.'],
291 * @param string $file transformation file
292 * @param string $expectedName expected name
294 #[DataProvider('providerGetName')]
295 public function testGetName(string $file, string $expectedName): void
299 $this->transformations
->getName($file),
303 /** @return mixed[][] */
304 public static function providerGetName(): array
306 return [['../../../../test', ''], ['Input/Text_Plain_SqlEditor', 'SQL'], ['Output/Text_Plain_Sql', 'SQL']];