Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
- Tiers\TierPolicy (pure), TierRepository (atomic reached marking), Tiers\Recorder hooked before close-at-target - Admin tiers screen; goal target derived from the final tier - Overlay goal widget shows Tier N of M and fills within the tier; alert pill; donate form 'Next unlock' - Installer DB_VERSION 4 (sg_goal_tiers) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39840a20d7
commit
4c379a0245
20 files changed
+1089
-14
No files matched your search
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Overlay\OverlayDataSource;
|
||||
use StreamGoals\Overlay\OverlayService;
|
||||
use StreamGoals\Overlay\Payloads;
|
||||
use StreamGoals\Overlay\RecentDonationsSource;
|
||||
use StreamGoals\Overlay\TiersSource;
|
||||
|
||||
final class OverlayTiersTest extends TestCase {
|
||||
|
||||
private function tiers( array $reached = array() ): array {
|
||||
$rows = array( array( 1, 10000, 'Giveaway' ), array( 2, 25000, 'Microphone' ), array( 3, 50000, '24h stream' ) );
|
||||
return array_map( static fn( $r ) => array( 'id' => $r[0], 'target_minor' => $r[1], 'title' => $r[2], 'description' => 'd' . $r[0], 'reached_at' => in_array( $r[0], $reached, true ) ? '2026-01-01 00:00:00' : null ), $rows );
|
||||
}
|
||||
|
||||
private function svc( array $tiers, int $raised, array $db = array(), array $unlocked = array(), ?object &$probe = null ): OverlayService {
|
||||
$probe = new class( $tiers, $raised, $db, $unlocked ) implements OverlayDataSource, RecentDonationsSource, TiersSource {
|
||||
public int $reachedQueries = 0;
|
||||
public function __construct( private array $tiers, private int $raised, private array $db, private array $unlocked ) {}
|
||||
public function activeGoal(): ?array { return array( 'id' => 7, 'title' => 'Stream', 'currency' => 'USD', 'target_minor' => 50000 ); }
|
||||
public function raisedMinor( array $goal ): int { return $this->raised; }
|
||||
public function stateValue( int $goalId, string $key ): ?string { return null; }
|
||||
public function counter( int $goalId ): array { return array( 'label' => '', 'value' => 0 ); }
|
||||
public function bonusesFor( array $ids ): array { return array(); }
|
||||
public function donations( ?int $sinceId, int $limit ): array { return $this->db; }
|
||||
public function recentDonations( int $limit ): array { return $this->db; }
|
||||
public function tiersFor( int $goalId ): array { return $this->tiers; }
|
||||
public function tiersReachedFor( array $ids ): array { ++$this->reachedQueries; return $this->unlocked; }
|
||||
};
|
||||
return new OverlayService( $probe );
|
||||
}
|
||||
|
||||
private function row( int $id ): array {
|
||||
return array( 'id' => $id, 'base_minor' => 2500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' );
|
||||
}
|
||||
|
||||
public function test_untiered_goal_payload_is_unchanged(): void {
|
||||
$g = Payloads::goal( array( 'title' => 'G', 'currency' => 'USD', 'target_minor' => 20000 ), 5000 );
|
||||
$this->assertArrayNotHasKey( 'tiers', $g );
|
||||
$this->assertSame( 25, $g['percent'] );
|
||||
$this->assertSame( $g, Payloads::goal( array( 'title' => 'G', 'currency' => 'USD', 'target_minor' => 20000 ), 5000, array() ) );
|
||||
$this->assertArrayNotHasKey( 'tiers', $this->svc( array(), 100 )->payload( 'goal', 1 ) );
|
||||
}
|
||||
|
||||
public function test_tiered_goal_percent_is_within_tier_and_displays(): void {
|
||||
$g = $this->svc( $this->tiers( array( 1 ) ), 17500 )->payload( 'goal', 1 );
|
||||
$this->assertSame( 50, $g['percent'] );
|
||||
$this->assertSame( 17500, $g['raised_minor'] );
|
||||
$this->assertSame( '$175.00', $g['raised_display'] );
|
||||
$this->assertSame( 25000, $g['target_minor'] );
|
||||
$this->assertSame( '$250.00', $g['target_display'] );
|
||||
$t = $g['tiers'];
|
||||
$this->assertSame( 3, $t['count'] );
|
||||
$this->assertSame( 2, $t['index'] );
|
||||
$this->assertFalse( $t['all_reached'] );
|
||||
$this->assertSame( array( 'title' => 'Microphone', 'description' => 'd2', 'target_minor' => 25000, 'target_display' => '$250.00', 'floor_minor' => 10000 ), $t['current'] );
|
||||
}
|
||||
|
||||
public function test_tiered_goal_below_first_tier_and_all_reached(): void {
|
||||
$g = $this->svc( $this->tiers(), 2500 )->payload( 'goal', 1 );
|
||||
$this->assertSame( 25, $g['percent'] );
|
||||
$this->assertSame( 1, $g['tiers']['index'] );
|
||||
$this->assertSame( 0, $g['tiers']['current']['floor_minor'] );
|
||||
$all = $this->svc( $this->tiers( array( 1, 2, 3 ) ), 60000 )->payload( 'goal', 1 );
|
||||
$this->assertSame( 100, $all['percent'] );
|
||||
$this->assertTrue( $all['tiers']['all_reached'] );
|
||||
$this->assertNull( $all['tiers']['current'] );
|
||||
$this->assertSame( 3, $all['tiers']['index'] );
|
||||
$this->assertSame( '$500.00', $all['target_display'] );
|
||||
$this->assertSame( '$600.00', $all['raised_display'] );
|
||||
}
|
||||
|
||||
public function test_tier_text_is_sanitized(): void {
|
||||
$t = $this->tiers();
|
||||
$t[0]['title'] = '<script>x</script>Hi' . str_repeat( 'a', 300 );
|
||||
$g = $this->svc( $t, 0 )->payload( 'goal', 1 );
|
||||
$this->assertStringNotContainsString( '<', $g['tiers']['current']['title'] );
|
||||
$this->assertLessThanOrEqual( 120, mb_strlen( $g['tiers']['current']['title'] ) );
|
||||
}
|
||||
|
||||
public function test_alert_tiers_in_since_and_recent_with_one_query(): void {
|
||||
$probe = null;
|
||||
$svc = $this->svc( array(), 0, array( $this->row( 3 ), $this->row( 4 ) ), array( 3 => array( array( 'title' => 'Giveaway' ), array( 'title' => 'Microphone' ) ) ), $probe );
|
||||
$since = $svc->payload( 'alerts', 1, 0 );
|
||||
$this->assertSame( 1, $probe->reachedQueries );
|
||||
$recent = $svc->payload( 'alerts', 1, null, true );
|
||||
$this->assertSame( 2, $probe->reachedQueries );
|
||||
$this->assertSame( $since['alerts'], $recent['alerts'] );
|
||||
$this->assertSame( array( array( 'title' => 'Giveaway' ), array( 'title' => 'Microphone' ) ), $recent['alerts'][0]['tiers'] );
|
||||
$this->assertSame( array(), $recent['alerts'][1]['tiers'] );
|
||||
$svc->payload( 'alerts', 1, null ); // baseline poll: no lookup
|
||||
$this->assertSame( 2, $probe->reachedQueries );
|
||||
}
|
||||
|
||||
public function test_alert_tier_events_sanitized_and_capped(): void {
|
||||
$ev = array( array( 'title' => '<b>Hi</b>' ), array( 'title' => '' ), 'junk', array( 'title' => str_repeat( 'é', 200 ) ) );
|
||||
for ( $i = 0; $i < 9; $i++ ) {
|
||||
$ev[] = array( 'title' => "T$i" );
|
||||
}
|
||||
$out = Payloads::tierEvents( $ev );
|
||||
$this->assertCount( 5, $out );
|
||||
$this->assertSame( array( 'title' => 'Hi' ), $out[0] );
|
||||
$this->assertSame( 80, mb_strlen( $out[1]['title'] ) );
|
||||
$this->assertSame( array(), Payloads::tierEvents( 'nope' ) );
|
||||
}
|
||||
|
||||
public function test_etag_changes_when_a_tier_unlocks_or_current_tier_moves(): void {
|
||||
$a = Payloads::etag( $this->svc( array(), 0, array( $this->row( 3 ) ) )->payload( 'alerts', 1, null, true ) );
|
||||
$b = Payloads::etag( $this->svc( array(), 0, array( $this->row( 3 ) ), array( 3 => array( array( 'title' => 'X' ) ) ) )->payload( 'alerts', 1, null, true ) );
|
||||
$this->assertNotSame( $a, $b );
|
||||
$g1 = Payloads::etag( $this->svc( $this->tiers(), 9000 )->payload( 'goal', 1 ) );
|
||||
$g2 = Payloads::etag( $this->svc( $this->tiers( array( 1 ) ), 10000 )->payload( 'goal', 1 ) );
|
||||
$this->assertNotSame( $g1, $g2 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Tiers\TierPolicy;
|
||||
|
||||
final class TierPolicyTest extends TestCase {
|
||||
|
||||
private function tiers( array $reachedIds = array() ): array {
|
||||
$rows = array( array( 1, 10000, 'Giveaway' ), array( 2, 25000, 'Microphone' ), array( 3, 50000, '24h stream' ) );
|
||||
$out = array();
|
||||
foreach ( $rows as [ $id, $t, $title ] ) {
|
||||
$out[] = array( 'id' => $id, 'target_minor' => $t, 'title' => $title, 'description' => '', 'reached_at' => in_array( $id, $reachedIds, true ) ? '2026-01-01 00:00:00' : null );
|
||||
}
|
||||
return array_reverse( $out ); // unsorted on purpose
|
||||
}
|
||||
|
||||
public function test_current_tier_and_index(): void {
|
||||
$this->assertSame( 1, TierPolicy::currentTier( $this->tiers() )['id'] );
|
||||
$this->assertSame( 1, TierPolicy::currentIndex( $this->tiers() ) );
|
||||
$this->assertSame( 3, TierPolicy::currentTier( $this->tiers( array( 1, 2 ) ) )['id'] );
|
||||
$this->assertSame( 3, TierPolicy::currentIndex( $this->tiers( array( 1, 2 ) ) ) );
|
||||
$this->assertNull( TierPolicy::currentTier( $this->tiers( array( 1, 2, 3 ) ) ) );
|
||||
$this->assertSame( 3, TierPolicy::currentIndex( $this->tiers( array( 1, 2, 3 ) ) ) );
|
||||
$this->assertTrue( TierPolicy::allReached( $this->tiers( array( 1, 2, 3 ) ) ) );
|
||||
$this->assertFalse( TierPolicy::allReached( $this->tiers( array( 1 ) ) ) );
|
||||
$this->assertFalse( TierPolicy::allReached( array() ) );
|
||||
$this->assertSame( 0, TierPolicy::currentIndex( array() ) );
|
||||
}
|
||||
|
||||
public function test_floor(): void {
|
||||
$this->assertSame( 0, TierPolicy::floorMinor( $this->tiers() ) );
|
||||
$this->assertSame( 10000, TierPolicy::floorMinor( $this->tiers( array( 1 ) ) ) );
|
||||
$this->assertSame( 25000, TierPolicy::floorMinor( $this->tiers( array( 1, 2 ) ) ) );
|
||||
$this->assertSame( 50000, TierPolicy::floorMinor( $this->tiers( array( 1, 2, 3 ) ) ), 'all reached: top target' );
|
||||
}
|
||||
|
||||
public function test_percent_within_tier_clamped(): void {
|
||||
$this->assertSame( 0, TierPolicy::percent( 0, 0, 10000 ) );
|
||||
$this->assertSame( 50, TierPolicy::percent( 5000, 0, 10000 ) );
|
||||
$this->assertSame( 50, TierPolicy::percent( 17500, 10000, 25000 ) );
|
||||
$this->assertSame( 0, TierPolicy::percent( 10000, 10000, 25000 ), 'exactly at the floor' );
|
||||
$this->assertSame( 0, TierPolicy::percent( 500, 10000, 25000 ), 'raised below the floor clamps to 0' );
|
||||
$this->assertSame( 100, TierPolicy::percent( 25000, 10000, 25000 ), 'exactly at the target' );
|
||||
$this->assertSame( 100, TierPolicy::percent( 99999, 10000, 25000 ), 'above clamps to 100' );
|
||||
$this->assertSame( 0, TierPolicy::percent( 5, 100, 100 ), 'degenerate range, not reached' );
|
||||
$this->assertSame( 100, TierPolicy::percent( 100, 100, 100 ), 'degenerate range, reached' );
|
||||
}
|
||||
|
||||
public function test_percent_for_walks_the_ladder(): void {
|
||||
$this->assertSame( 25, TierPolicy::percentFor( $this->tiers(), 2500 ) );
|
||||
$this->assertSame( 0, TierPolicy::percentFor( $this->tiers( array( 1 ) ), 10000 ) );
|
||||
$this->assertSame( 50, TierPolicy::percentFor( $this->tiers( array( 1 ) ), 17500 ) );
|
||||
$this->assertSame( 100, TierPolicy::percentFor( $this->tiers( array( 1, 2, 3 ) ), 60000 ) );
|
||||
$this->assertSame( 0, TierPolicy::percentFor( array(), 500 ) );
|
||||
$this->assertSame( 7500, TierPolicy::remainingMinor( $this->tiers(), 2500 ) );
|
||||
$this->assertSame( 0, TierPolicy::remainingMinor( $this->tiers( array( 1, 2, 3 ) ), 60000 ) );
|
||||
}
|
||||
|
||||
public function test_newly_reached_several_in_one_donation_in_order(): void {
|
||||
$ids = array_column( TierPolicy::newlyReached( $this->tiers(), 30000 ), 'id' );
|
||||
$this->assertSame( array( 1, 2 ), $ids );
|
||||
$this->assertSame( array( 1, 2, 3 ), array_column( TierPolicy::newlyReached( $this->tiers(), 50000 ), 'id' ), 'exactly at the last target counts' );
|
||||
$this->assertSame( array(), TierPolicy::newlyReached( $this->tiers(), 9999 ) );
|
||||
$this->assertSame( array( 2 ), array_column( TierPolicy::newlyReached( $this->tiers( array( 1 ) ), 30000 ), 'id' ), 'reached ones are skipped' );
|
||||
}
|
||||
|
||||
public function test_replay_is_idempotent_at_policy_level(): void {
|
||||
$tiers = $this->tiers();
|
||||
$first = TierPolicy::newlyReached( $tiers, 30000 );
|
||||
foreach ( $tiers as &$t ) {
|
||||
if ( in_array( $t['id'], array_column( $first, 'id' ), true ) ) {
|
||||
$t['reached_at'] = '2026-02-02 00:00:00';
|
||||
}
|
||||
}
|
||||
unset( $t );
|
||||
$this->assertSame( array(), TierPolicy::newlyReached( $tiers, 30000 ) );
|
||||
$this->assertSame( array(), TierPolicy::newlyReached( $tiers, 12000 ), 'a lower total (refund) never un-reaches or re-reaches' );
|
||||
}
|
||||
|
||||
public function test_derived_target_is_highest(): void {
|
||||
$this->assertSame( 50000, TierPolicy::derivedGoalTarget( $this->tiers() ) );
|
||||
$this->assertSame( 0, TierPolicy::derivedGoalTarget( array() ) );
|
||||
}
|
||||
|
||||
public function test_status(): void {
|
||||
$t = $this->tiers( array( 1 ) );
|
||||
$by = array();
|
||||
foreach ( $t as $row ) {
|
||||
$by[ $row['id'] ] = TierPolicy::status( $t, $row );
|
||||
}
|
||||
$this->assertSame( array( 3 => 'upcoming', 2 => 'current', 1 => 'reached' ), $by );
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ validation */
|
||||
|
||||
private function v( ?int $target, string $title = 'Mic', string $desc = '', ?int $edit = null, int $raised = 0, ?array $tiers = null ): array {
|
||||
return TierPolicy::validate( $tiers ?? $this->tiers( array( 1 ) ), $edit, $target, $title, $desc, $raised, 100, '$1.00' );
|
||||
}
|
||||
|
||||
public function test_valid_new_tier(): void {
|
||||
$r = $this->v( 70000, ' New desk ', ' nice ', null, 12000 );
|
||||
$this->assertNull( $r['error'] );
|
||||
$this->assertSame( array( 'target_minor' => 70000, 'title' => 'New desk', 'description' => 'nice' ), $r['data'] );
|
||||
}
|
||||
|
||||
public function test_rejects_bad_values(): void {
|
||||
$this->assertNotNull( $this->v( null )['error'] );
|
||||
$this->assertNotNull( $this->v( 0 )['error'] );
|
||||
$this->assertStringContainsString( 'minimum', (string) $this->v( 50 )['error'] );
|
||||
$this->assertStringContainsString( 'title', (string) $this->v( 70000, ' ' )['error'] . ' title' );
|
||||
$this->assertNotNull( $this->v( 70000, ' ' )['error'] );
|
||||
$this->assertNotNull( $this->v( 70000, str_repeat( 'a', 121 ) )['error'] );
|
||||
$this->assertNull( $this->v( 70000, str_repeat( 'a', 120 ) )['error'] );
|
||||
$this->assertNotNull( $this->v( 70000, 'x', str_repeat( 'd', 256 ) )['error'] );
|
||||
$this->assertNull( $this->v( 70000, 'x', str_repeat( 'd', 255 ) )['error'] );
|
||||
}
|
||||
|
||||
public function test_target_must_be_unique_and_above_raised(): void {
|
||||
$this->assertStringContainsString( 'already', (string) $this->v( 25000 )['error'] );
|
||||
$this->assertStringContainsString( 'already raised', (string) $this->v( 20000, 'x', '', null, 20000 )['error'], 'equal to raised unlocks instantly' );
|
||||
$this->assertNull( $this->v( 20001, 'x', '', null, 20000 )['error'] );
|
||||
}
|
||||
|
||||
public function test_reached_tier_target_is_locked_but_text_editable(): void {
|
||||
$r = $this->v( 999999, 'Renamed', 'd', 1, 12000 );
|
||||
$this->assertNull( $r['error'] );
|
||||
$this->assertSame( 10000, $r['data']['target_minor'] );
|
||||
$this->assertSame( 'Renamed', $r['data']['title'] );
|
||||
$r2 = $this->v( null, 'Renamed', '', 1, 12000 );
|
||||
$this->assertSame( 10000, $r2['data']['target_minor'] );
|
||||
}
|
||||
|
||||
public function test_edit_unreached_keeps_or_moves_target(): void {
|
||||
$this->assertNull( $this->v( 25000, 'Mic2', '', 2, 12000 )['error'], 'unchanged target is fine' );
|
||||
$this->assertNull( $this->v( 30000, 'Mic', '', 2, 12000 )['error'] );
|
||||
$this->assertStringContainsString( 'already', (string) $this->v( 50000, 'Mic', '', 2, 12000 )['error'] );
|
||||
$this->assertStringContainsString( 'no longer', (string) $this->v( 30000, 'Mic', '', 99, 12000 )['error'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Tiers\TierRepository;
|
||||
|
||||
final class TierRepositoryTest extends TestCase {
|
||||
|
||||
private function fake( array $rows = array(), $var = null ): object {
|
||||
if ( ! defined( 'ARRAY_A' ) ) {
|
||||
define( 'ARRAY_A', 'ARRAY_A' );
|
||||
}
|
||||
return new class( $rows, $var ) {
|
||||
public string $prefix = 'wp_';
|
||||
public array $sql = array();
|
||||
public int $insert_id = 9;
|
||||
public array $queryResults = array();
|
||||
public function __construct( public array $rows, public $var ) {}
|
||||
public function prepare( string $q, ...$args ): string {
|
||||
$i = 0;
|
||||
return (string) preg_replace_callback( '/%[dsf]/', static function ( $m ) use ( &$i, $args ) {
|
||||
$v = $args[ $i++ ];
|
||||
return '%s' === $m[0] ? "'" . $v . "'" : (string) $v;
|
||||
}, $q );
|
||||
}
|
||||
public function query( string $q ) { $this->sql[] = $q; return array_shift( $this->queryResults ) ?? 1; }
|
||||
public function get_results( string $q, $o = null ): array { $this->sql[] = $q; return $this->rows; }
|
||||
public function get_var( string $q ) { $this->sql[] = $q; return $this->var; }
|
||||
public function insert( string $t, array $d, array $f ) { $this->sql[] = 'INSERT ' . $t . ' ' . json_encode( $d ); return 1; }
|
||||
};
|
||||
}
|
||||
|
||||
private function row( int $id, int $target, ?string $reached = null ): array {
|
||||
return array( 'id' => (string) $id, 'goal_id' => '7', 'target_minor' => (string) $target, 'title' => "T$id", 'description' => '', 'reached_at' => $reached, 'reached_donation_id' => null, 'created_at' => '2026-01-01 00:00:00' );
|
||||
}
|
||||
|
||||
public function test_for_goal_orders_by_target_and_casts(): void {
|
||||
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
||||
$rows = ( new TierRepository( $db ) )->forGoal( 7 );
|
||||
$this->assertStringContainsString( 'WHERE goal_id = 7 ORDER BY target_minor ASC', $db->sql[0] );
|
||||
$this->assertSame( 10000, $rows[0]['target_minor'] );
|
||||
$this->assertNull( $rows[0]['reached_at'] );
|
||||
}
|
||||
|
||||
public function test_mark_reached_is_atomic_per_tier_and_only_for_reachable(): void {
|
||||
$db = $this->fake( array( $this->row( 1, 10000 ), $this->row( 2, 25000 ), $this->row( 3, 50000 ), $this->row( 4, 5000, '2026-01-01 00:00:00' ) ) );
|
||||
$marked = ( new TierRepository( $db ) )->markReached( 7, 30000, 55 );
|
||||
$this->assertSame( array( 1, 2 ), $marked );
|
||||
$updates = array_values( array_filter( $db->sql, static fn( $q ) => str_starts_with( $q, 'UPDATE' ) ) );
|
||||
$this->assertCount( 2, $updates );
|
||||
foreach ( $updates as $q ) {
|
||||
$this->assertStringContainsString( 'reached_at IS NULL', $q );
|
||||
$this->assertStringContainsString( 'reached_donation_id = 55', $q );
|
||||
$this->assertStringContainsString( 'UPDATE wp_sg_goal_tiers', $q );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_mark_reached_replay_loses_race_and_reports_nothing(): void {
|
||||
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
||||
$db->queryResults = array( 0 ); // someone else already marked it
|
||||
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 7, 30000, 55 ) );
|
||||
}
|
||||
|
||||
public function test_mark_reached_ignores_invalid_ids(): void {
|
||||
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
||||
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 0, 30000, 55 ) );
|
||||
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 7, 30000, 0 ) );
|
||||
$this->assertSame( array(), $db->sql );
|
||||
}
|
||||
|
||||
public function test_reached_by_donation_is_one_query_and_grouped(): void {
|
||||
$db = $this->fake( array( array( 'reached_donation_id' => '5', 'title' => 'A' ), array( 'reached_donation_id' => '5', 'title' => 'B' ), array( 'reached_donation_id' => '6', 'title' => 'C' ) ) );
|
||||
$out = ( new TierRepository( $db ) )->reachedByDonation( array( 5, 6, 6, 0, -1 ) );
|
||||
$this->assertCount( 1, $db->sql );
|
||||
$this->assertStringContainsString( 'reached_donation_id IN (5,6)', $db->sql[0] );
|
||||
$this->assertSame( array( 5 => array( array( 'title' => 'A' ), array( 'title' => 'B' ) ), 6 => array( array( 'title' => 'C' ) ) ), $out );
|
||||
$this->assertSame( array(), ( new TierRepository( $this->fake() ) )->reachedByDonation( array() ) );
|
||||
}
|
||||
|
||||
public function test_sync_goal_target_sets_highest_and_skips_untiered(): void {
|
||||
$db = $this->fake( array(), '50000' );
|
||||
$this->assertTrue( ( new TierRepository( $db ) )->syncGoalTarget( 7 ) );
|
||||
$this->assertStringContainsString( 'MAX(target_minor)', $db->sql[0] );
|
||||
$this->assertStringContainsString( 'UPDATE wp_sg_goals SET target_minor = 50000 WHERE id = 7', $db->sql[1] );
|
||||
$none = $this->fake( array(), null );
|
||||
$this->assertFalse( ( new TierRepository( $none ) )->syncGoalTarget( 7 ) );
|
||||
$this->assertCount( 1, $none->sql, 'no UPDATE when the goal has no tiers' );
|
||||
}
|
||||
|
||||
public function test_installer_defines_tier_table_unique_key_and_version(): void {
|
||||
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Core/Installer.php' );
|
||||
$this->assertSame( 1, preg_match( "/DB_VERSION = '(\\d+)'/", $src, $m ) );
|
||||
$this->assertGreaterThanOrEqual( 4, (int) $m[1] );
|
||||
$this->assertStringContainsString( 'CREATE TABLE {$p}goal_tiers (', $src );
|
||||
$this->assertStringContainsString( 'UNIQUE KEY goal_target (goal_id,target_minor)', $src );
|
||||
$this->assertStringContainsString( 'reached_donation_id BIGINT UNSIGNED NULL', $src );
|
||||
}
|
||||
|
||||
public function test_donation_store_marks_tiers_before_close_at_target(): void {
|
||||
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/PayPal/DonationStore.php' );
|
||||
$start = strpos( $src, 'function notify_applied' );
|
||||
$body = substr( $src, $start, strpos( $src, 'function close_goal_if_target_reached' ) - $start );
|
||||
$a = strpos( $body, 'Recorder::forDonation' );
|
||||
$tier = strpos( $body, 'Tiers\\Recorder::forDonation' );
|
||||
$close = strpos( $body, 'self::close_goal_if_target_reached' );
|
||||
$this->assertNotFalse( $tier );
|
||||
$this->assertNotFalse( $a );
|
||||
$this->assertLessThan( $tier, strpos( $body, 'call_engine' ), 'engine first' );
|
||||
$this->assertLessThan( $close, $tier, 'tiers before the close check' );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user