Skip to content

Commit

Permalink
Better naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Sep 29, 2020
1 parent 261314e commit 91155ee
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 46 deletions.
4 changes: 2 additions & 2 deletions config/Component/Repository.php
Expand Up @@ -8,7 +8,7 @@
use App\Service\Parameters;
use App\Module\User\Entity\User;
use App\Module\User\Entity\Token;
use App\Module\User\Form\Registration;
use App\Module\User\Form\Register;
use App\Module\User\Repository\UserRepository;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Auth\IdentityRepositoryInterface;
Expand All @@ -24,7 +24,7 @@
Reference::to(Aliases::class),
Reference::to(ConnectionInterface::class),
Reference::to(Mailer::class),
Reference::to(Registration::class),
Reference::to(Register::class),
Reference::to(Token::class),
Reference::to(User::class),
Reference::to(UrlGeneratorInterface::class)
Expand Down
14 changes: 7 additions & 7 deletions src/Module/User/Action/Register.php
Expand Up @@ -8,7 +8,7 @@
use App\Module\User\Repository\UserRepository;
use App\Service\Parameters;
use App\Service\View;
use App\Module\User\Form\Registration as RegistrationForm;
use App\Module\User\Form\Register as RegisterForm;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Auth\IdentityRepositoryInterface;
Expand All @@ -20,21 +20,21 @@ final class Register
public function register(
Parameters $app,
IdentityRepositoryInterface $identityRepository,
RegistrationForm $registrationForm,
RegisterForm $registerForm,
ServerRequestInterface $request,
DataResponseFactoryInterface $responseFactory,
UrlGeneratorInterface $url,
View $view
): ResponseInterface {
$body = $request->getParsedBody();
$method = $request->getMethod();
$registrationForm->ip($request->getServerParams()['REMOTE_ADDR']);
$registerForm->ip($request->getServerParams()['REMOTE_ADDR']);

/** @var UserRepository $identityRepository */
if (
$method === 'POST'
&& $registrationForm->load($body)
&& $registrationForm->validate()
&& $registerForm->load($body)
&& $registerForm->validate()
&& $identityRepository->register()
) {
/** @var UserRepository $identityRepository */
Expand All @@ -59,10 +59,10 @@ public function register(
);
}

if ($app->get('user.registration')) {
if ($app->get('user.register')) {
return $view
->viewPath('@user/resources/views')
->renderWithLayout('/registration/register', ['data' => $registrationForm]);
->renderWithLayout('/registration/register', ['data' => $registerForm]);
}

throw new RuntimeException('Module register user is disabled in the application configuration.');
Expand Down
2 changes: 1 addition & 1 deletion src/Module/User/Config/Params.php
Expand Up @@ -32,7 +32,7 @@ public function getParams(): array
'passwordRecovery' => true,

/** Registration enabled/disabled module user. */
'registration' => true,
'register' => true,

/** Configure the subject confirm email. */
'subjectConfirm' => 'Confirm account.',
Expand Down
Expand Up @@ -12,7 +12,7 @@
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\MatchRegularExpression;

final class Registration extends FormModel
final class Register extends FormModel
{
private string $email = '';
private string $username = '';
Expand All @@ -36,6 +36,11 @@ public function attributeLabels(): array
];
}

public function formName(): string
{
return 'Register';
}

public function ip(string $value): void
{
$this->ip = $value;
Expand Down
24 changes: 12 additions & 12 deletions src/Module/User/Repository/UserRepository.php
Expand Up @@ -9,7 +9,7 @@
use App\Service\Parameters;
use App\Module\User\Entity\User;
use App\Module\User\Entity\Token;
use App\Module\User\Form\Registration as RegistrationForm;
use App\Module\User\Form\Register as RegisterForm;
use Yiisoft\Aliases\Aliases;
use Yiisoft\ActiveRecord\ActiveRecord;
use Yiisoft\ActiveRecord\ActiveQuery;
Expand All @@ -34,7 +34,7 @@ final class UserRepository implements IdentityRepositoryInterface
private Aliases $aliases;
private ConnectionInterface $db;
private Mailer $mailer;
private RegistrationForm $registrationForm;
private RegisterForm $registerForm;
private Token $token;
private User $user;
private UrlGeneratorInterface $url;
Expand All @@ -44,7 +44,7 @@ public function __construct(
Aliases $aliases,
ConnectionInterface $db,
Mailer $mailer,
RegistrationForm $registrationForm,
RegisterForm $registerForm,
Token $token,
User $user,
UrlGeneratorInterface $url
Expand All @@ -54,7 +54,7 @@ public function __construct(
$this->db = $db;
$this->mailer = $mailer;
$this->token = $token;
$this->registrationForm = $registrationForm;
$this->registerForm = $registerForm;
$this->user = $user;
$this->url = $url;
}
Expand Down Expand Up @@ -118,13 +118,13 @@ public function register(): bool
throw new RuntimeException('Calling "' . __CLASS__ . '::' . __METHOD__ . '" on existing user');
}

if ($this->findUserByUsernameOrEmail($this->registrationForm->getAttributeValue('email'))) {
$this->registrationForm->addError('email', 'Email already registered.');
if ($this->findUserByUsernameOrEmail($this->registerForm->getAttributeValue('email'))) {
$this->registerForm->addError('email', 'Email already registered.');
return false;
}

if ($this->findUserByUsernameOrEmail($this->registrationForm->getAttributeValue('username'))) {
$this->registrationForm->addError('username', 'Username already registered.');
if ($this->findUserByUsernameOrEmail($this->registerForm->getAttributeValue('username'))) {
$this->registerForm->addError('username', 'Username already registered.');
return false;
}

Expand Down Expand Up @@ -232,15 +232,15 @@ private function insertRecordFromFormModel(): void
{
$password = $this->app->get('user.generatingPassword')
? $this->generate(8)
: $this->registrationForm->getAttributeValue('password');
: $this->registerForm->getAttributeValue('password');

$this->user->username($this->registrationForm->getAttributeValue('username'));
$this->user->email($this->registrationForm->getAttributeValue('email'));
$this->user->username($this->registerForm->getAttributeValue('username'));
$this->user->email($this->registerForm->getAttributeValue('email'));
$this->user->unconfirmedEmail(null);
$this->user->password($password);
$this->user->passwordHash($password);
$this->user->authKey();
$this->user->registrationIp($this->registrationForm->getAttributeValue('ip'));
$this->user->registrationIp($this->registerForm->getAttributeValue('ip'));

if ($this->app->get('user.confirmation') === false) {
$this->user->confirmedAt();
Expand Down
2 changes: 1 addition & 1 deletion src/Module/User/resources/views/registration/register.php
Expand Up @@ -14,7 +14,7 @@
* @var \App\Service\Parameters $app
* @var \Yiisoft\Assets\AssetManager $assetManager
* @var string|null $csrf
* @var \App\Module\User\Form\Registration $data
* @var \App\Module\User\Form\Register $data
* @var \Yiisoft\Router\UrlGeneratorInterface $url
* @var \Yiisoft\Router\UrlMatcherInterface $urlMatcher
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Module/User/resources/views/registration/resend.php
Expand Up @@ -49,7 +49,7 @@

<hr class = 'mb-2'></hr>

<?php if ($app->get('user.registration')) : ?>
<?php if ($app->get('user.register')) : ?>
<p class = 'text-center'>
<?= Html::a('Don\'t have an account - Sign up!', $url->generate('registration/register')) ?>
</p>
Expand Down
42 changes: 21 additions & 21 deletions tests/Acceptance/RegisterCest.php
Expand Up @@ -21,9 +21,9 @@ public function testRegisterPage(AcceptanceTester $I): void

public function testRegisterSuccessDataDefaultAccountConfirmationFalse(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'administrator@example.com');
$I->fillField('#registration-username', 'admin');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'administrator@example.com');
$I->fillField('#register-username', 'admin');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
Expand All @@ -44,9 +44,9 @@ public function testRegisterEmptyData(AcceptanceTester $I): void

public function testRegisterWrongEmailData(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'register');
$I->fillField('#registration-username', 'register');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'register');
$I->fillField('#register-username', 'register');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
Expand All @@ -56,9 +56,9 @@ public function testRegisterWrongEmailData(AcceptanceTester $I): void

public function testRegisterEmailExistData(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'administrator@example.com');
$I->fillField('#registration-username', 'administrator');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'administrator@example.com');
$I->fillField('#register-username', 'administrator');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
Expand All @@ -68,18 +68,18 @@ public function testRegisterEmailExistData(AcceptanceTester $I): void

public function testsRegisterInvalidUsernameData(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'demo@example.com');
$I->fillField('#registration-username', '**admin');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'demo@example.com');
$I->fillField('#register-username', '**admin');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
$I->see('Value is invalid.');

$I->wantTo('register invalid data.');
$I->fillField('#registration-email', 'demo@example.com');
$I->fillField('#registration-username', '**');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'demo@example.com');
$I->fillField('#register-username', '**');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');


Expand All @@ -90,9 +90,9 @@ public function testsRegisterInvalidUsernameData(AcceptanceTester $I): void

public function testRegisterUsernameExistData(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'demo@example.com');
$I->fillField('#registration-username', 'admin');
$I->fillField('#registration-password', '123456');
$I->fillField('#register-email', 'demo@example.com');
$I->fillField('#register-username', 'admin');
$I->fillField('#register-password', '123456');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
Expand All @@ -102,9 +102,9 @@ public function testRegisterUsernameExistData(AcceptanceTester $I): void

public function testRegisterInvalidPasswordData(AcceptanceTester $I): void
{
$I->fillField('#registration-email', 'demo@example.com');
$I->fillField('#registration-username', 'demo');
$I->fillField('#registration-password', '123');
$I->fillField('#register-email', 'demo@example.com');
$I->fillField('#register-username', 'demo');
$I->fillField('#register-password', '123');
$I->click('Register', '#form-registration-register');

$I->expectTo('see registration register validation.');
Expand Down

0 comments on commit 91155ee

Please sign in to comment.