From d6ef4fb7bb67e07f3b884d11304be8b5df150f34 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 2 Jul 2026 16:11:32 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B09687=20-=20Fix=20incorrect=20return=20ty?= =?UTF-8?q?pe=20for=20the=20DBObjectSearch::ApplyDataFilter()=20function?= =?UTF-8?q?=20(#939)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * N°9687 - Fix incorrect return type for the DBObjectSearch::ApplyDataFilter() function * N°9687 - Add unit test * N°9687 - Improve unit test after code review * N°9687 - Improve unit test after code review --- core/dbobjectsearch.class.php | 3 +- core/dbunionsearch.class.php | 3 +- .../DBSearch/DBSearchApplyDataFiltersTest.php | 67 +++++++++++++++++++ .../N9687_CustomGetSelectFilterClass.php | 24 +++++++ 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 tests/php-unit-tests/unitary-tests/core/DBSearch/DBSearchApplyDataFiltersTest.php create mode 100644 tests/php-unit-tests/unitary-tests/core/DBSearch/Fixtures/N9687_CustomGetSelectFilterClass.php diff --git a/core/dbobjectsearch.class.php b/core/dbobjectsearch.class.php index 0f32ab3efb..d702c1d0bf 100644 --- a/core/dbobjectsearch.class.php +++ b/core/dbobjectsearch.class.php @@ -1928,9 +1928,8 @@ class DBObjectSearch extends DBSearch /** * @inheritDoc - * @return DBObjectSearch */ - protected function ApplyDataFilters(): DBObjectSearch + protected function ApplyDataFilters(): DBSearch { if ($this->IsAllDataAllowed() || $this->IsDataFiltered()) { return $this; diff --git a/core/dbunionsearch.class.php b/core/dbunionsearch.class.php index a9118974fb..8566655aba 100644 --- a/core/dbunionsearch.class.php +++ b/core/dbunionsearch.class.php @@ -676,9 +676,8 @@ class DBUnionSearch extends DBSearch /** * @inheritDoc - * @return DBUnionSearch */ - protected function ApplyDataFilters(): DBUnionSearch + protected function ApplyDataFilters(): DBSearch { if ($this->IsAllDataAllowed() || $this->IsDataFiltered()) { return $this; diff --git a/tests/php-unit-tests/unitary-tests/core/DBSearch/DBSearchApplyDataFiltersTest.php b/tests/php-unit-tests/unitary-tests/core/DBSearch/DBSearchApplyDataFiltersTest.php new file mode 100644 index 0000000000..b503023b90 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/DBSearch/DBSearchApplyDataFiltersTest.php @@ -0,0 +1,67 @@ +sOriginalUserRightsSelectModuleClass = get_class(UserRights::GetModuleInstance()); + + $this->RequireOnceUnitTestFile('Fixtures/N9687_CustomGetSelectFilterClass.php'); + } + + /** + * @inheritDoc + */ + public function tearDown(): void + { + // Restore original UserRights select module to not interfere with next tests + UserRights::SelectModule($this->sOriginalUserRightsSelectModuleClass); + + parent::tearDown(); + } + + public function testApplyDataFiltersOnDBObjectSearchShouldAcceptGetSelectFilterClassReturningDBUnionSearch() + { + // Use custom select filter that returns a DBUnionSearch + $sPreviousSelectModuleClass = get_class(UserRights::GetModuleInstance()); + UserRights::SelectModule('\\Combodo\\iTop\\Test\\UnitTest\\Core\\Fixtures\\N9687_CustomGetSelectFilterClass'); + + // Create a user and login, otherwise the select filter won't apply + self::CreateUser('test_dbsearch_applydatafilters', 3); + UserRights::Login('test_dbsearch_applydatafilters'); + + // Create a person + $oCreatedPerson = $this->CreatePerson(microtime()); + + // Try to retrieve it using the select filter + $oSearch = DBObjectSearch::FromOQL("SELECT Person WHERE id = {$oCreatedPerson->GetKey()}"); + $oFilteredSearch = $this->InvokeNonPublicMethod(DBObjectSearch::class, 'ApplyDataFilters', $oSearch); + + // Restore original select module to not interfere with next tests + UserRights::SelectModule($sPreviousSelectModuleClass); + + $this->assertEquals(DBUnionSearch::class, get_class($oFilteredSearch), "DBObjectSearch::ApplyDataFilters() should be able to return a \DBUnionSearch"); + } +} diff --git a/tests/php-unit-tests/unitary-tests/core/DBSearch/Fixtures/N9687_CustomGetSelectFilterClass.php b/tests/php-unit-tests/unitary-tests/core/DBSearch/Fixtures/N9687_CustomGetSelectFilterClass.php new file mode 100644 index 0000000000..fc2a53054b --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/DBSearch/Fixtures/N9687_CustomGetSelectFilterClass.php @@ -0,0 +1,24 @@ +