Files
prautoblogger/tests/bootstrap.php
peptiderepo ae32876b6b fix: dossier page 403 — options.php-parent pattern (v0.19.3)
Dossier page dead on prod (403) since v0.19.2 deploy. Root cause:
on_register_menu() registered under 'prautoblogger-settings' parent then
unset the slug from $submenu to hide the nav entry. At request time
get_admin_page_parent() scanned $submenu, found nothing, fell back to
orphan hookname 'admin_page_prautoblogger-dossier'. Render callback was
registered under 'prautoblogger_page_prautoblogger-dossier'. Mismatch ->
wp_die(403). Same hookname-mismatch class as the board 404 (v0.19.1).

Fix (seq 21 §Required fix items 1-6):

1. options.php-parent: add_submenu_page('options.php', ...) -> hookname
   is 'admin_page_prautoblogger-dossier' at BOTH registration and request
   time. Hidden by construction; no $submenu mutation needed or permitted.
   Add PARENT_SLUG = 'options.php' constant.

2. Asset enqueue gate: updated from 'prautoblogger_page_*' to
   'admin_page_*' prefix to match options.php-parent hookname.

3. Deep-link URLs (admin.php?page=prautoblogger-dossier&post_id=N):
   parent-agnostic; unchanged. Asserted in
   test_url_for_post_contains_slug_and_post_id +
   test_url_for_post_exact_shape.

4. Regression test upgraded to request-time faithful:
   test_request_time_hookname_matches_registration_hookname simulates
   get_admin_page_parent() against post-registration $submenu state.
   FAILS on v0.19.2 hide-by-unset (4 failures, captured in evidence):
     - test_request_time_hookname_matches_registration_hookname FAIL
     - test_dossier_parent_slug_is_options_php FAIL
     - test_no_submenu_mutation_after_registration FAIL
     - test_asset_enqueue_gate_uses_admin_page_prefix FAIL
   PASSES on v0.19.3 options.php-parent (9/9 OK, captured in evidence).
   test_hide_by_unset_would_fail_request_time_check documents broken
   pattern inline.

5. ARCHITECTURE.md §22b: hidden-page convention cross-reference + v0.19.3
   incident record added. §23: page registration description updated to
   options.php-parent. File tree entry updated.
   CONVENTIONS.md §Hidden Admin Pages: full rule + both incident history
   (board 404 v0.19.1, dossier 403 v0.19.2) + test methodology.

6. Version 0.19.2 -> 0.19.3, CHANGELOG entry. tests/bootstrap.php:
   PRAUTOBLOGGER_PLUGIN_URL + PRAUTOBLOGGER_VERSION constants added
   (resolves 'Undefined constant' in enqueue tests).

Deletion audit: removed lines are the broken 'prautoblogger-settings'
parent string, the 12-line $submenu mutation block, the wrong hook prefix
'prautoblogger_page_*'. No other logic removed.

Agent-Session: dossier-403-hotfix-20260612
2026-06-12 11:18:15 +00:00

221 lines
7.8 KiB
PHP

