Skip to content
wiki.fftac.org

Site Deployment Tests - Source Excerpt 01

Back to Site Deployment Tests

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/antichrist-content-system/tests/site-deployment-tests.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/antichrist-content-system/tests/site-deployment-tests.php

<?php
/**
 * Deployment regression tests for Antichrist.net site shell, routes, and locale dictionaries.
 *
 * Run with:
 * studio wp eval-file wp-content/plugins/antichrist-content-system/tests/site-deployment-tests.php
 *
 * @package AntichristNet
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit( 1 );
}

if ( ! function_exists( 'acn_cs_seed_content_system' ) ) {
	fwrite( STDERR, "Antichrist.net Content System is not loaded.\n" );
	exit( 1 );
}

$GLOBALS['acn_site_failures'] = array();

/**
 * Assert helper.
 *
 * @param bool   $condition Condition.
 * @param string $message Failure message.
 * @throws Exception When assertion fails.
 */
function acn_site_test_assert( $condition, $message ) {
	if ( ! $condition ) {
		throw new Exception( $message );
	}
}

/**
 * Run one test case.
 *
 * @param string   $name Test name.
 * @param callable $callback Test callback.
 */
function acn_site_test_case( $name, $callback ) {
	try {
		call_user_func( $callback );
		echo "PASS {$name}\n";
	} catch ( Throwable $throwable ) {
		$GLOBALS['acn_site_failures'][] = "{$name}: " . $throwable->getMessage();
		echo "FAIL {$name}: " . $throwable->getMessage() . "\n";
	}
}

/**
 * Run a callback with a forced public locale.
 *
 * @param string   $locale Locale tag.
 * @param callable $callback Callback.
 * @return mixed
 */
function acn_site_test_with_locale( $locale, $callback ) {
	$locale_filter = function () use ( $locale ) {
		return $locale;
	};

	add_filter( 'acn_current_locale_tag', $locale_filter );
	add_filter( 'acnmb_current_locale', $locale_filter );
	try {
		return call_user_func( $callback );
	} finally {
		remove_filter( 'acn_current_locale_tag', $locale_filter );
		remove_filter( 'acnmb_current_locale', $locale_filter );
	}
}

/**
 * Build a front-end test URL without passing through localized home_url filters.
 *
 * @param string $path Absolute path.
 * @return string
 */
function acn_site_test_url( $path ) {
	if ( class_exists( '\UAIXLocaleRouter\Support\Path' ) ) {
		return \UAIXLocaleRouter\Support\Path::build_front_url( $path );
	}

	if ( class_exists( '\Ns12LocaleRouter\Support\Path' ) ) {
		return \Ns12LocaleRouter\Support\Path::build_front_url( $path );
	}

	return untrailingslashit( (string) get_option( 'home' ) ) . '/' . ltrim( (string) $path, '/' );
}

/**
 * Request a front-end route and return the HTTP status code.
 *
 * @param string $path Absolute path.
 * @return int
 */
function acn_site_test_route_status( $path ) {
	$response = wp_remote_get(
		acn_site_test_url( $path ),
		array(
			'redirection' => 0,
			'timeout'     => 30,
		)
	);

	if ( is_wp_error( $response ) ) {
		throw new Exception( $path . ' request failed: ' . $response->get_error_message() );
	}

	return (int) wp_remote_retrieve_response_code( $response );
}

/**
 * Return all seeded page definitions that must survive deployment.
 *
 * @return array
 */
function acn_site_test_expected_pages() {
	$pages = function_exists( 'acn_cs_pages' ) ? acn_cs_pages() : array();

	if ( function_exists( 'acn_reports_pages' ) ) {
		$pages = array_merge( $pages, acn_reports_pages() );
	}

	return $pages;
}

/**
 * Render page body text for public-content assertions.
 *
 * @param WP_Post    $post Page post.
 * @param string|null $locale Optional forced locale.
 * @return string
 */
function acn_site_test_rendered_page_text( $post, $locale = null ) {
	if ( ! $locale ) {
		$html = do_shortcode( (string) $post->post_content );
		return trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( $html ) ) );
	}

	$page_post = $post;
	$html = acn_site_test_with_locale(
		$locale,
		function () use ( $page_post ) {
			global $post, $wp_query;

			$previous_post = $post;
			$previous_query = $wp_query;

			$wp_query = new WP_Query();
			$wp_query->queried_object    = $page_post;
			$wp_query->queried_object_id = $page_post->ID;
			$wp_query->post              = $page_post;
			$wp_query->posts             = array( $page_post );
			$wp_query->post_count        = 1;
			$wp_query->is_page           = true;
			$wp_query->is_singular       = true;
			$wp_query->is_main_query     = true;
			$post = $page_post;
			setup_postdata( $page_post );

			try {
				return apply_filters( 'the_content', (string) $page_post->post_content );
			} finally {
				wp_reset_postdata();
				$post     = $previous_post;
				$wp_query = $previous_query;
			}
		}
	);

	return trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( $html ) ) );
}

