Skip to content

Commit

Permalink
IBX-1310: Created slots for assigning/unassigning user to groups (#3125)
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 authored Dec 10, 2021
1 parent 17a60f8 commit da90d00
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
75 changes: 75 additions & 0 deletions eZ/Publish/API/Repository/Tests/UserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,39 @@ public function testAssignUserToUserGroup()
);
}

/**
* @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup
*/
public function testAssignUserToGroupWithLocationsValidation(): void
{
$repository = $this->getRepository();
$userService = $repository->getUserService();
$locationService = $repository->getLocationService();

$administratorGroupId = $this->generateId('group', 12);

$user = $this->createUserVersion1();

$group = $userService->loadUserGroup($administratorGroupId);
$groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId);

// Count number of child locations before assigning user to group
$count = $locationService->getLocationChildCount($groupLocation);
$expectedCount = $count + 1;

$userService->assignUserToUserGroup(
$user,
$group
);

$this->refreshSearch($repository);

// Count number of child locations after assigning user to group
$actualCount = $locationService->getLocationChildCount($groupLocation);

self::assertEquals($expectedCount, $actualCount);
}

/**
* Test for the assignUserToUserGroup() method.
*
Expand Down Expand Up @@ -2224,6 +2257,48 @@ public function testUnAssignUserFromUserGroupThrowsBadStateArgumentException()
/* END: Use Case */
}

/**
* @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
*/
public function testUnAssignUserToGroupWithLocationValidation(): void
{
$repository = $this->getRepository();
$userService = $repository->getUserService();
$locationService = $repository->getLocationService();

$editorsGroupId = $this->generateId('group', 13);
$anonymousGroupId = $this->generateId('group', 42);

$user = $this->createUserVersion1();

$this->refreshSearch($repository);

$group = $userService->loadUserGroup($editorsGroupId);
$groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId);

// Count number of child locations before unassigning user from group
$count = $locationService->getLocationChildCount($groupLocation);
$expectedCount = $count - 1;

// Assigning user to different group to avoid removing all groups from user
$userService->assignUserToUserGroup(
$user,
$userService->loadUserGroup($anonymousGroupId)
);

$userService->unAssignUserFromUserGroup(
$user,
$userService->loadUserGroup($editorsGroupId)
);

$this->refreshSearch($repository);

// Count number of child locations after unassigning user from group
$actualCount = $locationService->getLocationChildCount($groupLocation);

self::assertEquals($expectedCount, $actualCount);
}

/**
* Test that multi-language logic for the loadUserGroup method respects prioritized language list.
*
Expand Down
25 changes: 25 additions & 0 deletions eZ/Publish/Core/Search/Common/Slot/AssignUserToUserGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
*/
namespace eZ\Publish\Core\Search\Common\Slot;

use eZ\Publish\Core\SignalSlot\Signal;

/**
* A Search Engine slot handling AssignUserToUserGroupSignal.
*/
final class AssignUserToUserGroup extends AbstractSubtree
{
public function receive(Signal $signal): void
{
if (!$signal instanceof Signal\UserService\AssignUserToUserGroupSignal) {
return;
}

$content = $this->persistenceHandler->contentHandler()->load($signal->userId);
$this->searchHandler->indexContent($content);
}
}
26 changes: 26 additions & 0 deletions eZ/Publish/Core/Search/Common/Slot/UnAssignUserFromUserGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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.
*/
namespace eZ\Publish\Core\Search\Common\Slot;

use eZ\Publish\Core\SignalSlot\Signal;
use eZ\Publish\Core\Search\Common\Slot;

/**
* A Search Engine slot handling UnAssignUserFromUserGroup.
*/
final class UnAssignUserFromUserGroup extends Slot
{
public function receive(Signal $signal): void
{
if (!$signal instanceof Signal\UserService\UnAssignUserFromUserGroupSignal) {
return;
}

$content = $this->persistenceHandler->contentHandler()->load($signal->userId);
$this->searchHandler->indexContent($content);
}
}

0 comments on commit da90d00

Please sign in to comment.