Files
wp-donations/tests/Unit/AdminStoreRulesTest.php
T
shadowdaoandClaude Sonnet 5.5 875ca42054
Test and Release / test (push) Successful in 1m18s
Test and Release / release (push) Successful in 4s
CSV export: only completed donations (not abandoned checkouts)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 12:14:04 -08:00

72 lines
2.8 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Admin\Store;
/**
* Regression: Store::rules() must return rules whose `config` is a decoded array, not the raw JSON string.
* (A by-reference foreach over a cast temporary once discarded the decoding and crashed the Rules screen.)
*/
final class AdminStoreRulesTest extends TestCase {
private $previous;
protected function setUp(): void {
if ( ! defined( 'ARRAY_A' ) ) {
define( 'ARRAY_A', 'ARRAY_A' ); // WordPress constant; not loaded in plain PHPUnit
}
$this->previous = $GLOBALS['wpdb'] ?? null;
$GLOBALS['wpdb'] = new class() {
public string $prefix = 'wp_';
public function prepare( string $q, ...$args ): string { return $q; }
public function get_results( string $q, $output = null ): array {
return array(
array( 'id' => '1', 'goal_id' => '1', 'type' => 'add_time', 'config' => '{"seconds_per_unit":60,"unit_minor":100,"max_timer_seconds":5400}', 'enabled' => '1', 'sort' => '0' ),
array( 'id' => '2', 'goal_id' => '1', 'type' => 'counter', 'config' => 'not json', 'enabled' => '0', 'sort' => '1' ),
array( 'id' => '3', 'goal_id' => '1', 'type' => 'bonus', 'config' => '{"min_minor":5000}', 'enabled' => '1', 'sort' => '2' ),
);
}
};
}
protected function tearDown(): void {
$GLOBALS['wpdb'] = $this->previous;
}
public function test_every_rule_config_is_a_decoded_array(): void {
$rules = Store::rules( 1 );
$this->assertCount( 3, $rules );
foreach ( $rules as $r ) {
$this->assertIsArray( $r['config'], 'rule ' . $r['id'] . ' config was not decoded' );
}
$this->assertSame( 5400, $rules[0]['config']['max_timer_seconds'] );
$this->assertSame( array(), $rules[1]['config'], 'invalid JSON becomes an empty config, not a crash' );
$this->assertSame( 5000, $rules[2]['config']['min_minor'] );
}
public function test_rule_lookup_returns_decoded_config(): void {
$r = Store::rule( 3, 1 );
$this->assertIsArray( $r['config'] );
$this->assertNull( Store::rule( 99, 1 ) );
}
public function test_export_only_selects_completed_non_test_donations(): void {
$captured = array();
$GLOBALS['wpdb'] = new class( $captured ) {
public string $prefix = 'wp_';
public function __construct( public array &$sql ) {}
public function esc_like( string $s ): string { return $s; }
public function prepare( string $q, ...$args ): string { return $q; }
public function get_results( string $q, $output = null ): array { $this->sql[] = $q; return array(); }
};
Store::exportBatch( 0, 100 );
$this->assertNotEmpty( $captured );
$this->assertStringContainsString( "status = 'completed'", $captured[0], 'abandoned/unpaid checkouts must not be exported' );
$this->assertStringContainsString( 'goal_id > 0', $captured[0], 'test donations must not be exported' );
}
}