Files
404-enhancer/_cli_render.php
T
2026-11-16 12:24:05 -05:00

49 lines
2.0 KiB
PHP

<?php
/**
* Temporary CLI render test: boots WordPress with themes and renders templates/404.php
* as if a visitor hit a missing URL. NOT part of the plugin. Delete after running.
*/
error_reporting( E_ALL );
ini_set( 'display_errors', '1' );
define( 'WP_USE_THEMES', true );
$_SERVER['REQUEST_URI'] = '/this-page-does-not-exist-2024/';
$_SERVER['REQUEST_METHOD'] = 'GET';
require dirname( __DIR__, 3 ) . '/wp-load.php';
$GLOBALS['wp_query']->set_404();
status_header( 404 );
update_option( 'fe_custom_message', '<p>Sorry, we moved things around.</p>' );
update_option( 'fe_enabled_post_types', array( 'post', 'page' ) );
update_option( 'fe_suggestions_per_page', 3 );
ob_start();
include __DIR__ . '/templates/404.php';
$html = ob_get_clean();
$fail = 0;
function check( $label, $cond, $extra = '' ) {
global $fail;
if ( ! $cond ) {
$fail++;
}
printf( "%s %-52s %s\n", $cond ? 'PASS' : 'FAIL', $label, $extra );
}
check( 'renders without fatal', strlen( $html ) > 500, 'len=' . strlen( $html ) );
check( 'has .fe-404 wrapper', false !== strpos( $html, 'class="fe-404"' ) );
check( 'has heading', false !== strpos( $html, 'fe-404-heading' ) );
check( 'shows requested path', false !== strpos( $html, '/this-page-does-not-exist-2024/' ) );
check( 'shows custom message', false !== strpos( $html, 'Sorry, we moved things around.' ) );
check( 'has search form', false !== strpos( $html, 'fe-search-form' ) && false !== strpos( $html, 'name="s"' ) );
check( 'search prefilled from URL tokens', false !== strpos( $html, 'value="this does exist 2024"' ), '' );
check( 'has did-you-mean or sections container', false !== strpos( $html, 'fe-did-you-mean' ) || false !== strpos( $html, 'fe-section' ) );
check( 'stylesheet enqueued on 404', wp_style_is( 'fe-404', 'enqueued' ), '' );
check( 'no PHP warnings in output', false === strpos( $html, 'Warning:' ) && false === strpos( $html, 'Fatal error' ) && false === strpos( $html, 'Notice:' ) );
echo $fail ? "\nRENDER RESULT: $fail FAILURES\n" : "\nRENDER RESULT: ALL PASS\n";
exit( $fail ? 1 : 0 );