Skip to content

Commit 498fa04

Browse files
committed
test(cloud): cover entitlements normalization + token/nonce helpers
Add tests/CloudConnectTest.php to the dependency-free suite (php tests/run.php): entitlements normalization (fail-open to free, feature coercion, 12h staleness boundary, garbage-shape safety), is_hex nonce validation, connect-token sanitization + length cap, and plan labelling.
1 parent 0a79b50 commit 498fa04

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

tests/CloudConnectTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Tests for the pure, WordPress-free helpers in WebDecoy_Cloud_Connect:
7+
* entitlements normalization (fail-open + staleness), connect-token/nonce
8+
* validation, and plan labelling. These are the parts of the connect flow that
9+
* can be exercised without a WordPress runtime.
10+
*
11+
* Run: php tests/run.php
12+
*/
13+
14+
if (!defined('ABSPATH')) {
15+
define('ABSPATH', '/tmp/');
16+
}
17+
require_once dirname(__DIR__) . '/includes/class-webdecoy-cloud-connect.php';
18+
19+
$t = ['TestRunner', 'test'];
20+
$same = ['TestRunner', 'assertSame'];
21+
$true = ['TestRunner', 'assertTrue'];
22+
23+
echo "\nCloud Connect: entitlements normalization\n";
24+
25+
$t('empty payload fails open to free (all features false, stale)', function () use ($same, $true) {
26+
$e = WebDecoy_Cloud_Connect::normalize_entitlements([], 1000000);
27+
$same('free', $e['plan'], 'plan defaults to free');
28+
$same('', $e['channel'], 'channel defaults empty');
29+
$same(false, $e['digest']['enabled'], 'digest defaults off');
30+
$same(0, $e['fetched_at'], 'no timestamp -> 0');
31+
$true($e['stale'] === true, 'never-fetched is stale');
32+
foreach (['actor_feed', 'enrichment', 'alerts', 'edge_push', 'decoy_packs', 'woo_intel'] as $key) {
33+
$same(false, $e['features'][$key], "feature {$key} defaults false");
34+
}
35+
});
36+
37+
$t('a full payload is normalized to typed values', function () use ($same, $true) {
38+
$now = 2000000;
39+
$raw = [
40+
'plan' => 'free_connected',
41+
'channel' => 'wordpress',
42+
'features' => [
43+
'actor_feed' => true,
44+
'enrichment' => 1, // truthy non-bool -> true
45+
'alerts' => false,
46+
'edge_push' => 0, // falsy -> false
47+
// decoy_packs + woo_intel omitted -> default false
48+
],
49+
'digest' => ['enabled' => true],
50+
'fetched_at' => $now - 100, // fresh
51+
];
52+
$e = WebDecoy_Cloud_Connect::normalize_entitlements($raw, $now);
53+
$same('free_connected', $e['plan']);
54+
$same('wordpress', $e['channel']);
55+
$same(true, $e['features']['actor_feed']);
56+
$same(true, $e['features']['enrichment'], 'truthy coerced to bool true');
57+
$same(false, $e['features']['alerts']);
58+
$same(false, $e['features']['edge_push'], 'falsy coerced to bool false');
59+
$same(false, $e['features']['decoy_packs'], 'omitted feature is false');
60+
$same(false, $e['features']['woo_intel'], 'omitted feature is false');
61+
$same(true, $e['digest']['enabled']);
62+
$same($now - 100, $e['fetched_at']);
63+
$true($e['stale'] === false, 'recent fetch is not stale');
64+
});
65+
66+
$t('staleness flips at the 12h boundary', function () use ($true) {
67+
$now = 1000000;
68+
// 11h old -> fresh; 13h old -> stale (threshold is 12h = 43200s).
69+
$fresh = WebDecoy_Cloud_Connect::normalize_entitlements(['fetched_at' => $now - (11 * 3600)], $now);
70+
$stale = WebDecoy_Cloud_Connect::normalize_entitlements(['fetched_at' => $now - (13 * 3600)], $now);
71+
$true($fresh['stale'] === false, '11h old is fresh');
72+
$true($stale['stale'] === true, '13h old is stale');
73+
});
74+
75+
$t('garbage feature/digest shapes never fatal, always typed', function () use ($same) {
76+
$e = WebDecoy_Cloud_Connect::normalize_entitlements([
77+
'plan' => 123, // non-string -> default free
78+
'features' => 'nope', // non-array -> all false
79+
'digest' => 'nope', // non-array -> off
80+
], 500);
81+
$same('free', $e['plan'], 'non-string plan falls back to free');
82+
$same(false, $e['features']['actor_feed']);
83+
$same(false, $e['digest']['enabled']);
84+
});
85+
86+
echo "\nCloud Connect: token & nonce validation\n";
87+
88+
$t('is_hex validates a 64-char connect nonce', function () use ($true) {
89+
$good = str_repeat('a1b2', 16); // 64 hex chars
90+
$true(WebDecoy_Cloud_Connect::is_hex($good, 64) === true, '64 hex chars pass');
91+
$true(WebDecoy_Cloud_Connect::is_hex($good, 32) === false, 'wrong length fails');
92+
$true(WebDecoy_Cloud_Connect::is_hex('zzzz', 4) === false, 'non-hex fails');
93+
$true(WebDecoy_Cloud_Connect::is_hex('', 64) === false, 'empty fails');
94+
$true(WebDecoy_Cloud_Connect::is_hex('deadbeef') === true, 'any-length hex passes with no length arg');
95+
});
96+
97+
$t('sanitize_connect_token strips unsafe chars and caps length', function () use ($same, $true) {
98+
$same('abcXYZ-9._', WebDecoy_Cloud_Connect::sanitize_connect_token('abcXYZ-9._'), 'url-safe token preserved');
99+
$same('abcscriptdef', WebDecoy_Cloud_Connect::sanitize_connect_token('abc<script>def'), 'angle brackets stripped, alnum kept');
100+
$same('a1b2c3', WebDecoy_Cloud_Connect::sanitize_connect_token("a1b2\nc3 "), 'newlines and spaces stripped');
101+
$same('', WebDecoy_Cloud_Connect::sanitize_connect_token(' '), 'whitespace-only -> empty');
102+
$long = str_repeat('a', 500);
103+
$true(strlen(WebDecoy_Cloud_Connect::sanitize_connect_token($long)) === 256, 'capped at 256');
104+
});
105+
106+
echo "\nCloud Connect: plan labels\n";
107+
108+
$t('plan_label humanizes slugs', function () use ($same) {
109+
$same('Free Connected', WebDecoy_Cloud_Connect::plan_label('free_connected'));
110+
$same('Pro', WebDecoy_Cloud_Connect::plan_label('pro'));
111+
$same('Team Annual', WebDecoy_Cloud_Connect::plan_label('team_annual'));
112+
$same('Connected', WebDecoy_Cloud_Connect::plan_label(''), 'empty slug -> generic label');
113+
});

0 commit comments

Comments
 (0)