diff --git a/webapp/tests/Functional/TestHelperFormTrait.php b/webapp/tests/Functional/TestHelperFormTrait.php index d131b9b378b88a8ef348091a8063e7c5e4441358..9275814142d4baf50d8826bf9dd2aa54fe6ec5f8 100644 --- a/webapp/tests/Functional/TestHelperFormTrait.php +++ b/webapp/tests/Functional/TestHelperFormTrait.php @@ -33,6 +33,7 @@ public function checkFormField( string $htmlFormName, string $fieldIdsuffix, array $fieldAttributes = [ 'type' => 'text'], + array $notAllowedFieldAttributes = [], string $labelText = '', string $helpText = '', string $fieldHtmlTag = 'input', @@ -45,7 +46,12 @@ public function checkFormField( $this->checkAttribute($crawler, "#$fieldId" . '_help', ['_text' => $helpText]); $fieldAttributes['aria-describedby'] = $fieldId . '_help'; } - $this->checkAttribute($crawler, "$fieldHtmlTag#$fieldId", $fieldAttributes); + $this->checkAttribute( + crawler: $crawler, + cssFilter: "$fieldHtmlTag#$fieldId", + attributesExpected: $fieldAttributes, + notAllowedAttributes: $notAllowedFieldAttributes + ); } @@ -161,7 +167,8 @@ public function commonCheckWhenFormIsSentWithoutSimilarNewPasswordFields( string $formName, string $fieldName, int $minPasswordLength, - string $methodNameToSendFormWithWrongData = 'methodNameToSendFormWithWrongData' + string $methodNameToSendFormWithWrongData = 'methodNameToSendFormWithWrongData', + string $invalidFeedback = 'The new password fields must match.', ): void { $NewPassword_first = $this->generateRandomString($minPasswordLength + 2); $NewPassword_second = "bad_repeat_NewPassword_" . $NewPassword_first; @@ -176,8 +183,8 @@ public function commonCheckWhenFormIsSentWithoutSimilarNewPasswordFields( $this->commonCheckerIfFormFieldIsInvalid( crawler: $crawler, cssFilterOfInvadFormField: "#${formName}_${fieldName}_first", - cssFilterOfInvalidFeedback: '.invalid-feedback', - invalidFeedback: 'The new password fields must match.', + cssFilterOfInvalidFeedback: "#${formName}_${fieldName}_first_help + .invalid-feedback", + invalidFeedback: "$invalidFeedback", ); } @@ -207,7 +214,7 @@ public function commonCheckWhenFormIsSentWithTooSmallRepeatPassword( $this->commonCheckerIfFormFieldIsInvalid( crawler: $crawler, cssFilterOfInvadFormField: "#${formName}_${fieldName}_first", - cssFilterOfInvalidFeedback: '.invalid-feedback', + cssFilterOfInvalidFeedback: "#${formName}_${fieldName}_first_help + .invalid-feedback", invalidFeedback: 'Password size is too small.', ); } diff --git a/webapp/tests/Functional/TestHelperTrait.php b/webapp/tests/Functional/TestHelperTrait.php index b9cbc91d613913e355391dcc0ee1d900ea2d1d90..283c9abbb7bef86c53969c101a2550df36d11664 100644 --- a/webapp/tests/Functional/TestHelperTrait.php +++ b/webapp/tests/Functional/TestHelperTrait.php @@ -64,6 +64,7 @@ public function getListOfEmailsInInvalidFormat(): array 'this_is_not_a_valid_email@example', 'this_is_not_a_valid_email@example.', '@example.org', + 'john @example.org', ]; } @@ -131,8 +132,10 @@ public function checkRedirectionToNewUrl( public function checkAttribute( Crawler $crawler, string $cssFilter, - array $attributesExpected = ['_text' => '', '_name' => 'a', 'href' => '#'] + array $attributesExpected = ['_text' => '', '_name' => 'a', 'href' => '#'], + array $notAllowedAttributes = [], ): void { + // Expected attributes $attributeKeys = array_keys($attributesExpected); $attributesCount = count($attributeKeys); $attributes = $crawler @@ -150,7 +153,31 @@ public function checkAttribute( } else { $value = $attributes[0][$keyIndex]; } - $this->assertEquals("$expected", "$value"); + $this->assertEquals( + expected: "$expected", + actual: "$value", + message: "Invalid value for [ $keyName ] atttribut, \non [ $cssFilter ] element" + ); + } + } + + + // Not allowed attributes + if (count($notAllowedAttributes) >= 1) { + $notAllowedAttributesExtractor = $crawler + ->filter("$cssFilter") + ->extract($notAllowedAttributes); + $errorMsg = "At least one of not allowed attributes is present\n"; + $errorMsg .= "for the following CSS filter : [ $cssFilter ]\n"; + $errorMsg .= "\nNot allowed attributs:\n" . print_r($notAllowedAttributes, true); + $errorMsg .= "\nCSS extractor:\n" . print_r($notAllowedAttributesExtractor, true); + $this->assertEquals(1, count($notAllowedAttributesExtractor),); + if (count($notAllowedAttributes) === 1) { + $this->assertEquals('', $notAllowedAttributesExtractor[0], "$errorMsg"); + } else { + foreach ($notAllowedAttributesExtractor[0] as $key => $value) { + $this->assertEquals('', $value, "$errorMsg"); + } } } }