File: TransformationsTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.2-really%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 140,312 kB
  • sloc: javascript: 228,447; php: 166,904; xml: 17,847; sql: 504; sh: 275; makefile: 209; python: 205
file content (358 lines) | stat: -rw-r--r-- 11,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\ConfigStorage\RelationParameters;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Transformations;

/**
 * @covers \PhpMyAdmin\Transformations
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Transformations::class)]
class TransformationsTest extends AbstractTestCase
{
    /** @var Transformations */
    private $transformations;

    /**
     * Set up global environment.
     */
    protected function setUp(): void
    {
        parent::setUp();
        $GLOBALS['table'] = 'table';
        $GLOBALS['db'] = 'db';
        $GLOBALS['cfg'] = [
            'ServerDefault' => 1,
            'ActionLinksMode' => 'icons',
        ];
        $GLOBALS['server'] = 1;
        $GLOBALS['cfg']['Server']['pmadb'] = 'pmadb';
        $GLOBALS['cfg']['Server']['user'] = 'user';
        $GLOBALS['cfg']['Server']['bookmarktable'] = '';
        $GLOBALS['cfg']['Server']['relation'] = '';
        $GLOBALS['cfg']['Server']['table_info'] = '';
        $GLOBALS['cfg']['Server']['table_coords'] = '';
        $GLOBALS['cfg']['Server']['column_info'] = 'column_info';
        $GLOBALS['cfg']['DBG']['sql'] = false;

        $this->transformations = new Transformations();
    }

    /**
     * Test for parsing options.
     *
     * @param string $input    String to parse
     * @param array  $expected Expected result
     *
     * @dataProvider getOptionsData
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('getOptionsData')]
    public function testGetOptions(string $input, array $expected): void
    {
        self::assertSame($expected, $this->transformations->getOptions($input));
    }

    /**
     * Data provided for parsing options
     */
    public static function getOptionsData(): array
    {
        return [
            [
                'option1 , option2 ',
                [
                    'option1 ',
                    ' option2 ',
                ],
            ],
            [
                "'option1' ,' option2' ",
                [
                    'option1',
                    ' option2',
                ],
            ],
            [
                "'2,3' ,' ,, option ,,' ",
                [
                    '2,3',
                    ' ,, option ,,',
                ],
            ],
            [
                "'',,",
                [
                    '',
                    '',
                    '',
                ],
            ],
            [
                '',
                [],
            ],
        ];
    }

    /**
     * Test for getting available types.
     */
    public function testGetTypes(): void
    {
        self::assertEquals([
            'mimetype' => [
                'Application/Octetstream' => 'Application/Octetstream',
                'Image/JPEG' => 'Image/JPEG',
                'Image/PNG' => 'Image/PNG',
                'Text/Plain' => 'Text/Plain',
                'Text/Octetstream' => 'Text/Octetstream',
            ],
            'transformation' => [
                0 => 'Application/Octetstream: Download',
                1 => 'Application/Octetstream: Hex',
                2 => 'Image/JPEG: Inline',
                3 => 'Image/JPEG: Link',
                4 => 'Image/PNG: Inline',
                5 => 'Text/Octetstream: Sql',
                6 => 'Text/Plain: Binarytoip',
                7 => 'Text/Plain: Bool2Text',
                8 => 'Text/Plain: Dateformat',
                9 => 'Text/Plain: External',
                10 => 'Text/Plain: Formatted',
                11 => 'Text/Plain: Imagelink',
                12 => 'Text/Plain: Json',
                13 => 'Text/Plain: Sql',
                14 => 'Text/Plain: Xml',
                15 => 'Text/Plain: Link',
                16 => 'Text/Plain: Longtoipv4',
                17 => 'Text/Plain: PreApPend',
                18 => 'Text/Plain: Substring',
            ],
            'transformation_file' => [
                0 => 'Output/Application_Octetstream_Download.php',
                1 => 'Output/Application_Octetstream_Hex.php',
                2 => 'Output/Image_JPEG_Inline.php',
                3 => 'Output/Image_JPEG_Link.php',
                4 => 'Output/Image_PNG_Inline.php',
                5 => 'Output/Text_Octetstream_Sql.php',
                6 => 'Output/Text_Plain_Binarytoip.php',
                7 => 'Output/Text_Plain_Bool2Text.php',
                8 => 'Output/Text_Plain_Dateformat.php',
                9 => 'Output/Text_Plain_External.php',
                10 => 'Output/Text_Plain_Formatted.php',
                11 => 'Output/Text_Plain_Imagelink.php',
                12 => 'Output/Text_Plain_Json.php',
                13 => 'Output/Text_Plain_Sql.php',
                14 => 'Output/Text_Plain_Xml.php',
                15 => 'Text_Plain_Link.php',
                16 => 'Text_Plain_Longtoipv4.php',
                17 => 'Text_Plain_PreApPend.php',
                18 => 'Text_Plain_Substring.php',
            ],
            'input_transformation' => [
                'Image/JPEG: Upload',
                'Text/Plain: FileUpload',
                'Text/Plain: Iptobinary',
                'Text/Plain: Iptolong',
                'Text/Plain: JsonEditor',
                'Text/Plain: RegexValidation',
                'Text/Plain: SqlEditor',
                'Text/Plain: XmlEditor',
                'Text/Plain: Link',
                'Text/Plain: Longtoipv4',
                'Text/Plain: PreApPend',
                'Text/Plain: Substring',
            ],
            'input_transformation_file' => [
                'Input/Image_JPEG_Upload.php',
                'Input/Text_Plain_FileUpload.php',
                'Input/Text_Plain_Iptobinary.php',
                'Input/Text_Plain_Iptolong.php',
                'Input/Text_Plain_JsonEditor.php',
                'Input/Text_Plain_RegexValidation.php',
                'Input/Text_Plain_SqlEditor.php',
                'Input/Text_Plain_XmlEditor.php',
                'Text_Plain_Link.php',
                'Text_Plain_Longtoipv4.php',
                'Text_Plain_PreApPend.php',
                'Text_Plain_Substring.php',
            ],
        ], $this->transformations->getAvailableMimeTypes());
    }

    /**
     * Tests getting mime types for table
     */
    public function testGetMime(): void
    {
        $_SESSION['relation'] = [];
        $_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
            'db' => 'pmadb',
            'mimework' => true,
            'trackingwork' => true,
            'column_info' => 'column_info',
        ])->toArray();
        self::assertSame([
            'o' => [
                'column_name' => 'o',
                'mimetype' => 'Text/plain',
                'transformation' => 'Sql',
                'transformation_options' => '',
                'input_transformation' => 'regex',
                'input_transformation_options' => '/pma/i',
            ],
            'col' => [
                'column_name' => 'col',
                'mimetype' => 'T',
                'transformation' => 'O/P',
                'transformation_options' => '',
                'input_transformation' => 'i/p',
                'input_transformation_options' => '',
            ],
        ], $this->transformations->getMime('pma_test', 'table1'));
    }

    /**
     * Test for clear
     */
    public function testClear(): void
    {
        // Mock dbi
        $dbi = $this->getMockBuilder(DatabaseInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $dbi->expects($this->any())
            ->method('tryQuery')
            ->willReturn(true);
        $GLOBALS['dbi'] = $dbi;

        // Case 1 : no configuration storage
        $actual = $this->transformations->clear('db');
        self::assertFalse($actual);

        $_SESSION['relation'] = [];
        $_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
            'db' => 'pmadb',
            'mimework' => true,
            'column_info' => 'column_info',
        ])->toArray();

        // Case 2 : database delete
        $actual = $this->transformations->clear('db');
        self::assertTrue($actual);

        // Case 3 : table delete
        $actual = $this->transformations->clear('db', 'table');
        self::assertTrue($actual);

        // Case 4 : column delete
        $actual = $this->transformations->clear('db', 'table', 'col');
        self::assertTrue($actual);
    }

    /**
     * @param string $value    value
     * @param string $expected expected result
     *
     * @dataProvider fixupData
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('fixupData')]
    public function testFixup(string $value, string $expected): void
    {
        self::assertSame($expected, $this->transformations->fixUpMime($value));
    }

    public static function fixupData(): array
    {
        return [
            [
                'text_plain_bool2text.php',
                'Text_Plain_Bool2Text.php',
            ],
            [
                'application_octetstream_download.php',
                'Application_Octetstream_Download.php',
            ],
            [
                'text_plain_json.php',
                'Text_Plain_Json.php',
            ],
            [
                'image_jpeg_link.php',
                'Image_JPEG_Link.php',
            ],
            [
                'text_plain_dateformat.php',
                'Text_Plain_Dateformat.php',
            ],
        ];
    }

    /**
     * Test for getDescription
     *
     * @param string $file                transformation file
     * @param string $expectedDescription expected description
     *
     * @dataProvider providerGetDescription
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('providerGetDescription')]
    public function testGetDescription(string $file, string $expectedDescription): void
    {
        self::assertSame($expectedDescription, $this->transformations->getDescription($file));
    }

    public static function providerGetDescription(): array
    {
        return [
            [
                '../../../../test',
                '',
            ],
            [
                'Input/Text_Plain_SqlEditor',
                'Syntax highlighted CodeMirror editor for SQL.',
            ],
            [
                'Output/Text_Plain_Sql',
                'Formats text as SQL query with syntax highlighting.',
            ],
        ];
    }

    /**
     * Test for getName
     *
     * @param string $file         transformation file
     * @param string $expectedName expected name
     *
     * @dataProvider providerGetName
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('providerGetName')]
    public function testGetName(string $file, string $expectedName): void
    {
        self::assertSame($expectedName, $this->transformations->getName($file));
    }

    public static function providerGetName(): array
    {
        return [
            [
                '../../../../test',
                '',
            ],
            [
                'Input/Text_Plain_SqlEditor',
                'SQL',
            ],
            [
                'Output/Text_Plain_Sql',
                'SQL',
            ],
        ];
    }
}