Skip to content

Commit 45d2eb4

Browse files
author
Soare Robert-Daniel
committed
dev: add a fixture for license toggle
1 parent 4fc195f commit 45d2eb4

7 files changed

Lines changed: 266 additions & 4 deletions

File tree

bin/wp-env/mu-plugins/ppom-e2e-bootstrap.php

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,82 @@
2222
define( 'PPOM_E2E_META_IDS_OPTION', 'ppom_e2e_fixture_meta_ids' );
2323
}
2424

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+
2568
/**
2669
* The attach modal only enables tag selection when the license filter returns valid.
2770
* 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.
2872
*/
2973
add_filter(
3074
'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 '';
3383
},
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
35101
);
36102

37103
/**
@@ -1044,6 +1110,47 @@ function ppom_e2e_attach_ppom_group() {
10441110
add_action( 'wp_ajax_ppom_e2e_attach_ppom_group', 'ppom_e2e_attach_ppom_group' );
10451111
add_action( 'wp_ajax_nopriv_ppom_e2e_attach_ppom_group', 'ppom_e2e_attach_ppom_group' );
10461112

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+
10471154
/**
10481155
* Reset PPOM E2E fixture state.
10491156
*
@@ -1074,6 +1181,7 @@ function ppom_e2e_reset_state() {
10741181
}
10751182

10761183
delete_option( PPOM_E2E_META_IDS_OPTION );
1184+
delete_option( PPOM_E2E_LICENSE_FIXTURE_OPTION );
10771185

10781186
if ( defined( 'PPOM_PRODUCT_META_KEY' ) ) {
10791187
delete_post_meta_by_key( PPOM_PRODUCT_META_KEY );

tests/e2e/fixtures/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export {
1010
buildSelectField,
1111
buildTextField,
1212
} from './fields.js';
13+
export { getPpomLicenseFixture, setPpomLicenseFixture } from './license.js';
1314
export {
1415
createProductCategory,
1516
createProductTag,

tests/e2e/fixtures/license.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { postBootstrapAction } from './internal.js';
2+
3+
/**
4+
* Read the current PPOM E2E license fixture from the server.
5+
*
6+
* @param {import('@wordpress/e2e-test-utils-playwright').RequestUtils} requestUtils Authenticated request utils.
7+
* @return {Promise<{ status: string, plan: number }>}
8+
*/
9+
export async function getPpomLicenseFixture( requestUtils ) {
10+
return postBootstrapAction( requestUtils, 'ppom_e2e_read_license_fixture', {} );
11+
}
12+
13+
/**
14+
* Control PPOM E2E license stubs (product_ppom_license_status / product_ppom_license_plan).
15+
*
16+
* @param {import('@wordpress/e2e-test-utils-playwright').RequestUtils} requestUtils Authenticated request utils.
17+
* @param {object} options
18+
* @param {boolean} options.valid When true, filters report a valid license.
19+
* @param {number} [options.plan=1] Plan tier 1 (Essential) through 3 (VIP) when valid.
20+
* @return {Promise<{ status: string, plan: number }>} Resolved fixture from the server.
21+
*/
22+
export async function setPpomLicenseFixture( requestUtils, { valid, plan = 1 } ) {
23+
return postBootstrapAction( requestUtils, 'ppom_e2e_set_license_fixture', {
24+
status: valid ? 'valid' : 'invalid',
25+
plan: String( plan ),
26+
} );
27+
}

tests/e2e/specs/attach-modal.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createProductTag,
88
createSimpleProduct,
99
getPpomAttachRowMeta,
10+
setPpomLicenseFixture,
1011
} from "../fixtures/index.js";
1112
import { addNewField, createSimpleGroupField, fillFieldNameAndId, pickFieldTypeInModal, saveFieldInModal, saveFields } from "../utils";
1213

@@ -109,6 +110,8 @@ test.describe("Attach Modal", () => {
109110
admin,
110111
requestUtils,
111112
} ) => {
113+
await setPpomLicenseFixture( requestUtils, { valid: true, plan: 1 } );
114+
112115
// --- Setup: catalog tag term + PPOM group (no product is tagged here) ---
113116
const tag = await createProductTag( requestUtils );
114117
const { ppomId } = await createSimpleGroupField( admin, page );

tests/e2e/specs/bootstrap-fixtures.spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
createProductVariation,
1111
createSimpleProduct,
1212
createVariableProduct,
13+
getPpomLicenseFixture,
14+
setPpomLicenseFixture,
1315
} from '../fixtures/index.js';
14-
import { getE2EBootstrapNonce } from '../fixtures/internal.js';
16+
import { getE2EBootstrapNonce, resetE2EState } from '../fixtures/internal.js';
1517

