|
22 | 22 | define( 'PPOM_E2E_META_IDS_OPTION', 'ppom_e2e_fixture_meta_ids' ); |
23 | 23 | } |
24 | 24 |
|
| 25 | +if ( ! defined( 'PPOM_E2E_LICENSE_FIXTURE_OPTION' ) ) { |
| 26 | + define( 'PPOM_E2E_LICENSE_FIXTURE_OPTION', 'ppom_e2e_license_fixture' ); |
| 27 | +} |
| 28 | + |
| 29 | +if ( ! defined( 'PPOM_E2E_LICENSE_FILTER_PRIORITY' ) ) { |
| 30 | + // Late priority so E2E overrides win over in-plugin license hooks. |
| 31 | + define( 'PPOM_E2E_LICENSE_FILTER_PRIORITY', PHP_INT_MAX - 10 ); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Default license fixture: valid Essential plan (wp-env has no store key). |
| 36 | + * |
| 37 | + * @return array{status:string,plan:int} |
| 38 | + */ |
| 39 | +function ppom_e2e_default_license_fixture() { |
| 40 | + return array( |
| 41 | + 'status' => 'valid', |
| 42 | + 'plan' => 1, |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Resolved license fixture for filters and AJAX responses. |
| 48 | + * |
| 49 | + * @return array{status:string,plan:int} |
| 50 | + */ |
| 51 | +function ppom_e2e_get_license_fixture() { |
| 52 | + $defaults = ppom_e2e_default_license_fixture(); |
| 53 | + $stored = get_option( PPOM_E2E_LICENSE_FIXTURE_OPTION, null ); |
| 54 | + |
| 55 | + if ( ! is_array( $stored ) ) { |
| 56 | + return $defaults; |
| 57 | + } |
| 58 | + |
| 59 | + $status = isset( $stored['status'] ) && 'invalid' === $stored['status'] ? 'invalid' : 'valid'; |
| 60 | + $plan = isset( $stored['plan'] ) ? max( 1, min( 3, absint( $stored['plan'] ) ) ) : $defaults['plan']; |
| 61 | + |
| 62 | + return array( |
| 63 | + 'status' => $status, |
| 64 | + 'plan' => $plan, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
25 | 68 | /** |
26 | 69 | * The attach modal only enables tag selection when the license filter returns valid. |
27 | 70 | * wp-env runs the free build without a store key; unlock valid for automated admin UI tests. |
| 71 | + * Use ppom_e2e_set_license_fixture to simulate inactive licenses in E2E. |
28 | 72 | */ |
29 | 73 | add_filter( |
30 | 74 | 'product_ppom_license_status', |
31 | | - static function () { |
32 | | - return 'valid'; |
| 75 | + static function ( $value ) { |
| 76 | + $config = ppom_e2e_get_license_fixture(); |
| 77 | + |
| 78 | + if ( 'valid' === $config['status'] ) { |
| 79 | + return 'valid'; |
| 80 | + } |
| 81 | + |
| 82 | + return ''; |
33 | 83 | }, |
34 | | - 5 |
| 84 | + PPOM_E2E_LICENSE_FILTER_PRIORITY, |
| 85 | + 1 |
| 86 | +); |
| 87 | + |
| 88 | +add_filter( |
| 89 | + 'product_ppom_license_plan', |
| 90 | + static function ( $value ) { |
| 91 | + $config = ppom_e2e_get_license_fixture(); |
| 92 | + |
| 93 | + if ( 'valid' !== $config['status'] ) { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + return (int) $config['plan']; |
| 98 | + }, |
| 99 | + PPOM_E2E_LICENSE_FILTER_PRIORITY, |
| 100 | + 1 |
35 | 101 | ); |
36 | 102 |
|
37 | 103 | /** |
@@ -1044,6 +1110,47 @@ function ppom_e2e_attach_ppom_group() { |
1044 | 1110 | add_action( 'wp_ajax_ppom_e2e_attach_ppom_group', 'ppom_e2e_attach_ppom_group' ); |
1045 | 1111 | add_action( 'wp_ajax_nopriv_ppom_e2e_attach_ppom_group', 'ppom_e2e_attach_ppom_group' ); |
1046 | 1112 |
|
| 1113 | +/** |
| 1114 | + * Set PPOM license fixture for E2E (drives product_ppom_license_* filters). |
| 1115 | + * |
| 1116 | + * @return void |
| 1117 | + */ |
| 1118 | +function ppom_e2e_set_license_fixture() { |
| 1119 | + ppom_e2e_require_capability(); |
| 1120 | + ppom_e2e_require_nonce(); |
| 1121 | + |
| 1122 | + $status_raw = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : ''; |
| 1123 | + $plan_raw = isset( $_POST['plan'] ) ? absint( wp_unslash( $_POST['plan'] ) ) : 0; |
| 1124 | + |
| 1125 | + $status = ( 'invalid' === $status_raw ) ? 'invalid' : 'valid'; |
| 1126 | + $plan = max( 1, min( 3, $plan_raw > 0 ? $plan_raw : 1 ) ); |
| 1127 | + |
| 1128 | + $stored = array( |
| 1129 | + 'status' => $status, |
| 1130 | + 'plan' => $plan, |
| 1131 | + ); |
| 1132 | + |
| 1133 | + update_option( PPOM_E2E_LICENSE_FIXTURE_OPTION, $stored, false ); |
| 1134 | + |
| 1135 | + wp_send_json_success( ppom_e2e_get_license_fixture() ); |
| 1136 | +} |
| 1137 | +add_action( 'wp_ajax_ppom_e2e_set_license_fixture', 'ppom_e2e_set_license_fixture' ); |
| 1138 | +add_action( 'wp_ajax_nopriv_ppom_e2e_set_license_fixture', 'ppom_e2e_set_license_fixture' ); |
| 1139 | + |
| 1140 | +/** |
| 1141 | + * Read the current PPOM license fixture (for E2E assertions). |
| 1142 | + * |
| 1143 | + * @return void |
| 1144 | + */ |
| 1145 | +function ppom_e2e_read_license_fixture() { |
| 1146 | + ppom_e2e_require_capability(); |
| 1147 | + ppom_e2e_require_nonce(); |
| 1148 | + |
| 1149 | + wp_send_json_success( ppom_e2e_get_license_fixture() ); |
| 1150 | +} |
| 1151 | +add_action( 'wp_ajax_ppom_e2e_read_license_fixture', 'ppom_e2e_read_license_fixture' ); |
| 1152 | +add_action( 'wp_ajax_nopriv_ppom_e2e_read_license_fixture', 'ppom_e2e_read_license_fixture' ); |
| 1153 | + |
1047 | 1154 | /** |
1048 | 1155 | * Reset PPOM E2E fixture state. |
1049 | 1156 | * |
@@ -1074,6 +1181,7 @@ function ppom_e2e_reset_state() { |
1074 | 1181 | } |
1075 | 1182 |
|
1076 | 1183 | delete_option( PPOM_E2E_META_IDS_OPTION ); |
| 1184 | + delete_option( PPOM_E2E_LICENSE_FIXTURE_OPTION ); |
1077 | 1185 |
|
1078 | 1186 | if ( defined( 'PPOM_PRODUCT_META_KEY' ) ) { |
1079 | 1187 | delete_post_meta_by_key( PPOM_PRODUCT_META_KEY ); |
|
0 commit comments