From 9025f6831270181c60044dca657e36b1c09eb971 Mon Sep 17 00:00:00 2001 From: cconard96 Date: Tue, 1 Oct 2019 10:48:54 -0400 Subject: [PATCH 1/5] Fix search with non-standard foreign keys fixes #6375 Add tests --- inc/search.class.php | 7 +++--- tests/functionnal/Search.php | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/inc/search.class.php b/inc/search.class.php index 126214490b6..e9a5f38d2b4 100755 --- a/inc/search.class.php +++ b/inc/search.class.php @@ -3410,7 +3410,7 @@ static function addSelect($itemtype, $ID, $meta = 0, $meta_type = 0) { && (!isset($CFG_GLPI["union_search_type"][$itemtype]) || ($CFG_GLPI["union_search_type"][$itemtype] != $table))) || !empty($complexjoin)) - && ($searchopt[$ID]["linkfield"] != getForeignKeyFieldForTable($table))) { + && getTableNameForForeignKeyField($searchopt[$ID]["linkfield"]) != $table) { $addtable .= "_".$searchopt[$ID]["linkfield"]; } @@ -4846,7 +4846,8 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, } // Multiple link possibilies case - if (!empty($linkfield) && ($linkfield != getForeignKeyFieldForTable($new_table))) { + $ref_fk = getForeignKeyFieldForTable($new_table); + if (!empty($linkfield) && ($linkfield != $ref_fk) && strpos($linkfield, $ref_fk . '_') !== 0) { $nt .= "_".$linkfield; $AS = " AS `$nt`"; } @@ -4867,7 +4868,7 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, // Do not take into account standard linkfield $tocheck = $nt.".".$linkfield; - if ($linkfield == getForeignKeyFieldForTable($new_table)) { + if ($linkfield == $ref_fk || strpos($linkfield, $ref_fk . '_') === 0) { $tocheck = $nt; } diff --git a/tests/functionnal/Search.php b/tests/functionnal/Search.php index 39d1c4f3bd0..6d2a3a7eec2 100644 --- a/tests/functionnal/Search.php +++ b/tests/functionnal/Search.php @@ -953,6 +953,33 @@ function testManageParams() { } + public function addSelectProvider() { + return [ + 'special_fk' => [[ + 'itemtype' => 'Computer', + 'ID' => 24, // users_id_tech + 'sql' => '`glpi_users`.`name` AS `ITEM_Computer_24`, `glpi_users`.`realname` AS `ITEM_Computer_24_realname`, + `glpi_users`.`id` AS `ITEM_Computer_24_id`, `glpi_users`.`firstname` AS `ITEM_Computer_24_firstname`,' + ]], + 'regular_fk' => [[ + 'itemtype' => 'Computer', + 'ID' => 70, // users_id + 'sql' => '`glpi_users`.`name` AS `ITEM_Computer_70`, `glpi_users`.`realname` AS `ITEM_Computer_70_realname`, + `glpi_users`.`id` AS `ITEM_Computer_70_id`, `glpi_users`.`firstname` AS `ITEM_Computer_70_firstname`,' + ]], + ]; + } + + /** + * @dataProvider addSelectProvider + */ + public function testAddSelect($provider) { + $sql_select = \Search::addSelect($provider['itemtype'], $provider['ID']); + + $this->string($this->cleanSQL($sql_select)) + ->isEqualTo($this->cleanSQL($provider['sql'])); + } + public function addLeftJoinProvider() { return [ 'itemtype_item_revert' => [[ @@ -980,6 +1007,26 @@ public function addLeftJoinProvider() { AND `glpi_projectteams`.`itemtype` = 'Contact' )" ]], + 'special_fk' => [[ + 'itemtype' => 'Computer', + 'table' => \User::getTable(), + 'field' => 'name', + 'linkfield' => 'users_id_tech', + 'meta' => false, + 'meta_type' => null, + 'joinparams' => [], + 'sql' => "LEFT JOIN `glpi_users` ON (`glpi_computers`.`users_id_tech` = `glpi_users`.`id` )" + ]], + 'regular_fk' => [[ + 'itemtype' => 'Computer', + 'table' => \User::getTable(), + 'field' => 'name', + 'linkfield' => 'users_id', + 'meta' => false, + 'meta_type' => null, + 'joinparams' => [], + 'sql' => "LEFT JOIN `glpi_users` ON (`glpi_computers`.`users_id` = `glpi_users`.`id` )" + ]], ]; } From 663b51c361b16c2ec1360e1b0e13402b975a9012 Mon Sep 17 00:00:00 2001 From: Curtis Conard Date: Thu, 3 Oct 2019 06:18:19 -0400 Subject: [PATCH 2/5] Update inc/search.class.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Cédric Anne --- inc/search.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/search.class.php b/inc/search.class.php index e9a5f38d2b4..4686bce134b 100755 --- a/inc/search.class.php +++ b/inc/search.class.php @@ -4868,7 +4868,7 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, // Do not take into account standard linkfield $tocheck = $nt.".".$linkfield; - if ($linkfield == $ref_fk || strpos($linkfield, $ref_fk . '_') === 0) { + if (getTableNameForForeignKeyField($linkfield) !== $new_table) { $tocheck = $nt; } From 6326c18b3b5f4364033e5c3c75b4ca7adba4335e Mon Sep 17 00:00:00 2001 From: cconard96 Date: Thu, 3 Oct 2019 06:24:35 -0400 Subject: [PATCH 3/5] Revert "Update inc/search.class.php" This reverts commit 663b51c361b16c2ec1360e1b0e13402b975a9012. --- inc/search.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/search.class.php b/inc/search.class.php index 4686bce134b..e9a5f38d2b4 100755 --- a/inc/search.class.php +++ b/inc/search.class.php @@ -4868,7 +4868,7 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, // Do not take into account standard linkfield $tocheck = $nt.".".$linkfield; - if (getTableNameForForeignKeyField($linkfield) !== $new_table) { + if ($linkfield == $ref_fk || strpos($linkfield, $ref_fk . '_') === 0) { $tocheck = $nt; } From 8fbcad284ae247aa5d67eca5bcd2f549e9e51275 Mon Sep 17 00:00:00 2001 From: Curtis Conard Date: Thu, 3 Oct 2019 07:58:52 -0400 Subject: [PATCH 4/5] Update inc/search.class.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Cédric Anne --- inc/search.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/search.class.php b/inc/search.class.php index e9a5f38d2b4..519a29aaa68 100755 --- a/inc/search.class.php +++ b/inc/search.class.php @@ -4868,7 +4868,7 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, // Do not take into account standard linkfield $tocheck = $nt.".".$linkfield; - if ($linkfield == $ref_fk || strpos($linkfield, $ref_fk . '_') === 0) { + if (getTableNameForForeignKeyField($linkfield) == $new_table) { $tocheck = $nt; } From cf7a5baf1ef9d7cc2365ea254186475ff03ec409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 4 Oct 2019 13:28:39 +0200 Subject: [PATCH 5/5] Try to apply suggestion --- inc/search.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inc/search.class.php b/inc/search.class.php index 519a29aaa68..d122552a1a8 100755 --- a/inc/search.class.php +++ b/inc/search.class.php @@ -4846,8 +4846,7 @@ static function addLeftJoin($itemtype, $ref_table, array &$already_link_tables, } // Multiple link possibilies case - $ref_fk = getForeignKeyFieldForTable($new_table); - if (!empty($linkfield) && ($linkfield != $ref_fk) && strpos($linkfield, $ref_fk . '_') !== 0) { + if (!empty($linkfield) && getTableNameForForeignKeyField($linkfield) != $new_table) { $nt .= "_".$linkfield; $AS = " AS `$nt`"; }