/**
 * Extract source English phrases that must not leak into localized pages.
 *
 * @param array $page Seeded page definition.
 * @return array
 */
function acn_site_test_source_english_fragments( $page ) {
	$source = '';
	if ( ! empty( $page['title'] ) ) {
		$source .= ' ' . (string) $page['title'];
	}
	if ( ! empty( $page['content'] ) ) {
		$source .= ' ' . wp_strip_all_tags( strip_shortcodes( (string) $page['content'] ) );
	}

	$source = html_entity_decode( preg_replace( '/\s+/', ' ', $source ), ENT_QUOTES, 'UTF-8' );
	$parts  = preg_split( '/[.!?;:]+/', $source );
	$fragments = array();

	foreach ( $parts as $part ) {
		$fragment = trim( preg_replace( '/[^A-Za-z0-9,\-\s]+/', '', (string) $part ) );
		$fragment = trim( preg_replace( '/\s+/', ' ', $fragment ) );
		if ( strlen( $fragment ) < 32 ) {
			continue;
		}
		if ( ! preg_match( '/\b(the|and|this|that|with|from|into|public|research|archive|source|topic|discussion|community|page)\b/i', $fragment ) ) {
			continue;
		}
		$fragments[] = $fragment;
		if ( count( $fragments ) >= 12 ) {
			break;
		}
	}

	return array_values( array_unique( $fragments ) );
}

/**
 * Return the active locale repository class.
 *
 * @return string
 */
function acn_site_test_locale_repository_class() {
	if ( class_exists( '\UAIXLocaleRouter\LocaleRepository' ) ) {
		return '\UAIXLocaleRouter\LocaleRepository';
	}

	if ( class_exists( '\Ns12LocaleRouter\LocaleRepository' ) ) {
		return '\Ns12LocaleRouter\LocaleRepository';
	}

	return '';
}

/**
 * Return the active dictionary repository class.
 *
 * @return string
 */
function acn_site_test_dictionary_repository_class() {
	if ( class_exists( '\UAIXLocaleRouter\DictionaryRepository' ) ) {
		return '\UAIXLocaleRouter\DictionaryRepository';
	}

	if ( class_exists( '\Ns12LocaleRouter\DictionaryRepository' ) ) {
		return '\Ns12LocaleRouter\DictionaryRepository';
	}

	return '';
}

/**
 * Return the active locale-router path helper class.
 *
 * @return string
 */
function acn_site_test_path_class() {
	if ( class_exists( '\UAIXLocaleRouter\Support\Path' ) ) {
		return '\UAIXLocaleRouter\Support\Path';
	}

	if ( class_exists( '\Ns12LocaleRouter\Support\Path' ) ) {
		return '\Ns12LocaleRouter\Support\Path';
	}

	return '';
}

/**
 * Return enabled locale records.
 *
 * @return array
 */
function acn_site_test_enabled_locales() {
	$locale_repository = acn_site_test_locale_repository_class();
	acn_site_test_assert( '' !== $locale_repository, 'Locale repository missing.' );

	$enabled = $locale_repository::get_enabled_locales();
	acn_site_test_assert( is_array( $enabled ) && ! empty( $enabled ), 'No enabled locales found.' );

	foreach ( $enabled as $locale ) {
		acn_site_test_assert( ! empty( $locale['tag'] ), 'Enabled locale is missing tag.' );
		acn_site_test_assert( ! empty( $locale['urlTag'] ), $locale['tag'] . ' is missing URL tag.' );
	}

	$enabled_tags = wp_list_pluck( $enabled, 'tag' );
	$required_tags = function_exists( 'acn_cs_required_locale_tags' ) ? acn_cs_required_locale_tags() : array( 'en-US', 'es-US', 'fr-FR', 'zh-CN' );
	sort( $enabled_tags );
	sort( $required_tags );
	acn_site_test_assert( $required_tags === $enabled_tags, 'Enabled locales should be exactly ' . implode( ', ', $required_tags ) . '; found ' . implode( ', ', $enabled_tags ) . '.' );

	return $enabled;
}

/**
 * Return normalized public route path for a seeded page path.
 *
 * @param string $path Page path.
 * @return string
 */
function acn_site_test_public_route_path( $path ) {
	$path = trim( (string) $path, '/' );

	return '' === $path ? '/' : '/' . $path . '/';
}

/**
 * Return a locale-prefixed route path for a seeded page.
 *
 * @param string $path Page path.
 * @param array  $locale Locale record.
 * @return string
 */
function acn_site_test_localized_route_path( $path, $locale ) {
	return '/' . trim( (string) $locale['urlTag'], '/' ) . acn_site_test_public_route_path( $path );
}

/**
 * Strip an enabled locale prefix using the active router path helper.
 *
 * @param string $route Route path.
 * @return string
 */
function acn_site_test_strip_locale_prefix( $route ) {
	$path_class = acn_site_test_path_class();

	if ( '' !== $path_class ) {
		return $path_class::strip_locale_prefix( $route );
	}

	return acn_site_test_public_route_path( $route );
}