'200', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'REQUEST_METHOD' => 'GET', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'SCRIPT_FILENAME' => __DIR__ . '/request.php', 'SEBSTR_KDP_TEST_URL' => $url, 'SEBSTR_KDP_TEST_COUNTRY' => $country, 'SEBSTR_KDP_TEST_PEER' => $case[7] ?? '104.16.0.1', 'SEBSTR_KDP_TEST_KDP' => ($case[5] ?? true) ? '1' : '0', 'SEBSTR_KDP_TEST_SELLER' => ($case[6] ?? true) ? '1' : '0', ]; $process = proc_open(['php-cgi', '-d', 'display_errors=stderr', '-d', 'log_errors=0'], [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, null, $environment); check(is_resource($process), 'Start php-cgi'); $output = stream_get_contents($pipes[1]); $errors = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); check(proc_close($process) === 0, "$label CGI error $errors $output"); $locationPattern = str_replace(preg_quote('{seller-id}', '~'), '[A-Z0-9]+', preg_quote($expected, '~')); check(str_contains($output, "Status: $status ") && preg_match("~^Location: $locationPattern\\r?$~m", $output) === 1, "$label unexpected response $output $errors"); check(str_contains($output, 'clicks=1 logs=1'), "$label tracking mismatch"); check(str_contains($output, 'Cache-Control: private, no-store, max-age=0') === ($status === 302), "$label browser cache header mismatch"); check(str_contains($output, 'Cloudflare-CDN-Cache-Control: no-store') === ($status === 302), "$label Cloudflare cache header mismatch"); } echo count($cases) . " CGI redirect cases and five IP checks passed.\n"; define('YOURLS_ABSPATH', dirname(__DIR__, 4) . '/'); define('YOURLS_ADMIN', true); require YOURLS_ABSPATH . 'includes/functions-plugins.php'; function yourls_get_option($name, $default = false) { return $GLOBALS['test_options'][$name] ?? $default; } function yourls_update_option($name, $value) { $GLOBALS['test_options'][$name] = $value; return true; } function yourls_get_db($context) { return new class { public function add_plugin_page($slug, $title, $callback): void { $GLOBALS['test_pages'][$slug] = $callback; } }; } function yourls_create_nonce($action) { return 'test-' . $action; } function yourls_verify_nonce($action, $nonce) { if ($nonce !== yourls_create_nonce($action)) { throw new InvalidArgumentException('Invalid nonce'); } } require dirname(__DIR__) . '/plugin.php'; yourls_do_action('plugins_loaded'); $page = $GLOBALS['test_pages']['sebstr_amazon_kdp'] ?? null; check(is_callable($page), 'YOURLS admin page registration'); $_SERVER['REQUEST_METHOD'] = 'POST'; $key = Sebstr\AmazonKdp\SETTINGS; $_POST = ['operation' => 'save', 'asin' => 'testbook01', 'title' => '', 'nonce' => 'invalid']; ob_start(); try { $page(); check(false, 'Invalid nonce accepted'); } catch (InvalidArgumentException $exception) { check(yourls_get_option($key, []) === [], 'Invalid nonce changed registry'); } finally { ob_end_clean(); } $_POST['nonce'] = yourls_create_nonce($key); ob_start(); $page(); $html = ob_get_clean(); check(str_contains($html, '<Standard>'), 'Title is escaped'); check(yourls_get_option($key) === ['TESTBOOK01' => ''], 'ASIN and title saved'); $_POST = ['operation' => 'remove', 'asin' => 'TESTBOOK01', 'nonce' => yourls_create_nonce($key)]; ob_start(); $page(); ob_end_clean(); check(yourls_get_option($key) === [], 'ASIN removed'); echo "Admin registration, nonce rejection, title escaping, addition and removal passed.\n";