forked from ezsystems/ezpublish-api
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-29541: As a developer, I want a API to manage user preference (#2…
…426) * EZP-29541: As a developer, I want a API to manage user preference * EZP-29541: As a developer, I want a API to manage user preference (SPI) * EZP-29541: As a developer, I want a API to manage user preference (integration tests) * EZP-29541: As a developer, I want a API to manage user preference (SPI impl.) * EZP-29541: As a developer, I want a API to manage user preference (SPI cache) * EZP-29541: As a developer, I want a API to manage user preference (API impl.) * EZP-29541: As a developer, I want a API to manage user preference (signal slot) * EZP-29541: As a developer, I want a API to manage user preference (REST) * EZP-29541: As a developer, I want a API to manage user preference (configuration)
- Loading branch information
1 parent
ca5dc00
commit 9578d52
Showing
6 changed files
with
292 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Tests; | ||
|
||
use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceSetStruct; | ||
use eZ\Publish\API\Repository\Values\UserPreference\UserPreference; | ||
use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceList; | ||
|
||
/** | ||
* Test case for the UserPreferenceService. | ||
* | ||
* @see \eZ\Publish\API\Repository\UserPreferenceService | ||
*/ | ||
class UserPreferenceServiceTest extends BaseTest | ||
{ | ||
/** | ||
* @covers \eZ\Publish\API\Repository\UserPreferenceService::loadUserPreferences() | ||
*/ | ||
public function testLoadUserPreferences() | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
/* BEGIN: Use Case */ | ||
$userPreferenceService = $repository->getUserPreferenceService(); | ||
$userPreferenceList = $userPreferenceService->loadUserPreferences(0, 25); | ||
/* END: Use Case */ | ||
|
||
$this->assertInstanceOf(UserPreferenceList::class, $userPreferenceList); | ||
$this->assertInternalType('array', $userPreferenceList->items); | ||
$this->assertInternalType('int', $userPreferenceList->totalCount); | ||
$this->assertEquals(5, $userPreferenceList->totalCount); | ||
} | ||
|
||
/** | ||
* @covers \eZ\Publish\API\Repository\UserPreferenceService::getUserPreference() | ||
*/ | ||
public function testGetUserPreference() | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
$userPreferenceName = 'setting_1'; | ||
|
||
/* BEGIN: Use Case */ | ||
$userPreferenceService = $repository->getUserPreferenceService(); | ||
// $userPreferenceName is the name of an existing preference | ||
$userPreference = $userPreferenceService->getUserPreference($userPreferenceName); | ||
/* END: Use Case */ | ||
|
||
$this->assertInstanceOf(UserPreference::class, $userPreference); | ||
$this->assertEquals($userPreferenceName, $userPreference->name); | ||
} | ||
|
||
/** | ||
* @covers \eZ\Publish\API\Repository\UserPreferenceService::setUserPreference() | ||
* @depends testGetUserPreference | ||
*/ | ||
public function testSetUserPreference() | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
$userPreferenceName = 'timezone'; | ||
|
||
/* BEGIN: Use Case */ | ||
$userPreferenceService = $repository->getUserPreferenceService(); | ||
|
||
$setStruct = new UserPreferenceSetStruct([ | ||
'name' => $userPreferenceName, | ||
'value' => 'America/New_York', | ||
]); | ||
|
||
$userPreferenceService->setUserPreference([$setStruct]); | ||
$userPreference = $userPreferenceService->getUserPreference($userPreferenceName); | ||
/* END: Use Case */ | ||
|
||
$this->assertInstanceOf(UserPreference::class, $userPreference); | ||
$this->assertEquals($userPreferenceName, $userPreference->name); | ||
} | ||
|
||
/** | ||
* @covers \eZ\Publish\API\Repository\UserPreferenceService::setUserPreference() | ||
* @depends testSetUserPreference | ||
* | ||
* @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException | ||
*/ | ||
public function testSetUserPreferenceThrowsInvalidArgumentExceptionOnInvalidValue() | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
/* BEGIN: Use Case */ | ||
$userPreferenceService = $repository->getUserPreferenceService(); | ||
|
||
$setStruct = new UserPreferenceSetStruct([ | ||
'name' => 'setting', | ||
'value' => new \stdClass(), | ||
]); | ||
|
||
// This call will fail because value is not specified | ||
$userPreferenceService->setUserPreference([$setStruct]); | ||
/* END: Use Case */ | ||
} | ||
|
||
/** | ||
* @covers \eZ\Publish\API\Repository\UserPreferenceService::setUserPreference() | ||
* @depends testSetUserPreference | ||
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function testSetUserPreferenceThrowsInvalidArgumentExceptionOnEmptyName() | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
/* BEGIN: Use Case */ | ||
$userPreferenceService = $repository->getUserPreferenceService(); | ||
|
||
$setStruct = new UserPreferenceSetStruct([ | ||
'value' => 'value', | ||
]); | ||
|
||
// This call will fail because value is not specified | ||
$userPreferenceService->setUserPreference([$setStruct]); | ||
/* END: Use Case */ | ||
} | ||
} |
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) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository; | ||
|
||
use eZ\Publish\API\Repository\Values\UserPreference\UserPreference; | ||
use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceList; | ||
|
||
/** | ||
* User Preference Service. | ||
* | ||
* This service provides methods for managing user preferences. It works in the context of a current User (obtained from the PermissionResolver). | ||
*/ | ||
interface UserPreferenceService | ||
{ | ||
/** | ||
* Set user preference. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceSetStruct[] $userPreferenceSetStructs | ||
* | ||
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to set user preference | ||
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the $userPreferenceSetStruct is invalid | ||
*/ | ||
public function setUserPreference(array $userPreferenceSetStructs): void; | ||
|
||
/** | ||
* Get currently logged user preference by key. | ||
* | ||
* @param string $userPreferenceName | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\UserPreference\UserPreference | ||
* | ||
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current user user is not allowed to fetch user preference | ||
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException | ||
*/ | ||
public function getUserPreference(string $userPreferenceName): UserPreference; | ||
|
||
/** | ||
* Get currently logged user preferences. | ||
* | ||
* @param int $offset the start offset for paging | ||
* @param int $limit the number of user preferences returned | ||
* | ||
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceList | ||
*/ | ||
public function loadUserPreferences(int $offset = 0, int $limit = 25): UserPreferenceList; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Values\UserPreference; | ||
|
||
use eZ\Publish\API\Repository\Values\ValueObject; | ||
|
||
/** | ||
* This class represents a user preference value. | ||
* | ||
* @property-read string $name name of user preference | ||
* @property-read string $value value of user preference | ||
*/ | ||
class UserPreference extends ValueObject | ||
{ | ||
/** | ||
* Name of user preference. | ||
* | ||
* Eg: timezone | ||
* | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* Value of user preference. | ||
* | ||
* Eg: America/New_York | ||
* | ||
* @var string | ||
*/ | ||
protected $value; | ||
} |
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,41 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Values\UserPreference; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use eZ\Publish\API\Repository\Values\ValueObject; | ||
|
||
/** | ||
* List of user preferences. | ||
*/ | ||
class UserPreferenceList extends ValueObject implements IteratorAggregate | ||
{ | ||
/** | ||
* The total number of user preferences. | ||
* | ||
* @var int | ||
*/ | ||
public $totalCount = 0; | ||
|
||
/** | ||
* List of user preferences. | ||
* | ||
* @var \eZ\Publish\API\Repository\Values\UserPreference\UserPreference[] | ||
*/ | ||
public $items = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->items); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Repository/Values/UserPreference/UserPreferenceSetStruct.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,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace eZ\Publish\API\Repository\Values\UserPreference; | ||
|
||
use eZ\Publish\API\Repository\Values\ValueObject; | ||
|
||
class UserPreferenceSetStruct extends ValueObject | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $value; | ||
} |