2014-05-07 17:39:40 +02:00
|
|
|
<?php
|
|
|
|
class ActivateUserEmailJobTest extends AbstractTest
|
|
|
|
{
|
|
|
|
public function testSending()
|
|
|
|
{
|
|
|
|
getConfig()->registration->needEmailForRegistering = true;
|
|
|
|
Mailer::mockSending();
|
|
|
|
|
|
|
|
$user = $this->mockUser();
|
|
|
|
$user->setUnconfirmedEmail('godzilla@whitestar.gov');
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function() use ($user)
|
|
|
|
{
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assert->areEqual(1, Mailer::getMailCounter());
|
|
|
|
|
|
|
|
$tokens = Mailer::getMailsSent()[0]->tokens;
|
|
|
|
$tokenText = $tokens['token'];
|
2014-05-08 08:54:08 +02:00
|
|
|
$token = TokenModel::getByToken($tokenText);
|
2014-05-07 17:39:40 +02:00
|
|
|
|
|
|
|
$this->assert->areEqual($user->getId(), $token->getUser()->getId());
|
|
|
|
$this->assert->isTrue(strpos($tokens['link'], $tokenText) !== false);
|
|
|
|
|
|
|
|
return $tokenText;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfirming()
|
|
|
|
{
|
|
|
|
getConfig()->registration->needEmailForRegistering = true;
|
|
|
|
Mailer::mockSending();
|
|
|
|
|
|
|
|
$user = $this->mockUser();
|
|
|
|
$user->setUnconfirmedEmail('godzilla@whitestar.gov');
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->areEqual('godzilla@whitestar.gov', $user->getUnconfirmedEmail());
|
|
|
|
$this->assert->areEqual(null, $user->getConfirmedEmail());
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function() use ($user)
|
|
|
|
{
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$tokenText = Mailer::getMailsSent()[0]->tokens['token'];
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function() use ($tokenText)
|
|
|
|
{
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_TOKEN => $tokenText,
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
//reload local entity after changes done by the job
|
2014-05-08 08:54:08 +02:00
|
|
|
$user = UserModel::getById($user->getId());
|
2014-05-07 17:39:40 +02:00
|
|
|
|
|
|
|
$this->assert->areEqual(null, $user->getUnconfirmedEmail());
|
|
|
|
$this->assert->areEqual('godzilla@whitestar.gov', $user->getConfirmedEmail());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testUsingTokenTwice()
|
|
|
|
{
|
|
|
|
getConfig()->registration->needEmailForRegistering = true;
|
|
|
|
Mailer::mockSending();
|
|
|
|
|
|
|
|
$user = $this->mockUser();
|
|
|
|
$user->setUnconfirmedEmail('godzilla@whitestar.gov');
|
|
|
|
UserModel::save($user);
|
|
|
|
|
|
|
|
$this->assert->areEqual('godzilla@whitestar.gov', $user->getUnconfirmedEmail());
|
|
|
|
$this->assert->areEqual(null, $user->getConfirmedEmail());
|
|
|
|
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$tokenText = Mailer::getMailsSent()[0]->tokens['token'];
|
|
|
|
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_TOKEN => $tokenText,
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assert->throws(function() use ($tokenText)
|
|
|
|
{
|
|
|
|
Api::run(
|
|
|
|
new ActivateUserEmailJob(),
|
|
|
|
[
|
2014-05-12 00:13:18 +02:00
|
|
|
JobArgs::ARG_TOKEN => $tokenText,
|
2014-05-07 17:39:40 +02:00
|
|
|
]);
|
|
|
|
}, 'This token was already used');
|
|
|
|
}
|
|
|
|
}
|