- Actions\Matcher (exact / every), ActionRepository, Recorder (idempotent via unique key) - Admin: Rewards screen with live preview, queue with Mark done, menu badge - Donate form shows actions as payment buttons; overlay 'actions' widget; alerts list triggered actions - Installer DB_VERSION 2 (sg_actions, sg_action_events) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
68 lines
2.8 KiB
PHP
68 lines
2.8 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Admin\ActionInput;
|
|
|
|
final class ActionInputTest extends TestCase {
|
|
|
|
private function post( array $o = array() ): array {
|
|
return $o + array( 'title' => 'Eat a bean', 'description' => '', 'amount' => '5.00', 'mode' => 'every', 'goal_id' => '0', 'sort' => '' );
|
|
}
|
|
|
|
private function v( array $o = array(), int $min = 100 ): array {
|
|
return ActionInput::validate( $this->post( $o ), $min, 'USD', array( 3, 4 ), 40 );
|
|
}
|
|
|
|
public function test_valid_input(): void {
|
|
$r = $this->v( array( 'title' => ' <b>Eat</b> a bean ', 'description' => 'Yum', 'goal_id' => '3' ) );
|
|
$this->assertNull( $r['error'] );
|
|
$this->assertSame( array( 'goal_id' => 3, 'title' => 'Eat a bean', 'description' => 'Yum', 'amount_minor' => 500, 'mode' => 'every', 'sort' => 40 ), $r['data'] );
|
|
}
|
|
|
|
public function test_title_required_and_max_120(): void {
|
|
$this->assertNull( $this->v( array( 'title' => ' ' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'title' => str_repeat( 'a', 121 ) ) )['data'] );
|
|
$this->assertNotNull( $this->v( array( 'title' => str_repeat( 'a', 120 ) ) )['data'] );
|
|
}
|
|
|
|
public function test_description_max_255(): void {
|
|
$this->assertNull( $this->v( array( 'description' => str_repeat( 'a', 256 ) ) )['data'] );
|
|
$this->assertNotNull( $this->v( array( 'description' => str_repeat( 'a', 255 ) ) )['data'] );
|
|
}
|
|
|
|
public function test_amount_must_reach_the_minimum(): void {
|
|
$low = $this->v( array( 'amount' => '0.99' ) );
|
|
$this->assertNull( $low['data'] );
|
|
$this->assertStringContainsString( 'minimum', (string) $low['error'] );
|
|
$this->assertNotNull( $this->v( array( 'amount' => '1.00' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'amount' => '4.99' ), 500 )['data'] );
|
|
$this->assertNotNull( $this->v( array( 'amount' => '5' ), 500 )['data'] );
|
|
}
|
|
|
|
public function test_bad_amounts(): void {
|
|
foreach ( array( '', '0', '-5', 'abc', '5.123' ) as $bad ) {
|
|
$this->assertNull( $this->v( array( 'amount' => $bad ) )['data'], "amount '$bad'" );
|
|
}
|
|
}
|
|
|
|
public function test_mode_whitelist(): void {
|
|
$this->assertNotNull( $this->v( array( 'mode' => 'exact' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'mode' => 'always' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'mode' => null ) )['data'] );
|
|
}
|
|
|
|
public function test_goal_must_exist_or_be_zero(): void {
|
|
$this->assertNotNull( $this->v( array( 'goal_id' => '4' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'goal_id' => '99' ) )['data'] );
|
|
$this->assertNull( $this->v( array( 'goal_id' => '-1' ) )['data'] );
|
|
}
|
|
|
|
public function test_sort(): void {
|
|
$this->assertSame( 7, $this->v( array( 'sort' => '7' ) )['data']['sort'] );
|
|
$this->assertNull( $this->v( array( 'sort' => 'x' ) )['data'] );
|
|
}
|
|
}
|