File: ImportOdsTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.2-really%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, 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 (248 lines) | stat: -rw-r--r-- 10,651 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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Plugins\Import;

use PhpMyAdmin\File;
use PhpMyAdmin\Plugins\Import\ImportOds;
use PhpMyAdmin\Tests\AbstractTestCase;

use function __;
use function str_repeat;

/**
 * @covers \PhpMyAdmin\Plugins\Import\ImportOds
 * @requires extension zip
 */
#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('zip')]
class ImportOdsTest extends AbstractTestCase
{
    /** @var ImportOds */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp(): void
    {
        parent::setUp();
        $GLOBALS['server'] = 0;
        $GLOBALS['plugin_param'] = 'csv';
        $this->object = new ImportOds();

        //setting
        $GLOBALS['finished'] = false;
        $GLOBALS['read_limit'] = 100000000;
        $GLOBALS['offset'] = 0;
        $GLOBALS['cfg']['Server']['DisableIS'] = false;

        /**
         * Load interface for zip extension.
        */
        $GLOBALS['read_multiply'] = 10;
        $GLOBALS['import_type'] = 'ods';

        //variable for Ods
        $_REQUEST['ods_recognize_percentages'] = true;
        $_REQUEST['ods_recognize_currency'] = true;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown(): void
    {
        parent::tearDown();
        unset($this->object);
    }

    /**
     * Test for getProperties
     *
     * @group medium
     */
    #[\PHPUnit\Framework\Attributes\Group('medium-group')]
    public function testGetProperties(): void
    {
        $properties = $this->object->getProperties();
        self::assertSame(__('OpenDocument Spreadsheet'), $properties->getText());
        self::assertSame('ods', $properties->getExtension());
        self::assertSame(__('Options'), $properties->getOptionsText());
    }

    /**
     * Test for doImport
     *
     * @group medium
     */
    #[\PHPUnit\Framework\Attributes\Group('medium-group')]
    public function testDoImport(): void
    {
        //$sql_query_disabled will show the import SQL detail
        //$import_notice will show the import detail result
        global $import_notice, $sql_query, $sql_query_disabled;
        $sql_query_disabled = false;

        $GLOBALS['import_file'] = 'test/test_data/db_test.ods';
        $_REQUEST['ods_empty_rows'] = true;

        parent::setGlobalDbi();

        $importHandle = new File($GLOBALS['import_file']);
        $importHandle->setDecompressContent(true);
        $importHandle->open();

        //Test function called
        $this->object->doImport($importHandle);

        self::assertStringContainsString(
            'CREATE DATABASE IF NOT EXISTS `ODS_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci',
            $sql_query
        );
        self::assertStringContainsString('CREATE TABLE IF NOT EXISTS `ODS_DB`.`pma_bookmark`', $sql_query);
        self::assertStringContainsString(
            'INSERT INTO `ODS_DB`.`pma_bookmark` (`A`, `B`, `C`, `D`) VALUES (1, \'dbbase\', NULL, \'ddd\');',
            $sql_query
        );

        //asset that all databases and tables are imported
        self::assertStringContainsString(
            'The following structures have either been created or altered.',
            $import_notice
        );
        self::assertStringContainsString('Go to database: `ODS_DB`', $import_notice);
        self::assertStringContainsString('Edit settings for `ODS_DB`', $import_notice);
        self::assertStringContainsString('Go to table: `pma_bookmark`', $import_notice);
        self::assertStringContainsString('Edit settings for `pma_bookmark`', $import_notice);

        //asset that the import process is finished
        self::assertTrue($GLOBALS['finished']);
    }

    public static function dataProviderOdsEmptyRows(): array
    {
        return [
            'remove empty columns' => [true],
            'keep empty columns' => [false],
        ];
    }

    /**
     * Test for doImport using second dataset
     *
     * @group medium
     * @dataProvider dataProviderOdsEmptyRows
     * @requires extension simplexml
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderOdsEmptyRows')]
    #[\PHPUnit\Framework\Attributes\RequiresPhpExtension('simplexml')]
    public function testDoImportDataset2(bool $odsEmptyRowsMode): void
    {
        //$sql_query_disabled will show the import SQL detail
        //$import_notice will show the import detail result
        global $import_notice, $sql_query, $sql_query_disabled;
        $sql_query_disabled = false;

        $GLOBALS['import_file'] = 'test/test_data/import-slim.ods.xml';
        $_REQUEST['ods_col_names'] = true;
        $_REQUEST['ods_empty_rows'] = $odsEmptyRowsMode;

        parent::setGlobalDbi();

        $importHandle = new File($GLOBALS['import_file']);
        $importHandle->setDecompressContent(false);// Not compressed
        $importHandle->open();

        // The process could probably detect that all the values for columns V to BL are empty
        // That would make the empty columns not needed and would create a cleaner structure

        $endOfSql = ');;';

        if (! $odsEmptyRowsMode) {
            $fullCols = 'NULL' . str_repeat(', NULL', 18);// 19 empty cells
            $endOfSql = '),' . "\n" . ' (' . $fullCols . '),' . "\n" . ' (' . $fullCols . ');;';
        }

        //Test function called
        $this->object->doImport($importHandle);

        $lengths = mb_strlen("Stück") === 5 ? [
            'Name' => 41,
            'keywords' => 15,
            'verkuerztHaltbar' => 21,
            'Gebinde' => 71,
        ] : [
            'Name' => 42,
            'keywords' => 17,
            'verkuerztHaltbar' => 22,
            'Gebinde' => 72,
        ];

        self::assertSame('CREATE DATABASE IF NOT EXISTS `ODS_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
        . 'CREATE TABLE IF NOT EXISTS `ODS_DB`.`Shop` ('
        . '`Artikelnummer` varchar(7), `Name` varchar(' . $lengths['Name'] . '), `keywords` varchar(' . $lengths['keywords'] . '), `EK_Preis` varchar(21),'
        . ' `Preis` varchar(23), `Details` varchar(10), `addInfo` varchar(22), `Einheit` varchar(3),'
        . ' `Wirkstoff` varchar(10), `verkuerztHaltbar` varchar(' . $lengths['verkuerztHaltbar'] . '), `kuehlkette` varchar(7),'
        . ' `Gebinde` varchar(' . $lengths['Gebinde'] . '), `Verbrauchsnachweis` varchar(7), `Genehmigungspflichtig` varchar(7),'
        . ' `Gefahrstoff` varchar(11), `GefahrArbeitsbereich` varchar(14), `Verwendungszweck` varchar(10),'
        . ' `Verbrauch` varchar(10), `showLagerbestand` varchar(7));;'
        . 'CREATE TABLE IF NOT EXISTS `ODS_DB`.`Feuille 1` (`value` varchar(19));;'
        . 'INSERT INTO `ODS_DB`.`Shop` ('
        . '`Artikelnummer`, `Name`, `keywords`, `EK_Preis`, `Preis`, `Details`, `addInfo`, `Einheit`,'
        . ' `Wirkstoff`, `verkuerztHaltbar`, `kuehlkette`, `Gebinde`, `Verbrauchsnachweis`,'
        . ' `Genehmigungspflichtig`, `Gefahrstoff`, `GefahrArbeitsbereich`, `Verwendungszweck`,'
        . ' `Verbrauch`, `showLagerbestand`) VALUES ('
        . 'NULL, NULL, \'Schlüsselwörter\', \'Einkaufspreis (Netto)\', \'VK-Preis (Orientierung)\', NULL,'
        . ' \'Hintergrundinformation\', \'VPE\', NULL, \'verkürzte Haltbarkeit\', \'ja/nein\','
        . ' \'Stück,Rolle,Pack,Flasche,Sack,Eimer,Karton,Palette,Beutel,Kanister,Paar\', \'ja/nein\','
        . ' \'ja/nein\', \'GHS01-GHS09\', \'Arbeitsbereich\', NULL, NULL, \'ja/nein\'),' . "\n"
        . ' (\'1005\', \'Beatmungsfilter\', NULL, \'0.85\', \'1,2\', NULL, NULL, \'5\', NULL, NULL, \'nein\','
        . ' \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'04-3-06\', \'Absaugkatheter, CH06 grün\', NULL, \'0.13\', \'0,13\', NULL, NULL, \'1\','
        . ' NULL, NULL,'
        . ' NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'04-3-10\', \'Absaugkatheter, CH10 schwarz\', NULL, \'0.13\', \'0,13\', NULL, NULL, \'1\','
        . ' NULL, NULL, NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'04-3-18\', \'Absaugkatheter, CH18 rot\', NULL, \'0.13\', \'0,13\', NULL, NULL, \'1\','
        . ' NULL, NULL, NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'06-38\', \'Bakterienfilter\', NULL, \'1.25\', \'1,25\', NULL, NULL, \'1\', NULL, NULL, NULL,'
        . ' \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'05-453\', \'Blockerspritze für Larynxtubus, Erwachsen\', NULL, \'2.6\', \'2,6\', NULL, NULL,'
        . ' \'1\', NULL, NULL, NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'04-402\', \'Absaugschlauch mit Fingertip für Accuvac\', NULL, \'1.7\', \'1,7\', NULL, NULL,'
        . ' \'1\', NULL, NULL, NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\'),' . "\n"
        . ' (\'02-580\', \'Einmalbeatmungsbeutel, Erwachsen\', NULL, \'8.9\', \'8,9\', NULL, NULL,'
        . ' \'1\', NULL, NULL, NULL, \'Stück\', \'nein\', \'nein\', NULL, NULL, NULL, NULL, \'ja\''
         . $endOfSql
         . 'INSERT INTO `ODS_DB`.`Feuille 1` (`value`) VALUES ('
         . '\'test@example.org\'),' . "\n"
         . ' (\'123 45\'),' . "\n"
         . ' (\'123 \'),' . "\n"
         . ' (\'test@example.fr\'),' . "\n"
         . ' (\'https://example.org\'),' . "\n"
         . ' (\'example.txt\'),' . "\n"
         . ' (\'\\\'Feuille 1\\\'!A1:A4\'),' . "\n"
         . ' (\'1,50\'),' . "\n"
         . ' (\'0.05\'),' . "\n"
         . ' (\'true\'),' . "\n"
         . ' (\'12\')'
         . ($odsEmptyRowsMode ? '' : ',' . "\n" . ' (NULL)')
         . ($odsEmptyRowsMode ? ';;' : ',' . "\n" . ' (NULL);;'), $sql_query);

        //asset that all databases and tables are imported
        self::assertStringContainsString(
            'The following structures have either been created or altered.',
            $import_notice
        );
        self::assertStringContainsString('Go to database: `ODS_DB`', $import_notice);
        self::assertStringContainsString('Edit settings for `ODS_DB`', $import_notice);
        self::assertStringContainsString('Go to table: `Shop`', $import_notice);
        self::assertStringContainsString('Edit settings for `Shop`', $import_notice);

        //asset that the import process is finished
        self::assertTrue($GLOBALS['finished']);
    }
}