- Core\GoalTargetPolicy; atomic close in the capture path; friendly 'goal reached' message - In-flight checkouts still capture; new orders for a closed goal are refused - Rewards menu overlay: card hugs content, text scales to the window, resize-aware, scroll only as a last resort Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
87 lines
4.0 KiB
PHP
87 lines
4.0 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Core\FeeCalculator;
|
|
use StreamGoals\Core\GoalTargetPolicy;
|
|
use StreamGoals\Core\Settings;
|
|
use StreamGoals\PayPal\OrderValidator;
|
|
use StreamGoals\PayPal\ValidationException;
|
|
|
|
final class GoalTargetPolicyTest extends TestCase {
|
|
|
|
private function goal( array $o = array() ): array {
|
|
return array_merge( array( 'id' => 3, 'currency' => 'USD', 'status' => 'active', 'target_minor' => 10000, 'close_at_target' => 1, 'counts_fees' => 0, 'starts_at' => null, 'ends_at' => null ), $o );
|
|
}
|
|
|
|
public function test_below_equal_and_over_target(): void {
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal(), 9999 ) );
|
|
$this->assertTrue( GoalTargetPolicy::shouldClose( $this->goal(), 10000 ) );
|
|
$this->assertTrue( GoalTargetPolicy::shouldClose( $this->goal(), 25000 ) );
|
|
}
|
|
|
|
public function test_flag_off_or_missing_never_closes(): void {
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'close_at_target' => 0 ) ), 99999 ) );
|
|
$g = $this->goal();
|
|
unset( $g['close_at_target'] );
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $g, 99999 ) );
|
|
}
|
|
|
|
public function test_only_active_goals_are_closed(): void {
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'status' => 'complete' ) ), 20000 ) );
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'status' => 'paused' ) ), 20000 ) );
|
|
}
|
|
|
|
public function test_target_zero_never_closes(): void {
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'target_minor' => 0 ) ), 0 ) );
|
|
$this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'target_minor' => 0 ) ), 500 ) );
|
|
}
|
|
|
|
public function test_base_vs_total_basis(): void {
|
|
$this->assertSame( 'base_minor', GoalTargetPolicy::amountColumn( $this->goal() ) );
|
|
$this->assertSame( 'total_minor', GoalTargetPolicy::amountColumn( $this->goal( array( 'counts_fees' => 1 ) ) ) );
|
|
}
|
|
|
|
public function test_closed_at_target_detection_and_pause_is_not_closed(): void {
|
|
$this->assertTrue( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'complete' ) ) ) );
|
|
$this->assertFalse( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'paused' ) ) ) );
|
|
$this->assertFalse( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'complete', 'close_at_target' => 0 ) ) ) );
|
|
}
|
|
|
|
public function test_checkbox_parsing(): void {
|
|
$this->assertSame( 1, GoalTargetPolicy::flagFromPost( '1' ) );
|
|
$this->assertSame( 1, GoalTargetPolicy::flagFromPost( 'on' ) );
|
|
$this->assertSame( 0, GoalTargetPolicy::flagFromPost( null ) );
|
|
$this->assertSame( 0, GoalTargetPolicy::flagFromPost( '' ) );
|
|
$this->assertSame( 0, GoalTargetPolicy::flagFromPost( '0' ) );
|
|
$this->assertSame( 0, GoalTargetPolicy::flagFromPost( array( '1' ) ) );
|
|
}
|
|
|
|
public function test_new_orders_for_a_closed_goal_get_a_friendly_409(): void {
|
|
try {
|
|
OrderValidator::validate( array( 'amount' => '5.00' ), $this->goal( array( 'status' => 'complete' ) ), Settings::defaults(), new FeeCalculator( 349, 49 ), 1800000000 );
|
|
$this->fail( 'expected refusal' );
|
|
} catch ( ValidationException $e ) {
|
|
$this->assertSame( 'goal_inactive', $e->errorCode );
|
|
$this->assertSame( 409, $e->status );
|
|
$this->assertStringContainsString( 'Thank you', $e->getMessage() );
|
|
}
|
|
}
|
|
|
|
public function test_paused_goal_keeps_generic_message(): void {
|
|
try {
|
|
OrderValidator::quote( array( 'amount' => '5.00' ), $this->goal( array( 'status' => 'paused' ) ), Settings::defaults(), new FeeCalculator( 349, 49 ), 1800000000 );
|
|
$this->fail( 'expected refusal' );
|
|
} catch ( ValidationException $e ) {
|
|
$this->assertSame( 'This goal is not accepting donations.', $e->getMessage() );
|
|
}
|
|
}
|
|
|
|
public function test_goal_open_for_active_flagged_goal(): void {
|
|
$this->assertTrue( OrderValidator::goalIsOpen( $this->goal(), 1800000000 ) );
|
|
$this->assertFalse( OrderValidator::goalIsOpen( $this->goal( array( 'status' => 'complete' ) ), 1800000000 ) );
|
|
}
|
|
}
|