Files
wp-multi-city/tests/RedirectTest.php
T
Vladimir Bryzgalov 82d2345d12 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>
2026-05-17 17:01:19 +05:00

109 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace WPMultiCity\Tests;
use WPMultiCity\Redirect;
final class RedirectTest extends TestCase
{
public function test_register_attaches_wp_action_at_priority_9998(): void
{
Redirect::register();
self::assertSame(
9998,
has_action('wp', [Redirect::class, 'handle']),
'Redirect::register() must hook wp action at priority 9998'
);
}
public function test_handle_skips_when_current_town_equals_main(): void
{
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
$_SERVER['REQUEST_URI'] = '/spb/about/';
\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;
});
Redirect::handle();
self::assertTrue(true);
}
public function test_handle_skips_when_post_not_set(): void
{
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
$_SERVER['REQUEST_URI'] = '/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;
});
unset($GLOBALS['post']);
Redirect::handle();
self::assertTrue(true);
}
public function test_handle_skips_when_url_already_has_town_prefix(): void
{
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
\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;
});
$GLOBALS['post'] = (object) ['ID' => 84];
Redirect::handle();
self::assertTrue(true);
}
public function test_handle_redirects_when_post_is_page_unic_clone_without_prefix(): void
{
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
$_SERVER['REQUEST_URI'] = '/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;
});
$GLOBALS['post'] = (object) ['ID' => 84];
\Brain\Monkey\Functions\when('get_permalink')->justReturn('https://site.com/about/');
\Brain\Monkey\Functions\expect('wp_safe_redirect')
->once()
->with('/tyumen/about/', 301)
->andThrow(new \RuntimeException('redirect-called'));
try {
Redirect::handle();
self::fail('Expected redirect to be issued');
} catch (\RuntimeException $e) {
self::assertSame('redirect-called', $e->getMessage());
}
}
}