Fix Rules screen crash: Store::rules() discarded decoded config (by-ref foreach over a cast copy)
Also make an unreadable rule non-fatal on the Rules screen, and add a regression test. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3bbd77b661
commit
27646c1001
3 files changed
+67
-3
No files matched your search
@@ -0,0 +1,55 @@
|
||||
<?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 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user