| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration; |
| 4 |
|
| 5 |
use Extendify\Config; |
| 6 |
use Extendify\PartnerData; |
| 7 |
use Extendify\AdminPageRouter; |
| 8 |
use WP_UnitTestCase; |
| 9 |
|
| 10 |
/** |
| 11 |
* redirectOnce() bounces a useAgentOnboarding partner from |
| 12 |
* ?page=extendify-launch to ?page=extendify-auto-launch on the first |
| 13 |
* fresh-site request. The bounce must carry the partner's deep-link params |
| 14 |
* (objective/title/structure/tone/…) — previously it passed only `page`, |
| 15 |
* silently dropping every other param on first login (issue #2246). |
| 16 |
*/ |
| 17 |
class AdminPageRouterTest extends WP_UnitTestCase |
| 18 |
{ |
| 19 |
public static function setUpBeforeClass(): void |
| 20 |
{ |
| 21 |
parent::setUpBeforeClass(); |
| 22 |
// PartnerData opts out without a partner id, which gates the |
| 23 |
// useAgentOnboarding round-trip redirectOnce() reads. |
| 24 |
if (!defined('EXTENDIFY_PARTNER_ID')) { |
| 25 |
define('EXTENDIFY_PARTNER_ID', 'test-partner'); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function setUp(): void |
| 30 |
{ |
| 31 |
parent::setUp(); |
| 32 |
Config::$showLaunch = true; |
| 33 |
delete_option('extendify_launch_loaded'); |
| 34 |
delete_transient('extendify_partner_data_cache_check'); |
| 35 |
update_option('extendify_partner_data_v2', ['useAgentOnboarding' => true]); |
| 36 |
new PartnerData(); |
| 37 |
$_GET = []; |
| 38 |
} |
| 39 |
|
| 40 |
public function tearDown(): void |
| 41 |
{ |
| 42 |
$_GET = []; |
| 43 |
parent::tearDown(); |
| 44 |
} |
| 45 |
|
| 46 |
public function test_launch_to_auto_launch_redirect_preserves_deep_link_params() |
| 47 |
{ |
| 48 |
$_GET = [ |
| 49 |
'page' => 'extendify-launch', |
| 50 |
'objective' => 'sell-products', |
| 51 |
'title' => 'Acme Co', |
| 52 |
'structure' => 'multi-page', |
| 53 |
'tone' => 'professional', |
| 54 |
]; |
| 55 |
|
| 56 |
$captured = $this->captureRedirect(fn () => (new AdminPageRouter())->redirectOnce()); |
| 57 |
|
| 58 |
$params = []; |
| 59 |
parse_str(wp_parse_url($captured, PHP_URL_QUERY), $params); |
| 60 |
|
| 61 |
$this->assertSame('extendify-auto-launch', $params['page']); |
| 62 |
$this->assertSame('sell-products', $params['objective'] ?? null); |
| 63 |
$this->assertSame('Acme Co', $params['title'] ?? null); |
| 64 |
$this->assertSame('multi-page', $params['structure'] ?? null); |
| 65 |
$this->assertSame('professional', $params['tone'] ?? null); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* handleLaunchRedirect() routes a useAgentOnboarding partner from |
| 70 |
* /wp-admin/?launch-redirect to ?page=extendify-auto-launch while |
| 71 |
* onboarding is still pending. Like redirectOnce(), that hop must carry the |
| 72 |
* partner's deep-link params — it previously redirected to a bare |
| 73 |
* admin.php?page=… and dropped every param the partner passed. |
| 74 |
*/ |
| 75 |
public function test_launch_redirect_entry_preserves_deep_link_params() |
| 76 |
{ |
| 77 |
Config::$launchCompleted = false; |
| 78 |
$_GET = [ |
| 79 |
'launch-redirect' => '1', |
| 80 |
'objective' => 'sell-products', |
| 81 |
'title' => 'Acme Co', |
| 82 |
'structure' => 'multi-page', |
| 83 |
'tone' => 'professional', |
| 84 |
]; |
| 85 |
|
| 86 |
$captured = $this->captureRedirect(fn () => (new AdminPageRouter())->handleLaunchRedirect()); |
| 87 |
|
| 88 |
$params = []; |
| 89 |
parse_str(wp_parse_url($captured, PHP_URL_QUERY), $params); |
| 90 |
|
| 91 |
$this->assertSame('extendify-auto-launch', $params['page']); |
| 92 |
$this->assertSame('sell-products', $params['objective'] ?? null); |
| 93 |
$this->assertSame('Acme Co', $params['title'] ?? null); |
| 94 |
$this->assertSame('multi-page', $params['structure'] ?? null); |
| 95 |
$this->assertSame('professional', $params['tone'] ?? null); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Runs $fn, trapping the wp_safe_redirect() that ends redirectOnce(), and |
| 100 |
* returns the destination. wp_safe_redirect() exits after the wp_redirect |
| 101 |
* filter, so we throw from the filter to short-circuit before the exit. |
| 102 |
*/ |
| 103 |
private function captureRedirect(callable $fn): string |
| 104 |
{ |
| 105 |
$captured = null; |
| 106 |
$trap = function ($location) use (&$captured) { |
| 107 |
$captured = $location; |
| 108 |
throw new \Exception('redirect-trapped'); |
| 109 |
}; |
| 110 |
add_filter('wp_redirect', $trap, 1); |
| 111 |
try { |
| 112 |
$fn(); |
| 113 |
} catch (\Exception $e) { |
| 114 |
if ($e->getMessage() !== 'redirect-trapped') { |
| 115 |
throw $e; |
| 116 |
} |
| 117 |
} finally { |
| 118 |
remove_filter('wp_redirect', $trap, 1); |
| 119 |
} |
| 120 |
|
| 121 |
$this->assertNotNull($captured, 'redirectOnce() did not redirect'); |
| 122 |
|
| 123 |
return $captured; |
| 124 |
} |
| 125 |
} |
| 126 |
|