Skip to content

Commit

Permalink
Merge pull request #22 from elstc/feature/fixes-auto-issuer
Browse files Browse the repository at this point in the history
fixes AutoIssuerComponent::getIssuerFromUserArray
  • Loading branch information
nojimage authored Sep 6, 2024
2 parents 5507a2e + 14b0cbd commit d27ba0e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Controller/Component/AutoIssuerComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Controller/Component/AutoIssuerComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit d27ba0e

Please sign in to comment.