fix(wpmc 1.0.1): guard ACF helpers with did_action('acf/init')
Add private Helpers::acf_ready() that checks did_action('acf/init') > 0,
then guard the five ACF-dependent methods (alt_get_field, index_town_alt_page_id,
main_town_slug, page_unic_swap, is_routable_path) so they return safe defaults
(null/true) before ACF initialises. Fixes the acf_get_value "called too early"
notice triggered when home_url filter fires before acf/init.
Update existing tests in HelpersTest, LinkFilterTest, RedirectTest, RouterTest
to stub did_action => 1 where ACF-ready behaviour is expected. Add four new
guard tests covering before/after acf/init for main_town_slug and is_routable_path.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -133,6 +133,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_alt_get_field_applies_do_shortcode_to_string_values(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
// expect() must come before when('function_exists') so Brain Monkey's
|
||||
// FunctionStub::__construct can eval-define get_field() before the shim
|
||||
// fakes function_exists('get_field') === true.
|
||||
@@ -153,6 +154,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_alt_get_field_returns_arrays_as_is(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
// expect() before when('function_exists') to avoid FunctionStub seeing
|
||||
// the shim and skipping the eval-declaration of get_field.
|
||||
// do_shortcode is intentionally NOT registered: if the implementation
|
||||
@@ -167,6 +169,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_alt_get_field_post_id_filter_overrides_caller_id(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('title', 99, true)
|
||||
@@ -207,13 +210,27 @@ final class HelpersTest extends TestCase
|
||||
self::assertTrue(function_exists('wpmc_get_term_by_slug'));
|
||||
self::assertTrue(function_exists('wpmc_alt_get_field'));
|
||||
self::assertTrue(function_exists('wpmc_remove_domain_link'));
|
||||
self::assertTrue(function_exists('wpmc_main_town_slug'));
|
||||
|
||||
// remove_domain_link is pure (no WP function dependencies) — exercise the proxy.
|
||||
self::assertSame('/foo/', wpmc_remove_domain_link('https://site.com/foo/'));
|
||||
}
|
||||
|
||||
public function test_wpmc_main_town_slug_delegates_to_helpers(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('main_town_slug', 'option')
|
||||
->andReturn('spb');
|
||||
|
||||
require_once dirname(__DIR__) . '/includes/wpmc-functions.php';
|
||||
self::assertSame('spb', wpmc_main_town_slug());
|
||||
}
|
||||
|
||||
public function test_main_town_slug_reads_option_via_acf(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('main_town_slug', 'option')
|
||||
@@ -224,6 +241,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_main_town_slug_returns_null_when_option_empty(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('main_town_slug', 'option')
|
||||
@@ -234,6 +252,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_is_routable_path_returns_true_when_no_exclusions(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('page_no_rep', 'option')
|
||||
@@ -244,6 +263,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_is_routable_path_returns_false_when_path_matches_exclusion(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('page_no_rep', 'option')
|
||||
@@ -257,6 +277,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_is_routable_path_uses_request_uri_when_path_null(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
$_SERVER['REQUEST_URI'] = '/terms/';
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
@@ -268,6 +289,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_page_unic_swap_returns_clone_id_when_row_matches(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
$term->slug = 'tyumen';
|
||||
@@ -285,6 +307,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_page_unic_swap_returns_null_when_no_row_matches(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
$term->slug = 'tyumen';
|
||||
@@ -298,6 +321,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_page_unic_swap_caches_within_request(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
@@ -314,6 +338,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_index_town_alt_page_id_returns_int_when_option_set(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('index_town_alt', 'option')
|
||||
@@ -324,6 +349,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_index_town_alt_page_id_returns_null_when_zero(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('index_town_alt', 'option')
|
||||
@@ -334,6 +360,7 @@ final class HelpersTest extends TestCase
|
||||
|
||||
public function test_reset_cache_clears_page_unic_cache(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
@@ -348,6 +375,48 @@ final class HelpersTest extends TestCase
|
||||
self::assertTrue(true);
|
||||
}
|
||||
|
||||
public function test_main_town_slug_returns_null_before_acf_init(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\expect('did_action')
|
||||
->with('acf/init')
|
||||
->andReturn(0);
|
||||
// get_field should NOT be called — no expectation means Brain Monkey throws if it is.
|
||||
|
||||
self::assertNull(Helpers::main_town_slug());
|
||||
}
|
||||
|
||||
public function test_main_town_slug_calls_get_field_after_acf_init(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('main_town_slug', 'option')
|
||||
->andReturn('spb');
|
||||
|
||||
self::assertSame('spb', Helpers::main_town_slug());
|
||||
}
|
||||
|
||||
public function test_is_routable_path_returns_true_before_acf_init(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\expect('did_action')
|
||||
->with('acf/init')
|
||||
->andReturn(0);
|
||||
// get_field should NOT be called.
|
||||
|
||||
self::assertTrue(Helpers::is_routable_path('/some/path/'));
|
||||
}
|
||||
|
||||
public function test_is_routable_path_checks_exclusions_after_acf_init(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('page_no_rep', 'option')
|
||||
->andReturn([['link' => '/excluded/']]);
|
||||
|
||||
self::assertFalse(Helpers::is_routable_path('/excluded/page/'));
|
||||
}
|
||||
|
||||
public function test_is_bypassed_returns_false_by_default(): void
|
||||
{
|
||||
self::assertFalse(Helpers::is_bypassed());
|
||||
|
||||
Reference in New Issue
Block a user