Skip to content

Commit

Permalink
feat: handle all CRUD for UserGroup on main site (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink authored Jan 17, 2025
1 parent a52fccb commit c99a63c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 53 deletions.
9 changes: 5 additions & 4 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(
*/
$userGroupConfig = new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService);
$userHelperConfig = new \Municipio\Helper\User\Config\UserConfig();
$userHelper = new \Municipio\Helper\User\User($this->wpService, $this->acfService, $userHelperConfig, $userGroupConfig);
$userHelper = new \Municipio\Helper\User\User($this->wpService, $this->acfService, $userHelperConfig, $userGroupConfig, new \Municipio\Helper\Term\Term($this->wpService, $this->acfService));

/**
* User group
Expand Down Expand Up @@ -391,7 +391,7 @@ private function setUpBrokenLinksIntegration(): void
private function setupLoginLogout(): void
{
//Needs setUser to be called before using the user object
$userHelper = new User($this->wpService, $this->acfService, new UserConfig(), new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService));
$userHelper = new User($this->wpService, $this->acfService, new UserConfig(), new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService), new \Municipio\Helper\Term\Term($this->wpService, $this->acfService));

$filterAuthUrls = new \Municipio\Admin\Login\RelationalLoginLogourUrls($this->wpService);
$filterAuthUrls->addHooks();
Expand Down Expand Up @@ -432,7 +432,7 @@ private function setupUserGroupFeature(): void
// Setup dependencies
$userGroupRestrictionConfig = new \Municipio\Admin\Private\Config\UserGroupRestrictionConfig();
$userHelperConfig = new \Municipio\Helper\User\Config\UserConfig();
$userHelper = new \Municipio\Helper\User\User($this->wpService, $this->acfService, $userHelperConfig, $config);
$userHelper = new \Municipio\Helper\User\User($this->wpService, $this->acfService, $userHelperConfig, $config, new \Municipio\Helper\Term\Term($this->wpService, $this->acfService));

// Create user group taxonomy
(new \Municipio\UserGroup\CreateUserGroupTaxonomy($this->wpService, $config))->addHooks();
Expand Down Expand Up @@ -468,6 +468,7 @@ private function setupUserGroupFeature(): void
*/
private function setUpMiniOrangeIntegration(): void
{
$userHelper = new \Municipio\Helper\User\User($this->wpService, $this->acfService, new \Municipio\Helper\User\Config\UserConfig(), new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService), new \Municipio\Helper\Term\Term($this->wpService, $this->acfService));
$termHelper = new \Municipio\Helper\Term\Term($this->wpService, $this->acfService);
$userGroupConfig = new \Municipio\UserGroup\Config\UserGroupConfig($this->wpService);
$config = new \Municipio\Integrations\MiniOrange\Config\MiniOrangeConfig($this->wpService);
Expand Down Expand Up @@ -495,7 +496,7 @@ private function setUpMiniOrangeIntegration(): void
}

// Set group as taxonomy
$setGroupAsTaxonomy = new \Municipio\Integrations\MiniOrange\SetUserGroupFromSsoLoginGroup($this->wpService, $termHelper, $userGroupConfig);
$setGroupAsTaxonomy = new \Municipio\Integrations\MiniOrange\SetUserGroupFromSsoLoginGroup($this->wpService, $userHelper);
$setGroupAsTaxonomy->addHooks();
}

Expand Down
16 changes: 16 additions & 0 deletions library/Helper/User/Contracts/SetUserGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Municipio\Helper\User\Contracts;

interface SetUserGroup
{
/**
* Set user group from group name.
*
* @param string $groupName
* @param null|\WP_User|int $user
*
* @return void
*/
public function setUserGroup(string $groupName, null|\WP_User|int $user = null): void;
}
77 changes: 67 additions & 10 deletions library/Helper/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Municipio\Helper\User;

