-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/lib/MVC/Symfony/Templating/Twig/Extension/UserExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Core\MVC\Symfony\Templating\Twig\Extension; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\UserService; | ||
use Ibexa\Contracts\Core\Repository\Values\User\User; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class UserExtension extends AbstractExtension | ||
{ | ||
private UserService $userService; | ||
|
||
private PermissionResolver $permissionResolver; | ||
|
||
public function __construct(UserService $userService, PermissionResolver $permissionResolver) | ||
{ | ||
$this->userService = $userService; | ||
$this->permissionResolver = $permissionResolver; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction( | ||
'ibexa_get_current_user', | ||
[$this, 'getCurrentUser'] | ||
), | ||
new TwigFunction( | ||
'ibexa_is_current_user', | ||
[$this, 'isCurrentUser'] | ||
), | ||
]; | ||
} | ||
|
||
public function getCurrentUser(): User | ||
{ | ||
return $this->userService->loadUser( | ||
$this->permissionResolver->getCurrentUserReference()->getUserId() | ||
); | ||
} | ||
|
||
public function isCurrentUser(User $user): bool | ||
{ | ||
return $this->permissionResolver->getCurrentUserReference()->getUserId() === $user->getUserId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tests/lib/MVC/Symfony/Templating/Twig/Extension/UserExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Core\MVC\Symfony\Templating\Twig\Extension; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\UserService; | ||
use Ibexa\Contracts\Core\Repository\Values\User\User; | ||
use Ibexa\Contracts\Core\Repository\Values\User\UserReference; | ||
use Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\UserExtension; | ||
use Twig\Test\IntegrationTestCase; | ||
|
||
final class UserExtensionTest extends IntegrationTestCase | ||
{ | ||
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver&\PHPUnit\Framework\MockObject\MockObject */ | ||
private PermissionResolver $permissionResolver; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\UserService&\PHPUnit\Framework\MockObject\MockObject */ | ||
private UserService $userService; | ||
|
||
/** @var array<int, \Ibexa\Contracts\Core\Repository\Values\User\User> */ | ||
private array $users = []; | ||
|
||
private int $currentUserId; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->userService = $this->createMock(UserService::class); | ||
$this->userService | ||
->method('loadUser') | ||
->willReturnCallback(fn (int $id): User => $this->users[$id]); | ||
|
||
$this->permissionResolver = $this->createMock(PermissionResolver::class); | ||
$this->permissionResolver | ||
->method('getCurrentUserReference') | ||
->willReturnCallback(function (): UserReference { | ||
$reference = $this->createMock(UserReference::class); | ||
$reference->method('getUserId')->willReturn($this->currentUserId); | ||
|
||
return $reference; | ||
}); | ||
|
||
$this->getUser(10, true); | ||
} | ||
|
||
protected function getExtensions(): array | ||
{ | ||
return [ | ||
new UserExtension( | ||
$this->userService, | ||
$this->permissionResolver | ||
), | ||
]; | ||
} | ||
|
||
public function getUser(int $id, bool $isCurrent = false): User | ||
{ | ||
if (!isset($this->users[$id])) { | ||
$user = $this->createMock(User::class); | ||
$user->method('getUserId')->willReturn($id); | ||
|
||
$this->users[$id] = $user; | ||
|
||
if ($isCurrent) { | ||
$this->currentUserId = $id; | ||
} | ||
} | ||
|
||
return $this->users[$id]; | ||
} | ||
|
||
protected function getFixturesDir(): string | ||
{ | ||
return __DIR__ . '/_fixtures/user_functions'; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...Symfony/Templating/Twig/Extension/_fixtures/content_functions/ibexa_field_group_name.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
"ibexa_field_group_name" function | ||
--TEMPLATE-- | ||
{{ ibexa_field_group_name('content') }} | ||
--DATA-- | ||
return []; | ||
--EXPECT-- | ||
Content |
23 changes: 23 additions & 0 deletions
23
...ib/MVC/Symfony/Templating/Twig/Extension/_fixtures/content_functions/ibexa_has_field.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
"ibexa_has_field" function | ||
--TEMPLATE-- | ||
{{ ibexa_has_field(content, 'existing') ? 'YES' : 'NO' }} | ||
{{ ibexa_has_field(content, 'non-existing') ? 'YES' : 'NO' }} | ||
|
||
--DATA-- | ||
return [ | ||
'content' => $this->getContent( | ||
'test_content', | ||
[ | ||
'ezstring' => [ | ||
'id' => 125, | ||
'fieldDefIdentifier' => 'existing', | ||
'value' => 'value', | ||
'languageCode' => 'eng-GB', | ||
], | ||
] | ||
), | ||
]; | ||
--EXPECT-- | ||
YES | ||
NO |
8 changes: 8 additions & 0 deletions
8
...VC/Symfony/Templating/Twig/Extension/_fixtures/user_functions/ibexa_get_current_user.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
"ibexa_get_current_user" function | ||
--TEMPLATE-- | ||
{{ ibexa_get_current_user().getUserId() }} | ||
--DATA-- | ||
return []; | ||
--EXPECT-- | ||
10 |
13 changes: 13 additions & 0 deletions
13
...MVC/Symfony/Templating/Twig/Extension/_fixtures/user_functions/ibexa_is_current_user.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
"ibexa_is_current_user" function | ||
--TEMPLATE-- | ||
{{ ibexa_is_current_user(user_foo) ? 'YES' : 'NO' }} | ||
{{ ibexa_is_current_user(user_bar) ? 'YES' : 'NO' }} | ||
--DATA-- | ||
return [ | ||
'user_foo' => $this->getUser(10, true), | ||
'user_bar' => $this->getUser(11, false), | ||
]; | ||
--EXPECT-- | ||
YES | ||
NO |