<?php
/**
* PHPUnit bootstrap for PRAutoBlogger unit tests.
*
* Loads composer autoloader and defines WordPress constants.
* Brain\Monkey setup is done in BaseTestCase, not here.
*
* @package PRAutoBlogger\Tests
*/
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
// Define WordPress constants that source files expect.
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', '/tmp/wordpress/' );
}
if ( ! defined( 'PRAB_VERSION' ) ) {
define( 'PRAB_VERSION', '1.0.0-test' );
}
if ( ! defined( 'PRAB_PLUGIN_DIR' ) ) {
define( 'PRAB_PLUGIN_DIR', dirname( __DIR__ ) . '/' );
}
// Pipeline v2 Phase 1 defaults (source of truth: prautoblogger.php).
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_RUN_CEILING_USD' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_RUN_CEILING_USD', 0.50 );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_REQUEST_JSON_RETENTION_DAYS' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_REQUEST_JSON_RETENTION_DAYS', 14 );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_REASONING_MAX_TOKENS' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_REASONING_MAX_TOKENS', 2048 );
}
// WordPress time constants used by CostTracker and ContentAnalyzer.
if ( ! defined( 'DAY_IN_SECONDS' ) ) {
define( 'DAY_IN_SECONDS', 86400 );
}
if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
define( 'HOUR_IN_SECONDS', 3600 );
}
// Plugin constants used by OpenRouter provider retry logic.
if ( ! defined( 'PRAUTOBLOGGER_MAX_RETRIES' ) ) {
define( 'PRAUTOBLOGGER_MAX_RETRIES', 3 );
}
if ( ! defined( 'PRAUTOBLOGGER_API_TIMEOUT_SECONDS' ) ) {
define( 'PRAUTOBLOGGER_API_TIMEOUT_SECONDS', 30 );
}
if ( ! defined( 'PRAUTOBLOGGER_RETRY_BASE_DELAY_SECONDS' ) ) {
// Zero in the test bootstrap so retry-path tests don't spend real wall
// time in sleep(). Production default is 2 (see prautoblogger.php).
define( 'PRAUTOBLOGGER_RETRY_BASE_DELAY_SECONDS', 0 );
}
// Default model slugs used by Publisher, ChiefEditor, Activator, settings UI.
// Source of truth: prautoblogger.php. Test-only duplicate so unit tests can
// load without the main plugin bootstrap running.
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_ANALYSIS_MODEL' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_ANALYSIS_MODEL', 'google/gemini-2.5-flash-lite' );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_WRITING_MODEL' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_WRITING_MODEL', 'google/gemini-2.5-flash-lite' );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_EDITOR_MODEL' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_EDITOR_MODEL', 'google/gemini-2.5-flash-lite' );
}
// Default image model slug used by OpenRouter/Runware pricing fallbacks and
// settings UI. Source of truth: prautoblogger.php line 63.
// Historically this was defined implicitly by CloudflareImageProviderTest::setUp()
// — that test was deleted in v0.10.0 (cloudflare-workers-ai-removal), so the
// define now lives here where it belongs.
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL', 'runware:100@1' );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_PROVIDER' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_PROVIDER', 'runware' );
}
// Default image style suffix used by legacy ImagePromptBuilder paths / settings UI.
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX', 'Style: test infomercial style.' );
}
// Editorial style template (v0.16.0): default for prautoblogger_image_style_template.
// Must contain exactly one {{ topic_summary }} token (brief A5).
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_TEMPLATE' ) ) {
define(
'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_TEMPLATE',
"Editorial scientific illustration. Subject: {{ topic_summary }}. NO TEXT. NO PEOPLE. NO LOGOS. NO LABELS."
);
}
// WordPress database constants used in $wpdb queries.
if ( ! defined( 'ARRAY_A' ) ) {
define( 'ARRAY_A', 'ARRAY_A' );
}
if ( ! defined( 'OBJECT' ) ) {
define( 'OBJECT', 'OBJECT' );
}
/**
* Minimal WP_Query stub for unit tests.
*
* IdeaScorer uses WP_Query to check for existing posts with empty results.
* Board data provider tests inject posts via WP_Query::$_test_posts_queue
* (static FIFO): push arrays of WP_Post objects before instantiating the
* provider, and each new WP_Query() call will dequeue one batch.
*/
if ( ! class_exists( 'WP_Query' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
class WP_Query {
/** @var array<int, array> FIFO queue of post-arrays for tests. */
public static $_test_posts_queue = [];
/** @var array */
public $posts = [];
/** @var int */
public $found_posts = 0;
/** @var bool */
public $have_posts = false;
public function __construct( $args = [] ) {
if ( ! empty( self::$_test_posts_queue ) ) {
$this->posts = array_shift( self::$_test_posts_queue );
$this->found_posts = count( $this->posts );
$this->have_posts = $this->found_posts > 0;
}
}
public function have_posts(): bool {
return $this->have_posts;
}
public function the_post(): void {
// No-op.
}
}
}
// NOTE: Do NOT stub wp_strip_all_tags() or sanitize_text_field() here.
// Brain Monkey / Patchwork manages these stubs in individual tests via
// Functions\when(). Defining them in bootstrap causes Patchwork's
// "DefinedTooEarly" error.
// WP_Error stub — used by model registry and image pipeline tests.
// This is safe to define here because it's a class (not a function),
// so Patchwork doesn't need to intercept its definition.
if ( ! class_exists( 'WP_Error' ) ) {
class WP_Error {
public $errors = [];
public $error_data = [];
public function __construct( $code = '', $message = '', $data = '' ) {
if ( $code ) {
$this->errors[ $code ][] = $message;
if ( $data ) {
$this->error_data[ $code ] = $data;
}
}
}
public function get_error_code() {
$codes = array_keys( $this->errors );
return $codes ? $codes[0] : '';
}
public function get_error_message( $code = '' ) {
if ( ! $code ) {
$code = $this->get_error_code();
}
return isset( $this->errors[ $code ] ) ? $this->errors[ $code ][0] : '';
}
}
}
/**
* Minimal WP_Post stub for unit tests.
*
* Board data provider tests return WP_Post instances from WP_Query.
* Class defined here so instanceof checks in production code work correctly.
*/
if ( ! class_exists( 'WP_Post' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
class WP_Post {
/** @var int */
public $ID = 0;
/** @var string */
public $post_title = '';
/** @var string */
public $post_status = 'publish';
/** @var string */
public $post_date_gmt = '';
/** @var string */
public $post_modified_gmt = '';
/**
* @param array<string, mixed> $data
*/
public function __construct( array $data = [] ) {
foreach ( $data as $key => $value ) {
$this->$key = $value;
}
}
}
}
// Plugin URL and version constants used by admin enqueue methods.
// Defined here so all tests that load admin classes can stub WP enqueue calls
// without hitting "Undefined constant" errors.
if ( ! defined( 'PRAUTOBLOGGER_PLUGIN_URL' ) ) {
define( 'PRAUTOBLOGGER_PLUGIN_URL', 'https://test.example.com/wp-content/plugins/prautoblogger/' );
}
if ( ! defined( 'PRAUTOBLOGGER_VERSION' ) ) {
define( 'PRAUTOBLOGGER_VERSION', '0.19.3-test' );
}
if ( ! defined( 'PRAUTOBLOGGER_PLUGIN_DIR' ) ) {
define( 'PRAUTOBLOGGER_PLUGIN_DIR', dirname( __DIR__ ) . '/' );
}