Skip to content

Commit

Permalink
Some improvements (glpi-project#1338)
Browse files Browse the repository at this point in the history
* Ensure entity is reset in session after each test

* improve recursivity detection in getEntitiesRestrictRequest
  • Loading branch information
trasher committed Nov 24, 2016
1 parent ad2c468 commit 97e220b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
30 changes: 25 additions & 5 deletions inc/db.function.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 .= ')';
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/DbFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ protected function tearDown() {
}
}
unset($DB->objcreated);

//reset entity in session
Session::changeActiveEntities(getItemByTypeName('Entity', '_test_root_entity', true), true);
}


Expand Down

0 comments on commit 97e220b

Please sign in to comment.