From 3a8540e91f334fe47fb08441feaa9b603dd1e599 Mon Sep 17 00:00:00 2001 From: Fabrice Gangler <fabrice.gangler@adullact.org> Date: Tue, 25 Feb 2025 12:01:09 +0100 Subject: [PATCH] test: allow to disallow some HTML attributs + improve some CSS selector --- .../tests/Functional/TestHelperFormTrait.php | 17 +++++++--- webapp/tests/Functional/TestHelperTrait.php | 31 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/webapp/tests/Functional/TestHelperFormTrait.php b/webapp/tests/Functional/TestHelperFormTrait.php index d131b9b..9275814 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 b9cbc91..283c9ab 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"); + } } } } -- GitLab