From 02d382de1bd5e8423113a05c688b7f0df32725f9 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Sun, 5 Jul 2026 15:50:29 +1000 Subject: [PATCH] [#665] Fixed 'XmlTrait' XML-format assertions ignoring fixture content. Claude-Session: https://claude.ai/code/session_01JTm9W2rNMjgzxqPVD6BGFi --- STEPS.md | 8 ++++++++ src/XmlTrait.php | 15 ++++++++++++--- tests/behat/features/xml.feature | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/STEPS.md b/STEPS.md index c2b85b2f..dc6960ba 100644 --- a/STEPS.md +++ b/STEPS.md @@ -2399,6 +2399,10 @@ Assert that a response is valid XML ```gherkin Then the response should be in XML format +# Content set by a fixture step is validated instead of the page content. +Given the response content from the file "xml_valid.xml" +Then the response should be in XML format + ``` @@ -2413,6 +2417,10 @@ Assert that a response is not valid XML ```gherkin Then the response should not be in XML format +# Content set by a fixture step is validated instead of the page content. +Given the response content from the file "xml_invalid.xml" +Then the response should not be in XML format + ``` diff --git a/src/XmlTrait.php b/src/XmlTrait.php index fa3800ab..0cb4c0c0 100644 --- a/src/XmlTrait.php +++ b/src/XmlTrait.php @@ -124,12 +124,15 @@ public function xmlSetResponseContentDirect(PyStringNode $content): void { * * @code * Then the response should be in XML format + * + * # Content set by a fixture step is validated instead of the page content. + * Given the response content from the file "xml_valid.xml" + * Then the response should be in XML format * @endcode */ #[Then('the response should be in XML format')] public function xmlAssertResponseIsXml(): void { - $content = $this->getSession()->getPage()->getContent(); - $this->xmlLoadDocument($content); + $this->xmlEnsureDocument(); } /** @@ -137,11 +140,17 @@ public function xmlAssertResponseIsXml(): void { * * @code * Then the response should not be in XML format + * + * # Content set by a fixture step is validated instead of the page content. + * Given the response content from the file "xml_invalid.xml" + * Then the response should not be in XML format * @endcode */ #[Then('the response should not be in XML format')] public function xmlAssertResponseIsNotXml(): void { - $content = $this->getSession()->getPage()->getContent(); + // Resolve content the same way as xmlEnsureDocument(): prefer content set + // by the fixture steps, falling back to the live page content. + $content = $this->xmlTestContent ?? $this->getSession()->getPage()->getContent(); $doc = new \DOMDocument(); libxml_clear_errors(); diff --git a/tests/behat/features/xml.feature b/tests/behat/features/xml.feature index 2223490e..46f6cce6 100644 --- a/tests/behat/features/xml.feature +++ b/tests/behat/features/xml.feature @@ -11,6 +11,22 @@ Feature: Check that XmlTrait works When I go to "/sites/default/files/xml_with_warnings.xml" Then the response should be in XML format + Scenario: Assert "Then the response should be in XML format" honours content set from a fixture file over the page content + When I go to "/sites/default/files/xml_invalid.xml" + And the response content from the file "xml_valid.xml" + Then the response should be in XML format + + Scenario: Assert "Then the response should be in XML format" honours content set from a PyString over the page content + When I go to "/sites/default/files/xml_invalid.xml" + And the response content is the following: + """ + + + Blue Widget + + """ + Then the response should be in XML format + @trait:XmlTrait Scenario: Assert that negative assertion for "Then the response should be in XML format" fails with an exception Given some behat configuration @@ -29,6 +45,11 @@ Feature: Check that XmlTrait works When I go to "/sites/default/files/xml_invalid.xml" Then the response should not be in XML format + Scenario: Assert "Then the response should not be in XML format" honours content set from a fixture file over the page content + When I go to "/sites/default/files/xml_valid.xml" + And the response content from the file "xml_invalid.xml" + Then the response should not be in XML format + @trait:XmlTrait Scenario: Assert that negative assertion for "Then the response should not be in XML format" fails with an error Given some behat configuration