feat(wpmc S4): Router::route internal page + page_unic swap
This commit is contained in:
@@ -60,7 +60,15 @@ final class Router
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal page handling lands in Task 13.
|
$page_id = \url_to_postid($url_original);
|
||||||
|
if (!$page_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$swap = Helpers::page_unic_swap($page_id, $town_slug);
|
||||||
|
if ($swap !== null) {
|
||||||
|
$page_id = $swap;
|
||||||
|
}
|
||||||
|
self::install_query($page_id, $town_slug, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function strip_town_prefix(string $uri, string $town_slug): string
|
private static function strip_town_prefix(string $uri, string $town_slug): string
|
||||||
|
|||||||
@@ -166,4 +166,72 @@ final class RouterTest extends TestCase
|
|||||||
|
|
||||||
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_route_resolves_internal_page_via_url_to_postid(): void
|
||||||
|
{
|
||||||
|
\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\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\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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user