upskill-event-manager/wordpress-dev/vendor/wp-phpunit/wp-phpunit/includes/testcase-xml.php
bengizmo 37f7b426b6 feat: Implement auto page creation & fix login E2E tests
Implements automatic creation of required plugin pages (Community Login,
Trainer Registration, Trainer Dashboard) upon plugin activation. This
addresses E2E test failures caused by missing pages in the test
environment.

- Adds activation hook in `hvac-community-events.php` to call
  `hvac_ce_create_required_pages`.
- The callback function checks for existing pages by slug and creates
  them using `wp_insert_post` if missing. Includes debug logging.

Also fixes issues identified during E2E test debugging:
- Corrects fatal error in `includes/community/class-login-handler.php`
  by replacing undefined constant `HVAC_COMMUNITY_EVENTS_PATH` with
  `HVAC_CE_PLUGIN_DIR`.
- Updates `tests/e2e/tests/login.spec.ts` to use the correct selector
  `#wp-submit` for the login form submit button instead of
  `button[type="submit"]`.

Documentation updates:
- Adds `docs/automatic-page-creation-plan.md`.
- Updates `README.md` regarding automatic page creation.
- Updates Memory Bank files (`decisionLog.md`, `progress.md`,
  `activeContext.md`).

Note: Activation hook logging did not appear during WP-CLI activation,
requiring further investigation if page creation issues persist. E2E
test confirmation pending.
2025-03-28 17:18:21 -03:00

92 lines
4.2 KiB
PHP

<?php
abstract class WP_Test_XML_TestCase extends WP_UnitTestCase {
/**
* Load XML from a string.
*
* @param string $xml
* @param int $options Bitwise OR of the {@link https://www.php.net/manual/en/libxml.constants.php libxml option constants}.
* Default is 0.
* @return DOMDocument The DOMDocument object loaded from the XML.
*/
public function loadXML( $xml, $options = 0 ) {
// Suppress PHP warnings generated by DOMDocument::loadXML(), which would cause
// PHPUnit to incorrectly report an error instead of a just a failure.
$internal = libxml_use_internal_errors( true );
libxml_clear_errors();
$xml_dom = new DOMDocument();
$xml_dom->loadXML( $xml, $options );
$libxml_last_error = libxml_get_last_error();
$this->assertFalse(
isset( $libxml_last_error->message ),
isset( $libxml_last_error->message ) ? sprintf( 'Non-well-formed XML: %s.', $libxml_last_error->message ) : ''
);
// Restore default error handler.
libxml_use_internal_errors( $internal );
libxml_clear_errors();
return $xml_dom;
}
/**
* Normalize an XML document to make comparing two documents easier.
*
* @param string $xml
* @param int $options Bitwise OR of the {@link https://www.php.net/manual/en/libxml.constants.php libxml option constants}.
* Default is 0.
* @return string The normalized form of `$xml`.
*/
public function normalizeXML( $xml, $options = 0 ) {
if ( ! class_exists( 'XSLTProcessor' ) ) {
$this->markTestSkipped( 'This test requires the XSL extension.' );
}
static $xslt_proc;
if ( ! $xslt_proc ) {
$xslt_proc = new XSLTProcessor();
$xslt_proc->importStyleSheet( simplexml_load_file( __DIR__ . '/normalize-xml.xsl' ) );
}
return $xslt_proc->transformToXML( $this->loadXML( $xml, $options ) );
}
/**
* Reports an error identified by `$message` if the namespace normalized form of the XML document in `$actualXml`
* is equal to the namespace normalized form of the XML document in `$expectedXml`.
*
* This is similar to {@link https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertXmlStringEqualsXmlString assertXmlStringEqualsXmlString()}
* except that differences in namespace prefixes are normalized away, such that given
* `$actualXml = "<root xmlns='urn:wordpress.org'><child/></root>";` and
* `$expectedXml = "<ns0:root xmlns:ns0='urn:wordpress.org'><ns0:child></ns0:root>";`
* then `$this->assertXMLEquals( $expectedXml, $actualXml )` will succeed.
*
* @param string $expectedXml
* @param string $actualXml
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertXMLEquals( $expectedXml, $actualXml, $message = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$this->assertSame( $this->normalizeXML( $expectedXml ), $this->normalizeXML( $actualXml ), $message ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
/**
* Reports an error identified by `$message` if the namespace normalized form of the XML document in `$actualXml`
* is not equal to the namespace normalized form of the XML document in `$expectedXml`.
*
* This is similar to {@link https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertXmlStringEqualsXmlString assertXmlStringNotEqualsXmlString()}
* except that differences in namespace prefixes are normalized away, such that given
* `$actualXml = "<root xmlns='urn:wordpress.org'><child></root>";` and
* `$expectedXml = "<ns0:root xmlns:ns0='urn:wordpress.org'><ns0:child/></ns0:root>";`
* then `$this->assertXMLNotEquals( $expectedXml, $actualXml )` will fail.
*
* @param string $expectedXml
* @param string $actualXml
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertXMLNotEquals( $expectedXml, $actualXml, $message = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$this->assertNotEquals( $this->normalizeXML( $expectedXml ), $this->normalizeXML( $actualXml ), $message ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
}