Fix Rules screen crash: Store::rules() discarded decoded config (by-ref foreach over a cast copy)
Test and Release / test (push) Successful in 1m14s
Test and Release / release (push) Successful in 5s

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:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 11:47:13 -08:00
1 parent 3bbd77b661
commit 27646c1001
3 files changed
+67 -3

No files matched your search

+7 -1
View File
@@ -197,7 +197,13 @@ final class GoalsPage {
foreach ( $rules as $r ) {
$rid = (int) $r['id'];
echo '<tr class="' . ( $r['enabled'] ? '' : 'sg-off' ) . '"><td>' . esc_html( RuleConfig::TYPES[ $r['type'] ] ?? (string) $r['type'] ) . ( $r['enabled'] ? '' : ' <em>(off)</em>' ) . '</td>';
echo '<td>' . esc_html( RuleConfig::summary( (string) $r['type'], $r['config'], $cur ) ) . '</td><td class="sg-actions">';
try {
$summary = RuleConfig::summary( (string) $r['type'], $r['config'], $cur );
} catch ( \Throwable $e ) {
// One unreadable rule must not take down the whole screen; say what is wrong so it can be fixed or deleted.
$summary = 'Could not read this rule (' . $e->getMessage() . '). You can delete it and add it again.';
}
echo '<td>' . esc_html( $summary ) . '</td><td class="sg-actions">';
echo '<a class="button" href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'rules', 'goal' => $goalId, 'rule' => $rid ) ) ) . '#sg-rule-form">Edit</a> ';
Admin::form( 'sg_rule_toggle', array( 'goal_id' => $goalId, 'rule_id' => $rid, 'enabled' => $r['enabled'] ? 0 : 1 ), 'sg-inline' );
echo '<button class="button">' . ( $r['enabled'] ? 'Turn off' : 'Turn on' ) . '</button></form> ';
+5 -2
View File
@@ -26,11 +26,14 @@ final class Store {
public static function rules( int $goalId ): array {
global $wpdb;
$rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . self::t( 'rules' ) . ' WHERE goal_id = %d ORDER BY sort ASC, id ASC', $goalId ), ARRAY_A );
foreach ( (array) $rows as &$r ) {
$rows = (array) $rows;
// Iterate $rows itself by reference: `foreach ( (array) $rows as &$r )` would modify a temporary copy and discard the decoding.
foreach ( $rows as &$r ) {
$cfg = json_decode( (string) $r['config'], true );
$r['config'] = is_array( $cfg ) ? $cfg : array();
}
return (array) $rows;
unset( $r );
return $rows;
}
public static function rule( int $ruleId, int $goalId ): ?array {
+55
View File
@@ -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 ) );
}
}