Unify the editorial canvas across section pages, keep release tracks out of Journal, and give each song a listening surface with audio, metadata, platform links, sharing, and optional featured art. Document and seed the local The No Names fixture without changing production data.
451 lines
27 KiB
PHP
451 lines
27 KiB
PHP
<?php
|
|
/**
|
|
* Tests for editable editorial patterns.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/** Verify pattern metadata, parsing, safety, and approved starter content. */
|
|
class PatternsTest extends WP_UnitTestCase {
|
|
/** Required pattern slugs. */
|
|
const PATTERNS = array(
|
|
'home-intro',
|
|
'home-projects',
|
|
'home-music',
|
|
'about',
|
|
'project-detail',
|
|
'music-landing',
|
|
'music-release',
|
|
'podcast-landing',
|
|
'podcast-show',
|
|
'podcast-episode',
|
|
);
|
|
|
|
/** Every pattern has valid metadata, parseable content, localization, and no unsafe material. */
|
|
public function test_all_patterns_are_parseable_localized_and_safe() {
|
|
foreach ( self::PATTERNS as $name ) {
|
|
$path = get_theme_file_path( 'patterns/' . $name . '.php' );
|
|
$this->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( '<!-- wp:', $content, $name );
|
|
$this->assertStringNotContainsString( '<?php', $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '/\b(?:TODO|lorem|example\.com)\b/i', $source . $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '#(?:/Users/|/home/|\bSuno\b|\bstems?\b|voice model|draft lyrics?|prompt directory)#i', $source . $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '#<(?:script|iframe|audio|form)\b|\[/?[a-z][^\]]*\]#i', $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '/(?:Spotify|Apple Music|YouTube Music|Bandcamp)/i', $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '/(?:Silicon Ranch|mailto:)/i', $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '/<p[^>]*>[^<]*(?:add|enter|insert|replace|select|choose|TODO)[^<]*<\/p>/i', $content, $name );
|
|
}
|
|
}
|
|
|
|
/** Local art is generated through escaped theme URIs with intrinsic dimensions. */
|
|
public function test_art_patterns_use_only_escaped_local_theme_images() {
|
|
foreach ( array( 'home-music', 'music-landing', 'podcast-landing', 'podcast-show' ) as $name ) {
|
|
$source = file_get_contents( get_theme_file_path( 'patterns/' . $name . '.php' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
|
|
$uses_responsive_helper = str_contains( $source, 'chrnx_get_topic_portrait_markup(' );
|
|
$uses_direct_uri = str_contains( $source, 'get_theme_file_uri(' ) && str_contains( $source, 'esc_url(' );
|
|
$this->assertTrue( $uses_responsive_helper || $uses_direct_uri, $name );
|
|
$this->assertMatchesRegularExpression( '/<img\b[^>]*\bwidth="1122"[^>]*\bheight="1402"/i', $content, $name );
|
|
$this->assertMatchesRegularExpression( '/<img\b[^>]*\bloading="lazy"[^>]*\bdecoding="async"/i', $content, $name );
|
|
}
|
|
$music = $this->render_pattern( get_theme_file_path( 'patterns/music-landing.php' ) );
|
|
$this->assertStringContainsString( 'the-no-names-collage-768.webp', $music );
|
|
$this->assertStringContainsString( 'the-no-names-musician-cutout-768.webp', $music );
|
|
$this->assertSame( 2, substr_count( $music, '<img' ) );
|
|
|
|
$show = $this->render_pattern( get_theme_file_path( 'patterns/podcast-show.php' ) );
|
|
$this->assertStringContainsString( 'alter-ego-podcasts-768.webp', $show );
|
|
$this->assertStringContainsString( 'alt=""', $show );
|
|
$this->assertStringContainsString( 'aria-hidden="true"', $show );
|
|
}
|
|
|
|
/** Home music layers a lazy responsive collage and musician in one frame. */
|
|
public function test_home_music_uses_responsive_decorative_collage_layer() {
|
|
$source = file_get_contents( get_theme_file_path( 'patterns/home-music.php' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/home-music.php' ) );
|
|
|
|
$this->assertStringContainsString( "chrnx_get_topic_portrait_markup( 'music-collage'", $source );
|
|
$this->assertStringContainsString( 'chrnx-character-stage__atmosphere', $content );
|
|
$this->assertStringContainsString( 'the-no-names-collage-384.avif 384w', $content );
|
|
$this->assertStringContainsString( 'the-no-names-collage-768.webp 768w', $content );
|
|
$this->assertStringContainsString( 'sizes="(max-width: 599px) 100vw, 720px"', $content );
|
|
$this->assertMatchesRegularExpression( '/class="chrnx-character-stage__collage"[^>]*loading="lazy"[^>]*alt=""/', $content );
|
|
$this->assertStringContainsString( 'the-no-names-musician-cutout-768.webp', $content );
|
|
$this->assertLessThan(
|
|
strpos( $content, 'the-no-names-musician-cutout-768.webp' ),
|
|
strpos( $content, 'the-no-names-collage-384.avif' )
|
|
);
|
|
}
|
|
|
|
/** Music copy contains only the exact approved public facts and release statuses. */
|
|
public function test_music_patterns_use_approved_honest_copy() {
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/music-landing.php' ) );
|
|
foreach ( array(
|
|
'Grunge mexicano para cerebros que no se callan.',
|
|
'grunge en español mexicano desde Sinaloa',
|
|
'estudio crudo, cero live',
|
|
'IA generativa',
|
|
'AI SLOP',
|
|
'Kurt Cobain',
|
|
) as $copy ) {
|
|
$this->assertStringContainsString( $copy, wp_strip_all_tags( $content ), $copy );
|
|
}
|
|
|
|
// Release names moved out of the pattern into the chrnx_release vocabulary.
|
|
$releases = chrnx_get_editorial_taxonomies()['chrnx_release']['terms'];
|
|
foreach ( array(
|
|
'El Hermoso Caos que habita en mi cabeza',
|
|
'Ando Bien Arriba',
|
|
'Aquí se Rompió una Jerga',
|
|
'Side B',
|
|
) as $release ) {
|
|
$this->assertContains( $release, $releases, $release );
|
|
}
|
|
$this->assertStringContainsString( 'chrnx/release-list', file_get_contents( get_theme_file_path( 'patterns/music-landing.php' ) ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
|
|
$this->assertDoesNotMatchRegularExpression( '/concert|rehearsal|coming live|tour/i', $content );
|
|
}
|
|
|
|
/** About copy is dignified, bounded, and contains the one approved external link. */
|
|
public function test_about_pattern_uses_approved_profile_copy_and_link() {
|
|
$source = file_get_contents( get_theme_file_path( 'patterns/about.php' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/about.php' ) );
|
|
foreach ( array( 'autistic Level 1', 'independent researcher', 'software developer', 'emotion modeling', 'game AI', 'Lead Developer @Expressive Tea', 'JavaScript', 'Python', 'open source', 'amateur data science', 'father of three wonderful children', 'musician', 'gamer', 'pet-friendly' ) as $copy ) {
|
|
$this->assertStringContainsString( $copy, wp_strip_all_tags( $content ), $copy );
|
|
}
|
|
$this->assertStringContainsString( 'https://arxiv.org/abs/2603.05352', $content );
|
|
$this->assertStringContainsString( 'esc_url(', $source );
|
|
$this->assertStringContainsString( 'target="_blank"', $content );
|
|
$this->assertStringContainsString( 'rel="noopener noreferrer external"', $content );
|
|
$this->assertDoesNotMatchRegularExpression( '/diagnos|inspir|overcame|despite/i', $content );
|
|
$plain = wp_strip_all_tags( $content );
|
|
$this->assertLessThan( strpos( $plain, 'autistic Level 1' ), strpos( $plain, 'Lead Developer @Expressive Tea' ) );
|
|
$this->assertLessThan( strpos( $plain, 'autistic Level 1' ), strpos( $plain, 'emotion modeling and game AI' ) );
|
|
$this->assertLessThan( strpos( $plain, 'father of three wonderful children' ), strpos( $plain, 'autistic Level 1' ) );
|
|
}
|
|
|
|
/** Reusable project/release/show/episode structures do not invent visible starter data. */
|
|
public function test_structural_patterns_expose_named_editable_regions_without_fake_claims() {
|
|
$expectations = array(
|
|
'project-detail' => array( 'chrnx/project-status', 'chrnx/project-summary', 'chrnx/project-links', 'chrnx/project-technologies', 'chrnx/project-publications', 'chrnx/project-related-posts' ),
|
|
'music-release' => array( 'chrnx/release-title', 'chrnx/release-status', 'chrnx/release-summary', 'chrnx/release-artwork', 'chrnx/release-tracklist' ),
|
|
'podcast-show' => array( 'chrnx/show-cover', 'chrnx/show-description', 'chrnx/show-hosts', 'chrnx/show-latest-episode', 'chrnx/show-archive', 'chrnx/show-listening-destinations' ),
|
|
'podcast-episode' => array( 'chrnx/episode-show', 'chrnx/episode-number', 'chrnx/episode-duration', 'chrnx/episode-participants', 'chrnx/episode-canonical-url', 'chrnx/episode-embed', 'chrnx/episode-show-notes' ),
|
|
);
|
|
foreach ( $expectations as $name => $labels ) {
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
|
|
foreach ( $labels as $label ) {
|
|
$this->assertStringContainsString( '"metadata":{"name":"' . $label . '"}', $content, $name . ':' . $label );
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Insertable page patterns render below the template's one H1 without duplicating post titles. */
|
|
public function test_insertable_page_patterns_preserve_single_template_h1() {
|
|
global $post;
|
|
$patterns = array( 'about', 'music-landing', 'podcast-landing', 'project-detail', 'podcast-show', 'podcast-episode' );
|
|
$template = file_get_contents( get_theme_file_path( 'templates/page.html' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local integration fixture.
|
|
foreach ( $patterns as $name ) {
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
|
|
$this->assertStringNotContainsString( 'wp:post-title', $content, $name );
|
|
$this->assertDoesNotMatchRegularExpression( '/<h1\b|wp:heading \{[^}]*"level":1/', $content, $name );
|
|
$this->assertNotEmpty( parse_blocks( $content ), $name );
|
|
$page_id = self::factory()->post->create(
|
|
array(
|
|
'post_type' => 'page',
|
|
'post_status' => 'publish',
|
|
'post_title' => 'Template page title',
|
|
'post_content' => $content,
|
|
)
|
|
);
|
|
$previous_post = $post;
|
|
$post = get_post( $page_id );
|
|
setup_postdata( $post );
|
|
$rendered = '';
|
|
try {
|
|
foreach ( parse_blocks( $template ) as $template_block ) {
|
|
$rendered .= ( new WP_Block(
|
|
$template_block,
|
|
array(
|
|
'postId' => $page_id,
|
|
'postType' => 'page',
|
|
)
|
|
) )->render();
|
|
}
|
|
} finally {
|
|
wp_reset_postdata();
|
|
$post = $previous_post;
|
|
}
|
|
$this->assertSame( 1, substr_count( $rendered, '<h1' ), $name );
|
|
}
|
|
|
|
foreach ( array( 'about', 'music-landing', 'podcast-landing' ) as $name ) {
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
|
|
$this->assertMatchesRegularExpression( '/<!-- wp:heading(?: \{"level":2\})? -->\s*<h2\b/', $content, $name );
|
|
}
|
|
}
|
|
|
|
/** Section openings expose one block-valid three-layer character stage. */
|
|
public function test_character_led_patterns_use_the_exact_shared_stage_api() {
|
|
$contracts = array(
|
|
'home-projects' => array(
|
|
'heading' => 'Proyectos',
|
|
// The project names come from the project list query now, not the pattern.
|
|
'order' => array( 'Proyectos' ),
|
|
'art' => 'alter-ego-code-768.webp',
|
|
'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-768.webp',
|
|
'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' ),
|
|
'art' => 'the-no-names-musician-cutout-768.webp',
|
|
'art_alt' => 'The No Names musician portrait',
|
|
'atmosphere' => 'chrnx-character-stage__collage',
|
|
'atmos_alt' => 'The No Names collage',
|
|
'atmosphere_hidden' => false,
|
|
),
|
|
'podcast-landing' => array(
|
|
'heading' => 'Podcasts',
|
|
'order' => array( 'Podcasts', 'Conversations about the work behind software, research, music, and games.' ),
|
|
'art' => 'alter-ego-podcasts-768.webp',
|
|
'art_alt' => '',
|
|
'atmosphere' => 'chrnx-character-stage__frequency',
|
|
),
|
|
'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-768.webp',
|
|
'art_alt' => '',
|
|
),
|
|
);
|
|
|
|
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 );
|
|
if ( false === ( $contract['atmosphere_hidden'] ?? true ) ) {
|
|
$this->assertFalse( $children[0]->hasAttribute( 'aria-hidden' ), $name );
|
|
} else {
|
|
$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( '<picture class="chrnx-responsive-portrait" aria-hidden="true">', $art_markup, $name );
|
|
} else {
|
|
$this->assertStringNotContainsString( '<picture class="chrnx-responsive-portrait" aria-hidden=', $art_markup, $name );
|
|
}
|
|
$this->assertSame( 1, substr_count( $dom->saveHTML( $children[1] ), '>' . $contract['heading'] . '</h2>' ), $name );
|
|
$this->assertStringNotContainsString( '<h1', strtolower( $content ), $name );
|
|
|
|
$plain = wp_strip_all_tags( $dom->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 );
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Meaningful Music collage text remains exposed to assistive technology. */
|
|
public function test_music_landing_meaningful_collage_is_not_hidden_by_the_atmosphere_layer() {
|
|
$content = $this->render_pattern( get_theme_file_path( 'patterns/music-landing.php' ) );
|
|
$dom = $this->load_html_fragment( $content );
|
|
$xpath = new DOMXPath( $dom );
|
|
$stage = $xpath->query( '//*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage ")]' )->item( 0 );
|
|
$collage = $xpath->query( '//img[@alt="The No Names collage"]' )->item( 0 );
|
|
|
|
$this->assertInstanceOf( DOMElement::class, $stage );
|
|
$this->assertInstanceOf( DOMElement::class, $collage );
|
|
$this->assertCount( 3, $xpath->query( './*', $stage ) );
|
|
$this->assertCount( 0, $xpath->query( 'ancestor::*[@aria-hidden="true"]', $collage ) );
|
|
$this->assertCount( 0, $xpath->query( 'ancestor::*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage__content ")]', $collage ) );
|
|
$this->assertCount( 1, $xpath->query( './*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage__atmosphere ")]//picture', $stage ) );
|
|
$this->assertCount( 1, $xpath->query( './*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage__art ")]//picture', $stage ) );
|
|
}
|
|
|
|
/** Music and Podcast compose their complete landing structures around one exact stage. */
|
|
public function test_music_and_podcast_landing_compositions_are_structurally_complete() {
|
|
$music = $this->render_pattern( get_theme_file_path( 'patterns/music-landing.php' ) );
|
|
$music_dom = $this->load_html_fragment( $music );
|
|
$music_xpath = new DOMXPath( $music_dom );
|
|
$music_stage = $music_xpath->query( '//*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage ")]' )->item( 0 );
|
|
$music_root = $music_xpath->query( '//*[@id="chrnx-test-root"]/*[1]' )->item( 0 );
|
|
|
|
$this->assertInstanceOf( DOMElement::class, $music_stage );
|
|
// Twice: once in the WebP srcset, once as the <img> fallback src.
|
|
$this->assertSame( 2, substr_count( $music, 'the-no-names-collage-768.webp' ) );
|
|
$this->assertSame( 2, substr_count( $music, 'the-no-names-musician-cutout-768.webp' ) );
|
|
$this->assertSame( 1, $music_xpath->query( './*[1]//img[@alt="The No Names collage"]', $music_stage )->length );
|
|
$this->assertSame( 0, $music_xpath->query( './*[2]//img[contains(@class,"chrnx-character-stage__collage")]', $music_stage )->length );
|
|
$this->assertSame( 1, $music_xpath->query( './*[3]//img[@alt="The No Names musician portrait"]', $music_stage )->length );
|
|
$this->assertSame( 3, $music_xpath->query( './*[2]/*[self::h2 or self::p]', $music_stage )->length );
|
|
$process = $music_xpath->query( './p[contains(. ,"Miren, miren: esto es")]', $music_root )->item( 0 );
|
|
$releases = $music_xpath->query( './h3[normalize-space()="Releases in progress"]', $music_root )->item( 0 );
|
|
$this->assertInstanceOf( DOMElement::class, $process );
|
|
$this->assertInstanceOf( DOMElement::class, $releases );
|
|
$this->assertSame( $music_stage, $process->previousElementSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
|
|
$this->assertSame( $process, $releases->previousElementSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
|
|
// The h4 release headings are rendered by chrnx/release-list now.
|
|
$this->assertSame( array( 2, 3 ), $this->heading_levels( $music_dom ) );
|
|
|
|
$podcast = $this->render_pattern( get_theme_file_path( 'patterns/podcast-landing.php' ) );
|
|
$podcast_dom = $this->load_html_fragment( $podcast );
|
|
$podcast_xpath = new DOMXPath( $podcast_dom );
|
|
$podcast_stage = $podcast_xpath->query( '//*[contains(concat(" ", normalize-space(@class), " "), " chrnx-character-stage ")]' )->item( 0 );
|
|
$this->assertInstanceOf( DOMElement::class, $podcast_stage );
|
|
$this->assertSame( 2, substr_count( $podcast, 'podcast-frequency-grunge-1536.webp' ) );
|
|
$podcast_atmosphere = $podcast_dom->saveHTML( $podcast_xpath->query( './*[1]', $podcast_stage )->item( 0 ) );
|
|
$this->assertStringContainsString( '<picture class="chrnx-responsive-atmosphere" aria-hidden="true">', $podcast_atmosphere );
|
|
$this->assertStringContainsString( 'alt=""', $podcast_atmosphere );
|
|
$podcast_art = $podcast_dom->saveHTML( $podcast_xpath->query( './*[3]', $podcast_stage )->item( 0 ) );
|
|
$this->assertStringContainsString( '<picture class="chrnx-responsive-portrait" aria-hidden="true">', $podcast_art );
|
|
$this->assertStringContainsString( 'alter-ego-podcasts-768.webp', $podcast_art );
|
|
$this->assertStringContainsString( 'alt=""', $podcast_art );
|
|
$this->assertSame( 1, substr_count( $podcast, '<!-- wp:chrnx/latest-podcast /-->' ) );
|
|
$this->assertSame( 1, substr_count( $podcast, '"metadata":{"name":"chrnx/podcast-show-discovery"}' ) );
|
|
$this->assertSame( 1, substr_count( $podcast, '"metadata":{"name":"chrnx/podcast-archive"}' ) );
|
|
$this->assertStringNotContainsString( 'chrnx/podcast-latest', $podcast );
|
|
$this->assertLessThan( strpos( $podcast, '<!-- wp:chrnx/latest-podcast /-->' ), strpos( $podcast, '<!-- /wp:group -->', strpos( $podcast, 'chrnx-character-stage__art' ) ) );
|
|
|
|
$pages = json_decode( (string) file_get_contents( get_theme_file_path( 'tests/fixtures/pages.json' ) ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local JSON fixture.
|
|
$podcasts = array_values( array_filter( $pages, static fn( $page ) => 'podcasts' === $page['slug'] ) );
|
|
$this->assertCount( 7, $pages );
|
|
$this->assertCount( 1, $podcasts );
|
|
$this->assertSame( '<!-- wp:pattern {"slug":"chrnx/podcast-landing"} /-->', $podcasts[0]['content'] );
|
|
}
|
|
|
|
/** 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 ), '<h2' ), $name );
|
|
$this->assertStringNotContainsString( '<h1', strtolower( $content ), $name );
|
|
}
|
|
}
|
|
|
|
/** Parse an HTML fragment for structural assertions. */
|
|
private function load_html_fragment( $content ) {
|
|
$dom = new DOMDocument();
|
|
$dom->loadHTML( '<?xml encoding="utf-8" ?><div id="chrnx-test-root">' . $content . '</div>', 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 heading levels in document order. */
|
|
private function heading_levels( DOMDocument $dom ) {
|
|
$levels = array();
|
|
foreach ( ( new DOMXPath( $dom ) )->query( '//h1|//h2|//h3|//h4|//h5|//h6' ) as $heading ) {
|
|
$levels[] = (int) substr( strtolower( $heading->tagName ), 1 ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- DOM API property.
|
|
}
|
|
return $levels;
|
|
}
|
|
|
|
/** 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();
|
|
}
|
|
}
|