43e3efc5a2
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>
332 lines
12 KiB
PHP
332 lines
12 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMultiCity\Tests;
|
|
|
|
use WPMultiCity\Redirect;
|
|
|
|
final class RedirectTest extends TestCase
|
|
{
|
|
public function test_register_attaches_template_redirect_at_priority_1(): void
|
|
{
|
|
Redirect::register();
|
|
|
|
self::assertSame(
|
|
1,
|
|
has_action('template_redirect', [Redirect::class, 'handle']),
|
|
'Redirect::register() must hook template_redirect at priority 1'
|
|
);
|
|
}
|
|
|
|
public function test_bail_when_is_admin(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(true);
|
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
|
|
Redirect::handle();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_bail_when_not_404(): void
|
|
{
|
|
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
|
|
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
|
\Brain\Monkey\Functions\when('is_404')->justReturn(false);
|
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
|
|
Redirect::handle();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_bail_when_main_town_slug_null(): 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 null;
|
|
return false;
|
|
});
|
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
|
|
Redirect::handle();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_bail_when_request_uri_root(): 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'] = '/';
|
|
\Brain\Monkey\Functions\expect('wp_safe_redirect')->never();
|
|
\Brain\Monkey\Functions\expect('get_posts')->never();
|
|
|
|
Redirect::handle();
|
|
|
|
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) {
|
|
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'] = '/orphan-slug/';
|
|
\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')
|
|
->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_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()'
|
|
);
|
|
}
|
|
|
|
}
|