Files
yii2-usuario/tests/functional/SettingsCept.php
Leonardo Forconi 1c1ce6ccff
Some checks failed
Build / build (ubuntu-latest, 7.4) (push) Has been cancelled
Build / build (ubuntu-latest, 8.0) (push) Has been cancelled
Build / build (ubuntu-latest, 8.1) (push) Has been cancelled
Build / build (ubuntu-latest, 8.2) (push) Has been cancelled
Build / build (ubuntu-latest, 8.3) (push) Has been cancelled
Build / build (ubuntu-latest, 8.4) (push) Has been cancelled
refactor: renamed namespace from Da\User to pcrt\User
All PHP namespaces and related references have been updated from Da\User to pcrt\User throughout the codebase, including composer.json autoload settings and all source files. This change standardizes the project under the new pcrt\User namespace for improved clarity and maintainability.
2025-10-15 10:22:32 +02:00

115 lines
4.1 KiB
PHP

<?php
/**
* @var Codeception\Scenario
*/
use pcrt\User\Model\Token;
use pcrt\User\Model\User;
use tests\_fixtures\ProfileFixture;
use tests\_fixtures\UserFixture;
use yii\helpers\Html;
$I = new FunctionalTester($scenario);
$I->wantTo('ensure that account settings page work');
$I->haveFixtures(['user' => UserFixture::className(), 'profile' => ProfileFixture::className()]);
$user = $I->grabFixture('user', 'user');
$I->amLoggedInAs($user);
$I->amOnRoute('/user/settings/account');
$I->amGoingTo('check that current password is required and must be valid');
$I->fillField('#settingsform-email', $user->email);
$I->fillField('#settingsform-username', $user->username);
$I->fillField('#settingsform-current_password', 'wrong');
$I->click('Save');
$I->see('Current password is not valid');
$I->amGoingTo('check that email is changing properly');
$I->fillField('#settingsform-email', 'new_user@example.com');
$I->fillField('#settingsform-username', $user->username);
$I->fillField('#settingsform-current_password', 'qwerty');
$I->click('Save');
$I->seeRecord(User::className(), ['email' => $user->email, 'unconfirmed_email' => 'new_user@example.com']);
$I->see('A confirmation message has been sent to your new email address');
$user = $I->grabRecord(User::class, ['id' => $user->id]);
$token = $I->grabRecord(Token::class, ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRM_NEW_EMAIL]);
/** @var \yii\mail\MessageInterface $message */
$message = $I->grabLastSentEmail();
$I->assertArrayHasKey($user->unconfirmed_email, $message->getTo());
$I->assertStringContainsString(Html::encode($token->getUrl()), utf8_encode(quoted_printable_decode($message->toString())));
Yii::$app->user->logout();
$I->amGoingTo('log in using new email address before clicking the confirmation link');
$I->amOnRoute('/user/security/login');
$I->fillField('#loginform-login', 'new_user@example.com');
$I->fillField('#loginform-password', 'qwerty');
$I->click('Sign in');
$I->see('Invalid login or password');
$I->amGoingTo('log in using new email address after clicking the confirmation link');
$emailChangeService = Yii::createObject(\pcrt\User\Service\EmailChangeService::class, [$token->code, $user]);
$emailChangeService->run();
$I->fillField('#loginform-login', 'new_user@example.com');
$I->fillField('#loginform-password', 'qwerty');
$I->click('Sign in');
$I->see('Logout');
$I->seeRecord(User::className(), [
'id' => 1,
'email' => 'new_user@example.com',
'unconfirmed_email' => null,
]);
$I->amGoingTo('reset email changing process');
$I->amOnRoute('/user/settings/account');
$I->fillField('#settingsform-email', 'user@example.com');
$I->fillField('#settingsform-username', $user->username);
$I->fillField('#settingsform-new_password', null);
$I->fillField('#settingsform-current_password', 'qwerty');
$I->click('Save');
$I->see('A confirmation message has been sent to your new email address');
$I->seeRecord(User::className(), [
'id' => 1,
'email' => 'new_user@example.com',
'unconfirmed_email' => 'user@example.com',
]);
$I->fillField('#settingsform-email', 'new_user@example.com');
$I->fillField('#settingsform-username', $user->username);
$I->fillField('#settingsform-new_password', null);
$I->fillField('#settingsform-current_password', 'qwerty');
$I->click('Save');
$I->see('Your account details have been updated');
$I->seeRecord(User::className(), [
'id' => 1,
'email' => 'new_user@example.com',
'unconfirmed_email' => null,
]);
$I->amGoingTo('change username and password');
$I->fillField('#settingsform-email', 'new_user@example.com');
$I->fillField('#settingsform-username', 'nickname');
$I->fillField('#settingsform-new_password', '123654');
$I->fillField('#settingsform-current_password', 'qwerty');
$I->click('Save');
$I->see('Your account details have been updated');
$I->seeRecord(User::className(), [
'username' => 'nickname',
'email' => 'new_user@example.com',
]);
Yii::$app->user->logout();
$I->amGoingTo('login with new credentials');
$I->amOnRoute('/user/security/login');
$I->fillField('#loginform-login', 'new_user@example.com');
$I->fillField('#loginform-password', '123654');
$I->click('Sign in');
$I->see('Logout');