use WpService\Contracts\{AddQueryArg, GetBlogDetails, GetUserMeta, IsWpError, WpGetObjectTerms, WpGetCurrentUser, GetUserBy};
use AcfService\Contracts\GetField;
use Municipio\Helper\Term\Contracts\CreateOrGetTermIdFromString;
use Municipio\Helper\User\Config\UserConfigInterface;
use Municipio\Helper\User\Contracts\{GetRedirectToGroupUrl, UserHasRole, GetUserGroup, GetUserGroupUrl, GetUserGroupUrlType, GetUserPrefersGroupUrl, GetUser};
use Municipio\Helper\User\Contracts\{GetRedirectToGroupUrl, UserHasRole, GetUserGroup, GetUserGroupUrl, GetUserGroupUrlType, GetUserPrefersGroupUrl, GetUser, SetUserGroup};
use Municipio\Helper\User\FieldResolver\UserGroupUrl;
use Municipio\UserGroup\Config\UserGroupConfigInterface;
use WP_Term;
use WP_User;
use WpService\WpService;

/**
* User helper.
Expand All @@ -21,16 +22,18 @@ class User implements
GetUserGroupUrlType,
GetUserPrefersGroupUrl,
GetUser,
GetRedirectToGroupUrl
GetRedirectToGroupUrl,
SetUserGroup
{
/**
* Constructor.
*/
public function __construct(
private WpGetCurrentUser&WpGetObjectTerms&IsWpError&GetUserMeta&GetBlogDetails&GetUserBy&AddQueryArg $wpService,
private WpService $wpService,
private GetField $acfService,
private UserConfigInterface $userConfig,
private UserGroupConfigInterface $userGroupConfig
private UserGroupConfigInterface $userGroupConfig,
private CreateOrGetTermIdFromString $termHelper
) {
}

Expand Down Expand Up @@ -95,7 +98,10 @@ public function getUserGroup(null|\WP_User|int $user = null): ?WP_Term
return null;
}

$this->maybeSwitchToMainSite();
$userGroup = $this->wpService->wpGetObjectTerms($user->ID, $this->userGroupConfig->getUserGroupTaxonomy());
$this->switchToCurrentBlogIfSwitched();

if (empty($userGroup) || $this->wpService->isWpError($userGroup)) {
return null;
}
Expand Down Expand Up @@ -143,7 +149,9 @@ public function getUserGroupUrlType(?WP_Term $term = null, null|WP_User|int $use
$term ??= $this->getUserGroup($user);
$termId = $this->userGroupConfig->getUserGroupTaxonomy($user) . '_' . $term->term_id;

$this->maybeSwitchToMainSite();
return $this->acfService->getField('user_group_type_of_link', $termId) ?: null;
$this->switchToCurrentBlogIfSwitched();
}

/**
Expand All @@ -157,11 +165,10 @@ public function getUserPrefersGroupUrl(null|WP_User|int $user = null): ?bool
return null;
}

$perfersGroupUrl = $this->wpService->getUserMeta(
$user->ID,
$this->userConfig->getUserPrefersGroupUrlMetaKey(),
true
);
$this->maybeSwitchToMainSite();
$perfersGroupUrl = $this->wpService->getUserMeta($user->ID, $this->userConfig->getUserPrefersGroupUrlMetaKey(), true);
$this->switchToCurrentBlogIfSwitched();

if ($perfersGroupUrl) {
return true;
}
Expand Down Expand Up @@ -191,4 +198,54 @@ public function getRedirectToGroupUrl(null|WP_User|int $user = null): ?string

return null;
}

/**
* Switch to main site if multisite and not on main site.
*/
private function maybeSwitchToMainSite(): void
{
if (!$this->wpService->isMultisite() || $this->wpService->isMainSite()) {
return;
}

if ($this->wpService->getMainSiteId() === $this->wpService->getCurrentBlogId()) {
return;
}

$this->wpService->switchToBlog($this->wpService->getMainSiteId());
}

/**
* Switch back from main site if multisite and switched.
*/
private function switchToCurrentBlogIfSwitched(): void
{
if (!$this->wpService->isMultisite() || !$this->wpService->msIsSwitched()) {
return;
}

$this->wpService->restoreCurrentBlog();
}

