Skip to content

Commit

Permalink
fix notcontains search option
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Oct 3, 2024
1 parent ea67d74 commit d9f2d67
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4744,8 +4744,52 @@ public static function addWhere($link, $nott, $itemtype, $ID, $searchtype, $val,

switch ($searchtype) {
case "notcontains":
$nott = !$nott;
//negated, use contains case
// FIXME
// `field LIKE '%test%'` condition is not supposed to be relevant, and can sometimes result in SQL performances issues/warnings/errors,
// or at least to unexpected results, when following datatype are used:
// - integer
// - number
// - decimal
// - count
// - mio
// - percentage
// - timestamp
// - datetime
// - date_delay
// - mac
// - color
// - language
// Values should be filtered to accept only valid pattern according to given datatype.

if (isset($searchopt[$ID]["datatype"]) && ($searchopt[$ID]["datatype"] === 'decimal')) {
$matches = [];
if (preg_match('/^(\d+.?\d?)/', $val, $matches)) {
$val = $matches[1];
if (!str_contains($val, '.')) {
$val .= '.';
}
}
}

// To search for '&' in rich text
if (
(($searchopt[$ID]['datatype'] ?? null) === 'text')
&& (($searchopt[$ID]['htmltext'] ?? null) === true)
) {
$val = str_replace('&', '38;amp;', $val);
}
if ($should_use_subquery) {
// Subquery will be needed to get accurate results
$use_subquery_on_text_search = true;

// Potential negation will be handled by the subquery operator
$SEARCH = self::makeTextSearch($val, false);
$subquery_operator = $nott ? "IN" : "NOT IN";
} else {
$SEARCH = self::makeTextSearch($val, $nott);
}
break;

case "contains":
// FIXME
// `field LIKE '%test%'` condition is not supposed to be relevant, and can sometimes result in SQL performances issues/warnings/errors,
Expand Down
103 changes: 103 additions & 0 deletions tests/functional/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units;

use DbTestCase;

/* Test for inc/search.class.php */

class Search extends DbTestCase
{
public function testAddWhere()
{
$itemtype = \Computer::class;
$search_option_id = 2;
$output = \Search::addWhere(
'',
0,
$itemtype,
$search_option_id,
'equals',
42,
0
);
// Ignore leading and trailing spaces not relevant)
$output = trim($output);
$this->string($output)->isEqualTo("(`glpi_computers`.`id` = 42)");

$output = \Search::addWhere(
'',
0,
$itemtype,
$search_option_id,
'notequals',
42,
0
);
// Ignore leading and trailing spaces not relevant)
$output = trim($output);
$this->string($output)->isEqualTo("(`glpi_computers`.`id` <> 42)");

// This Search option needs to be a number without min or max to be handled with contains or notcontains operators
$itemtype = \Printer::class;
$search_option_id = 12;
$output = \Search::addWhere(
'',
0,
$itemtype,
$search_option_id,
'contains',
42,
0
);
// Ignore leading and trailing spaces not relevant)
$output = trim($output);
$this->string($output)->isEqualTo("(`glpi_printers`.`last_pages_counter` = 42)");

$output = \Search::addWhere(
'',
0,
$itemtype,
$search_option_id,
'notcontains',
42,
0
);
// Ignore leading and trailing spaces not relevant)
$output = trim($output);
$this->string($output)->isEqualTo("(`glpi_printers`.`last_pages_counter` <> 42)");
}
}

0 comments on commit d9f2d67

Please sign in to comment.