term_id = 42; $term->slug = 'tyumen'; $term->taxonomy = 'town'; \Brain\Monkey\Functions\expect('get_term_by') ->once() ->with('slug', 'tyumen', 'town') ->andReturn($term); $result = Helpers::get_term_by_slug('tyumen'); self::assertInstanceOf(\WP_Term::class, $result); self::assertSame(42, $result->term_id); self::assertSame('tyumen', $result->slug); } public function test_get_term_by_slug_returns_null_when_not_found(): void { \Brain\Monkey\Functions\expect('get_term_by') ->once() ->with('slug', 'nonexistent', 'town') ->andReturn(false); self::assertNull(Helpers::get_term_by_slug('nonexistent')); } public function test_get_term_by_slug_caches_results_within_request(): void { \Brain\Monkey\Functions\expect('get_term_by') ->once() ->with('slug', 'tyumen', 'town') ->andReturn(false); Helpers::get_term_by_slug('tyumen'); Helpers::get_term_by_slug('tyumen'); Helpers::get_term_by_slug('tyumen'); // Mockery ->once() at teardown verifies the cache stopped repeated calls. self::assertTrue(true); } public function test_current_town_returns_slug_when_uri_matches_existing_town(): void { $_SERVER['REQUEST_URI'] = '/tyumen/some-service/'; \Brain\Monkey\Functions\expect('get_terms') ->once() ->with(\Mockery::on(function ($args) { return ($args['taxonomy'] ?? null) === 'town' && ($args['hide_empty'] ?? null) === false && ($args['fields'] ?? null) === 'id=>slug'; })) ->andReturn([10 => 'spb', 11 => 'tyumen']); self::assertSame('tyumen', Helpers::current_town()); } public function test_current_town_returns_null_when_uri_does_not_match_and_no_filter(): void { $_SERVER['REQUEST_URI'] = '/some-random-page/'; \Brain\Monkey\Functions\expect('get_terms') ->andReturn([10 => 'spb', 11 => 'tyumen']); self::assertNull(Helpers::current_town()); } public function test_current_town_uses_main_town_slug_filter_when_uri_has_no_match(): void { $_SERVER['REQUEST_URI'] = '/'; \Brain\Monkey\Functions\expect('get_terms') ->andReturn([10 => 'spb', 11 => 'tyumen']); \Brain\Monkey\Filters\expectApplied('wpmc_main_town_slug') ->once() ->with(null) ->andReturn('spb'); self::assertSame('spb', Helpers::current_town()); } public function test_current_town_uri_match_wins_over_main_town_filter(): void { $_SERVER['REQUEST_URI'] = '/tyumen/'; \Brain\Monkey\Functions\expect('get_terms') ->andReturn([10 => 'spb', 11 => 'tyumen']); // main_town filter should not be consulted when URI matches. \Brain\Monkey\Filters\expectApplied('wpmc_main_town_slug')->never(); self::assertSame('tyumen', Helpers::current_town()); } public function test_current_town_slug_filter_overrides_final_value(): void { $_SERVER['REQUEST_URI'] = '/'; \Brain\Monkey\Functions\expect('get_terms') ->andReturn([]); \Brain\Monkey\Filters\expectApplied('wpmc_current_town_slug') ->once() ->andReturn('override-slug'); self::assertSame('override-slug', Helpers::current_town()); } public function test_current_town_memoizes_within_request(): void { $_SERVER['REQUEST_URI'] = '/tyumen/'; \Brain\Monkey\Functions\expect('get_terms') ->once() ->andReturn([10 => 'spb', 11 => 'tyumen']); Helpers::current_town(); Helpers::current_town(); Helpers::current_town(); self::assertTrue(true); } public function test_alt_get_field_applies_do_shortcode_to_string_values(): void { // expect() must come before when('function_exists') so Brain Monkey's // FunctionStub::__construct can eval-define get_field() before the shim // fakes function_exists('get_field') === true. \Brain\Monkey\Functions\expect('get_field') ->once() ->with('headline', 5, true) ->andReturn('Hello [bar]'); \Brain\Monkey\Functions\expect('do_shortcode') ->once() ->with('Hello [bar]') ->andReturn('Hello World'); \Brain\Monkey\Functions\when('function_exists')->alias(function ($name) { return $name === 'get_field'; }); self::assertSame('Hello World', Helpers::alt_get_field('headline', 5)); } public function test_alt_get_field_returns_arrays_as_is(): void { // expect() before when('function_exists') to avoid FunctionStub seeing // the shim and skipping the eval-declaration of get_field. // do_shortcode is intentionally NOT registered: if the implementation // calls it for a non-string, Brain Monkey throws MissingFunctionExpectations. \Brain\Monkey\Functions\expect('get_field') ->once() ->andReturn(['a', 'b']); \Brain\Monkey\Functions\when('function_exists')->alias(fn($n) => $n === 'get_field'); self::assertSame(['a', 'b'], Helpers::alt_get_field('list')); } public function test_alt_get_field_post_id_filter_overrides_caller_id(): void { \Brain\Monkey\Functions\expect('get_field') ->once() ->with('title', 99, true) ->andReturn(null); \Brain\Monkey\Functions\when('function_exists')->alias(fn($n) => $n === 'get_field'); \Brain\Monkey\Filters\expectApplied('wpmc_alt_get_field_post_id') ->once() ->with(5, 'title') ->andReturn(99); Helpers::alt_get_field('title', 5); self::assertTrue(true); } public function test_alt_get_field_returns_null_when_acf_get_field_missing(): void { \Brain\Monkey\Functions\when('function_exists')->justReturn(false); // get_field must never be called; not registering it means Brain Monkey // throws if it is, which is stronger than ->never(). self::assertNull(Helpers::alt_get_field('anything')); } public function test_remove_domain_link_handles_various_url_shapes(): void { self::assertSame('/foo/bar/', Helpers::remove_domain_link('https://site.com/foo/bar/')); self::assertSame('/foo?q=1#a', Helpers::remove_domain_link('http://site.com/foo?q=1#a')); self::assertSame('/foo/', Helpers::remove_domain_link('/foo/')); self::assertSame('mailto:x@y.com', Helpers::remove_domain_link('mailto:x@y.com')); self::assertSame('not a url', Helpers::remove_domain_link('not a url')); } public function test_wpmc_procedural_functions_proxy_to_helpers(): void { require_once dirname(__DIR__) . '/includes/wpmc-functions.php'; self::assertTrue(function_exists('wpmc_current_town')); self::assertTrue(function_exists('wpmc_get_term_by_slug')); self::assertTrue(function_exists('wpmc_alt_get_field')); self::assertTrue(function_exists('wpmc_remove_domain_link')); // remove_domain_link is pure (no WP function dependencies) — exercise the proxy. self::assertSame('/foo/', wpmc_remove_domain_link('https://site.com/foo/')); } public function test_main_town_slug_reads_option_via_acf(): void { \Brain\Monkey\Functions\expect('get_field') ->once() ->with('main_town_slug', 'option') ->andReturn('spb'); self::assertSame('spb', Helpers::main_town_slug()); } public function test_main_town_slug_returns_null_when_option_empty(): void { \Brain\Monkey\Functions\expect('get_field') ->once() ->with('main_town_slug', 'option') ->andReturn(false); self::assertNull(Helpers::main_town_slug()); } public function test_is_routable_path_returns_true_when_no_exclusions(): void { \Brain\Monkey\Functions\expect('get_field') ->once() ->with('page_no_rep', 'option') ->andReturn(false); self::assertTrue(Helpers::is_routable_path('/foo/bar/')); } public function test_is_routable_path_returns_false_when_path_matches_exclusion(): void { \Brain\Monkey\Functions\expect('get_field') ->once() ->with('page_no_rep', 'option') ->andReturn([ ['link' => '/privacy/'], ['link' => '/terms/'], ]); self::assertFalse(Helpers::is_routable_path('/privacy/')); } public function test_is_routable_path_uses_request_uri_when_path_null(): void { $_SERVER['REQUEST_URI'] = '/terms/'; \Brain\Monkey\Functions\expect('get_field') ->once() ->with('page_no_rep', 'option') ->andReturn([['link' => '/terms/']]); self::assertFalse(Helpers::is_routable_path()); } }