assertSame( "'" . $v, Csv::cell( $v ) ); } $this->assertSame( 'hello', Csv::cell( 'hello' ) ); $this->assertSame( '10.00', Csv::cell( '10.00' ) ); $this->assertSame( '', Csv::cell( null ) ); $this->assertSame( "\"a\",\"'=b\",\"say \"\"hi\"\"\"\r\n", Csv::line( array( 'a', '=b', 'say "hi"' ) ) ); } public function test_amount_parsing(): void { $this->assertSame( 100, Input::amountToMinor( '1', 'USD' ) ); $this->assertSame( 100050, Input::amountToMinor( '$1,000.50', 'USD' ) ); $this->assertSame( 500, Input::amountToMinor( '500', 'JPY' ) ); $this->assertNull( Input::amountToMinor( '1.234', 'USD' ) ); $this->assertNull( Input::amountToMinor( '-5', 'USD' ) ); $this->assertNull( Input::amountToMinor( 'abc', 'USD' ) ); $this->assertNull( Input::amountToMinor( '', 'USD' ) ); $this->assertNull( Input::amountToMinor( array( '1' ), 'USD' ) ); } public function test_percent_and_misc_input(): void { $this->assertSame( 349, Input::percentToBps( '3.49' ) ); $this->assertSame( 350, Input::percentToBps( '3.5%' ) ); $this->assertSame( 0, Input::percentToBps( '0' ) ); $this->assertNull( Input::percentToBps( '31' ) ); $this->assertNull( Input::percentToBps( 'x' ) ); $this->assertSame( "bad\nword", Input::words( " Bad\r\nword\nBAD" ) ); $this->assertSame( 'big_donation', Input::slug( '!!!' ) ); $this->assertSame( 'mega_win', Input::slug( 'Mega Win' ) ); $this->assertTrue( Input::secretUnchanged( Input::maskSecret( 'abc' ) ) ); $this->assertTrue( Input::secretUnchanged( '' ) ); $this->assertFalse( Input::secretUnchanged( 'newsecret' ) ); $this->assertSame( '', Input::maskSecret( '' ) ); } public function test_add_time_sanitize_uses_exact_config_keys(): void { $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, 'max_timer_seconds' => null, 'basis' => 'base' ), $r['config'] ); } public function test_sanitize_validation_errors(): void { $this->assertNotEmpty( RuleConfig::sanitize( 'add_time', array(), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'add_time', array( 'time_amount' => '0', 'time_unit' => 'minutes', 'unit_amount' => '1' ), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'add_time', array( 'time_amount' => '1', 'time_unit' => 'minutes', 'unit_amount' => '0' ), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'add_time', array( 'time_amount' => '1', 'time_unit' => 'minutes', 'unit_amount' => '1', 'cap_minutes' => 'x' ), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'counter', array( 'step' => '1' ), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'bonus', array( 'min_amount' => '0' ), 'USD' )['errors'] ); $this->assertNotEmpty( RuleConfig::sanitize( 'nope', array(), 'USD' )['errors'] ); $this->assertNull( RuleConfig::sanitize( 'nope', array(), 'USD' )['config'] ); } public function test_counter_and_bonus_sanitize(): void { $c = RuleConfig::sanitize( 'counter', array( 'step' => '1', 'unit_amount' => '10', 'carry_over' => '1', 'label' => 'Pies' ), 'USD' ); $this->assertSame( array( 'step' => 1, 'per_minor' => 1000, 'carry_over' => true, 'basis' => 'base', 'label' => 'Pies' ), $c['config'] ); $b = RuleConfig::sanitize( 'bonus', array( 'min_amount' => '50', 'event' => 'Big Donation', 'basis' => 'total' ), 'USD' ); $this->assertSame( array( 'min_minor' => 5000, 'event' => 'big_donation', 'basis' => 'total' ), $b['config'] ); } public function test_summaries(): void { $this->assertSame( 'Adds 1 minute for every $1.00', RuleConfig::summary( 'add_time', array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ), 'USD' ) ); $this->assertStringContainsString( 'at most 10 minutes per donation', RuleConfig::summary( 'add_time', array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'cap_seconds_per_donation' => 600 ), 'USD' ) ); $this->assertStringContainsString( 'Adds 1 to "Pies" for every $10.00', RuleConfig::summary( 'counter', array( 'step' => 1, 'per_minor' => 1000, 'label' => 'Pies', 'carry_over' => true ), 'USD' ) ); $this->assertStringContainsString( '$50.00 or more', RuleConfig::summary( 'bonus', array( 'min_minor' => 5000, 'event' => 'big' ), 'USD' ) ); $this->assertSame( '1 hour 1 minute 1 second', Format::duration( 3661 ) ); } public function test_preview_for_25_dollars(): void { $rules = array( array( 'type' => 'add_time', 'config' => array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ) ), array( 'type' => 'counter', 'config' => array( 'step' => 1, 'per_minor' => 1000, 'label' => 'Pies' ) ), array( 'type' => 'bonus', 'config' => array( 'min_minor' => 5000, 'event' => 'big' ) ), ); $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, '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'] ); } public function test_templates(): void { $t = Templates::build( 'subathon', 'USD', 100000 ); $this->assertSame( 60, $t['rules'][0]['config']['seconds_per_unit'] ); $this->assertSame( 100, $t['rules'][0]['config']['unit_minor'] ); $c = Templates::build( 'counter', 'USD', 5000, 'Pies' ); $this->assertSame( 1000, $c['rules'][0]['config']['per_minor'] ); $this->assertSame( 'Pies', $c['goal']['title'] ); $this->assertSame( 10, Templates::build( 'counter', 'JPY', 5000 )['rules'][0]['config']['per_minor'] ); $this->assertSame( array(), Templates::build( 'simple', 'USD', 5000 )['rules'] ); $this->assertFalse( Templates::exists( 'x' ) ); } public function test_donor_name_and_test_flag(): void { $this->assertSame( 'Anonymous', Format::donorName( array( 'is_anonymous' => 1, 'donor_name' => 'Secret' ) ) ); $this->assertSame( 'Anonymous', Format::donorName( array( 'is_anonymous' => 0, 'donor_name' => '' ) ) ); $this->assertSame( 'Sam', Format::donorName( array( 'is_anonymous' => 0, 'donor_name' => 'Sam' ) ) ); $this->assertTrue( Format::isTest( array( 'paypal_order_id' => 'TEST-abc' ) ) ); $this->assertFalse( Format::isTest( array( 'paypal_order_id' => '5O1234' ) ) ); } }