MNT Fix test for required password fields in kitchen sink (#11111)

This commit is contained in:
Guy Sartorelli 2024-01-19 13:27:32 +13:00 committed by GitHub
parent cc0edf242e
commit b979ce5896
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms\Tests; namespace SilverStripe\Forms\Tests;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\ConfirmedPasswordField; use SilverStripe\Forms\ConfirmedPasswordField;
@ -11,6 +12,7 @@ use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\PasswordValidator; use SilverStripe\Security\PasswordValidator;
use SilverStripe\View\SSViewer;
class ConfirmedPasswordFieldTest extends SapphireTest class ConfirmedPasswordFieldTest extends SapphireTest
{ {
@ -437,29 +439,37 @@ class ConfirmedPasswordFieldTest extends SapphireTest
*/ */
public function testChildFieldsAreRequired(bool $canBeEmpty, bool $required, bool $childrenRequired, bool $expectRequired) public function testChildFieldsAreRequired(bool $canBeEmpty, bool $required, bool $childrenRequired, bool $expectRequired)
{ {
$form = new Form(); // CWP front-end templates break this logic - but there's no easy fix for that.
$field = new ConfirmedPasswordField('Test'); // For the most part we are interested in ensuring this works in the CMS with default templates.
$field->setForm($form); $originalThemes = SSViewer::get_themes();
$field->setCanBeEmpty($canBeEmpty); SSViewer::set_themes(LeftAndMain::config()->uninherited('admin_themes'));
$requiredFields = []; try {
if ($required) { $form = new Form();
$requiredFields[] = 'Test'; $field = new ConfirmedPasswordField('Test');
} $field->setForm($form);
if ($childrenRequired) { $field->setCanBeEmpty($canBeEmpty);
$requiredFields[] = 'Test[_Password]'; $requiredFields = [];
$requiredFields[] = 'Test[_ConfirmPassword]'; if ($required) {
} $requiredFields[] = 'Test';
$form->setValidator(new RequiredFields($requiredFields)); }
if ($childrenRequired) {
$requiredFields[] = 'Test[_Password]';
$requiredFields[] = 'Test[_ConfirmPassword]';
}
$form->setValidator(new RequiredFields($requiredFields));
$rendered = $field->Field(); $rendered = $field->Field();
$fieldOneRegex = '<input\s+type="password"\s+name="Test\[_Password\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>'; $fieldOneRegex = '<input\s+type="password"\s+name="Test\[_Password\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>';
$fieldTwoRegex = '<input\s+type="password"\s+name="Test\[_ConfirmPassword\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>'; $fieldTwoRegex = '<input\s+type="password"\s+name="Test\[_ConfirmPassword\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>';
$regex = '/' . $fieldOneRegex . '.*' . $fieldTwoRegex . '/isu'; $regex = '/' . $fieldOneRegex . '.*' . $fieldTwoRegex . '/isu';
if ($expectRequired) { if ($expectRequired) {
$this->assertMatchesRegularExpression($regex, $rendered); $this->assertMatchesRegularExpression($regex, $rendered);
} else { } else {
$this->assertDoesNotMatchRegularExpression($regex, $rendered); $this->assertDoesNotMatchRegularExpression($regex, $rendered);
}
} finally {
SSViewer::set_themes($originalThemes);
} }
} }
} }