Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail when adding newPassword validation rules #16320

Open
webrgp opened this issue Dec 12, 2024 · 6 comments
Open

Tests fail when adding newPassword validation rules #16320

webrgp opened this issue Dec 12, 2024 · 6 comments

Comments

@webrgp
Copy link

webrgp commented Dec 12, 2024

I've added the following user password validation rule:

[
    "newPassword",
    "match",
    "pattern" =>
    "/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^\w])(?=.{8,})/", // {8,} = 8 character min
    "message" => Craft::t(
        "app",
        "New Password requires at least one lowercase, uppercase, number, and special character.",
    ),
],

Which works on the site, but when I try running my Codeception tests, which runs fine without the rule, I get:

Codeception PHP Testing Framework v5.0.13 https://stand-with-ukraine.pp.ua
<pre>Exception 'TypeError' with message 'craft\services\Users::getUserPreferences(): Argument #1 ($userId) must be of type int, null given, called in /var/www/vendor/craftcms/cms/src/services/Users.php on line 389' 

in /var/www/vendor/craftcms/cms/src/services/Users.php:365

Stack trace:
#0 /var/www/vendor/craftcms/cms/src/services/Users.php(389): craft\services\Users-&gt;getUserPreferences()
#1 /var/www/vendor/craftcms/cms/src/migrations/Install.php(1182): craft\services\Users-&gt;saveUserPreferences()
#2 /var/www/vendor/craftcms/cms/src/migrations/Install.php(77): craft\migrations\Install-&gt;insertDefaultData()
#3 /var/www/vendor/craftcms/cms/src/test/TestSetup.php(499): craft\migrations\Install-&gt;safeUp()
#4 /var/www/vendor/craftcms/cms/src/test/Craft.php(252): craft\test\TestSetup::setupCraftDb()
#5 /var/www/vendor/craftcms/cms/src/test/Craft.php(137): craft\test\Craft-&gt;setupDb()
#6 /var/www/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php(52): craft\test\Craft-&gt;_beforeSuite()
#7 /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php(230): Codeception\Subscriber\Module-&gt;beforeSuite()
#8 /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php(59): Symfony\Component\EventDispatcher\EventDispatcher-&gt;callListeners()
#9 /var/www/vendor/codeception/codeception/src/Codeception/SuiteManager.php(148): Symfony\Component\EventDispatcher\EventDispatcher-&gt;dispatch()
#10 /var/www/vendor/codeception/codeception/src/Codeception/Codecept.php(260): Codeception\SuiteManager-&gt;run()
#11 /var/www/vendor/codeception/codeception/src/Codeception/Codecept.php(216): Codeception\Codecept-&gt;runSuite()
#12 /var/www/vendor/codeception/codeception/src/Codeception/Command/Run.php(435): Codeception\Codecept-&gt;run()
#13 /var/www/vendor/symfony/console/Command/Command.php(326): Codeception\Command\Run-&gt;execute()
#14 /var/www/vendor/symfony/console/Application.php(1078): Symfony\Component\Console\Command\Command-&gt;run()
#15 /var/www/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application-&gt;doRunCommand()
#16 /var/www/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application-&gt;doRun()
#17 /var/www/vendor/codeception/codeception/src/Codeception/Application.php(112): Symfony\Component\Console\Application-&gt;run()
#18 /var/www/vendor/codeception/codeception/app.php(45): Codeception\Application-&gt;run()
#19 /var/www/vendor/codeception/codeception/app.php(46): {closure}()
#20 /var/www/vendor/codeception/codeception/codecept(7): require('...')
#21 /var/www/vendor/bin/codecept(119): include('...')
#22 {main}</pre>


COMMAND DID NOT FINISH PROPERLY.

I believe the issue is here:

'password' => 'craftcms2018!!',

@webrgp
Copy link
Author

webrgp commented Dec 12, 2024

For now I am checking Craft::$app->getIsInstalled() before adding the validation.

@brandonkelly
Copy link
Member

What version of Craft CMS are you running?

@webrgp
Copy link
Author

webrgp commented Dec 13, 2024

