Replace text_dir global var with LanguageManager::$textDir static
[phpmyadmin.git] / tests / classes / StorageEngineTest.php
blobd04efac4387d32c8d655c5ae0740a86c63485759
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Cache;
8 use PhpMyAdmin\DatabaseInterface;
9 use PhpMyAdmin\Engines\Bdb;
10 use PhpMyAdmin\Engines\Berkeleydb;
11 use PhpMyAdmin\Engines\Binlog;
12 use PhpMyAdmin\Engines\Innobase;
13 use PhpMyAdmin\Engines\Innodb;
14 use PhpMyAdmin\Engines\Memory;
15 use PhpMyAdmin\Engines\Merge;
16 use PhpMyAdmin\Engines\MrgMyisam;
17 use PhpMyAdmin\Engines\Myisam;
18 use PhpMyAdmin\Engines\Ndbcluster;
19 use PhpMyAdmin\Engines\Pbxt;
20 use PhpMyAdmin\Engines\PerformanceSchema;
21 use PhpMyAdmin\StorageEngine;
22 use PhpMyAdmin\Tests\Stubs\DbiDummy;
23 use PHPUnit\Framework\Attributes\CoversClass;
24 use PHPUnit\Framework\Attributes\DataProvider;
25 use PHPUnit\Framework\Attributes\PreserveGlobalState;
26 use PHPUnit\Framework\Attributes\RunInSeparateProcess;
27 use PHPUnit\Framework\MockObject\MockObject;
29 use function json_encode;
31 #[CoversClass(StorageEngine::class)]
32 class StorageEngineTest extends AbstractTestCase
34 protected DatabaseInterface $dbi;
36 protected DbiDummy $dummyDbi;
38 /** @var StorageEngine&MockObject */
39 protected StorageEngine $object;
41 /**
42 * Sets up the fixture, for example, opens a network connection.
43 * This method is called before a test is executed.
45 protected function setUp(): void
47 parent::setUp();
49 $this->dummyDbi = $this->createDbiDummy();
50 $this->dbi = $this->createDatabaseInterface($this->dummyDbi);
51 DatabaseInterface::$instance = $this->dbi;
52 $this->object = $this->getMockForAbstractClass(
53 StorageEngine::class,
54 ['dummy'],
58 /**
59 * Tears down the fixture, for example, closes a network connection.
60 * This method is called after a test is executed.
62 protected function tearDown(): void
64 parent::tearDown();
66 unset($this->object);
69 #[PreserveGlobalState(false)]
70 #[RunInSeparateProcess]
71 public function testGetStorageEngines(): void
73 $this->assertEquals(
75 'dummy' => ['Engine' => 'dummy', 'Support' => 'YES', 'Comment' => 'dummy comment'],
76 'dummy2' => ['Engine' => 'dummy2', 'Support' => 'NO', 'Comment' => 'dummy2 comment'],
77 'FEDERATED' => [
78 'Engine' => 'FEDERATED',
79 'Support' => 'NO',
80 'Comment' => 'Federated MySQL storage engine',
82 'Pbxt' => ['Engine' => 'Pbxt', 'Support' => 'NO', 'Comment' => 'Pbxt storage engine'],
84 $this->object->getStorageEngines(),
88 public function testGetArray(): void
90 $actual = $this->object->getArray();
92 $this->assertEquals(
93 ['dummy' => ['name' => 'dummy', 'comment' => 'dummy comment', 'is_default' => false]],
94 $actual,
98 /**
99 * Test for StorageEngine::getEngine
101 * @param string $expectedClass Class that should be selected
102 * @param string $engineName Engine name
103 * @psalm-param class-string $expectedClass
105 #[DataProvider('providerGetEngine')]
106 public function testGetEngine(string $expectedClass, string $engineName): void
108 $actual = StorageEngine::getEngine($engineName);
109 $this->assertInstanceOf($expectedClass, $actual);
113 * Provider for testGetEngine
115 * @return mixed[]
117 public static function providerGetEngine(): array
119 return [
120 [StorageEngine::class, 'unknown engine'],
121 [Bdb::class, 'Bdb'],
122 [Berkeleydb::class, 'Berkeleydb'],
123 [Binlog::class, 'Binlog'],
124 [Innobase::class, 'Innobase'],
125 [Innodb::class, 'Innodb'],
126 [Memory::class, 'Memory'],
127 [Merge::class, 'Merge'],
128 [MrgMyisam::class, 'Mrg_Myisam'],
129 [Myisam::class, 'Myisam'],
130 [Ndbcluster::class, 'Ndbcluster'],
131 [Pbxt::class, 'Pbxt'],
132 [PerformanceSchema::class, 'Performance_Schema'],
137 * Test for isValid
139 public function testIsValid(): void
141 $this->assertTrue(
142 $this->object->isValid('PBMS'),
144 $this->assertTrue(
145 $this->object->isValid('dummy'),
147 $this->assertTrue(
148 $this->object->isValid('dummy2'),
150 $this->assertFalse(
151 $this->object->isValid('invalid'),
156 * Test for getPage
158 public function testGetPage(): void
160 $this->assertEquals(
162 $this->object->getPage('Foo'),
167 * Test for getInfoPages
169 public function testGetInfoPages(): void
171 $this->assertEquals(
173 $this->object->getInfoPages(),
178 * Test for getVariablesLikePattern
180 public function testGetVariablesLikePattern(): void
182 $this->assertEquals(
184 $this->object->getVariablesLikePattern(),
189 * Test for getMysqlHelpPage
191 public function testGetMysqlHelpPage(): void
193 $this->assertEquals(
194 'dummy-storage-engine',
195 $this->object->getMysqlHelpPage(),
200 * Test for getVariables
202 public function testGetVariables(): void
204 $this->assertEquals(
206 $this->object->getVariables(),
211 * Test for getSupportInformationMessage
213 public function testGetSupportInformationMessage(): void
215 $this->assertEquals(
216 'dummy is available on this MySQL server.',
217 $this->object->getSupportInformationMessage(),
220 $this->object->support = 1;
221 $this->assertEquals(
222 'dummy has been disabled for this MySQL server.',
223 $this->object->getSupportInformationMessage(),
226 $this->object->support = 2;
227 $this->assertEquals(
228 'dummy is available on this MySQL server.',
229 $this->object->getSupportInformationMessage(),
232 $this->object->support = 3;
233 $this->assertEquals(
234 'dummy is the default storage engine on this MySQL server.',
235 $this->object->getSupportInformationMessage(),
240 * Test for getComment
242 public function testGetComment(): void
244 $this->assertEquals(
245 'dummy comment',
246 $this->object->getComment(),
251 * Test for getTitle
253 public function testGetTitle(): void
255 $this->assertEquals(
256 'dummy',
257 $this->object->getTitle(),
262 * Test for resolveTypeSize
264 public function testResolveTypeSize(): void
266 $this->assertEquals(
267 [0 => 12, 1 => 'B'],
268 $this->object->resolveTypeSize(12),
272 public function testHasMroongaEngine(): void
274 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', [
276 (string) json_encode([]), // Fake result
279 $this->assertTrue(StorageEngine::hasMroongaEngine());
280 $this->assertTrue(StorageEngine::hasMroongaEngine()); // Does not call any query
282 Cache::remove('storage-engine.mroonga.has.mroonga_command'); // Cache clear
284 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', false);
285 $this->assertFalse(StorageEngine::hasMroongaEngine());
287 $this->dummyDbi->assertAllQueriesConsumed();
290 public function testGetMroongaLengths(): void
292 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', [
294 // Partial
295 (string) json_encode([
296 'WGS84GeoPoint' => [
297 'id' => 18,
298 'name' => 'WGS84GeoPoint',
299 'opened' => true,
300 'n_elements' => 4,
301 'type' => ['id' => 32, 'name' => 'type'],
302 'flags' => ['value' => 24, 'names' => 'KEY_GEO_POINT'],
303 'path' => null,
304 'size' => 8,
306 'mroonga_operations' => ['type' => ['id' => 51, 'name' => 'table:no_key']],
307 'mroonga_operations.type' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
308 'mroonga_operations.table' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
309 'mroonga_operations.record' => ['type' => ['id' => 64, 'name' => 'column:fix_size']],
310 'idx_correo' => ['type' => ['id' => 49, 'name' => 'table:pat_key']],
311 'idx_correo.id' => ['type' => ['id' => 64, 'name' => 'column:fix_size']],
312 'idx_correo.search' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
313 'idx_correo#idx_correo_search' => ['type' => ['id' => 49, 'name' => 'table:pat_key']],
314 'idx_correo#idx_correo_search.index' => ['type' => ['id' => 72, 'name' => 'column:index']],
318 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo\');', [
320 // Partial
321 (string) json_encode([
322 'id' => 265,
323 'name' => 'idx_correo',
324 'type' => ['id' => 49, 'name' => 'table:pat_key'],
325 'key' => [
326 'type' => ['id' => 8, 'name' => 'Int32', 'type' => ['id' => 32,'name' => 'type'], 'size' => 4],
327 'total_size' => 0,
328 'max_total_size' => 4294967294,
330 'value' => ['type' => null],
331 'n_records' => 0,
332 'disk_usage' => 4243456,
336 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo.id\');', [
338 // Full object
339 (string) json_encode([
340 'id' => 266,
341 'name' => 'id',
342 'table' => [
343 'id' => 265,
344 'name' => 'idx_correo',
345 'type' => ['id' => 49, 'name' => 'table:pat_key'],
346 'key' => [
347 'type' => [
348 'id' => 8,
349 'name' => 'Int32',
350 'type' => ['id' => 32, 'name' => 'type'],
351 'size' => 4,
353 'total_size' => 0,
354 'max_total_size' => 4294967294,
356 'value' => ['type' => null],
357 'n_records' => 0,
358 'disk_usage' => 4243456,
360 'full_name' => 'idx_correo.id',
361 'type' => ['name' => 'scalar', 'raw' => ['id' => 64, 'name' => 'column:fix_size']],
362 'value' => [
363 'type' => ['id' => 8, 'name' => 'Int32', 'type' => ['id' => 32,'name' => 'type'], 'size' => 4],
364 'compress' => null,
366 'disk_usage' => 4096,
370 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo.search\');', [
372 // Full object
373 (string) json_encode([
374 'id' => 267,
375 'name' => 'search',
376 'table' => [
377 'id' => 265,
378 'name' => 'idx_correo',
379 'type' => ['id' => 49, 'name' => 'table:pat_key'],
380 'key' => [
381 'type' => [
382 'id' => 8,
383 'name' => 'Int32',
384 'type' => ['id' => 32, 'name' => 'type'],
385 'size' => 4,
387 'total_size' => 0,
388 'max_total_size' => 4294967294,
390 'value' => ['type' => null],
391 'n_records' => 0,
392 'disk_usage' => 4243456,
394 'full_name' => 'idx_correo.search',
395 'type' => ['name' => 'scalar', 'raw' => ['id' => 65, 'name' => 'column:var_size']],
396 'value' => [
397 'type' => [
398 'id' => 16,
399 'name' => 'LongText',
400 'type' => ['id' => 32, 'name' => 'type'],
401 'size' => 2147483648,
403 'compress' => null,
405 'disk_usage' => 274432,
409 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo#idx_correo_search\');', [
411 // Partial
412 (string) json_encode([
413 'id' => 268,
414 'name' => 'idx_correo#idx_correo_search',
415 'type' => ['id' => 49, 'name' => 'table:pat_key'],
416 'key' => [
417 'type' => [
418 'id' => 14,
419 'name' => 'ShortText',
420 'type' => ['id' => 32, 'name' => 'type'],
421 'size' => 4096,
423 'total_size' => 0,
424 'max_total_size' => 4294967294,
426 'value' => ['type' => null],
427 'n_records' => 0,
428 'disk_usage' => 12878,
432 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo#idx_correo_search.index\');', [
434 // Full object
435 (string) json_encode([
436 'id' => 269,
437 'name' => 'index',
438 'table' => [
439 'id' => 268,
440 'name' => 'idx_correo#idx_correo_search',
441 'type' => ['id' => 49, 'name' => 'table:pat_key'],
442 'key' => [
443 'type' => [
444 'id' => 14,
445 'name' => 'ShortText',
446 'type' => ['id' => 32, 'name' => 'type'],
447 'size' => 4096,
449 'total_size' => 0,
450 'max_total_size' => 4294967294,
452 'value' => ['type' => null],
453 'n_records' => 0,
454 'disk_usage' => 4243456,
456 'full_name' => 'idx_correo#idx_correo_search.index',
457 'type' => ['name' => 'index', 'raw' => ['id' => 72, 'name' => 'column:index']],
458 'value' => [
459 'type' => [
460 'id' => 265,
461 'name' => 'idx_correo',
462 'type' => ['id' => 49, 'name' => 'table:pat_key'],
463 'size' => 4,
465 'section' => false,
466 'weight' => false,
467 'position' => true,
468 'size' => 'normal',
469 'statistics' => [
470 'max_section_id' => 0,
471 'n_garbage_segments' => 0,
472 'max_array_segment_id' => 0,
473 'n_array_segments' => 0,
474 'max_buffer_segment_id' => 0,
475 'n_buffer_segments' => 0,
476 'max_in_use_physical_segment_id' => 0,
477 'n_unmanaged_segments' => 0,
478 'total_chunk_size' => 0,
479 'max_in_use_chunk_id' => 0,
480 'n_garbage_chunks' => [
481 0 => 0,
482 1 => 0,
483 2 => 0,
484 3 => 0,
485 4 => 0,
486 5 => 0,
487 6 => 0,
488 7 => 0,
489 8 => 0,
490 9 => 0,
491 10 => 0,
492 11 => 0,
493 12 => 0,
494 13 => 0,
495 14 => 0,
499 'sources' => [
500 0 => [
501 'id' => 267,
502 'name' => 'search',
503 'table' => [
504 'id' => 265,
505 'name' => 'idx_correo',
506 'type' => ['id' => 49, 'name' => 'table:pat_key'],
507 'key' => [
508 'type' => [
509 'id' => 8,
510 'name' => 'Int32',
511 'type' => ['id' => 32, 'name' => 'type'],
512 'size' => 4,
514 'total_size' => 0,
515 'max_total_size' => 4294967294,
517 'value' => ['type' => null],
518 'n_records' => 0,
519 'disk_usage' => 4243456,
521 'full_name' => 'idx_correo.search',
524 'disk_usage' => 565248,
529 $this->dummyDbi->addSelectDb('my_db');
530 $lengths = StorageEngine::getMroongaLengths('my_db', 'idx_correo');
531 $this->dummyDbi->assertAllSelectsConsumed();
532 $this->assertSame([4521984, 578126], $lengths);
534 $this->dummyDbi->assertAllQueriesConsumed();