1618
async function postAjax( requestContext, params ) {
1719
const response = await requestContext.fetch( 'wp-admin/admin-ajax.php', {
@@ -202,4 +204,28 @@ test.describe( 'Bootstrap Fixtures', () => {
202204
await anonymousRequest.dispose();
203205
}
204206
} );
207+
208+
test( 'license fixture toggles via bootstrap AJAX and reset restores defaults', async ( {
209+
requestUtils,
210+
} ) => {
211+
await setPpomLicenseFixture( requestUtils, { valid: false } );
212+
213+
let snapshot = await getPpomLicenseFixture( requestUtils );
214+
215+
expect( snapshot.status ).toBe( 'invalid' );
216+
217+
await setPpomLicenseFixture( requestUtils, { valid: true, plan: 3 } );
218+
219+
snapshot = await getPpomLicenseFixture( requestUtils );
220+
221+
expect( snapshot.status ).toBe( 'valid' );
222+
expect( snapshot.plan ).toBe( 3 );
223+
224+
await resetE2EState( requestUtils );
225+
226+
snapshot = await getPpomLicenseFixture( requestUtils );
227+
228+
expect( snapshot.status ).toBe( 'valid' );
229+
expect( snapshot.plan ).toBe( 1 );
230+
} );
205231
} );

tests/unit/class-ppom-test-case.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ abstract class PPOM_Test_Case extends WP_UnitTestCase {
2828
*/
2929
protected $original_wc_customer;
3030

31+
/**
32+
* Whether test license filters are registered.
33+
*
34+
* @var bool
35+
*/
36+
protected $ppom_test_license_filters_active = false;
37+
38+
/**
39+
* @var bool
40+
*/
41+
protected $ppom_test_license_want_valid = true;
42+
43+
/**
44+
* @var int
45+
*/
46+
protected $ppom_test_license_plan_value = 1;
47+
3148
/**
3249
* Reset globals and settings used by the helper-heavy tests.
3350
*
@@ -68,9 +85,63 @@ public function tearDown(): void {
6885
WC()->customer = $this->original_wc_customer;
6986
}
7087

88+
$this->remove_ppom_license_filters();
89+
7190
parent::tearDown();
7291
}
7392

93+
/**
94+
* Stub Themeisle-style license filters for the current test.
95+
*
96+
* @param string $status Use 'valid' or 'invalid'.
97+
* @param int $plan Plan id 1–3 when valid (Essential / Plus / VIP tiers via get_license_category).
98+
*
99+
* @return void
100+
*/
101+
protected function with_ppom_license_filters( $status, $plan = 1 ) {
102+
$this->remove_ppom_license_filters();
103+
$this->ppom_test_license_want_valid = ( 'valid' === $status );
104+
$this->ppom_test_license_plan_value = max( 1, min( 3, (int) $plan ) );
105+
$priority = PHP_INT_MAX - 10;
106+
107+
add_filter( 'product_ppom_license_status', array( $this, 'ppom_test_filter_license_status' ), $priority, 1 );
108+
add_filter( 'product_ppom_license_plan', array( $this, 'ppom_test_filter_license_plan' ), $priority, 1 );
109+
$this->ppom_test_license_filters_active = true;
110+
}
111+
112+
/**
113+
* Remove license filters registered by with_ppom_license_filters().
114+
*
115+
* @return void
116+
*/
117+
protected function remove_ppom_license_filters() {
118+
if ( ! $this->ppom_test_license_filters_active ) {
119+
return;
120+
}
121+
$priority = PHP_INT_MAX - 10;
122+
remove_filter( 'product_ppom_license_status', array( $this, 'ppom_test_filter_license_status' ), $priority );
123+
remove_filter( 'product_ppom_license_plan', array( $this, 'ppom_test_filter_license_plan' ), $priority );
124+
$this->ppom_test_license_filters_active = false;
125+
}
126+
127+
/**
128+
* @param mixed $value Prior filter value.
129+
*
130+
* @return string
131+
*/
132+
public function ppom_test_filter_license_status( $value ) {
133+
return $this->ppom_test_license_want_valid ? 'valid' : '';
134+
}
135+
136+
/**
137+
* @param mixed $value Prior filter value.
138+
*
139+
* @return int
140+
*/
141+
public function ppom_test_filter_license_plan( $value ) {
142+
return $this->ppom_test_license_want_valid ? $this->ppom_test_license_plan_value : 0;
143+
}
144+
74145
/**
75146
* Create and persist a simple WooCommerce product.
76147
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* License filter helpers on PPOM_Test_Case.
4+
*
5+
* @package ppom-pro
6+
*/
7+
8+
require_once __DIR__ . '/class-ppom-test-case.php';
9+
10+
class Test_License_Fixture extends PPOM_Test_Case {
11+
12+
/**
13+
* @return void
14+
*/
15+
public function test_is_license_of_type_respects_fixture_filters() {
16+
$this->with_ppom_license_filters( 'invalid', 1 );
17+
$this->assertFalse( PPOM()->is_license_of_type( 'pro' ) );
18+
19+
$this->with_ppom_license_filters( 'valid', 1 );
20+
$this->assertTrue( PPOM()->is_license_of_type( 'pro' ) );
21+
$this->assertFalse( PPOM()->is_license_of_type( 'plus' ) );
22+
23+
$this->with_ppom_license_filters( 'valid', 2 );
24+
$this->assertTrue( PPOM()->is_license_of_type( 'plus' ) );
25+
}
26+
}

0 commit comments

Comments
 (0)