From f551dbd0adc34f94d0131c095497242dfdec5b7c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 30 Dec 2024 20:59:00 +0700 Subject: [PATCH] fix: handle namespaced helper found on Common helper --- system/Common.php | 2 +- tests/system/CommonHelperTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/system/Common.php b/system/Common.php index b647c9d247ad..d5880acf0363 100644 --- a/system/Common.php +++ b/system/Common.php @@ -598,7 +598,7 @@ function helper($filenames): void if (str_contains($filename, '\\')) { $path = $loader->locateFile($filename, 'Helpers'); - if ($path !== '') { + if ($path === false) { throw FileNotFoundException::forFileNotFound($filename); } diff --git a/tests/system/CommonHelperTest.php b/tests/system/CommonHelperTest.php index db304ecfdb70..8a41f7972a9e 100644 --- a/tests/system/CommonHelperTest.php +++ b/tests/system/CommonHelperTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter; +use CodeIgniter\Autoloader\Autoloader; use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Files\Exceptions\FileNotFoundException; use CodeIgniter\Test\CIUnitTestCase; use Config\Services; use PHPUnit\Framework\Attributes\CoversFunction; @@ -148,4 +150,33 @@ function foo_bar_baz(): string $this->assertSame($this->dummyHelpers[0], foo_bar_baz()); } + + public function testNamespacedHelperNotFound(): void + { + $this->expectException(FileNotFoundException::class); + + $locator = $this->getMockLocator(); + Services::injectMock('locator', $locator); + + helper('foo\barbaz'); + } + + public function testNamespacedHelperFound(): void + { + $autoloader = new Autoloader(); + $autoloader->addNamespace('Tests\Support\Helpers', TESTPATH . '_support/Helpers'); + $locator = new FileLocator($autoloader); + + Services::injectMock('locator', $locator); + + $found = true; + + try { + helper('Tests\Support\Helpers\baguette'); + } catch (FileNotFoundException) { + $found = false; + } + + $this->assertTrue($found); + } }