Files
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

166 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace WPMultiCity\Tests;
use WPMultiCity\LinkFilter;
final class LinkFilterTest extends TestCase
{
public function test_register_attaches_home_url_filter_at_9999(): void
{
LinkFilter::register();
self::assertSame(
9999,
has_filter('home_url', [LinkFilter::class, 'filter_home_url']),
'LinkFilter::register() must hook home_url at priority 9999'
);
}
public function test_register_attaches_post_link_filter_at_10(): void
{
LinkFilter::register();
self::assertSame(
10,
has_filter('post_link', [LinkFilter::class, 'filter_post_link']),
'LinkFilter::register() must hook post_link at priority 10'
);
}
public function test_register_attaches_acf_load_value_filter_at_9999(): void
{
LinkFilter::register();
self::assertSame(
9999,
has_filter('acf/load_value', [LinkFilter::class, 'filter_acf_load_value']),
'LinkFilter::register() must hook acf/load_value at priority 9999'
);
}
private function set_routing_context(string $town_slug = 'tyumen', string $main = 'spb', string $host = 'site.com'): void
{
$_SERVER['REQUEST_URI'] = '/' . $town_slug . '/';
$_SERVER['HTTP_HOST'] = $host;
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => $town_slug]);
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) use ($main) {
if ($name === 'main_town_slug') return $main;
return false; // page_no_rep empty, page_unic empty
});
}
public function test_add_town_prefix_no_change_when_current_town_equals_main(): void
{
$this->set_routing_context('spb', 'spb');
self::assertSame('/about/', LinkFilter::add_town_prefix('/about/'));
}
public function test_add_town_prefix_no_change_when_current_town_is_null(): void
{
$_SERVER['REQUEST_URI'] = '/random/';
$_SERVER['HTTP_HOST'] = 'site.com';
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']);
\Brain\Monkey\Functions\when('get_field')->justReturn(false);
self::assertSame('/about/', LinkFilter::add_town_prefix('/about/'));
}
public function test_add_town_prefix_prepends_town_to_path_only_url(): void
{
$this->set_routing_context();
self::assertSame('/tyumen/about/', LinkFilter::add_town_prefix('/about/'));
}
public function test_add_town_prefix_skips_path_already_prefixed(): void
{
$this->set_routing_context();
self::assertSame('/tyumen/about/', LinkFilter::add_town_prefix('/tyumen/about/'));
self::assertSame('/tyumen', LinkFilter::add_town_prefix('/tyumen'));
}
public function test_add_town_prefix_full_url_same_host(): void
{
$this->set_routing_context();
self::assertSame(
'https://site.com/tyumen/about/',
LinkFilter::add_town_prefix('https://site.com/about/')
);
self::assertSame(
'https://site.com/tyumen/',
LinkFilter::add_town_prefix('https://site.com/')
);
}
public function test_add_town_prefix_full_url_already_prefixed_no_double_prefix(): void
{
$this->set_routing_context();
self::assertSame(
'https://site.com/tyumen/about/',
LinkFilter::add_town_prefix('https://site.com/tyumen/about/')
);
}
public function test_add_town_prefix_external_host_no_change(): void
{
$this->set_routing_context();
self::assertSame(
'https://external.com/foo/',
LinkFilter::add_town_prefix('https://external.com/foo/')
);
}
public function test_add_town_prefix_non_http_scheme_no_change(): void
{
$this->set_routing_context();
self::assertSame('mailto:x@y.com', LinkFilter::add_town_prefix('mailto:x@y.com'));
self::assertSame('tel:+79991234567', LinkFilter::add_town_prefix('tel:+79991234567'));
}
public function test_add_town_prefix_respects_page_no_rep_exclusions(): void
{
$_SERVER['REQUEST_URI'] = '/tyumen/';
$_SERVER['HTTP_HOST'] = 'site.com';
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\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;
});
self::assertSame('/privacy/', LinkFilter::add_town_prefix('/privacy/'));
}
public function test_add_town_prefix_returns_unchanged_when_bypassed(): void
{
$this->set_routing_context();
$result = \WPMultiCity\Helpers::without_url_filters(function () {
return LinkFilter::add_town_prefix('/about/');
});
self::assertSame('/about/', $result);
}
public function test_filter_acf_load_value_returns_array_unchanged(): void
{
$this->set_routing_context();
self::assertSame(
['a', 'b'],
LinkFilter::filter_acf_load_value(['a', 'b'])
);
}
}