szurubooru/tests/JobTests/ActivateUserEmailJobTest.php

111 lines
2.7 KiB
PHP
Raw Normal View History

2014-05-07 17:39:40 +02:00
<?php
class ActivateUserEmailJobTest extends AbstractTest
{
public function testSending()
{
getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending();
$user = $this->userMocker->mockSingle();
2014-05-07 17:39:40 +02:00
$user->setUnconfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user);
$this->assert->areEqual(0, Mailer::getMailCounter());
$this->assert->doesNotThrow(function() use ($user)
{
Api::run(
new ActivateUserEmailJob(),
[
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'];
$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->userMocker->mockSingle();
2014-05-07 17:39:40 +02:00
$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(),
[
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(),
[
JobArgs::ARG_TOKEN => $tokenText,
2014-05-07 17:39:40 +02:00
]);
});
//reload local entity after changes done by the job
$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->userMocker->mockSingle();
2014-05-07 17:39:40 +02:00
$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(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
2014-05-07 17:39:40 +02:00
]);
$tokenText = Mailer::getMailsSent()[0]->tokens['token'];
Api::run(
new ActivateUserEmailJob(),
[
JobArgs::ARG_TOKEN => $tokenText,
2014-05-07 17:39:40 +02:00
]);
$this->assert->throws(function() use ($tokenText)
{
Api::run(
new ActivateUserEmailJob(),
[
JobArgs::ARG_TOKEN => $tokenText,
2014-05-07 17:39:40 +02:00
]);
}, 'This token was already used');
}
}