array( 'code' => 200 ), 'body' => '{"ok":true}' ); $GLOBALS['aisthetix_review_posts'] = array(); function get_option( $name, $default = false ) { // phpcs:ignore return array_key_exists( $name, $GLOBALS['aisthetix_review_options'] ) ? $GLOBALS['aisthetix_review_options'][ $name ] : $default; } function update_option( $name, $value ) { // phpcs:ignore $GLOBALS['aisthetix_review_options'][ $name ] = $value; return true; } function get_transient( $name ) { // phpcs:ignore return array_key_exists( $name, $GLOBALS['aisthetix_review_transients'] ) ? $GLOBALS['aisthetix_review_transients'][ $name ] : false; } function set_transient( $name, $value ) { // phpcs:ignore $GLOBALS['aisthetix_review_transients'][ $name ] = $value; return true; } function delete_transient( $name ) { // phpcs:ignore unset( $GLOBALS['aisthetix_review_transients'][ $name ] ); return true; } function add_action( $hook, $callback ) { // phpcs:ignore $GLOBALS['aisthetix_review_hooks'][ $hook ] = $callback; } function current_user_can( $capability ) { // phpcs:ignore return 'manage_woocommerce' === $capability && $GLOBALS['aisthetix_review_can_manage']; } function wp_remote_get() { // phpcs:ignore return array( 'response' => array( 'code' => 200 ), 'body' => json_encode( $GLOBALS['aisthetix_review_get_payload'] ), ); } function wp_remote_post( $url, $args ) { // phpcs:ignore $GLOBALS['aisthetix_review_posts'][] = array( 'url' => $url, 'args' => $args ); return $GLOBALS['aisthetix_review_post_response']; } function wp_remote_retrieve_response_code( $response ) { // phpcs:ignore return isset( $response['response']['code'] ) ? $response['response']['code'] : 0; } function wp_remote_retrieve_body( $response ) { // phpcs:ignore return isset( $response['body'] ) ? $response['body'] : ''; } function is_wp_error( $value ) { return $value instanceof Aisthetix_Review_Test_Error; } // phpcs:ignore function wp_json_encode( $value ) { return json_encode( $value ); } // phpcs:ignore function apply_filters( $hook, $value ) { return $value; } // phpcs:ignore function untrailingslashit( $value ) { return rtrim( $value, '/' ); } // phpcs:ignore function __( $value ) { return $value; } // phpcs:ignore function sanitize_text_field( $value ) { return $value; } // phpcs:ignore function sanitize_title( $value ) { return $value; } // phpcs:ignore function esc_url_raw( $value ) { return $value; } // phpcs:ignore function wp_unslash( $value ) { return $value; } // phpcs:ignore function esc_html__( $value ) { return $value; } // phpcs:ignore function esc_url( $value ) { return $value; } // phpcs:ignore function esc_attr( $value ) { return $value; } // phpcs:ignore function admin_url( $path = '' ) { return 'https://merchant.example/wp-admin/' . ltrim( $path, '/' ); } // phpcs:ignore function wp_nonce_field( $action, $name ) { // phpcs:ignore $GLOBALS['aisthetix_review_nonce_calls'][] = array( $action, $name ); echo ''; } function check_admin_referer( $action, $name ) { // phpcs:ignore $GLOBALS['aisthetix_review_nonce_calls'][] = array( $action, $name ); return true; } class Aisthetix_Review_Test_Error {} } namespace Aisthetix\TryOn { class Settings { const PAGE_SLUG = 'aisthetix-tryon'; } } namespace { require_once __DIR__ . '/../includes/class-plugin.php'; require_once __DIR__ . '/../includes/class-status.php'; require_once __DIR__ . '/../includes/class-review-prompt.php'; } namespace Aisthetix\TryOn { class TestableReviewPrompt extends ReviewPrompt { public $redirect_notice = null; protected function redirect_back( $notice = '' ) { $this->redirect_notice = $notice; } } } namespace { use Aisthetix\TryOn\ReviewPrompt; use Aisthetix\TryOn\Status; use Aisthetix\TryOn\TestableReviewPrompt; $passed = 0; $failed = 0; function check( $name, $expected, $actual ) { global $passed, $failed; if ( $expected === $actual ) { echo " ✓ {$name}\n"; $passed++; return; } echo " ✗ {$name}\n"; echo ' expected: ' . var_export( $expected, true ) . "\n"; echo ' actual: ' . var_export( $actual, true ) . "\n"; $failed++; } function default_status( $overrides = array() ) { return array_merge( array( 'active' => true, 'withinQuota' => true, 'plan' => 'trial', 'includedTryOns'=> 50, 'remaining' => 50, 'resetsAt' => '', ), $overrides ); } function reset_review_world( $connected = true, $state = array(), $can_manage = true, $status = array() ) { $GLOBALS['aisthetix_review_options'] = array(); $GLOBALS['aisthetix_review_transients'] = array(); $GLOBALS['aisthetix_review_hooks'] = array(); $GLOBALS['aisthetix_review_nonce_calls'] = array(); $GLOBALS['aisthetix_review_can_manage'] = $can_manage; $GLOBALS['aisthetix_review_get_payload'] = default_status( $status ); $GLOBALS['aisthetix_review_post_response'] = array( 'response' => array( 'code' => 200 ), 'body' => '{"ok":true}' ); $GLOBALS['aisthetix_review_posts'] = array(); $_GET = array( 'page' => 'aisthetix-tryon' ); if ( $connected ) { $GLOBALS['aisthetix_review_options']['aisthetix_tryon_publishable_key'] = 'pk_test'; $GLOBALS['aisthetix_review_options']['aisthetix_tryon_secret_key'] = 'sk_test'; } if ( ! empty( $state ) ) { $GLOBALS['aisthetix_review_options'][ ReviewPrompt::OPTION_STATE ] = $state; } } function render_review_prompt( $prompt ) { ob_start(); $prompt->render(); return ob_get_clean(); } echo "ReviewPrompt\n"; reset_review_world(); $constructor = new ReflectionMethod( ReviewPrompt::class, '__construct' ); check( 'the constructor receives the existing Status service', Status::class, $constructor->getParameters()[0]->getType()->getName() ); $status = new Status(); check( 'missing Core review data normalizes to incomplete', false, $status->get( true )['review']['completed'] ); reset_review_world( true, array(), true, array( 'review' => array( 'completed' => 'yes' ) ) ); $status = new Status(); check( 'malformed Core review data does not suppress the notice', false, $status->get( true )['review']['completed'] ); reset_review_world( true, array(), true, array( 'review' => array( 'completed' => true ) ) ); $prompt = new ReviewPrompt( new Status() ); check( 'a Core-completed review hides the WordPress notice', false, $prompt->should_render() ); reset_review_world(); $prompt = new ReviewPrompt( new Status() ); check( 'a connected store is immediately promptable', true, $prompt->should_render() ); check( 'the exact WordPress.org review URL is used', 'https://wordpress.org/support/plugin/aisthetix-virtual-try-on/reviews/#new-post', ReviewPrompt::REVIEW_URL ); $output = render_review_prompt( $prompt ); check( 'the external CTA opens in a new tab', true, false !== strpos( $output, 'target="_blank"' ) ); check( 'the external CTA uses the exact URL directly', true, false !== strpos( $output, 'href="' . ReviewPrompt::REVIEW_URL . '"' ) ); check( 'rendering and its external CTA do not complete the prompt', array(), $GLOBALS['aisthetix_review_options'][ ReviewPrompt::OPTION_STATE ] ?? array() ); check( 'the confirmation form includes the expected nonce', array( array( ReviewPrompt::ACTION_COMPLETE, '_aisthetix_nonce' ) ), $GLOBALS['aisthetix_review_nonce_calls'] ); reset_review_world( true, array( 'closed' => true, 'outcome' => 'dismissed', 'times_shown' => 2 ) ); $prompt = new ReviewPrompt( new Status() ); check( 'legacy dismissal and count state no longer suppress the notice', true, $prompt->should_render() ); reset_review_world( false ); $prompt = new ReviewPrompt( new Status() ); check( 'a disconnected store is not promptable', false, $prompt->should_render() ); reset_review_world( true, array(), false ); $prompt = new ReviewPrompt( new Status() ); check( 'the capability check prevents notice output', '', render_review_prompt( $prompt ) ); reset_review_world(); $prompt = new TestableReviewPrompt( new Status() ); $prompt->handle_completion(); check( 'the confirmation handler checks the expected nonce', array( ReviewPrompt::ACTION_COMPLETE, '_aisthetix_nonce' ), $GLOBALS['aisthetix_review_nonce_calls'][0] ); check( 'completion posts to Core’s exact endpoint', 'https://woo.aisthetix.fyi/api/storefront/review', $GLOBALS['aisthetix_review_posts'][0]['url'] ); check( 'completion uses the server-only secret header', 'sk_test', $GLOBALS['aisthetix_review_posts'][0]['args']['headers']['X-Aisthetix-Secret'] ); check( 'completion sends the exact JSON action', '{"action":"completed"}', $GLOBALS['aisthetix_review_posts'][0]['args']['body'] ); check( 'only successful Core completion writes the local mirror', array( 'completed' => true ), $GLOBALS['aisthetix_review_options'][ ReviewPrompt::OPTION_STATE ] ); check( 'successful confirmation returns to settings without an error', '', $prompt->redirect_notice ); check( 'the successful local mirror hides later renders', false, $prompt->should_render() ); reset_review_world(); $GLOBALS['aisthetix_review_post_response'] = array( 'response' => array( 'code' => 503 ), 'body' => '{"ok":true}' ); $prompt = new TestableReviewPrompt( new Status() ); $prompt->handle_completion(); check( 'a non-200 Core response does not write a local mirror', false, isset( $GLOBALS['aisthetix_review_options'][ ReviewPrompt::OPTION_STATE ] ) ); check( 'a non-200 Core response redirects with the retryable error', ReviewPrompt::ERROR_NOTICE, $prompt->redirect_notice ); check( 'a failed completion keeps the banner visible', true, $prompt->should_render() ); $_GET['aisthetix_review_notice'] = ReviewPrompt::ERROR_NOTICE; check( 'a failed completion renders a localized retryable error', true, false !== strpos( render_review_prompt( $prompt ), 'We could not record your review confirmation. Please try again.' ) ); reset_review_world(); $GLOBALS['aisthetix_review_post_response'] = array( 'response' => array( 'code' => 200 ), 'body' => '{"ok":false}' ); $prompt = new ReviewPrompt( new Status() ); check( 'an invalid Core success payload does not complete the notice', false, $prompt->complete_with_core() ); check( 'an invalid Core success payload leaves no local mirror', false, isset( $GLOBALS['aisthetix_review_options'][ ReviewPrompt::OPTION_STATE ] ) ); reset_review_world(); $GLOBALS['aisthetix_review_post_response'] = new Aisthetix_Review_Test_Error(); $prompt = new ReviewPrompt( new Status() ); check( 'a Core transport failure does not complete the notice', false, $prompt->complete_with_core() ); reset_review_world(); $prompt = new ReviewPrompt( new Status() ); $prompt->init(); check( 'only the explicit completion action is registered', true, isset( $GLOBALS['aisthetix_review_hooks']['admin_post_' . ReviewPrompt::ACTION_COMPLETE] ) ); check( 'no snooze, dismiss, or external-CTA action hooks remain', false, isset( $GLOBALS['aisthetix_review_hooks']['admin_post_aisthetix_tryon_review_later'] ) || isset( $GLOBALS['aisthetix_review_hooks']['admin_post_aisthetix_tryon_review_dismiss'] ) || isset( $GLOBALS['aisthetix_review_hooks']['admin_post_aisthetix_tryon_leave_review'] ) ); echo "\n{$passed} passed, {$failed} failed\n"; exit( $failed === 0 ? 0 : 1 ); }