82d2345d12
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>
247 lines
9.3 KiB
PHP
247 lines
9.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMultiCity\Tests;
|
|
|
|
use WPMultiCity\Router;
|
|
|
|
final class RouterTest extends TestCase
|
|
{
|
|
public function test_register_attaches_wp_action_at_priority_9999(): void
|
|
{
|
|
Router::register();
|
|
|
|
self::assertSame(
|
|
9999,
|
|
has_action('wp', [Router::class, 'route']),
|
|
'Router::register() must hook wp action at priority 9999'
|
|
);
|
|
}
|
|
|
|
public function test_register_attaches_query_vars_filter(): void
|
|
{
|
|
Router::register();
|
|
|
|
self::assertNotFalse(
|
|
has_filter('query_vars', [Router::class, 'add_town_query_var']),
|
|
'Router::register() must hook query_vars filter'
|
|
);
|
|
}
|
|
|
|
public function test_add_town_query_var_appends_town(): void
|
|
{
|
|
$vars = ['p', 'page_id'];
|
|
$result = Router::add_town_query_var($vars);
|
|
|
|
self::assertContains('town', $result);
|
|
}
|
|
|
|
public function test_route_skips_when_is_admin(): void
|
|
{
|
|
\Brain\Monkey\Functions\expect('get_terms')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(true);
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
|
|
public function test_route_skips_when_current_town_is_null(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\expect('url_to_postid')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/something-not-a-city/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']);
|
|
\Brain\Monkey\Functions\when('get_field')->justReturn(false);
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
|
|
public function test_route_skips_when_current_town_equals_main_town(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\expect('url_to_postid')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/spb/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']);
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
return false;
|
|
});
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
|
|
public function test_route_skips_when_path_not_routable(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\expect('url_to_postid')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/privacy/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
if ($name === 'page_no_rep') return [['link' => '/privacy/']];
|
|
return false;
|
|
});
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
|
|
public function test_route_uses_main_page_alt_term_meta_for_root_url(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
$term->slug = 'tyumen';
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name, $where = null) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
if ($name === 'page_no_rep') return false;
|
|
if ($name === 'main_page_alt' && $where === 'town_11') return 555;
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\when('get_post')->justReturn((object) ['ID' => 555]);
|
|
\Brain\Monkey\Functions\when('status_header')->justReturn(null);
|
|
|
|
Router::route();
|
|
|
|
self::assertInstanceOf(\WP_Query::class, $GLOBALS['wp_query']);
|
|
self::assertSame(555, $GLOBALS['wp_query']->constructor_args['page_id']);
|
|
self::assertSame('tyumen', $GLOBALS['wp_query']->constructor_args['town']);
|
|
self::assertTrue($GLOBALS['wp_query']->is_front_page);
|
|
self::assertTrue($GLOBALS['wp_query']->is_page);
|
|
}
|
|
|
|
public function test_route_falls_back_to_index_town_alt_option(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name, $where = null) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
if ($name === 'page_no_rep') return false;
|
|
if ($name === 'main_page_alt') return 0;
|
|
if ($name === 'index_town_alt') return 777;
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\when('get_post')->justReturn((object) ['ID' => 777]);
|
|
\Brain\Monkey\Functions\when('status_header')->justReturn(null);
|
|
|
|
Router::route();
|
|
|
|
self::assertSame(777, $GLOBALS['wp_query']->constructor_args['page_id']);
|
|
}
|
|
|
|
public function test_route_does_not_install_query_when_neither_main_nor_fallback_set(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\expect('get_post')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
return false;
|
|
});
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
|
|
public function test_route_resolves_internal_page_via_url_to_postid(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/about/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn(false);
|
|
\Brain\Monkey\Functions\expect('url_to_postid')
|
|
->once()
|
|
->with('/about/')
|
|
->andReturn(42);
|
|
\Brain\Monkey\Functions\when('get_post')->justReturn((object) ['ID' => 42]);
|
|
\Brain\Monkey\Functions\when('status_header')->justReturn(null);
|
|
|
|
Router::route();
|
|
|
|
self::assertSame(42, $GLOBALS['wp_query']->constructor_args['page_id']);
|
|
self::assertSame('tyumen', $GLOBALS['wp_query']->constructor_args['town']);
|
|
self::assertTrue($GLOBALS['wp_query']->is_page);
|
|
}
|
|
|
|
public function test_route_applies_page_unic_swap_when_clone_exists(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/about/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
$term->slug = 'tyumen';
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
if ($name === 'page_unic') return [
|
|
['town' => 11, 'page_old' => 42, 'page_new' => 84],
|
|
];
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\when('url_to_postid')->justReturn(42);
|
|
\Brain\Monkey\Functions\when('get_post')->justReturn((object) ['ID' => 84]);
|
|
\Brain\Monkey\Functions\when('status_header')->justReturn(null);
|
|
|
|
Router::route();
|
|
|
|
self::assertSame(84, $GLOBALS['wp_query']->constructor_args['page_id']);
|
|
}
|
|
|
|
public function test_route_does_not_install_query_when_url_to_postid_returns_zero(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\expect('get_post')->never();
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/unknown/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
if ($name === 'main_town_slug') return 'spb';
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\when('url_to_postid')->justReturn(0);
|
|
|
|
Router::route();
|
|
|
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
|
}
|
|
}
|