Add visitor-IP header setting, checkout-button toggles, and configurable timer maximum
Test and Release / test (push) Successful in 1m13s
Test and Release / release (push) Successful in 5s

- Core\ClientIp: trusted-header resolution for rate limiting behind Cloudflare/proxies
- Settings: show/hide Pay Later and Debit/Credit Card (Pay Later off by default)
- add_time rule: optional max_timer_seconds (uncapped by default), editable in rule form

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 11:40:06 -08:00
1 parent 4f92cfdade
commit e4d9042aba
14 files changed
+278 -16

No files matched your search

+17 -2
View File
@@ -52,7 +52,7 @@ final class AdminLogicTest extends TestCase {
$r = RuleConfig::sanitize( 'add_time', array( 'time_amount' => '1', 'time_unit' => 'minutes', 'unit_amount' => '1.00', 'cap_minutes' => '10', 'max_total_minutes' => '' ), 'USD' );
$this->assertSame( array(), $r['errors'] );
$this->assertSame(
array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'cap_seconds_per_donation' => 600, 'max_total_seconds' => null, 'basis' => 'base' ),
array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'cap_seconds_per_donation' => 600, 'max_total_seconds' => null, 'max_timer_seconds' => null, 'basis' => 'base' ),
$r['config']
);
}
@@ -92,8 +92,23 @@ final class AdminLogicTest extends TestCase {
$this->assertSame( array( 'Timer: +25 minutes', 'Pies: +2', 'Bonus "big" does not trigger' ), RuleConfig::preview( $rules, 2500, 'USD' ) );
}
public function test_max_timer_validation_and_summary(): void {
$base = array( 'time_amount' => '1', 'time_unit' => 'minutes', 'unit_amount' => '1.00' );
$this->assertNull( RuleConfig::sanitize( 'add_time', $base, 'USD' )['config']['max_timer_seconds'] );
$this->assertNull( RuleConfig::sanitize( 'add_time', $base + array( 'max_timer_minutes' => ' ' ), 'USD' )['config']['max_timer_seconds'] );
$this->assertSame( 86400, RuleConfig::sanitize( 'add_time', $base + array( 'max_timer_minutes' => '1440' ), 'USD' )['config']['max_timer_seconds'] );
foreach ( array( '0', '-5', 'abc', '1.5' ) as $bad ) {
$r = RuleConfig::sanitize( 'add_time', $base + array( 'max_timer_minutes' => $bad ), 'USD' );
$this->assertNotEmpty( $r['errors'], $bad );
$this->assertNull( $r['config'] );
}
$this->assertSame( 'Adds 1 minute for every $1.00, up to 1 day on the clock', RuleConfig::summary( 'add_time', array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'max_timer_seconds' => 86400 ), 'USD' ) );
$this->assertSame( 'Adds 1 minute for every $1.00', RuleConfig::summary( 'add_time', array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ), 'USD' ) );
$this->assertSame( array( 'Timer: +25 minutes (the timer never goes above 1 hour)' ), RuleConfig::preview( array( array( 'type' => 'add_time', 'config' => array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'max_timer_seconds' => 3600 ) ) ), 2500, 'USD' ) );
}
public function test_form_roundtrip(): void {
$cfg = array( 'seconds_per_unit' => 120, 'unit_minor' => 250, 'cap_seconds_per_donation' => 600, 'max_total_seconds' => null, 'basis' => 'base' );
$cfg = array( 'seconds_per_unit' => 120, 'unit_minor' => 250, 'cap_seconds_per_donation' => 600, 'max_total_seconds' => null, 'max_timer_seconds' => 86400, 'basis' => 'base' );
$form = RuleConfig::toForm( 'add_time', $cfg, 'USD' );
$back = RuleConfig::sanitize( 'add_time', $form, 'USD' );
$this->assertSame( $cfg, $back['config'] );
+59
View File
@@ -0,0 +1,59 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Core\ClientIp;
final class ClientIpTest extends TestCase {
private const PROXY = '172.70.1.1';
private const CLIENT = '203.0.113.9';
public function test_direct_mode_uses_remote_addr_and_ignores_headers(): void {
$srv = array( 'REMOTE_ADDR' => self::CLIENT, 'HTTP_CF_CONNECTING_IP' => '1.2.3.4', 'HTTP_X_FORWARDED_FOR' => '5.6.7.8' );
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, ClientIp::MODE_DIRECT ) );
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv ) );
}
public function test_cloudflare_mode_uses_cf_header(): void {
$srv = array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_CF_CONNECTING_IP' => self::CLIENT );
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, ClientIp::MODE_CLOUDFLARE ) );
}
public function test_cloudflare_mode_without_header_falls_back_to_remote_addr(): void {
$this->assertSame( self::PROXY, ClientIp::resolve( array( 'REMOTE_ADDR' => self::PROXY ), ClientIp::MODE_CLOUDFLARE ) );
}
public function test_invalid_header_values_are_ignored(): void {
foreach ( array( 'not-an-ip', '999.1.1.1', '', '1.2.3.4, 5.6.7.8', '<script>', "1.2.3.4\r\nX: y" ) as $bad ) {
$srv = array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_CF_CONNECTING_IP' => $bad );
$this->assertSame( self::PROXY, ClientIp::resolve( $srv, ClientIp::MODE_CLOUDFLARE ), "accepted bad value: $bad" );
}
}
public function test_x_real_ip_mode(): void {
$this->assertSame( self::CLIENT, ClientIp::resolve( array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_X_REAL_IP' => self::CLIENT ), ClientIp::MODE_REAL_IP ) );
}
public function test_forwarded_mode_takes_the_last_entry_not_the_spoofable_first(): void {
$srv = array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_X_FORWARDED_FOR' => '6.6.6.6, ' . self::CLIENT );
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, ClientIp::MODE_FORWARDED ) );
$this->assertSame( self::CLIENT, ClientIp::resolve( array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_X_FORWARDED_FOR' => self::CLIENT ), ClientIp::MODE_FORWARDED ) );
}
public function test_ipv6_is_accepted(): void {
$this->assertSame( '2001:db8::1', ClientIp::resolve( array( 'REMOTE_ADDR' => self::PROXY, 'HTTP_CF_CONNECTING_IP' => '2001:db8::1' ), ClientIp::MODE_CLOUDFLARE ) );
}
public function test_unknown_when_nothing_valid(): void {
$this->assertSame( 'unknown', ClientIp::resolve( array(), ClientIp::MODE_CLOUDFLARE ) );
$this->assertSame( 'unknown', ClientIp::resolve( array( 'REMOTE_ADDR' => 'garbage' ) ) );
}
public function test_unrecognised_mode_behaves_like_direct(): void {
$srv = array( 'REMOTE_ADDR' => self::CLIENT, 'HTTP_CF_CONNECTING_IP' => '1.2.3.4' );
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, 'something_else' ) );
}
}
+82
View File
@@ -81,6 +81,88 @@ final class RulesEngineTest extends TestCase {
$this->assertSame( 1000 + 100, $this->state->getInt( 1, 'timer_ends_at' ) );
}
private function ends(): int {
return $this->state->getInt( 1, 'timer_ends_at' );
}
public function test_max_timer_clamps_and_records_actual_delta(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1200 ) ) ) ); // now=1000
$this->state->setInt( 1, 'timer_ends_at', 2000 ); // 1000s remaining
$e->applyDonation( $this->d( 1, 1000 ) ); // wants +600, room 200
$this->assertSame( 2200, $this->ends() );
$this->assertSame( 200, $this->events->events['1:1'][0] );
$this->assertSame( 200, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_exactly_at_cap_adds_zero(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1000 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 2000 );
$e->applyDonation( $this->d( 1, 100 ) );
$this->assertSame( 2000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_lowered_below_existing_timer_does_not_shorten(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 100 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 9000 );
$e->applyDonation( $this->d( 1, 100 ) );
$this->assertSame( 9000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_uncapped_by_default_and_null(): void {
foreach ( array( array(), array( 'max_timer_seconds' => null ) ) as $extra ) {
$e = $this->engine( array( $this->timer( $extra ) ) );
$this->state->setInt( 1, 'timer_ends_at', 10000000 );
$e->applyDonation( $this->d( 1, 100000 ) );
$this->assertSame( 10000000 + 60000, $this->ends() );
}
}
public function test_max_timer_with_other_caps_most_restrictive_wins(): void {
// per-donation 50 is the tightest
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 500, 'cap_seconds_per_donation' => 50, 'max_total_seconds' => 400 ) ) ) );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1050, $this->ends() );
// max_total tightest: 30 left of 40 total, clock has room
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 500, 'max_total_seconds' => 40 ) ) ) );
$this->state->setInt( 1, 'timer_added:1', 10 );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1030, $this->ends() );
$this->assertSame( 40, $this->state->getInt( 1, 'timer_added:1' ) );
// clock cap tightest: only 20 of room
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 120, 'cap_seconds_per_donation' => 300, 'max_total_seconds' => 900 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 1100 );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1120, $this->ends() );
$this->assertSame( 20, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_with_expired_timer_starts_from_now(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 100 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 10 );
$e->applyDonation( $this->d( 1, 1000 ) ); // wants 600
$this->assertSame( 1100, $this->ends() );
}
public function test_max_timer_replay_idempotent_and_reversal_uses_clamped_delta(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1200 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 2000 );
$d = $this->d( 1, 1000 );
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( 2200, $this->ends() );
$e->reverseDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 2000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_invalid_value_rejected_by_rule(): void {
$this->expectException( \InvalidArgumentException::class );
\StreamGoals\Rules\AddTimeRule::fromConfig( array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'max_timer_seconds' => 0 ) );
}
public function test_reversal_undoes_and_is_not_double_applied(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );