From 14b0cbdf71104abdcd02d7a9d4bdd1674da56295 Mon Sep 17 00:00:00 2001 From: nojimage Date: Fri, 6 Sep 2024 11:39:31 +0900 Subject: [PATCH] fixes AutoIssuerComponent::getIssuerFromUserArray --- .../Component/AutoIssuerComponent.php | 7 ++- .../Component/AutoIssuerComponentTest.php | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/AutoIssuerComponent.php b/src/Controller/Component/AutoIssuerComponent.php index 1d92861..f1e7533 100644 --- a/src/Controller/Component/AutoIssuerComponent.php +++ b/src/Controller/Component/AutoIssuerComponent.php @@ -197,9 +197,14 @@ private function getIssuerFromUserArray(array|ArrayAccess|null $user): ?EntityIn } $table = $this->getUserModel(); + + if ($user instanceof EntityInterface && $user->getSource()) { + return is_a($user, $table->getEntityClass()) ? $user : null; + } + $userId = Hash::get($user, $table->getPrimaryKey()); if ($userId) { - return $table->get($userId); + return $table->find()->where([$table->getPrimaryKey() => $userId])->first(); } return null; diff --git a/tests/TestCase/Controller/Component/AutoIssuerComponentTest.php b/tests/TestCase/Controller/Component/AutoIssuerComponentTest.php index 42b9364..34b5184 100644 --- a/tests/TestCase/Controller/Component/AutoIssuerComponentTest.php +++ b/tests/TestCase/Controller/Component/AutoIssuerComponentTest.php @@ -12,6 +12,7 @@ use Cake\TestSuite\TestCase; use Elastic\ActivityLogger\Controller\Component\AutoIssuerComponent; use PHPUnit\Framework\MockObject\MockObject; +use TestApp\Model\Entity\Author; use TestApp\Model\Entity\User; use TestApp\Model\Table\ArticlesTable; use TestApp\Model\Table\AuthorsTable; @@ -167,6 +168,58 @@ public function testStartupWithNotAuthenticated(): void $this->assertNull($this->Authors->getLogIssuer()); } + /** + * Test Controller.startup Event hook + * + * @return void + */ + public function testStartupWithOtherIdentity(): void + { + // Set identity + $user = new Author([ + 'id' => 1, + ]); + $user->setSource('Authors'); + $this->request + ->method('getAttribute') + ->with('identity') + ->willReturn($user); + + // Dispatch Controller.startup Event + $event = new Event('Controller.startup'); + EventManager::instance()->dispatch($event); + + // If not authenticated, the issuer will not be set + $this->assertNull($this->Articles->getLogIssuer()); + $this->assertNull($this->Comments->getLogIssuer()); + $this->assertNull($this->Authors->getLogIssuer()); + } + + /** + * Test Controller.startup Event hook + * + * @return void + */ + public function testStartupWithUnknownIdentity(): void + { + // Set identity + $this->request + ->method('getAttribute') + ->with('identity') + ->willReturn(new User([ + 'id' => 0, + ])); + + // Dispatch Controller.startup Event + $event = new Event('Controller.startup'); + EventManager::instance()->dispatch($event); + + // If not authenticated, the issuer will not be set + $this->assertNull($this->Articles->getLogIssuer()); + $this->assertNull($this->Comments->getLogIssuer()); + $this->assertNull($this->Authors->getLogIssuer()); + } + /** * Test Authentication.afterIdentify Event hook *