Translated using Weblate (Portuguese)
[phpmyadmin.git] / tests / unit / Plugins / Export / ExportJsonTest.php
blobc558e45baed7285780c75efe9bec7e9a7acd2d4b
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Plugins\Export;
7 use PhpMyAdmin\ConfigStorage\Relation;
8 use PhpMyAdmin\Dbal\DatabaseInterface;
9 use PhpMyAdmin\Export\Export;
10 use PhpMyAdmin\Plugins\Export\ExportJson;
11 use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
12 use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
13 use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
14 use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
15 use PhpMyAdmin\Tests\AbstractTestCase;
16 use PhpMyAdmin\Transformations;
17 use PhpMyAdmin\Version;
18 use PHPUnit\Framework\Attributes\CoversClass;
19 use PHPUnit\Framework\Attributes\Medium;
20 use ReflectionMethod;
21 use ReflectionProperty;
23 #[CoversClass(ExportJson::class)]
24 #[Medium]
25 class ExportJsonTest extends AbstractTestCase
27 protected ExportJson $object;
29 /**
30 * Configures global environment.
32 protected function setUp(): void
34 parent::setUp();
36 $dbi = $this->createDatabaseInterface();
37 DatabaseInterface::$instance = $dbi;
38 Export::$outputKanjiConversion = false;
39 Export::$outputCharsetConversion = false;
40 Export::$bufferNeeded = false;
41 Export::$asFile = true;
42 Export::$saveOnServer = false;
43 $this->object = new ExportJson(
44 new Relation($dbi),
45 new Export($dbi),
46 new Transformations(),
50 /**
51 * tearDown for test cases
53 protected function tearDown(): void
55 parent::tearDown();
57 unset($this->object);
60 public function testSetProperties(): void
62 $method = new ReflectionMethod(ExportJson::class, 'setProperties');
63 $method->invoke($this->object, null);
65 $attrProperties = new ReflectionProperty(ExportJson::class, 'properties');
66 $properties = $attrProperties->getValue($this->object);
68 self::assertInstanceOf(ExportPluginProperties::class, $properties);
70 self::assertSame(
71 'JSON',
72 $properties->getText(),
75 self::assertSame(
76 'json',
77 $properties->getExtension(),
80 self::assertSame(
81 'application/json',
82 $properties->getMimeType(),
85 self::assertSame(
86 'Options',
87 $properties->getOptionsText(),
90 $options = $properties->getOptions();
92 self::assertInstanceOf(OptionsPropertyRootGroup::class, $options);
94 self::assertSame(
95 'Format Specific Options',
96 $options->getName(),
99 $generalOptionsArray = $options->getProperties();
100 $generalOptions = $generalOptionsArray->current();
102 self::assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
104 self::assertSame(
105 'general_opts',
106 $generalOptions->getName(),
109 $generalProperties = $generalOptions->getProperties();
111 $property = $generalProperties->current();
113 self::assertInstanceOf(HiddenPropertyItem::class, $property);
115 self::assertSame(
116 'structure_or_data',
117 $property->getName(),
121 public function testExportHeader(): void
123 $this->expectOutputString(
124 "[\n"
125 . '{"type":"header","version":"' . Version::VERSION
126 . '","comment":"Export to JSON plugin for PHPMyAdmin"},'
127 . "\n",
130 self::assertTrue(
131 $this->object->exportHeader(),
135 public function testExportFooter(): void
137 $this->expectOutputString(']' . "\n");
139 self::assertTrue(
140 $this->object->exportFooter(),
144 public function testExportDBHeader(): void
146 $this->expectOutputString('{"type":"database","name":"testDB"},' . "\n");
148 self::assertTrue(
149 $this->object->exportDBHeader('testDB'),
153 public function testExportDBFooter(): void
155 self::assertTrue(
156 $this->object->exportDBFooter('testDB'),
160 public function testExportDBCreate(): void
162 self::assertTrue(
163 $this->object->exportDBCreate('testDB'),
167 public function testExportData(): void
169 $this->expectOutputString(
170 '{"type":"table","name":"test_table","database":"test_db","data":' . "\n"
171 . '[' . "\n"
172 . '{"id":"1","name":"abcd","datetimefield":"2011-01-20 02:00:02"},' . "\n"
173 . '{"id":"2","name":"foo","datetimefield":"2010-01-20 02:00:02"},' . "\n"
174 . '{"id":"3","name":"Abcd","datetimefield":"2012-01-20 02:00:02"}' . "\n"
175 . ']' . "\n"
176 . '}' . "\n",
179 self::assertTrue($this->object->exportData(
180 'test_db',
181 'test_table',
182 'SELECT * FROM `test_db`.`test_table`;',
186 public function testExportComplexData(): void
188 // normalString binaryField textField blobField
189 $this->expectOutputString(
190 '{"type":"table","name":"test_table_complex","database":"test_db","data":'
191 . "\n[\n"
192 . '{"f1":"\"\'\"><iframe onload=alert(1)>\u0448\u0435\u043b\u043b\u044b",'
193 . '"f2":"0x3078313233343638353766656665",'
194 . '"f3":"My awesome\nText","f4":"0x307861663132333466363863353766656665"},' . "\n"
195 . '{"f1":null,"f2":null,"f3":null,"f4":null},' . "\n"
196 . '{"f1":"","f2":"0x307831","f3":"\u0448\u0435\u043b\u043b\u044b","f4":"0x307832"}' . "\n"
197 . "]\n}\n",
200 self::assertTrue(
201 $this->object->exportData(
202 'test_db',
203 'test_table_complex',
204 'SELECT * FROM `test_db`.`test_table_complex`;',
209 public function testExportRawComplexData(): void
211 $this->expectOutputString(
212 '{"type":"raw","data":'
213 . "\n[\n"
214 . '{"f1":"\"\'\"><iframe onload=alert(1)>\u0448\u0435\u043b\u043b\u044b",'
215 . '"f2":"0x3078313233343638353766656665",'
216 . '"f3":"My awesome\nText","f4":"0x307861663132333466363863353766656665"},' . "\n"
217 . '{"f1":null,"f2":null,"f3":null,"f4":null},' . "\n"
218 . '{"f1":"","f2":"0x307831","f3":"\u0448\u0435\u043b\u043b\u044b","f4":"0x307832"}' . "\n"
219 . "]\n}\n",
222 self::assertTrue(
223 $this->object->exportRawQuery(
224 null,
225 'SELECT * FROM `test_db`.`test_table_complex`;',