/**
* @inheritDoc
*/
public function setUserGroup(string $groupName, null|WP_User|int $user = null): void
{
$user = $this->getUser($user);

if (!$user) {
return;
}

$taxonomy = $this->userGroupConfig->getUserGroupTaxonomy();

$this->maybeSwitchToMainSite();

if ($termId = $this->termHelper->createOrGetTermIdFromString($groupName, $taxonomy)) {
$this->wpService->wpSetObjectTerms($user->ID, $termId, $taxonomy, false);
}

$this->switchToCurrentBlogIfSwitched();
}
}
19 changes: 13 additions & 6 deletions library/Helper/User/User.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Municipio\Helper\User;

use AcfService\Implementations\FakeAcfService;
use Municipio\Helper\Term\Contracts\CreateOrGetTermIdFromString;
use Municipio\Helper\User\Config\UserConfigInterface;
use Municipio\TestUtils\WpMockFactory;
use Municipio\UserGroup\Config\UserGroupConfigInterface;
Expand All @@ -20,7 +21,8 @@ public function testCanBeInstantiated()
new FakeWpService(),
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class)
);

$this->assertInstanceOf(User::class, $user);
Expand All @@ -35,7 +37,8 @@ public function testGetUserReturnsSameUserAsProvidedIfUserOfTypeWPUserIsProvided
new FakeWpService(),
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class)
);

$wpUser = WpMockFactory::createWpUser(['ID' => 1]);
Expand All @@ -52,7 +55,8 @@ public function testGetUserReturnsNullIfUserOfTypeWPUserIsProvidedAndIdIs0()
new FakeWpService(),
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class)
);

$wpUser = WpMockFactory::createWpUser(['ID' => 0]);
Expand All @@ -71,7 +75,8 @@ public function testGetUserReturnsCurrentUserIfNoUserIsProvidedAndUserIsLoggedIn
$wpService,
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class)
);

$this->assertEquals(123, $user->getUser()->ID);
Expand All @@ -90,7 +95,8 @@ public function testGetUserReturnsUserFromDbIfUserIdIsProvidedAndUserExists()
$wpService,
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class)
);

$this->assertEquals(123, $user->getUser(123)->ID);
Expand All @@ -109,7 +115,8 @@ public function testGetUserReturnsNullIfUserIdIsProvidedAndUserDoesNotExist()
$wpService,
new FakeAcfService(),
$this->createStub(UserConfigInterface::class),
$this->createStub(UserGroupConfigInterface::class)
$this->createStub(UserGroupConfigInterface::class),
$this->createStub(CreateOrGetTermIdFromString::class),
);

$this->assertNull($user->getUser(123));
Expand Down
15 changes: 3 additions & 12 deletions library/Integrations/MiniOrange/SetUserGroupFromSsoLoginGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Municipio\Integrations\MiniOrange;

use Municipio\Helper\Term\Contracts\CreateOrGetTermIdFromString;
use Municipio\Helper\User\Contracts\SetUserGroup;
use Municipio\HooksRegistrar\Hookable;
use Municipio\UserGroup\Config\UserGroupConfigInterface;
use WpService\Contracts\{AddAction, GetTermBy, IsWpError, WpInsertTerm, WpSetObjectTerms};

/**
Expand All @@ -17,8 +16,7 @@ class SetUserGroupFromSsoLoginGroup implements Hookable
*/
public function __construct(
private AddAction&WpSetObjectTerms&GetTermBy&WpInsertTerm&IsWpError $wpService,
private CreateOrGetTermIdFromString $termHelper,
private UserGroupConfigInterface $config
private SetUserGroup $userHelper
) {
}

