|
12 | 12 | use Cake\TestSuite\TestCase; |
13 | 13 | use Elastic\ActivityLogger\Controller\Component\AutoIssuerComponent; |
14 | 14 | use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use TestApp\Model\Entity\Author; |
15 | 16 | use TestApp\Model\Entity\User; |
16 | 17 | use TestApp\Model\Table\ArticlesTable; |
17 | 18 | use TestApp\Model\Table\AuthorsTable; |
@@ -167,6 +168,58 @@ public function testStartupWithNotAuthenticated(): void |
167 | 168 | $this->assertNull($this->Authors->getLogIssuer()); |
168 | 169 | } |
169 | 170 |
|
| 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 | + |
170 | 223 | /** |
171 | 224 | * Test Authentication.afterIdentify Event hook |
172 | 225 | * |
|
0 commit comments