Craft Pro 4.6.1

@brandonkelly
Copy link
Member

Update to the latest Craft 4 release as a starting point. If it’s still failing, please provide an updated stack trace.

@webrgp
Copy link
Author

webrgp commented Dec 18, 2024

I've updated to Craft 4.13.7 and got the same error:

Codeception PHP Testing Framework v5.1.2 https://stand-with-ukraine.pp.ua
<pre>Exception &apos;TypeError&apos; with message &apos;craft\services\Users::getUserPreferences(): Argument #1 ($userId) must be of type int, null given, called in /var/www/vendor/craftcms/cms/src/services/Users.php on line 389&apos; 

in /var/www/vendor/craftcms/cms/src/services/Users.php:365

Stack trace:
#0 /var/www/vendor/craftcms/cms/src/services/Users.php(389): craft\services\Users-&gt;getUserPreferences()
#1 /var/www/vendor/craftcms/cms/src/migrations/Install.php(1158): craft\services\Users-&gt;saveUserPreferences()
#2 /var/www/vendor/craftcms/cms/src/migrations/Install.php(83): craft\migrations\Install-&gt;insertDefaultData()
#3 /var/www/vendor/craftcms/cms/src/test/TestSetup.php(499): craft\migrations\Install-&gt;safeUp()
#4 /var/www/vendor/craftcms/cms/src/test/Craft.php(252): craft\test\TestSetup::setupCraftDb()
#5 /var/www/vendor/craftcms/cms/src/test/Craft.php(137): craft\test\Craft-&gt;setupDb()
#6 /var/www/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php(52): craft\test\Craft-&gt;_beforeSuite()
#7 /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php(230): Codeception\Subscriber\Module-&gt;beforeSuite()
#8 /var/www/vendor/symfony/event-dispatcher/EventDispatcher.php(59): Symfony\Component\EventDispatcher\EventDispatcher-&gt;callListeners()
#9 /var/www/vendor/codeception/codeception/src/Codeception/SuiteManager.php(148): Symfony\Component\EventDispatcher\EventDispatcher-&gt;dispatch()
#10 /var/www/vendor/codeception/codeception/src/Codeception/Codecept.php(260): Codeception\SuiteManager-&gt;run()
#11 /var/www/vendor/codeception/codeception/src/Codeception/Codecept.php(216): Codeception\Codecept-&gt;runSuite()
#12 /var/www/vendor/codeception/codeception/src/Codeception/Command/Run.php(646): Codeception\Codecept-&gt;run()
#13 /var/www/vendor/codeception/codeception/src/Codeception/Command/Run.php(467): Codeception\Command\Run-&gt;runSuites()
#14 /var/www/vendor/symfony/console/Command/Command.php(326): Codeception\Command\Run-&gt;execute()
#15 /var/www/vendor/symfony/console/Application.php(1078): Symfony\Component\Console\Command\Command-&gt;run()
#16 /var/www/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application-&gt;doRunCommand()
#17 /var/www/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application-&gt;doRun()
#18 /var/www/vendor/codeception/codeception/src/Codeception/Application.php(112): Symfony\Component\Console\Application-&gt;run()
#19 /var/www/vendor/codeception/codeception/app.php(45): Codeception\Application-&gt;run()
#20 /var/www/vendor/codeception/codeception/app.php(46): {closure}()
#21 /var/www/vendor/codeception/codeception/codecept(7): require(&apos;...&apos;)
#22 /var/www/vendor/bin/codecept(119): include(&apos;...&apos;)
#23 {main}</pre>


COMMAND DID NOT FINISH PROPERLY.

@webrgp
Copy link
Author

webrgp commented Dec 18, 2024

I've traced the execution the setupCraftDb static function that installs Craft with a hardcoded password:

cms/src/test/TestSetup.php

Lines 491 to 497 in 98ce266

$migration = new Install([
'db' => $connection,
'username' => self::USERNAME,
'password' => 'craftcms2018!!',
'email' => '[email protected]',
'site' => $site,
]);

Maybe username, email and password should be configurable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants