Skip to content

Commit

Permalink
Enable GLPI strict depreciations
Browse files Browse the repository at this point in the history
Correct direct query calls

Replace direct query() calls

Fix direct query
  • Loading branch information
trasher committed Oct 27, 2023
1 parent a9051b9 commit 3b02169
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 67 deletions.
12 changes: 8 additions & 4 deletions inc/deploypackage_profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ public static function getProfiles($deploypackages_id)
global $DB;

$prof = [];
$query = "SELECT `glpi_plugin_glpiinventory_deploypackages_profiles`.*
FROM `glpi_plugin_glpiinventory_deploypackages_profiles`
WHERE `plugin_glpiinventory_deploypackages_id` = '$deploypackages_id'";

foreach ($DB->request($query) as $data) {
$iterator = $DB->request([
'FROM' => 'glpi_plugin_glpiinventory_deploypackages_profiles',
'WHERE' => [
'plugin_glpiinventory_deploypackages_id' => $deploypackages_id
]
]);

foreach ($iterator as $data) {
$prof[$data['profiles_id']][] = $data;
}
return $prof;
Expand Down
2 changes: 1 addition & 1 deletion inc/setup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function uninstall()
$pfSetup->rrmdir(GLPI_PLUGIN_DOC_DIR . '/glpiinventory');
}

