/** *@covers ::getEntitiesRestrictRequest *@covers ::getEntitiesRestrictCriteria */ public function testGetEntityRestrict() { $this->Login(); // See all, really all $_SESSION['glpishowallentities'] = 1; // will be restored by setEntity call $this->assertEmpty(getEntitiesRestrictRequest('AND', 'glpi_computers')); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers')); $this->assertEquals('SELECT * FROM `glpi_computers`', $it->getSql()); // See all $this->setEntity('_test_root_entity', true); $this->assertEquals("WHERE ( `glpi_computers`.`entities_id` IN ('1', '2', '3') ) ", getEntitiesRestrictRequest('WHERE', 'glpi_computers')); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers')); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE `glpi_computers`.`entities_id` IN (1, 2, 3)', $it->getSql()); // Root entity $this->setEntity('_test_root_entity', false); $this->assertEquals("WHERE ( `glpi_computers`.`entities_id` IN ('1') ) ", getEntitiesRestrictRequest('WHERE', 'glpi_computers')); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers')); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE `glpi_computers`.`entities_id` IN (1)', $it->getSql()); // Child $this->setEntity('_test_child_1', false); $this->assertEquals("WHERE ( `glpi_computers`.`entities_id` IN ('2') ) ", getEntitiesRestrictRequest('WHERE', 'glpi_computers')); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers')); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE `glpi_computers`.`entities_id` IN (2)', $it->getSql()); // Child without table $this->assertEquals("WHERE ( `entities_id` IN ('2') ) ", getEntitiesRestrictRequest('WHERE')); $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria()); $this->assertEquals('SELECT * FROM `glpi_computers` WHERE `entities_id` IN (2)', $it->getSql()); // Child + parent $this->setEntity('_test_child_2', false); $this->assertEquals("WHERE ( `glpi_computers`.`entities_id` IN ('3') OR (`glpi_computers`.`is_recursive`='1' AND `glpi_computers`.`entities_id` IN ('0','1')) ) ", getEntitiesRestrictRequest('WHERE', 'glpi_computers', '', '', true)); $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 on glpi_entities $it = new DBmysqlIterator(NULL, 'glpi_entities', getEntitiesRestrictCriteria('glpi_entities', '', '', true)); $this->assertEquals('SELECT * FROM `glpi_entities` WHERE (`glpi_entities`.`id` IN (3, 0, 1))', $it->getSql()); //Child + parent -- automatic recusrivity detection $it = new DBmysqlIterator(NULL, 'glpi_computers', getEntitiesRestrictCriteria('glpi_computers', '', '', 'auto')); $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')) ) ", 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()); $it = new DBmysqlIterator(NULL, 'glpi_entities', getEntitiesRestrictCriteria('glpi_entities', '', 3, true)); $this->assertEquals('SELECT * FROM `glpi_entities` WHERE (`glpi_entities`.`id` IN (3, 0, 1))', $it->getSql()); $it = new DBmysqlIterator(NULL, 'glpi_entities', getEntitiesRestrictCriteria('glpi_entities', '', 7, true)); $this->assertEquals('SELECT * FROM `glpi_entities` WHERE `glpi_entities`.`id` = 7', $it->getSql()); }
/** * Count the number of elements in a table for a specific entity * * @param $table string table name * @param $condition string/array additional condition (default '') or criteria * * @return int nb of elements in table **/ function countElementsInTableForMyEntities($table, $condition = '') { /// TODO clean it / maybe include when review of SQL requests $itemtype = getItemTypeForTable($table); $item = new $itemtype(); $criteria = getEntitiesRestrictCriteria($table, '', '', $item->maybeRecursive()); if (is_array($condition)) { $criteria = array_merge($condition, $criteria); } else { if ($condition) { $criteria[] = $condition; } } return countElementsInTable($table, $criteria); }