Expand All @@ -41,20 +39,13 @@ public function addHooks(): void
*/
public function setUserGroupFromSsoLoginGroup(int $userId, string|array $groupName): void
{
$taxonomy = $this->config->getUserGroupTaxonomy();
$groupName = $this->getGroupNameFromMixed($groupName);

if (!$groupName || is_numeric($groupName)) {
return;
}

if (!$userId) {
return;
}

if ($termId = $this->termHelper->createOrGetTermIdFromString($groupName, $taxonomy)) {
$this->wpService->wpSetObjectTerms($userId, $termId, $taxonomy, false);
}
$this->userHelper->setUserGroup($groupName, $userId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Municipio\Integrations\MiniOrange;

use Municipio\Helper\Term\Contracts\CreateOrGetTermIdFromString;
use Municipio\UserGroup\Config\UserGroupConfigInterface;
use Municipio\Helper\User\Contracts\SetUserGroup;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;

Expand All @@ -14,9 +13,8 @@ class SetUserGroupFromSsoLoginGroupTest extends TestCase
*/
public function testCanBeInstantiated()
{
$termHelper = $this->createMock(CreateOrGetTermIdFromString::class);
$config = $this->createMock(UserGroupConfigInterface::class);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup(new FakeWpService(), $termHelper, $config);
$userHelper = $this->createMock(SetUserGroup::class);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup(new FakeWpService(), $userHelper);

$this->assertInstanceOf(SetUserGroupFromSsoLoginGroup::class, $setGroupAsTaxonomy);
}
Expand All @@ -27,10 +25,9 @@ public function testCanBeInstantiated()
public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfGroupNameIsNumeric()
{
$wpService = new FakeWpService();
$termHelper = $this->createMock(CreateOrGetTermIdFromString::class);
$config = $this->createMock(UserGroupConfigInterface::class);
$userHelper = $this->createMock(SetUserGroup::class);

$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $termHelper, $config);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $userHelper);
$setGroupAsTaxonomy->setUserGroupFromSsoLoginGroup(1, 1);

$this->assertArrayNotHasKey('wpSetObjectTerms', $wpService->methodCalls);
Expand All @@ -42,10 +39,9 @@ public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfGroup
public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfGroupNameIsEmpty()
{
$wpService = new FakeWpService();
$termHelper = $this->createMock(CreateOrGetTermIdFromString::class);
$config = $this->createMock(UserGroupConfigInterface::class);
$userHelper = $this->createMock(SetUserGroup::class);

$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $termHelper, $config);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $userHelper);
$setGroupAsTaxonomy->setUserGroupFromSsoLoginGroup(1, '');

$this->assertArrayNotHasKey('wpSetObjectTerms', $wpService->methodCalls);
Expand All @@ -57,10 +53,9 @@ public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfGroup
public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfUserIdIsEmpty()
{
$wpService = new FakeWpService();
$termHelper = $this->createMock(CreateOrGetTermIdFromString::class);
$config = $this->createMock(UserGroupConfigInterface::class);
$userHelper = $this->createMock(SetUserGroup::class);

$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $termHelper, $config);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $userHelper);
$setGroupAsTaxonomy->setUserGroupFromSsoLoginGroup(0, 'group');

$this->assertArrayNotHasKey('wpSetObjectTerms', $wpService->methodCalls);
Expand All @@ -72,14 +67,10 @@ public function testSetUserGroupFromSsoLoginGroupDoesNotConnectUserToTermIfUserI
public function testSetUserGroupFromSsoLoginGroupConnectsUserToTerm()
{
$wpService = new FakeWpService(['wpSetObjectTerms' => []]);
$termHelper = $this->createMock(CreateOrGetTermIdFromString::class);
$config = $this->createMock(UserGroupConfigInterface::class);
$userHelper = $this->createMock(SetUserGroup::class);
$userHelper->expects($this->once())->method('setUserGroup');

$termHelper->method('createOrGetTermIdFromString')->willReturn(1);

$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $termHelper, $config);
$setGroupAsTaxonomy = new SetUserGroupFromSsoLoginGroup($wpService, $userHelper);
$setGroupAsTaxonomy->setUserGroupFromSsoLoginGroup(1, 'group');

$this->assertArrayHasKey('wpSetObjectTerms', $wpService->methodCalls);
}
}

0 comments on commit c99a63c

Please sign in to comment.