Extract FK related methods from Util class
Creates the PhpMyAdmin\Utils\ForeignKey class. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
7359d45b6e
commit
caa3337411
@ -18,6 +18,7 @@ use PhpMyAdmin\Response;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function intval;
|
||||
|
||||
@ -127,7 +128,7 @@ final class ImportController extends AbstractController
|
||||
'offset' => $offset,
|
||||
'can_convert_kanji' => Encoding::canConvertKanji(),
|
||||
'charsets' => $charsets,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
'user_upload_dir' => Util::userDir($cfg['UploadDir'] ?? ''),
|
||||
'local_files' => Import::getLocalFiles($importList),
|
||||
]);
|
||||
|
||||
@ -28,6 +28,7 @@ use PhpMyAdmin\Tracker;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function array_search;
|
||||
use function ceil;
|
||||
@ -1431,7 +1432,7 @@ class StructureController extends AbstractController
|
||||
$this->render('database/structure/drop_form', [
|
||||
'url_params' => $_url_params,
|
||||
'full_query' => $full_query,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -1460,7 +1461,7 @@ class StructureController extends AbstractController
|
||||
$this->render('database/structure/empty_form', [
|
||||
'url_params' => $urlParams,
|
||||
'full_query' => $fullQuery,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -1488,7 +1489,7 @@ class StructureController extends AbstractController
|
||||
return;
|
||||
}
|
||||
|
||||
$default_fk_check_value = Util::handleDisableFKCheckInit();
|
||||
$default_fk_check_value = ForeignKey::handleDisableCheckInit();
|
||||
$sql_query = '';
|
||||
$sql_query_views = '';
|
||||
$selectedCount = count($selected);
|
||||
@ -1537,7 +1538,7 @@ class StructureController extends AbstractController
|
||||
$message = Message::error((string) $this->dbi->getError());
|
||||
}
|
||||
|
||||
Util::handleDisableFKCheckCleanup($default_fk_check_value);
|
||||
ForeignKey::handleDisableCheckCleanup($default_fk_check_value);
|
||||
|
||||
$message = Message::success();
|
||||
|
||||
@ -1565,7 +1566,7 @@ class StructureController extends AbstractController
|
||||
return;
|
||||
}
|
||||
|
||||
$default_fk_check_value = Util::handleDisableFKCheckInit();
|
||||
$default_fk_check_value = ForeignKey::handleDisableCheckInit();
|
||||
|
||||
$sql_query = '';
|
||||
$selectedCount = count($selected);
|
||||
@ -1592,7 +1593,7 @@ class StructureController extends AbstractController
|
||||
$_REQUEST['pos'] = $sql->calculatePosForLastPage($db, $table, $_REQUEST['pos']);
|
||||
}
|
||||
|
||||
Util::handleDisableFKCheckCleanup($default_fk_check_value);
|
||||
ForeignKey::handleDisableCheckCleanup($default_fk_check_value);
|
||||
|
||||
$message = Message::success();
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
use Throwable;
|
||||
|
||||
use function define;
|
||||
@ -650,12 +651,12 @@ final class ImportController extends AbstractController
|
||||
}
|
||||
|
||||
// Do the real import
|
||||
$default_fk_check = Util::handleDisableFKCheckInit();
|
||||
$default_fk_check = ForeignKey::handleDisableCheckInit();
|
||||
try {
|
||||
$import_plugin->doImport($importHandle ?? null, $sql_data);
|
||||
Util::handleDisableFKCheckCleanup($default_fk_check);
|
||||
ForeignKey::handleDisableCheckCleanup($default_fk_check);
|
||||
} catch (Throwable $e) {
|
||||
Util::handleDisableFKCheckCleanup($default_fk_check);
|
||||
ForeignKey::handleDisableCheckCleanup($default_fk_check);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ use PhpMyAdmin\Response;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function intval;
|
||||
|
||||
@ -111,7 +112,7 @@ final class ImportController extends AbstractController
|
||||
'offset' => $offset,
|
||||
'can_convert_kanji' => Encoding::canConvertKanji(),
|
||||
'charsets' => $charsets,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
'user_upload_dir' => Util::userDir($cfg['UploadDir'] ?? ''),
|
||||
'local_files' => Import::getLocalFiles($importList),
|
||||
]);
|
||||
|
||||
@ -17,6 +17,7 @@ use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function htmlentities;
|
||||
use function mb_strpos;
|
||||
@ -318,7 +319,7 @@ class SqlController extends AbstractController
|
||||
|
||||
$this->response->addJSON(
|
||||
'default_fk_check_value',
|
||||
Util::isForeignKeyCheck()
|
||||
ForeignKey::isCheckEnabled()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function is_array;
|
||||
use function sprintf;
|
||||
@ -55,7 +56,7 @@ class DeleteController extends AbstractController
|
||||
);
|
||||
|
||||
if ($mult_btn === __('Yes')) {
|
||||
$default_fk_check_value = Util::handleDisableFKCheckInit();
|
||||
$default_fk_check_value = ForeignKey::handleDisableCheckInit();
|
||||
$sql_query = '';
|
||||
|
||||
foreach ($selected as $row) {
|
||||
@ -77,7 +78,7 @@ class DeleteController extends AbstractController
|
||||
);
|
||||
}
|
||||
|
||||
Util::handleDisableFKCheckCleanup($default_fk_check_value);
|
||||
ForeignKey::handleDisableCheckCleanup($default_fk_check_value);
|
||||
|
||||
$disp_message = __('Your SQL query has been executed successfully.');
|
||||
$disp_query = $sql_query;
|
||||
@ -136,7 +137,7 @@ class DeleteController extends AbstractController
|
||||
'table' => $table,
|
||||
'selected' => $selected,
|
||||
'sql_query' => $sql_query,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ use PhpMyAdmin\Response;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function intval;
|
||||
|
||||
@ -121,7 +122,7 @@ final class ImportController extends AbstractController
|
||||
'offset' => $offset,
|
||||
'can_convert_kanji' => Encoding::canConvertKanji(),
|
||||
'charsets' => $charsets,
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
'user_upload_dir' => Util::userDir($cfg['UploadDir'] ?? ''),
|
||||
'local_files' => Import::getLocalFiles($importList),
|
||||
]);
|
||||
|
||||
@ -13,6 +13,7 @@ use PhpMyAdmin\Response;
|
||||
use PhpMyAdmin\Table;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
@ -86,7 +87,7 @@ final class RelationController extends AbstractController
|
||||
}
|
||||
|
||||
$relationsForeign = [];
|
||||
if (Util::isForeignKeySupported($storageEngine)) {
|
||||
if (ForeignKey::isSupported($storageEngine)) {
|
||||
$relationsForeign = $this->relation->getForeigners(
|
||||
$this->db,
|
||||
$this->table,
|
||||
@ -141,7 +142,7 @@ final class RelationController extends AbstractController
|
||||
|
||||
if (
|
||||
isset($_POST['destination_foreign_db'])
|
||||
&& Util::isForeignKeySupported($storageEngine)
|
||||
&& ForeignKey::isSupported($storageEngine)
|
||||
) {
|
||||
$relationsForeign = $this->relation->getForeigners(
|
||||
$this->db,
|
||||
@ -181,7 +182,7 @@ final class RelationController extends AbstractController
|
||||
// common form
|
||||
$engine = $this->dbi->getTable($this->db, $this->table)->getStorageEngine();
|
||||
$this->render('table/relation/common_form', [
|
||||
'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
|
||||
'is_foreign_key_supported' => ForeignKey::isSupported($engine),
|
||||
'db' => $this->db,
|
||||
'table' => $this->table,
|
||||
'cfg_relation' => $cfgRelation,
|
||||
|
||||
@ -37,6 +37,7 @@ use PhpMyAdmin\Tracker;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
use stdClass;
|
||||
|
||||
use function array_keys;
|
||||
@ -1574,7 +1575,7 @@ class StructureController extends AbstractController
|
||||
|
||||
return $this->template->render('table/structure/display_structure', [
|
||||
'collations' => $collations,
|
||||
'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
|
||||
'is_foreign_key_supported' => ForeignKey::isSupported($engine),
|
||||
'indexes' => Index::getFromTable($this->table, $this->db),
|
||||
'indexes_duplicates' => Index::findDuplicates($this->table, $this->db),
|
||||
'cfg_relation' => $this->relation->getRelationsParam(),
|
||||
@ -1726,7 +1727,7 @@ class StructureController extends AbstractController
|
||||
return $this->template->render('table/structure/display_table_stats', [
|
||||
'db' => $GLOBALS['db'],
|
||||
'table' => $GLOBALS['table'],
|
||||
'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
|
||||
'is_foreign_key_supported' => ForeignKey::isSupported($engine),
|
||||
'cfg_relation' => $this->relation->getRelationsParam(),
|
||||
'showtable' => $showtable,
|
||||
'table_info_num_rows' => $table_info_num_rows,
|
||||
|
||||
@ -10,6 +10,7 @@ use PhpMyAdmin\Query\Generator as QueryGenerator;
|
||||
use PhpMyAdmin\Relation;
|
||||
use PhpMyAdmin\Table;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function count;
|
||||
use function explode;
|
||||
@ -607,8 +608,8 @@ class Common
|
||||
|
||||
// native foreign key
|
||||
if (
|
||||
Util::isForeignKeySupported($type_T1)
|
||||
&& Util::isForeignKeySupported($type_T2)
|
||||
ForeignKey::isSupported($type_T1)
|
||||
&& ForeignKey::isSupported($type_T2)
|
||||
&& $type_T1 == $type_T2
|
||||
) {
|
||||
// relation exists?
|
||||
@ -757,8 +758,8 @@ class Common
|
||||
$type_T2 = mb_strtoupper($tables[$T2]['ENGINE']);
|
||||
|
||||
if (
|
||||
Util::isForeignKeySupported($type_T1)
|
||||
&& Util::isForeignKeySupported($type_T2)
|
||||
ForeignKey::isSupported($type_T1)
|
||||
&& ForeignKey::isSupported($type_T2)
|
||||
&& $type_T1 == $type_T2
|
||||
) {
|
||||
// InnoDB
|
||||
|
||||
@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Database\Designer;
|
||||
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
/**
|
||||
* Common functions for Designer
|
||||
@ -48,7 +48,7 @@ class DesignerTable
|
||||
*/
|
||||
public function supportsForeignkeys(): bool
|
||||
{
|
||||
return Util::isForeignKeySupported($this->tableEngine);
|
||||
return ForeignKey::isSupported($this->tableEngine);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -13,6 +13,7 @@ use PhpMyAdmin\SqlParser\Statements\AlterStatement;
|
||||
use PhpMyAdmin\SqlParser\Statements\DropStatement;
|
||||
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
|
||||
use PhpMyAdmin\SqlParser\Utils\Query;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function array_map;
|
||||
use function array_sum;
|
||||
@ -1756,7 +1757,7 @@ class Sql
|
||||
?string $completeQuery
|
||||
) {
|
||||
// Handle disable/enable foreign key checks
|
||||
$defaultFkCheck = Util::handleDisableFKCheckInit();
|
||||
$defaultFkCheck = ForeignKey::handleDisableCheckInit();
|
||||
|
||||
// Handle remembered sorting order, only for single table query.
|
||||
// Handling is not required when it's a union query
|
||||
@ -1857,7 +1858,7 @@ class Sql
|
||||
}
|
||||
|
||||
// Handle disable/enable foreign key checks
|
||||
Util::handleDisableFKCheckCleanup($defaultFkCheck);
|
||||
ForeignKey::handleDisableCheckCleanup($defaultFkCheck);
|
||||
|
||||
foreach ($warningMessages as $warning) {
|
||||
$message = Message::notice(Message::sanitize($warning));
|
||||
|
||||
@ -14,6 +14,7 @@ declare(strict_types=1);
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use PhpMyAdmin\Html\MySQLDocumentation;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
use function htmlspecialchars;
|
||||
use function sprintf;
|
||||
@ -132,7 +133,7 @@ class SqlQueryForm
|
||||
'display_tab' => $display_tab,
|
||||
'bookmarks' => $bookmarks,
|
||||
'can_convert_kanji' => Encoding::canConvertKanji(),
|
||||
'is_foreign_key_check' => Util::isForeignKeyCheck(),
|
||||
'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ class UtilExtension extends AbstractExtension
|
||||
),
|
||||
new TwigFunction(
|
||||
'is_foreign_key_supported',
|
||||
'PhpMyAdmin\Util::isForeignKeySupported'
|
||||
'PhpMyAdmin\Utils\ForeignKey::isSupported'
|
||||
),
|
||||
new TwigFunction(
|
||||
'link_or_button',
|
||||
|
||||
@ -76,13 +76,11 @@ use function strlen;
|
||||
use function strpos;
|
||||
use function strrev;
|
||||
use function strtolower;
|
||||
use function strtoupper;
|
||||
use function strtr;
|
||||
use function substr;
|
||||
use function time;
|
||||
use function trim;
|
||||
use function uksort;
|
||||
use function version_compare;
|
||||
|
||||
use const ENT_COMPAT;
|
||||
use const ENT_QUOTES;
|
||||
@ -1569,92 +1567,6 @@ class Util
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if this table's engine supports foreign keys
|
||||
*
|
||||
* @param string $engine engine
|
||||
*/
|
||||
public static function isForeignKeySupported($engine): bool
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$engine = strtoupper((string) $engine);
|
||||
if (($engine === 'INNODB') || ($engine === 'PBXT')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($engine === 'NDBCLUSTER' || $engine === 'NDB') {
|
||||
$ndbver = strtolower(
|
||||
$dbi->fetchValue('SELECT @@ndb_version_string')
|
||||
);
|
||||
if (substr($ndbver, 0, 4) === 'ndb-') {
|
||||
$ndbver = substr($ndbver, 4);
|
||||
}
|
||||
|
||||
return version_compare($ndbver, '7.3', '>=');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Foreign key check enabled?
|
||||
*/
|
||||
public static function isForeignKeyCheck(): bool
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'enable') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'disable') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $dbi->getVariable('FOREIGN_KEY_CHECKS') === 'ON';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle foreign key check request
|
||||
*
|
||||
* @return bool Default foreign key checks value
|
||||
*/
|
||||
public static function handleDisableFKCheckInit(): bool
|
||||
{
|
||||
/** @var DatabaseInterface $dbi */
|
||||
global $dbi;
|
||||
|
||||
$defaultFkCheckValue = $dbi->getVariable('FOREIGN_KEY_CHECKS') === 'ON';
|
||||
if (isset($_REQUEST['fk_checks'])) {
|
||||
if (empty($_REQUEST['fk_checks'])) {
|
||||
// Disable foreign key checks
|
||||
$dbi->setVariable('FOREIGN_KEY_CHECKS', 'OFF');
|
||||
} else {
|
||||
// Enable foreign key checks
|
||||
$dbi->setVariable('FOREIGN_KEY_CHECKS', 'ON');
|
||||
}
|
||||
}
|
||||
|
||||
return $defaultFkCheckValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup changes done for foreign key check
|
||||
*
|
||||
* @param bool $defaultFkCheckValue original value for 'FOREIGN_KEY_CHECKS'
|
||||
*/
|
||||
public static function handleDisableFKCheckCleanup(bool $defaultFkCheckValue): void
|
||||
{
|
||||
/** @var DatabaseInterface $dbi */
|
||||
global $dbi;
|
||||
|
||||
$dbi->setVariable(
|
||||
'FOREIGN_KEY_CHECKS',
|
||||
$defaultFkCheckValue ? 'ON' : 'OFF'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts GIS data to Well Known Text format
|
||||
*
|
||||
|
||||
101
libraries/classes/Utils/ForeignKey.php
Normal file
101
libraries/classes/Utils/ForeignKey.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Utils;
|
||||
|
||||
use PhpMyAdmin\DatabaseInterface;
|
||||
|
||||
use function strtolower;
|
||||
use function strtoupper;
|
||||
use function substr;
|
||||
use function version_compare;
|
||||
|
||||
final class ForeignKey
|
||||
{
|
||||
/**
|
||||
* Verifies if this table's engine supports foreign keys
|
||||
*
|
||||
* @param string $engine engine
|
||||
*/
|
||||
public static function isSupported($engine): bool
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$engine = strtoupper((string) $engine);
|
||||
if (($engine === 'INNODB') || ($engine === 'PBXT')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($engine === 'NDBCLUSTER' || $engine === 'NDB') {
|
||||
$ndbver = strtolower(
|
||||
$dbi->fetchValue('SELECT @@ndb_version_string')
|
||||
);
|
||||
if (substr($ndbver, 0, 4) === 'ndb-') {
|
||||
$ndbver = substr($ndbver, 4);
|
||||
}
|
||||
|
||||
return version_compare($ndbver, '7.3', '>=');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Foreign key check enabled?
|
||||
*/
|
||||
public static function isCheckEnabled(): bool
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'enable') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($GLOBALS['cfg']['DefaultForeignKeyChecks'] === 'disable') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $dbi->getVariable('FOREIGN_KEY_CHECKS') === 'ON';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle foreign key check request
|
||||
*
|
||||
* @return bool Default foreign key checks value
|
||||
*/
|
||||
public static function handleDisableCheckInit(): bool
|
||||
{
|
||||
/** @var DatabaseInterface $dbi */
|
||||
global $dbi;
|
||||
|
||||
$defaultCheckValue = $dbi->getVariable('FOREIGN_KEY_CHECKS') === 'ON';
|
||||
if (isset($_REQUEST['fk_checks'])) {
|
||||
if (empty($_REQUEST['fk_checks'])) {
|
||||
// Disable foreign key checks
|
||||
$dbi->setVariable('FOREIGN_KEY_CHECKS', 'OFF');
|
||||
} else {
|
||||
// Enable foreign key checks
|
||||
$dbi->setVariable('FOREIGN_KEY_CHECKS', 'ON');
|
||||
}
|
||||
}
|
||||
|
||||
return $defaultCheckValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup changes done for foreign key check
|
||||
*
|
||||
* @param bool $defaultCheckValue original value for 'FOREIGN_KEY_CHECKS'
|
||||
*/
|
||||
public static function handleDisableCheckCleanup(bool $defaultCheckValue): void
|
||||
{
|
||||
/** @var DatabaseInterface $dbi */
|
||||
global $dbi;
|
||||
|
||||
$dbi->setVariable(
|
||||
'FOREIGN_KEY_CHECKS',
|
||||
$defaultCheckValue ? 'ON' : 'OFF'
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -2659,7 +2659,7 @@
|
||||
<RedundantCast occurrences="1">
|
||||
<code>(string) strftime($string)</code>
|
||||
</RedundantCast>
|
||||
<RedundantCastGivenDocblockType occurrences="9">
|
||||
<RedundantCastGivenDocblockType occurrences="8">
|
||||
<code>(int) $meta->length</code>
|
||||
<code>(int) $timestamp</code>
|
||||
<code>(int) $timestamp</code>
|
||||
@ -2668,12 +2668,16 @@
|
||||
<code>(int) $timestamp</code>
|
||||
<code>(string) $aName</code>
|
||||
<code>(string) $aName</code>
|
||||
<code>(string) $engine</code>
|
||||
</RedundantCastGivenDocblockType>
|
||||
<TypeDoesNotContainType occurrences="1">
|
||||
<code>$ret === false</code>
|
||||
</TypeDoesNotContainType>
|
||||
</file>
|
||||
<file src="libraries/classes/Utils/ForeignKey.php">
|
||||
<RedundantCastGivenDocblockType occurrences="1">
|
||||
<code>(string) $engine</code>
|
||||
</RedundantCastGivenDocblockType>
|
||||
</file>
|
||||
<file src="libraries/classes/Utils/FormatConverter.php">
|
||||
<RedundantCast occurrences="1">
|
||||
<code>(string) long2ip((int) $buffer)</code>
|
||||
|
||||
@ -338,29 +338,6 @@ class UtilTest extends AbstractTestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for isForeignKeyCheck
|
||||
*/
|
||||
public function testIsForeignKeyCheck(): void
|
||||
{
|
||||
$GLOBALS['server'] = 1;
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'enable';
|
||||
$this->assertTrue(
|
||||
Util::isForeignKeyCheck()
|
||||
);
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'disable';
|
||||
$this->assertFalse(
|
||||
Util::isForeignKeyCheck()
|
||||
);
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'default';
|
||||
$this->assertTrue(
|
||||
Util::isForeignKeyCheck()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for getCharsetQueryPart
|
||||
*
|
||||
@ -884,52 +861,6 @@ class UtilTest extends AbstractTestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* foreign key supported test
|
||||
*
|
||||
* @param string $a Engine
|
||||
* @param bool $e Expected Value
|
||||
*
|
||||
* @covers \PhpMyAdmin\Util::isForeignKeySupported
|
||||
* @dataProvider providerIsForeignKeySupported
|
||||
*/
|
||||
public function testIsForeignKeySupported(string $a, bool $e): void
|
||||
{
|
||||
$GLOBALS['server'] = 1;
|
||||
|
||||
$this->assertEquals(
|
||||
$e,
|
||||
Util::isForeignKeySupported($a)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* data provider for foreign key supported test
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerIsForeignKeySupported(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'MyISAM',
|
||||
false,
|
||||
],
|
||||
[
|
||||
'innodb',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'pBxT',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'ndb',
|
||||
true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* format byte test, globals are defined
|
||||
*
|
||||
@ -2238,119 +2169,6 @@ class UtilTest extends AbstractTestCase
|
||||
$GLOBALS['dbi'] = $oldDbi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function providerFkChecks(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'',
|
||||
'OFF',
|
||||
],
|
||||
[
|
||||
'0',
|
||||
'OFF',
|
||||
],
|
||||
[
|
||||
'1',
|
||||
'ON',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerFkChecks
|
||||
*/
|
||||
public function testHandleDisableFKCheckInit(string $fkChecksValue, string $setVariableParam): void
|
||||
{
|
||||
$oldDbi = $GLOBALS['dbi'];
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$_REQUEST['fk_checks'] = $fkChecksValue;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('getVariable')
|
||||
->will($this->returnValue('ON'));
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue(Util::handleDisableFKCheckInit());
|
||||
|
||||
$GLOBALS['dbi'] = $oldDbi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerFkChecks
|
||||
*/
|
||||
public function testHandleDisableFKCheckInitVarFalse(string $fkChecksValue, string $setVariableParam): void
|
||||
{
|
||||
$oldDbi = $GLOBALS['dbi'];
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$_REQUEST['fk_checks'] = $fkChecksValue;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('getVariable')
|
||||
->will($this->returnValue('OFF'));
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertFalse(Util::handleDisableFKCheckInit());
|
||||
|
||||
$GLOBALS['dbi'] = $oldDbi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function providerFkCheckCleanup(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
true,
|
||||
'ON',
|
||||
],
|
||||
[
|
||||
false,
|
||||
'OFF',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerFkCheckCleanup
|
||||
*/
|
||||
public function testHandleDisableFKCheckCleanup(bool $fkChecksValue, string $setVariableParam): void
|
||||
{
|
||||
$oldDbi = $GLOBALS['dbi'];
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
Util::handleDisableFKCheckCleanup($fkChecksValue);
|
||||
|
||||
$GLOBALS['dbi'] = $oldDbi;
|
||||
}
|
||||
|
||||
public function testCurrentUserHasPrivilegeSkipGrantTables(): void
|
||||
{
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
|
||||
154
test/classes/Utils/ForeignKeyTest.php
Normal file
154
test/classes/Utils/ForeignKeyTest.php
Normal file
@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests\Utils;
|
||||
|
||||
use PhpMyAdmin\DatabaseInterface;
|
||||
use PhpMyAdmin\Tests\AbstractTestCase;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
class ForeignKeyTest extends AbstractTestCase
|
||||
{
|
||||
/**
|
||||
* foreign key supported test
|
||||
*
|
||||
* @param string $a Engine
|
||||
* @param bool $e Expected Value
|
||||
*
|
||||
* @dataProvider providerIsSupported
|
||||
*/
|
||||
public function testIsSupported(string $a, bool $e): void
|
||||
{
|
||||
$GLOBALS['server'] = 1;
|
||||
|
||||
$this->assertEquals(
|
||||
$e,
|
||||
ForeignKey::isSupported($a)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* data provider for foreign key supported test
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerIsSupported(): array
|
||||
{
|
||||
return [
|
||||
['MyISAM', false],
|
||||
['innodb', true],
|
||||
['pBxT', true],
|
||||
['ndb', true],
|
||||
];
|
||||
}
|
||||
|
||||
public function testIsCheckEnabled(): void
|
||||
{
|
||||
$GLOBALS['server'] = 1;
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'enable';
|
||||
$this->assertTrue(
|
||||
ForeignKey::isCheckEnabled()
|
||||
);
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'disable';
|
||||
$this->assertFalse(
|
||||
ForeignKey::isCheckEnabled()
|
||||
);
|
||||
|
||||
$GLOBALS['cfg']['DefaultForeignKeyChecks'] = 'default';
|
||||
$this->assertTrue(
|
||||
ForeignKey::isCheckEnabled()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function providerCheckInit(): array
|
||||
{
|
||||
return [
|
||||
['', 'OFF'],
|
||||
['0', 'OFF'],
|
||||
['1', 'ON'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerCheckInit
|
||||
*/
|
||||
public function testHandleDisableCheckInit(string $checksValue, string $setVariableParam): void
|
||||
{
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$_REQUEST['fk_checks'] = $checksValue;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('getVariable')
|
||||
->will($this->returnValue('ON'));
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue(ForeignKey::handleDisableCheckInit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerCheckInit
|
||||
*/
|
||||
public function testHandleDisableCheckInitVarFalse(string $checksValue, string $setVariableParam): void
|
||||
{
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$_REQUEST['fk_checks'] = $checksValue;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('getVariable')
|
||||
->will($this->returnValue('OFF'));
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertFalse(ForeignKey::handleDisableCheckInit());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function providerCheckCleanup(): array
|
||||
{
|
||||
return [
|
||||
[true, 'ON'],
|
||||
[false, 'OFF'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerCheckCleanup
|
||||
*/
|
||||
public function testHandleDisableCheckCleanup(bool $checkValue, string $setVariableParam): void
|
||||
{
|
||||
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$GLOBALS['dbi'] = $dbi;
|
||||
|
||||
$dbi->expects($this->once())
|
||||
->method('setVariable')
|
||||
->with('FOREIGN_KEY_CHECKS', $setVariableParam)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
ForeignKey::handleDisableCheckCleanup($checkValue);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user