diff --git a/src/Plugin/RulesAction/UserCreateLoginUrl.php b/src/Plugin/RulesAction/UserCreateLoginUrl.php new file mode 100644 index 00000000..fdf2c3a2 --- /dev/null +++ b/src/Plugin/RulesAction/UserCreateLoginUrl.php @@ -0,0 +1,43 @@ +isAuthenticated() && $user->isActive()) { + $value = user_pass_reset_url($user); + $this->setProvidedValue('one_time_login_url', $value); + } + } + +} diff --git a/tests/src/Functional/ActionsFormTest.php b/tests/src/Functional/ActionsFormTest.php index 48fc50a8..fca898a5 100644 --- a/tests/src/Functional/ActionsFormTest.php +++ b/tests/src/Functional/ActionsFormTest.php @@ -406,14 +406,17 @@ class ActionsFormTest extends RulesBrowserTestBase { [], ['user'], ], + '35. Create login URL' => [ + 'rules_user_create_login_url', + ], // Ban. - '35. Ban IP' => [ + '36. Ban IP' => [ 'rules_ban_ip', [], ['ip' => '192.0.2.1'], ], - '36. Unban IP' => [ + '37. Unban IP' => [ 'rules_unban_ip', [], ['ip' => '192.0.2.1'], diff --git a/tests/src/Unit/Integration/RulesAction/UserCreateLoginUrlTest.php b/tests/src/Unit/Integration/RulesAction/UserCreateLoginUrlTest.php new file mode 100644 index 00000000..d1ba4c4f --- /dev/null +++ b/tests/src/Unit/Integration/RulesAction/UserCreateLoginUrlTest.php @@ -0,0 +1,128 @@ +enableModule('user'); + + $this->action = $this->actionManager->createInstance('rules_user_create_login_url'); + } + + /** + * Test the summary method. + * + * @covers ::summary + */ + public function testSummary() { + $this->assertEquals('Generate one-time log in URL', $this->action->summary()); + } + + /** + * Test execute() method for active and authenticated users. + * + * @covers ::execute + */ + public function testUserCreateLoginUrlWithValidUser() { + $user = $this->getUserMock(self::ACTIVE, self::AUTHENTICATED); + + // ->shouldBeCalledTimes(1); + $this->action->setContextValue('user', $user->reveal()); + + $this->action->execute(); + + $result = $this->action->getProvidedContext('one_time_login_url'); + $this->assertEquals('uri', $result->getContextDefinition()->getDataType()); + // $this->assertEquals($variable, $result->getContextValue()); + } + + /** + * Test execute() method for active and anonymous users. + * + * @covers ::execute + */ + public function testUserCreateLoginUrlWithActiveAnonymousUser() { + $account = $this->prophesizeEntity(UserInterface::class); + $account->mail = 'klausi@example.com'; + } + + /** + * Test execute() method for blocked and authenticated users. + * + * @covers ::execute + */ + public function testUserCreateLoginUrlWithBlockedAuthenticatedUser() { + $account = $this->prophesizeEntity(UserInterface::class); + $account->mail = 'klausi@example.com'; + } + + /** + * Test execute() method for blocked and anonymous users. + * + * @covers ::execute + */ + public function testUserCreateLoginUrlWithBlockedAnonymousUser() { + $account = $this->prophesizeEntity(UserInterface::class); + $account->mail = 'klausi@example.com'; + } + + /** + * Creates a mock user. + * + * @param bool $active + * Is user activated. + * @param bool $authenticated + * Is user authenticated. + * + * @return \Drupal\user\UserInterface|\Prophecy\Prophecy\ProphecyInterface + * The mocked user object. + */ + protected function getUserMock($active, $authenticated) { + $user = $this->prophesizeEntity(UserInterface::class); + + $user->isActive()->willReturn($active); + $user->isAuthenticated()->willReturn($authenticated); + + return $user; + } + +}