-
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.
feat: add redirect to user group URL after SSO login for MiniOrange i…
…ntegration
- Loading branch information
Showing
3 changed files
with
105 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,43 @@ | ||
<?php | ||
|
||
namespace Municipio\UserGroup; | ||
|
||
use Municipio\Helper\User\Contracts\GetUserGroupUrl; | ||
use Municipio\HooksRegistrar\Hookable; | ||
use WpService\Contracts\AddFilter; | ||
|
||
/** | ||
* Redirect to user group URL after SSO login. | ||
*/ | ||
class RedirectToUserGroupUrlAfterSsoLogin implements Hookable | ||
{ | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(private GetUserGroupUrl $userHelper, private AddFilter $wpService) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function addHooks(): void | ||
{ | ||
$this->wpService->addFilter(\Municipio\Integrations\MiniOrange\AllowRedirectAfterSsoLogin::REDIRECT_URL_FILTER_HOOK, [$this, 'getRedirectUrl'], 10, 1); | ||
} | ||
|
||
/** | ||
* Get redirect URL. | ||
* | ||
* @param string $url | ||
* @return string | ||
*/ | ||
public function getRedirectUrl(string $url): string | ||
{ | ||
if (!$this->userHelper->getUserGroupUrl()) { | ||
return $url; | ||
} | ||
|
||
return $this->userHelper->getUserGroupUrl(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.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,59 @@ | ||
<?php | ||
|
||
namespace Municipio\UserGroup; | ||
|
||
use Municipio\Helper\User\Contracts\GetUserGroupUrl; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Implementations\FakeWpService; | ||
|
||
class RedirectToUserGroupUrlAfterSsoLoginTest extends TestCase | ||
{ | ||
/** | ||
* @testdox class can be instantiated | ||
*/ | ||
public function testCanBeInstantiated() | ||
{ | ||
$userHelper = $this->createMock(GetUserGroupUrl::class); | ||
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); | ||
$this->assertInstanceOf(RedirectToUserGroupUrlAfterSsoLogin::class, $sut); | ||
} | ||
|
||
/** | ||
* @testdox addHooks() adds a filter to the redirect URL | ||
*/ | ||
public function testAddHooksAddsFilterToRedirectUrl() | ||
{ | ||
$userHelper = $this->createMock(GetUserGroupUrl::class); | ||
$wpService = new FakeWpService(['addFilter' => true]); | ||
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, $wpService); | ||
|
||
$sut->addHooks(); | ||
|
||
$this->assertEquals( | ||
\Municipio\Integrations\MiniOrange\AllowRedirectAfterSsoLogin::REDIRECT_URL_FILTER_HOOK, | ||
$wpService->methodCalls['addFilter'][0][0] | ||
); | ||
} | ||
|
||
/** | ||
* @testdox getRedirectUrl() returns the URL from GetUserGroupUrl | ||
*/ | ||
public function testGetRedirectUrlReturnsUrlFromGetUserGroupUrl() | ||
{ | ||
$userHelper = $this->createMock(GetUserGroupUrl::class); | ||
$userHelper->method('getUserGroupUrl')->willReturn('http://example.com'); | ||
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); | ||
$this->assertEquals('http://example.com', $sut->getRedirectUrl('http://example.org')); | ||
} | ||
|
||
/** | ||
* @testdox getRedirectUrl() returns the original URL if GetUserGroupUrl returns null | ||
*/ | ||
public function testGetRedirectUrlReturnsOriginalUrlIfGetUserGroupUrlReturnsNull() | ||
{ | ||
$userHelper = $this->createMock(GetUserGroupUrl::class); | ||
$userHelper->method('getUserGroupUrl')->willReturn(null); | ||
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); | ||
$this->assertEquals('http://example.org', $sut->getRedirectUrl('http://example.org')); | ||
} | ||
} |