assertFileExists( $path, $name );
$source = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
$this->assertMatchesRegularExpression( '/^\s*<\?php\s*\/\*\*.*?\*\sTitle:\s*.+$/ms', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sSlug:\s*chrnx\/' . preg_quote( $name, '/' ) . '\s*$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sCategories:\s*\S.+$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sDescription:\s*\S.+$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sInserter:\s*(?:yes|no)\s*$/m', $source, $name );
if ( preg_match( '/(?:__|_e)\s*\(/', $source ) ) {
$this->assertStringContainsString( "'chrnx'", $source, $name );
}
$content = $this->render_pattern( $path );
$blocks = parse_blocks( $content );
$this->assertNotEmpty( $blocks, $name );
$this->assertSame( trim( $content ), trim( serialize_blocks( $blocks ) ), $name );
foreach ( $blocks as $parsed_block ) {
if ( null === $parsed_block['blockName'] ) {
$this->assertSame( '', trim( $parsed_block['innerHTML'] ), $name . ' has non-block content' );
}
}
$this->assertStringContainsString( '\s*
array(
'heading' => 'Selected work',
'order' => array( 'Selected work', 'Expressive Tea', 'Ailed', 'Published research' ),
'art' => 'alter-ego-code.png',
'art_alt' => '',
'atmosphere' => 'chrnx-character-stage__secondary-art',
),
'home-music' => array(
'heading' => 'The No Names',
'order' => array( 'The No Names', 'Grunge mexicano para cerebros que no se callan.', 'Un proyecto de banda activo hecho en Sinaloa: estudio crudo, cero live.' ),
'art' => 'the-no-names-musician-cutout.png',
'art_alt' => 'The No Names musician portrait',
'atmosphere' => 'chrnx-character-stage__collage',
),
'music-landing' => array(
'heading' => 'The No Names',
'order' => array( 'The No Names', 'Grunge mexicano para cerebros que no se callan.', 'The No Names es una banda activa', 'El proceso usa generative AI' ),
'art' => 'the-no-names-musician-cutout.png',
'art_alt' => 'The No Names musician portrait',
'atmosphere' => 'chrnx-character-stage__collage',
'atmos_alt' => 'The No Names collage',
),
'podcast-landing' => array(
'heading' => 'Podcasts',
'order' => array( 'Podcasts' ),
'art' => 'alter-ego-podcasts.png',
'art_alt' => '',
),
'about' => array(
'heading' => 'About',
'order' => array( 'About', 'I work as Lead Developer @Expressive Tea', 'I am a published researcher.', 'I am an autistic Level 1 software developer and researcher.', 'Beyond the work', 'I am the father of three wonderful children' ),
'art' => 'alter-ego-life.png',
'art_alt' => '',
'facets' => array( 'alter-ego-literature.png', 'alter-ego-gaming.png' ),
),
);
foreach ( $contracts as $name => $contract ) {
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
$dom = $this->load_html_fragment( $content );
$xpath = new DOMXPath( $dom );
$stages = $xpath->query( '//*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage ")]' );
$this->assertCount( 1, $stages, $name );
$stage = $stages->item( 0 );
$children = array();
foreach ( $stage->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
if ( XML_ELEMENT_NODE === $child->nodeType ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
$children[] = $child;
}
}
$this->assertCount( 3, $children, $name );
$this->assert_element_has_class( $children[0], 'chrnx-character-stage__atmosphere', $name );
$this->assertSame( 'true', $children[0]->getAttribute( 'aria-hidden' ), $name );
$this->assert_element_has_class( $children[1], 'chrnx-character-stage__content', $name );
$this->assert_element_has_class( $children[2], 'chrnx-character-stage__art', $name );
$this->assertCount( 1, $xpath->query( './/picture', $children[2] ), $name );
$art_markup = $dom->saveHTML( $children[2] );
$this->assertStringContainsString( $contract['art'], $art_markup, $name );
$this->assertStringContainsString( 'type="image/avif"', $art_markup, $name );
$this->assertStringContainsString( 'type="image/webp"', $art_markup, $name );
$this->assertStringContainsString( 'srcset="', $art_markup, $name );
$this->assertStringContainsString( 'loading="lazy"', $art_markup, $name );
$this->assertStringContainsString( 'alt="' . $contract['art_alt'] . '"', $art_markup, $name );
if ( '' === $contract['art_alt'] ) {
$this->assertStringContainsString( '', $art_markup, $name );
} else {
$this->assertStringNotContainsString( '' . $contract['heading'] . '
' ), $name );
$this->assertStringNotContainsString( 'saveHTML( $children[1] ) );
$previous = -1;
foreach ( $contract['order'] as $copy ) {
$position = strpos( $plain, $copy );
$this->assertNotFalse( $position, $name . ':' . $copy );
$this->assertGreaterThan( $previous, $position, $name . ':' . $copy );
$previous = $position;
}
if ( isset( $contract['atmosphere'] ) ) {
$atmosphere_markup = $dom->saveHTML( $children[0] );
$this->assertStringContainsString( $contract['atmosphere'], $atmosphere_markup, $name );
$this->assertStringContainsString( 'loading="lazy"', $atmosphere_markup, $name );
$this->assertStringContainsString( 'type="image/avif"', $atmosphere_markup, $name );
$this->assertStringContainsString( 'type="image/webp"', $atmosphere_markup, $name );
$this->assertStringContainsString( 'alt="' . ( $contract['atmos_alt'] ?? '' ) . '"', $atmosphere_markup, $name );
}
$this->assertCount( 0, $xpath->query( './/*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-facets ")]', $stage ), $name );
$facets = $xpath->query( '//*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-facets ")]' );
if ( isset( $contract['facets'] ) ) {
$this->assertCount( 1, $facets, $name );
$this->assertSame( $stage->parentNode, $facets->item( 0 )->parentNode, $name ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
$following_element = $stage->nextSibling; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
while ( $following_element && XML_ELEMENT_NODE !== $following_element->nodeType ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
$following_element = $following_element->nextSibling; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
}
$this->assertSame( $facets->item( 0 ), $following_element, $name );
$facets_markup = $dom->saveHTML( $facets->item( 0 ) );
foreach ( $contract['facets'] as $asset ) {
$this->assertStringContainsString( $asset, $facets_markup, $name );
}
$this->assertSame( 2, substr_count( $facets_markup, 'aria-hidden="true"' ), $name );
} else {
$this->assertCount( 0, $facets, $name );
}
}
}
/** Rendered pattern content survives parse/serialize without block-tree drift. */
public function test_character_led_patterns_preserve_editor_fidelity() {
foreach ( array( 'home-projects', 'home-music', 'music-landing', 'podcast-landing', 'about' ) as $name ) {
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
$parsed = parse_blocks( $content );
$serialized = serialize_blocks( $parsed );
$reparsed = parse_blocks( $serialized );
$initial_tree = $this->block_name_tree( $parsed, $name );
$round_trip = $this->block_name_tree( $reparsed, $name );
$this->assertSame( $initial_tree, $round_trip, $name );
$this->assertSame( 1, substr_count( strtolower( $content ), 'assertStringNotContainsString( 'loadHTML( '
' . $content . '
', LIBXML_NOERROR | LIBXML_NOWARNING );
return $dom;
}
/** Assert a DOM element carries one class token. */
private function assert_element_has_class( DOMElement $element, $class_name, $message ) {
$this->assertContains( $class_name, preg_split( '/\s+/', trim( $element->getAttribute( 'class' ) ) ), $message );
}
/** Return the ordered nested block-name tree while rejecting non-whitespace freeform nodes. */
private function block_name_tree( array $blocks, $message ) {
$tree = array();
foreach ( $blocks as $block ) {
if ( null === $block['blockName'] ) {
$this->assertSame( '', trim( $block['innerHTML'] ), $message . ' has freeform corruption' );
continue;
}
$tree[] = array( $block['blockName'], $this->block_name_tree( $block['innerBlocks'], $message ) );
}
return $tree;
}
/** Render a pattern file exactly as WordPress does. */
private function render_pattern( $path ) {
ob_start();
include $path;
return (string) ob_get_clean();
}
}