From 97e220bee24361614b098632987ba261475b8434 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 24 Nov 2016 16:13:47 +0100 Subject: [PATCH] Some improvements (#1338) * Ensure entity is reset in session after each test * improve recursivity detection in getEntitiesRestrictRequest --- inc/db.function.php | 30 +++++++++++++++++++++++++----- tests/DbFunctionTest.php | 4 ++-- tests/DbTestCase.php | 3 +++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/inc/db.function.php b/inc/db.function.php index 3db07a90571..10b4d86fb62 100644 --- a/inc/db.function.php +++ b/inc/db.function.php @@ -1682,6 +1682,10 @@ function getDbRelations() { function getEntitiesRestrictRequest($separator="AND", $table="", $field="",$value='', $is_recursive=false, $complete_request=false) { + if (empty($table)) { + Toolbox::logDebug(__METHOD__ . ' called without any `table` specified!'); + } + $query = $separator ." ( "; // !='0' needed because consider as empty @@ -1740,11 +1744,27 @@ function getEntitiesRestrictRequest($separator="AND", $table="", $field="",$valu } if (count($ancestors)) { - if ($table == 'glpi_entities') { - $query .= " OR $field IN ('" . implode("','",$ancestors) . "')"; - } else { - $recur = (empty($table) ? '`is_recursive`' : "`$table`.`is_recursive`"); - $query .= " OR ($recur='1' AND $field IN ('" . implode("','",$ancestors) . "'))"; + $query .= ' OR '; + + //flag recursivity + $is_recursive = false; + if (!empty($table) && $table != 'glpi_entities') { + $item = getItemForItemtype(getItemTypeForTable($table)); + if ($item !== false) { + $is_recursive = $item->maybeRecursive(); + } + } + + if ($is_recursive) { + $query .= '('; + $query .= (empty($table) ? '`is_recursive`' : "`$table`.`is_recursive`"); + $query .= "='1' AND "; + } + + $query .= "$field IN ('" . implode("','",$ancestors) . "')"; + + if ($is_recursive) { + $query .= ')'; } } } diff --git a/tests/DbFunctionTest.php b/tests/DbFunctionTest.php index 35d117fae7f..62b849f305d 100644 --- a/tests/DbFunctionTest.php +++ b/tests/DbFunctionTest.php @@ -382,8 +382,8 @@ public function testGetEntityRestrict() { $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers', '', '', true)); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE (`glpi_computers`.`entities_id` IN (3) OR (`glpi_computers`.`is_recursive` = 1 AND `glpi_computers`.`entities_id` IN (0, 1)))', $it->getSql()); - // Child + parent without table - $this->assertEquals("WHERE ( `entities_id` IN ('3') OR (`is_recursive`='1' AND `entities_id` IN ('0','1')) ) ", + // Child + parent without table - TODO: check the missing table warning has been added in logs + $this->assertEquals("WHERE ( `entities_id` IN ('3') OR `entities_id` IN ('0','1') ) ", getEntitiesRestrictRequest('WHERE', '', '', '', true)); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('', '', '', true)); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE (`entities_id` IN (3) OR (`is_recursive` = 1 AND `entities_id` IN (0, 1)))', $it->getSql()); diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index a5d8430e3ee..0d26b642f28 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -57,6 +57,9 @@ protected function tearDown() { } } unset($DB->objcreated); + + //reset entity in session + Session::changeActiveEntities(getItemByTypeName('Entity', '_test_root_entity', true), true); }