Skip to content

Commit d27ba0e

Browse files
authored
Merge pull request #22 from elstc/feature/fixes-auto-issuer
fixes AutoIssuerComponent::getIssuerFromUserArray
2 parents 5507a2e + 14b0cbd commit d27ba0e

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/Controller/Component/AutoIssuerComponent.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,14 @@ private function getIssuerFromUserArray(array|ArrayAccess|null $user): ?EntityIn
197197
}
198198

199199
$table = $this->getUserModel();
200+
201+
if ($user instanceof EntityInterface && $user->getSource()) {
202+
return is_a($user, $table->getEntityClass()) ? $user : null;
203+
}
204+
200205
$userId = Hash::get($user, $table->getPrimaryKey());
201206
if ($userId) {
202-
return $table->get($userId);
207+
return $table->find()->where([$table->getPrimaryKey() => $userId])->first();
203208
}
204209

205210
return null;

tests/TestCase/Controller/Component/AutoIssuerComponentTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cake\TestSuite\TestCase;
1313
use Elastic\ActivityLogger\Controller\Component\AutoIssuerComponent;
1414
use PHPUnit\Framework\MockObject\MockObject;
15+
use TestApp\Model\Entity\Author;
1516
use TestApp\Model\Entity\User;
1617
use TestApp\Model\Table\ArticlesTable;
1718
use TestApp\Model\Table\AuthorsTable;
@@ -167,6 +168,58 @@ public function testStartupWithNotAuthenticated(): void
167168
$this->assertNull($this->Authors->getLogIssuer());
168169
}
169170

171+
/**
172+
* Test Controller.startup Event hook
173+
*
174+
* @return void
175+
*/
176+
public function testStartupWithOtherIdentity(): void
177+
{
178+
// Set identity
179+
$user = new Author([
180+
'id' => 1,
181+
]);
182+
$user->setSource('Authors');
183+
$this->request
184+
->method('getAttribute')
185+
->with('identity')
186+
->willReturn($user);
187+
188+
// Dispatch Controller.startup Event
189+
$event = new Event('Controller.startup');
190+
EventManager::instance()->dispatch($event);
191+
192+
// If not authenticated, the issuer will not be set
193+
$this->assertNull($this->Articles->getLogIssuer());
194+
$this->assertNull($this->Comments->getLogIssuer());
195+
$this->assertNull($this->Authors->getLogIssuer());
196+
}
197+
198+
/**
199+
* Test Controller.startup Event hook
200+
*
201+
* @return void
202+
*/
203+
public function testStartupWithUnknownIdentity(): void
204+
{
205+
// Set identity
206+
$this->request
207+
->method('getAttribute')
208+
->with('identity')
209+
->willReturn(new User([
210+
'id' => 0,
211+
]));
212+
213+
// Dispatch Controller.startup Event
214+
$event = new Event('Controller.startup');
215+
EventManager::instance()->dispatch($event);
216+
217+
// If not authenticated, the issuer will not be set
218+
$this->assertNull($this->Articles->getLogIssuer());
219+
$this->assertNull($this->Comments->getLogIssuer());
220+
$this->assertNull($this->Authors->getLogIssuer());
221+
}
222+
170223
/**
171224
* Test Authentication.afterIdentify Event hook
172225
*

0 commit comments

Comments
 (0)