From d12c8a183cb0847a6f2858a5493ad9a2bd7b7fca Mon Sep 17 00:00:00 2001 From: odain Date: Thu, 30 Jan 2025 11:16:07 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B08143=20-=20Setup=20page=20in=20error=20i?= =?UTF-8?q?n=20support.combodo.com?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TwigBase/UI/UIBlockExtension.php | 4 +- .../InterfaceDiscovery/InterfaceDiscovery.php | 23 ++++++++++-- .../InterfaceDiscoveryTest.php | 37 ++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/sources/Application/TwigBase/UI/UIBlockExtension.php b/sources/Application/TwigBase/UI/UIBlockExtension.php index 91aaf79f3..b8d5f7fe4 100644 --- a/sources/Application/TwigBase/UI/UIBlockExtension.php +++ b/sources/Application/TwigBase/UI/UIBlockExtension.php @@ -30,9 +30,7 @@ class UIBlockExtension extends AbstractExtension $aFactoryClasses = InterfaceDiscovery::GetInstance()->FindItopClasses(iUIBlockFactory::class); foreach ($aFactoryClasses as $sFactoryClass) { - if (class_exists($sFactoryClass)){ - $aParsers[] = new UIBlockParser($sFactoryClass); - } + $aParsers[] = new UIBlockParser($sFactoryClass); } return $aParsers; diff --git a/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php b/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php index 2d46481b5..02299bfef 100644 --- a/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php +++ b/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php @@ -22,6 +22,7 @@ class InterfaceDiscovery private static InterfaceDiscovery $oInstance; private DataModelDependantCache $oCacheService; private ?array $aForcedClassMap = null; // For testing purposes + private bool $bCheckInterfaceImplementation = true; // false only for testing purposes const CACHE_NONE = 'CACHE_NONE'; const CACHE_DYNAMIC = 'CACHE_DYNAMIC'; // rebuild cache when files changes @@ -86,9 +87,15 @@ class InterfaceDiscovery continue; } - if ($this->IsInterfaceImplementation($sPHPClass, $sInterface)) { - $aMatchingClasses[] = $sPHPClass; + if ($this->bCheckInterfaceImplementation && ! $this->IsInterfaceImplementation($sPHPClass, $sInterface)) { + continue; } + + if (! class_exists($sPHPClass)){ + continue; + } + + $aMatchingClasses[] = $sPHPClass; } if ($this->GetCacheMode() !== self::CACHE_NONE) { @@ -241,7 +248,17 @@ class InterfaceDiscovery public function ReadClassesFromCache(string $sKey): array { - return $this->oCacheService->Fetch('InterfaceDiscovery', $sKey); + $aClasses = $this->oCacheService->Fetch('InterfaceDiscovery', $sKey); + + $aRealClasses = []; + foreach ($aClasses as $sPHPClass){ + if (! class_exists($sPHPClass)){ + continue; + } + + $aRealClasses[]=$sPHPClass; + } + return $aRealClasses; } protected function SaveClassesToCache(string $sKey, array $aMatchingClasses, array $aMoreInfo): void diff --git a/tests/php-unit-tests/unitary-tests/sources/Service/InterfaceDiscovery/InterfaceDiscoveryTest.php b/tests/php-unit-tests/unitary-tests/sources/Service/InterfaceDiscovery/InterfaceDiscoveryTest.php index 5f3689072..1a3f2aab1 100644 --- a/tests/php-unit-tests/unitary-tests/sources/Service/InterfaceDiscovery/InterfaceDiscoveryTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/Service/InterfaceDiscovery/InterfaceDiscoveryTest.php @@ -26,6 +26,7 @@ class InterfaceDiscoveryTest extends ItopDataTestCase protected function tearDown(): void { + $this->SetNonPublicProperty($this->oInterfaceDiscovery, 'bCheckInterfaceImplementation', true); $this->SetNonPublicProperty(InterfaceDiscovery::GetInstance(), 'aForcedClassMap', null); $this->oCacheService->SetStorageRootDir(null); self::RecurseRmdir($this->sCacheRootDir); @@ -49,6 +50,40 @@ class InterfaceDiscoveryTest extends ItopDataTestCase ); } + /** + * @covers N°8143 - Setup page in error + */ + public function testShouldSelectTheRequestedItopClassesAndExcludeUnexistingOnes() + { + $this->SetNonPublicProperty($this->oInterfaceDiscovery, 'bCheckInterfaceImplementation', false); + $this->GivenClassMap([ + 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory2' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php', + 'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory2' => APPROOT . '/sources/Application/UI/Base/Component/ButtonGroup/ButtonGroupUIBlockFactory.php', + ]); + + $this->AssertArraysHaveSameItems([], $this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class)); + } + + /** + * @covers N°8143 - Setup page in error + */ + public function testReadClassesFromCache_ShouldExcludeUnexistingClasses() + { + $oCacheService = $this->createMock(DataModelDependantCache::class); + $aCachedRealClasses = [ + 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory', + 'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory2', + ]; + $oCacheService->expects($this->once()) + ->method('Fetch') + ->with('InterfaceDiscovery', '123') + ->willReturn($aCachedRealClasses); + + $this->oInterfaceDiscovery->SetCacheService($oCacheService); + + $this->AssertArraysHaveSameItems([ 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory'], $this->oInterfaceDiscovery->ReadClassesFromCache('123')); + } + public function testShouldExcludeAliases() { $this->GivenClassMap([ @@ -87,7 +122,7 @@ class InterfaceDiscoveryTest extends ItopDataTestCase $this->assertGreaterThan(0, count($this->oInterfaceDiscovery->FindItopClasses(iUIBlockFactory::class))); $this->AssertDirectoryListingEquals([ 'autoload_classmaps.php', - '1ab1e62be3e9984a8176deeb20f049b1_iUIBlockFactory.php' + '1ab1e62be3e9984a8176deeb20f049b1_iUIBlockFactory.php', ], $this->sCacheRootDir.'/InterfaceDiscovery'); }