feat(wpmc 1.1.0): Redirect::handle reverse-slug lookup on is_404
Replace old triggering-on-page_new semantics with washanyanya-style: - template_redirect@1 + is_404() trigger - get_posts(name=$slug, post_type=page, status=[private,publish]) - match candidate.ID against page_unic[i].page_new - 301 to /<town>/<orig-canonical-path>/ Closes Gitea issue #1. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+46
-21
@@ -12,60 +12,85 @@ final class Redirect
|
|||||||
|
|
||||||
public static function handle(): void
|
public static function handle(): void
|
||||||
{
|
{
|
||||||
if (\is_admin()) {
|
if (\is_admin() || (\defined('DOING_AJAX') && \DOING_AJAX)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (\defined('DOING_AJAX') && \DOING_AJAX) {
|
if (!\is_404()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$main = Helpers::main_town_slug();
|
$main = Helpers::main_town_slug();
|
||||||
if ($main === null) {
|
if ($main === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
global $post;
|
|
||||||
if (!isset($post->ID)) {
|
$slug = self::extract_last_segment($_SERVER['REQUEST_URI'] ?? '/');
|
||||||
|
if ($slug === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$current_page_id = (int) $post->ID;
|
|
||||||
|
$candidates = \get_posts([
|
||||||
|
'name' => $slug,
|
||||||
|
'post_type' => 'page',
|
||||||
|
'post_status' => ['private', 'publish'],
|
||||||
|
'posts_per_page' => 1,
|
||||||
|
'no_found_rows' => true,
|
||||||
|
'suppress_filters' => false,
|
||||||
|
]);
|
||||||
|
if (empty($candidates)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$candidate_id = (int) $candidates[0]->ID;
|
||||||
|
|
||||||
if (!\function_exists('get_field')) {
|
if (!\function_exists('get_field')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$page_unic = \get_field('page_unic', 'option');
|
$rows = \get_field('page_unic', 'option');
|
||||||
if (!is_array($page_unic)) {
|
if (!is_array($rows)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
|
|
||||||
foreach ($page_unic as $row) {
|
foreach ($rows as $row) {
|
||||||
if (!is_array($row)) {
|
if (!is_array($row)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((int) ($row['page_new'] ?? 0) !== $current_page_id) {
|
if ((int) ($row['page_new'] ?? 0) !== $candidate_id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$town_term_id = (int) ($row['town'] ?? 0);
|
$town_term_id = (int) ($row['town'] ?? 0);
|
||||||
if (!$town_term_id) {
|
$orig_id = (int) ($row['page_old'] ?? 0);
|
||||||
|
if (!$town_term_id || !$orig_id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$term = \get_term_by('id', $town_term_id, 'town');
|
$term = \get_term_by('id', $town_term_id, 'town');
|
||||||
if (!($term instanceof \WP_Term) || $term->slug === '' || $term->slug === $main) {
|
if (!($term instanceof \WP_Term) || $term->slug === '' || $term->slug === $main) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$town_slug = $term->slug;
|
$orig_permalink = Helpers::without_url_filters(
|
||||||
if (preg_match('#^/' . preg_quote($town_slug, '#') . '(/|$)#', $request_uri)) {
|
static fn () => \get_permalink($orig_id)
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
$orig_id = (int) ($row['page_old'] ?? 0);
|
|
||||||
if (!$orig_id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$orig_permalink = Helpers::without_url_filters(static fn () => \get_permalink($orig_id));
|
|
||||||
if (!is_string($orig_permalink) || $orig_permalink === '') {
|
if (!is_string($orig_permalink) || $orig_permalink === '') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$path_only = Helpers::remove_domain_link($orig_permalink);
|
$path_only = Helpers::remove_domain_link($orig_permalink);
|
||||||
$redirect_to = '/' . $town_slug . $path_only;
|
$redirect_to = '/' . $term->slug . $path_only;
|
||||||
\wp_safe_redirect($redirect_to, 301);
|
\wp_safe_redirect($redirect_to, 301);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function extract_last_segment(string $uri): ?string
|
||||||
|
{
|
||||||
|
$path = strtok($uri, '?');
|
||||||
|
if ($path === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$parts = array_values(array_filter(
|
||||||
|
explode('/', $path),
|
||||||
|
static fn (string $p) => $p !== ''
|
||||||
|
));
|
||||||
|
$last = end($parts);
|
||||||
|
return $last !== false && $last !== '' ? $last : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+256
-33
@@ -18,71 +18,86 @@ final class RedirectTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_handle_skips_when_current_town_equals_main(): void
|
public function test_bail_when_is_admin(): void
|
||||||
{
|
{
|
||||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(true);
|
||||||
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
\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();
|
Redirect::handle();
|
||||||
|
|
||||||
self::assertTrue(true);
|
self::assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_handle_skips_when_post_not_set(): void
|
public function test_bail_when_not_404(): void
|
||||||
{
|
{
|
||||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
||||||
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
$_SERVER['REQUEST_URI'] = '/about/';
|
\Brain\Monkey\Functions\when('is_404')->justReturn(false);
|
||||||
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
|
||||||
if ($name === 'main_town_slug') return 'spb';
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
unset($GLOBALS['post']);
|
|
||||||
|
|
||||||
Redirect::handle();
|
Redirect::handle();
|
||||||
|
|
||||||
self::assertTrue(true);
|
self::assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_handle_skips_when_url_already_has_town_prefix(): void
|
public function test_bail_when_main_town_slug_null(): void
|
||||||
{
|
{
|
||||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
||||||
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
$_SERVER['REQUEST_URI'] = '/tyumen/about/';
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
||||||
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
||||||
if ($name === 'main_town_slug') return 'spb';
|
if ($name === 'main_town_slug') return null;
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
$GLOBALS['post'] = (object) ['ID' => 84];
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
Redirect::handle();
|
Redirect::handle();
|
||||||
|
|
||||||
self::assertTrue(true);
|
self::assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_handle_redirects_when_post_is_page_unic_clone_without_prefix(): void
|
public function test_bail_when_request_uri_root(): void
|
||||||
{
|
{
|
||||||
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
$_SERVER['REQUEST_URI'] = '/about/';
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
\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;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/';
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
\Brain\Monkey\Functions\expect('get_posts')->never();
|
||||||
|
|
||||||
$term = new \WP_Term();
|
Redirect::handle();
|
||||||
$term->term_id = 11;
|
|
||||||
$term->slug = 'tyumen';
|
|
||||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_bail_when_no_post_with_slug(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
||||||
|
if ($name === 'main_town_slug') return 'spb';
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([]);
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
|
Redirect::handle();
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_bail_when_post_not_in_page_unic(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
||||||
if ($name === 'main_town_slug') return 'spb';
|
if ($name === 'main_town_slug') return 'spb';
|
||||||
if ($name === 'page_unic') return [
|
if ($name === 'page_unic') return [
|
||||||
@@ -90,12 +105,94 @@ final class RedirectTest extends TestCase
|
|||||||
];
|
];
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
$GLOBALS['post'] = (object) ['ID' => 84];
|
$_SERVER['REQUEST_URI'] = '/orphan-slug/';
|
||||||
\Brain\Monkey\Functions\when('get_permalink')->justReturn('https://site.com/about/');
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 999],
|
||||||
|
]);
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
|
Redirect::handle();
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_bail_when_page_unic_empty(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
||||||
|
if ($name === 'main_town_slug') return 'spb';
|
||||||
|
if ($name === 'page_unic') return null;
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84],
|
||||||
|
]);
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
|
Redirect::handle();
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_bail_when_matched_town_is_main(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) {
|
||||||
|
if ($name === 'main_town_slug') return 'spb';
|
||||||
|
if ($name === 'page_unic') return [
|
||||||
|
['town' => 10, 'page_old' => 42, 'page_new' => 84],
|
||||||
|
];
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-spb/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$term = new \WP_Term();
|
||||||
|
$term->term_id = 10;
|
||||||
|
$term->slug = 'spb';
|
||||||
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||||
|
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
|
Redirect::handle();
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_redirect_for_private_clone(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\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;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84, 'post_status' => 'private'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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_permalink')->justReturn('https://site.com/tarif/');
|
||||||
|
|
||||||
\Brain\Monkey\Functions\expect('wp_safe_redirect')
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')
|
||||||
->once()
|
->once()
|
||||||
->with('/tyumen/about/', 301)
|
->with('/tyumen/tarif/', 301)
|
||||||
->andThrow(new \RuntimeException('redirect-called'));
|
->andThrow(new \RuntimeException('redirect-called'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -105,4 +202,130 @@ final class RedirectTest extends TestCase
|
|||||||
self::assertSame('redirect-called', $e->getMessage());
|
self::assertSame('redirect-called', $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_redirect_for_published_clone_with_own_url(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\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;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84, 'post_status' => 'publish'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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_permalink')->justReturn('https://site.com/tarif/');
|
||||||
|
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')
|
||||||
|
->once()
|
||||||
|
->with('/tyumen/tarif/', 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_redirect_strips_domain_from_get_permalink(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\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;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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_permalink')->justReturn('https://site.com/tarif/');
|
||||||
|
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')
|
||||||
|
->once()
|
||||||
|
->with('/tyumen/tarif/', 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_permalink_called_inside_without_url_filters(): void
|
||||||
|
{
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\when('is_404')->justReturn(true);
|
||||||
|
\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;
|
||||||
|
});
|
||||||
|
$_SERVER['REQUEST_URI'] = '/tarif-tyumen/';
|
||||||
|
\Brain\Monkey\Functions\when('get_posts')->justReturn([
|
||||||
|
(object) ['ID' => 84],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$term = new \WP_Term();
|
||||||
|
$term->term_id = 11;
|
||||||
|
$term->slug = 'tyumen';
|
||||||
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||||
|
|
||||||
|
$depth_during_call = -1;
|
||||||
|
\Brain\Monkey\Functions\when('get_permalink')->alias(
|
||||||
|
function () use (&$depth_during_call) {
|
||||||
|
$depth_during_call = \WPMultiCity\Helpers::is_bypassed() ? 1 : 0;
|
||||||
|
return 'https://site.com/tarif/';
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')
|
||||||
|
->once()
|
||||||
|
->andThrow(new \RuntimeException('redirect-called'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
Redirect::handle();
|
||||||
|
} catch (\RuntimeException $e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertSame(
|
||||||
|
1,
|
||||||
|
$depth_during_call,
|
||||||
|
'get_permalink must be wrapped in Helpers::without_url_filters'
|
||||||
|
);
|
||||||
|
self::assertFalse(
|
||||||
|
\WPMultiCity\Helpers::is_bypassed(),
|
||||||
|
'bypass depth must be restored to 0 after handle()'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WPMultiCity\Tests;
|
||||||
|
|
||||||
|
use WPMultiCity\Redirect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Isolated test for the DOING_AJAX bail-out in Redirect::handle().
|
||||||
|
*
|
||||||
|
* Must be in a separate file sorted AFTER RouterTest so that defining the
|
||||||
|
* DOING_AJAX constant does not contaminate Router::route() tests in RouterTest,
|
||||||
|
* which also bails early when DOING_AJAX is true.
|
||||||
|
*/
|
||||||
|
final class ZRedirectDoingAjaxTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_bail_when_doing_ajax(): void
|
||||||
|
{
|
||||||
|
if (!\defined('DOING_AJAX')) {
|
||||||
|
\define('DOING_AJAX', true);
|
||||||
|
}
|
||||||
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
||||||
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||||
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
||||||
|
|
||||||
|
Redirect::handle();
|
||||||
|
|
||||||
|
self::assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user