$result = $DB->query("SHOW TABLES;");
$result = $DB->doQuery("SHOW TABLES;");
while ($data = $DB->fetchArray($result)) {
if (
(strstr($data[0], "glpi_plugin_glpiinventory_"))
Expand Down
32 changes: 16 additions & 16 deletions install/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,7 @@ function do_rulematchedlog_migration($migration)
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
$DB->query($query);
$DB->doQuery($query);
}
$migration->changeField(
$newTable,
Expand Down Expand Up @@ -3138,7 +3138,7 @@ function do_configlogfield_migration($migration)
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
$DB->query($query);
$DB->doQuery($query);
}
$migration->changeField(
$newTable,
Expand Down Expand Up @@ -3223,7 +3223,7 @@ function do_networkport_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -3349,7 +3349,7 @@ function do_networkport_migration($migration)
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
$DB->query($query);
$DB->doQuery($query);
}
$migration->changeField(
$newTable,
Expand Down Expand Up @@ -3421,7 +3421,7 @@ function do_networkport_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -3755,7 +3755,7 @@ function do_networkport_migration($migration)
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
$DB->query($query);
$DB->doQuery($query);
}
$migration->changeField(
$newTable,
Expand Down Expand Up @@ -4041,7 +4041,7 @@ function do_printer_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -4197,7 +4197,7 @@ function do_printer_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -4404,7 +4404,7 @@ function do_printer_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -4679,7 +4679,7 @@ function do_networkequipment_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -4872,7 +4872,7 @@ function do_networkequipment_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -5101,7 +5101,7 @@ function do_configsecurity_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query('CREATE TABLE `' . $newTable . '` (
$DB->doQuery('CREATE TABLE `' . $newTable . '` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC');
Expand Down Expand Up @@ -5296,7 +5296,7 @@ function do_statediscovery_migration($migration)
renamePluginFields($migration, $newTable);

if (!$DB->tableExists($newTable)) {
$DB->query("CREATE TABLE `" . $newTable . "` (
$DB->doQuery("CREATE TABLE `" . $newTable . "` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC");
Expand Down Expand Up @@ -5901,7 +5901,7 @@ function do_deployuserinteraction_migration($migration)
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
$DB->query($query);
$DB->doQuery($query);
}
}

Expand Down Expand Up @@ -8235,7 +8235,7 @@ function migratePluginTables($migration, $a_table)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";
}

$DB->query($query);
$DB->doQuery($query);
}

foreach ($a_table['renamefields'] as $old => $new) {
Expand Down Expand Up @@ -8588,7 +8588,7 @@ function migrateTablesFromFusinvDeploy($migration)

//migrate fusinvdeploy_files
if ($DB->tableExists("glpi_plugin_fusinvdeploy_files")) {
$DB->query("TRUNCATE TABLE `glpi_plugin_glpiinventory_deployfiles`");
$DB->doQuery("TRUNCATE TABLE `glpi_plugin_glpiinventory_deployfiles`");
if ($DB->fieldExists("glpi_plugin_fusinvdeploy_files", "filesize")) {
$f_iterator = $DB->request([
'SELECT' => [
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

define("PLUGIN_GLPIINVENTORY_VERSION", "1.3.1");
// Minimal GLPI version, inclusive
define('PLUGIN_GLPI_INVENTORY_GLPI_MIN_VERSION', '10.0.10');
define('PLUGIN_GLPI_INVENTORY_GLPI_MIN_VERSION', '10.0.11');
// Maximum GLPI version, exclusive
define('PLUGIN_GLPI_INVENTORY_GLPI_MAX_VERSION', '10.0.99');
// Used for use config values in 'cache'
Expand Down
11 changes: 6 additions & 5 deletions tests/Installation/DatabaseTestsCommons.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function checkInstall($pluginname = '', $when = '')
$a_tables = [];
// SHOW TABLES;
$query = "SHOW TABLES";
$result = $DB->query($query);
$result = $DB->doQuery($query);
while ($data = $DB->fetchArray($result)) {
if (
(strstr($data[0], "tracker")
Expand Down Expand Up @@ -143,7 +143,7 @@ public function checkInstall($pluginname = '', $when = '')

foreach ($a_tables as $table) {
$query = "SHOW CREATE TABLE " . $table;
$result = $DB->query($query);
$result = $DB->doQuery($query);
while ($data = $DB->fetchArray($result)) {
$a_lines = explode("\n", $data['Create Table']);

Expand Down Expand Up @@ -283,9 +283,10 @@ public function checkInstall($pluginname = '', $when = '')
/*
* Verify in taskjob definition PluginFusinvsnmpIPRange not exist
*/
$query = "SELECT * FROM `glpi_plugin_glpiinventory_taskjobs`";
$result = $DB->query($query);
while ($data = $DB->fetchArray($result)) {
$request = $DB->request([
'FROM' => 'glpi_plugin_glpiinventory_taskjobs'
]);
foreach ($request as $data) {
$snmprangeip = 0;
if (strstr($data['targets'], "PluginFusinvsnmpIPRange")) {
$snmprangeip = 1;
Expand Down
6 changes: 3 additions & 3 deletions tests/Installation/InstallationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function testInstall()

// Delete if Table of FusionInventory or Tracker yet in DB
$query = "SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW'";
$result = $DB->query($query);
$result = $DB->doQuery($query);
while ($data = $DB->fetchArray($result)) {
if (strstr($data[0], "fusi")) {
$DB->dropView($data[0]);
}
}

$query = "SHOW TABLES";
$result = $DB->query($query);
$result = $DB->doQuery($query);
while ($data = $DB->fetchArray($result)) {
if (
strstr($data[0], "tracker")
Expand All @@ -71,7 +71,7 @@ public function testInstall()
$DB->dropTable($data[0]);
}
}
$DB->query('TRUNCATE TABLE glpi_plugins');
$DB->doQuery('TRUNCATE TABLE glpi_plugins');
$this->install();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Installation/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function update($version = '', $verify = false, $nbrules = 0)
$plugin->uninstall($plugin->fields['id']);

$query = "SHOW TABLES";
$result = $DB->query($query);
$result = $DB->doQuery($query);
while ($data = $DB->fetchArray($result)) {
if (
strstr($data[0], "tracker")
Expand Down
19 changes: 14 additions & 5 deletions tests/Integration/RestURLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class RestURLTest extends TestCase
{
public static function setUpBeforeClass(): void
{

// Delete all entities except root entity
$entity = new Entity();
$items = $entity->find();
Expand Down Expand Up @@ -100,15 +99,19 @@ public function prepareDb()
$ret = $entity->update($input);
$this->assertTrue($ret);

// active all modules
$query = "UPDATE `glpi_plugin_glpiinventory_agentmodules`"
. " SET `is_active`='1'";
$DB->query($query);
// active all modules
$DB->update(
'glpi_plugin_glpiinventory_agentmodules',
['is_active' => 1],
[new QueryExpression("1=1")]
);
}


/**
* @test
*
* @depends prepareDb
*/
public function getCollectUrlEnt1Entity()
{
Expand Down Expand Up @@ -146,6 +149,8 @@ public function getCollectUrlEnt1Entity()

/**
* @test
*
* @depends prepareDb
*/
public function getDeployUrlRootEntity()
{
Expand Down Expand Up @@ -182,6 +187,8 @@ public function getDeployUrlRootEntity()

/**
* @test
*
* @depends prepareDb
*/
public function getEsxUrlRootEntity()
{
Expand Down Expand Up @@ -218,6 +225,8 @@ public function getEsxUrlRootEntity()

/**
* @test
*
* @depends prepareDb
*/
public function getCollectUrlRootEntity()
{
Expand Down
42 changes: 25 additions & 17 deletions tests/Integration/Tasks/CronTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ public function prepareTaskNotRePrepareIfSuccessful()
$pfTask = new PluginGlpiinventoryTask();
$deploycommon = new PluginGlpiinventoryDeployCommon();

$DB->query("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjoblogs`");
$DB->query("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjobstates`");
$DB->doQuery("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjoblogs`");
$DB->doQuery("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjobstates`");

$pfTask->getFromDBByCrit(['name' => 'deploy']);
$this->assertArrayHasKey('id', $pfTask->fields);
Expand Down Expand Up @@ -692,8 +692,8 @@ public function cleanTasksAndJobs()
$pfTask->delete(['id' => $pfTask->fields['id']], true);

//Clean all taskjoblogs & states
$DB->query("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjoblogs`");
$DB->query("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjobstates`");
$DB->doQuery("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjoblogs`");
$DB->doQuery("TRUNCATE TABLE `glpi_plugin_glpiinventory_taskjobstates`");

//Find the on demand task
$tasks = $pfTask->find(['name' => 'ondemand']);
Expand All @@ -705,10 +705,12 @@ public function cleanTasksAndJobs()
//Prepare the task
PluginGlpiinventoryTask::cronTaskscheduler();

//Set the first job as successfull
$query = "SELECT DISTINCT `plugin_glpiinventory_taskjobstates_id`
FROM glpi_plugin_glpiinventory_taskjoblogs LIMIT 1";
foreach ($DB->request($query) as $data) {
//Set the first job as successfull
$iterator = $DB->request([
'FROM' => 'glpi_plugin_glpiinventory_taskjoblogs',
'LIMIT' => 1
]);
foreach ($iterator as $data) {
$pfTaskJobstate->changeStatusFinish($data['plugin_glpiinventory_taskjobstates_id'], 0, '');
}

Expand All @@ -721,9 +723,11 @@ public function cleanTasksAndJobs()
$datetime = new Datetime($_SESSION['glpi_currenttime']);
$datetime->modify('-4 days');

$query = "UPDATE `glpi_plugin_glpiinventory_taskjoblogs`
SET `date`='" . $datetime->format('Y-m-d') . " 00:00:00'";
$DB->query($query);
$DB->update(
'glpi_plugin_glpiinventory_taskjoblogs',
['date' => $datetime->format('Y-m-d') . " 00:00:00"],
[new QueryExpression("1=1")]
);

//No task & jobs should be removed because ask for cleaning 5 days from now
$index = $pfTask->cleanTasksAndJobs(5);
Expand Down Expand Up @@ -770,15 +774,19 @@ public function cleanTasksAndJobs()
));

//Set the first job as successfull
$query = "SELECT DISTINCT `plugin_glpiinventory_taskjobstates_id`
FROM glpi_plugin_glpiinventory_taskjoblogs";
foreach ($DB->request($query) as $data) {
$request = $DB->request([
'DISTINCT' => 'plugin_glpiinventory_taskjobstates_id',
'FROM' => 'glpi_plugin_glpiinventory_taskjoblogs',
]);
foreach ($request as $data) {
$pfTaskJobstate->changeStatusFinish($data['plugin_glpiinventory_taskjobstates_id'], 0, '');
}

$query = "UPDATE `glpi_plugin_glpiinventory_taskjoblogs`
SET `date`='" . $datetime->format('Y-m-d') . " 00:00:00'";
$DB->query($query);
$DB->update(
'glpi_plugin_glpiinventory_taskjoblogs',
['date' => $datetime->format('Y-m-d') . " 00:00:00"],
[new QueryExpression("1=1")]
);

//One taskjob is finished and should be cleaned
$index = $pfTask->cleanTasksAndJobs(2);
Expand Down
Loading

0 comments on commit 3b02169

Please sign in to comment.