From 22d2feb50beb8a459e6adc6c148e7ca69650e9f9 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Mon, 25 Mar 2024 12:39:21 +0100 Subject: [PATCH] Fix search tests --- .../content/search/driver/MysqlDriver.php | 10 +++++----- .../unit/search/AbstractDriverTestSuite.php | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/protected/humhub/modules/content/search/driver/MysqlDriver.php b/protected/humhub/modules/content/search/driver/MysqlDriver.php index 1fd6ed4727..b48d3c5c2b 100644 --- a/protected/humhub/modules/content/search/driver/MysqlDriver.php +++ b/protected/humhub/modules/content/search/driver/MysqlDriver.php @@ -123,19 +123,19 @@ class MysqlDriver extends AbstractDriver $againstQuery = ''; foreach ($query->andTerms as $keyword) { - $againstQuery .= '+' . $keyword . '* '; + $againstQuery .= '+' . rtrim($keyword, '*') . '* '; } foreach ($query->orTerms as $keyword) { - $againstQuery .= $keyword . '* '; + $againstQuery .= rtrim($keyword, '*') . '* '; } - foreach ($query->orTerms as $keyword) { - $againstQuery .= $keyword . ' '; + foreach ($query->notTerms as $keyword) { + $againstQuery .= '-' . $keyword . ' '; } return sprintf( 'MATCH(%s) AGAINST (%s IN BOOLEAN MODE)', implode(', ', $matchFields), - Yii::$app->db->quoteValue($againstQuery) + Yii::$app->db->quoteValue(trim($againstQuery)) ); } diff --git a/protected/humhub/modules/content/tests/codeception/unit/search/AbstractDriverTestSuite.php b/protected/humhub/modules/content/tests/codeception/unit/search/AbstractDriverTestSuite.php index bf5fc9e17e..c9d5b3d2ae 100644 --- a/protected/humhub/modules/content/tests/codeception/unit/search/AbstractDriverTestSuite.php +++ b/protected/humhub/modules/content/tests/codeception/unit/search/AbstractDriverTestSuite.php @@ -5,7 +5,7 @@ namespace humhub\modules\content\tests\codeception\unit\search; use humhub\modules\content\models\Content; use humhub\modules\content\Module; use humhub\modules\content\search\driver\AbstractDriver; -use humhub\modules\content\search\driver\MysqlDriver; +use humhub\modules\content\search\driver\ZendLucenceDriver; use humhub\modules\content\search\ResultSet; use humhub\modules\content\search\SearchRequest; use humhub\modules\content\services\ContentSearchService; @@ -47,28 +47,29 @@ abstract class AbstractDriverTestSuite extends HumHubDbTestCase #$this->assertEquals(1, count($this->getSearchResultByKeyword('Marabru')->results)); $this->assertEquals(1, count($this->getSearchResultByKeyword('Marabru Leav Abcd')->results)); - $this->assertEquals(0, count($this->getSearchResultByKeyword('+Marabru +Leav +Abcd')->results)); + $this->assertEquals(0, count($this->getSearchResultByKeyword('+Marabru +Leav* +Abcd')->results)); $this->assertEquals(0, count($this->getSearchResultByKeyword('Marabru Leav +Abcd')->results)); + $this->assertEquals(1, count($this->getSearchResultByKeyword('Some -Marabru')->results)); + // Wildcards $this->assertEquals(1, count($this->getSearchResultByKeyword('Marabr*')->results)); } public function testShortKeywords() { - if ($this->searchDriver instanceof MysqlDriver) { - // Not possible on MySQLDriver - return; - } - $space = Space::findOne(['id' => 1]); $this->becomeUser('Admin'); (new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Some Other']))->save(); (new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Marabru Leav Test X']))->save(); // Short keywords - $this->assertEquals(0, count($this->getSearchResultByKeyword('M')->results)); - $this->assertEquals(1, count($this->getSearchResultByKeyword('X')->results)); + $this->assertEquals(0, count($this->getSearchResultByKeyword('R')->results)); + $this->assertEquals(1, count($this->getSearchResultByKeyword('T')->results)); + if ($this->searchDriver instanceof ZendLucenceDriver) { + // MysqlDriver can find only at least 2 char exist after "X" + $this->assertEquals(1, count($this->getSearchResultByKeyword('X')->results)); + } } private function getSearchRequest(): SearchRequest