fc21fd1f06
Also: add acf_add_options_page to acf-stubs.php; add translation-function
stubs to acf-stubs.php; fix TestCase::tearDown to call Patchwork::restoreAll()
in a finally block so that when('function_exists') routes are always cleaned up
even when Mockery::close() throws InvalidCountException.
94 lines
3.3 KiB
PHP
94 lines
3.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMultiCity\Tests;
|
|
|
|
use WPMultiCity\OptionsPage;
|
|
|
|
final class OptionsPageTest extends TestCase
|
|
{
|
|
public function test_register_attaches_acf_init_action(): void
|
|
{
|
|
OptionsPage::register();
|
|
|
|
self::assertNotFalse(
|
|
has_action('acf/init', [OptionsPage::class, 'do_register']),
|
|
'OptionsPage::register() must hook acf/init'
|
|
);
|
|
}
|
|
|
|
public function test_register_attaches_main_town_slug_filter(): void
|
|
{
|
|
OptionsPage::register();
|
|
|
|
self::assertNotFalse(
|
|
has_filter('wpmc_main_town_slug', ['WPMultiCity\\Helpers', 'main_town_slug']),
|
|
'OptionsPage::register() must hook wpmc_main_town_slug filter'
|
|
);
|
|
}
|
|
|
|
public function test_do_register_is_noop_when_acf_missing(): void
|
|
{
|
|
// Order matters: expect() must come BEFORE function_exists alias so that
|
|
// Brain Monkey's FunctionStub::__construct sees the real function_exists
|
|
// and doesn't re-eval an already-declared stub.
|
|
\Brain\Monkey\Functions\expect('acf_add_options_page')->never();
|
|
\Brain\Monkey\Functions\expect('acf_add_local_field_group')->never();
|
|
\Brain\Monkey\Functions\when('function_exists')->alias(function ($name) {
|
|
return $name !== 'acf_add_options_page' && $name !== 'acf_add_local_field_group';
|
|
});
|
|
|
|
OptionsPage::do_register();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_do_register_creates_options_page(): void
|
|
{
|
|
\Brain\Monkey\Functions\expect('acf_add_options_page')
|
|
->once()
|
|
->with(\Mockery::on(function ($args) {
|
|
return ($args['menu_slug'] ?? null) === 'wp-multi-city'
|
|
&& ($args['page_title'] ?? null) === 'WP Multi City';
|
|
}));
|
|
\Brain\Monkey\Functions\when('acf_add_local_field_group')->justReturn();
|
|
\Brain\Monkey\Functions\when('function_exists')->alias(function ($name) {
|
|
return $name === 'acf_add_options_page' || $name === 'acf_add_local_field_group';
|
|
});
|
|
|
|
OptionsPage::do_register();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_do_register_registers_routing_field_group(): void
|
|
{
|
|
\Brain\Monkey\Functions\when('acf_add_options_page')->justReturn();
|
|
|
|
$captured = null;
|
|
\Brain\Monkey\Functions\expect('acf_add_local_field_group')
|
|
->once()
|
|
->with(\Mockery::on(function ($group) use (&$captured) {
|
|
$captured = $group;
|
|
return true;
|
|
}));
|
|
\Brain\Monkey\Functions\when('function_exists')->alias(function ($name) {
|
|
return $name === 'acf_add_options_page' || $name === 'acf_add_local_field_group';
|
|
});
|
|
|
|
OptionsPage::do_register();
|
|
|
|
self::assertSame('group_wpmc_routing', $captured['key']);
|
|
self::assertSame(
|
|
[[['param' => 'options_page', 'operator' => '==', 'value' => 'wp-multi-city']]],
|
|
$captured['location']
|
|
);
|
|
|
|
$names = array_column($captured['fields'], 'name');
|
|
self::assertContains('main_town_slug', $names);
|
|
self::assertContains('index_town_alt', $names);
|
|
self::assertContains('page_unic', $names);
|
|
self::assertContains('page_no_rep', $names);
|
|
}
|
|
}
|