Skip to content

Commit

Permalink
EZP-29541: As a developer, I want a API to manage user preference (#2…
Browse files Browse the repository at this point in the history
…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
mikadamczyk authored and Łukasz Serwatka committed Aug 29, 2018
1 parent ca5dc00 commit 9578d52
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Repository/Repository.php
Expand Up @@ -219,6 +219,13 @@ public function getBookmarkService();
*/
public function getNotificationService();

/**
* Get UserPreferenceService.
*
* @return \eZ\Publish\API\Repository\UserPreferenceService
*/
public function getUserPreferenceService();

/**
* Begin transaction.
*
Expand Down
128 changes: 128 additions & 0 deletions Repository/Tests/UserPreferenceServiceTest.php
@@ -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 */
}
}
54 changes: 54 additions & 0 deletions Repository/UserPreferenceService.php
@@ -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;
}
38 changes: 38 additions & 0 deletions Repository/Values/UserPreference/UserPreference.php
@@ -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;
}
41 changes: 41 additions & 0 deletions Repository/Values/UserPreference/UserPreferenceList.php
@@ -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 Repository/Values/UserPreference/UserPreferenceSetStruct.php
@@ -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;
}

0 comments on commit 9578d52

Please sign in to comment.