diff --git a/includes/Admin/GoalsPage.php b/includes/Admin/GoalsPage.php index 8f51570..fb415fd 100644 --- a/includes/Admin/GoalsPage.php +++ b/includes/Admin/GoalsPage.php @@ -197,7 +197,13 @@ final class GoalsPage { foreach ( $rules as $r ) { $rid = (int) $r['id']; echo '' . esc_html( RuleConfig::TYPES[ $r['type'] ] ?? (string) $r['type'] ) . ( $r['enabled'] ? '' : ' (off)' ) . ''; - echo '' . esc_html( RuleConfig::summary( (string) $r['type'], $r['config'], $cur ) ) . ''; + 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 '' . esc_html( $summary ) . ''; echo 'Edit '; Admin::form( 'sg_rule_toggle', array( 'goal_id' => $goalId, 'rule_id' => $rid, 'enabled' => $r['enabled'] ? 0 : 1 ), 'sg-inline' ); echo ' '; diff --git a/includes/Admin/Store.php b/includes/Admin/Store.php index 780159a..c25e7d5 100644 --- a/includes/Admin/Store.php +++ b/includes/Admin/Store.php @@ -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 { diff --git a/tests/Unit/AdminStoreRulesTest.php b/tests/Unit/AdminStoreRulesTest.php new file mode 100644 index 0000000..34b5b59 --- /dev/null +++ b/tests/Unit/AdminStoreRulesTest.php @@ -0,0 +1,55 @@ +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 ) ); + } +}