Skip to content

test: Search PHPUnit tests#213

Open
Kallyan01 wants to merge 26 commits into
mainfrom
test/search-phpunit-tests
Open

test: Search PHPUnit tests#213
Kallyan01 wants to merge 26 commits into
mainfrom
test/search-phpunit-tests

Conversation

@Kallyan01

@Kallyan01 Kallyan01 commented May 8, 2026

Copy link
Copy Markdown
Collaborator

What

Added Search Module PHPUnit test cases.

Why

Improves development workflow by enabling faster and more reliable validation of REST functionality, reducing repetitive manual testing efforts.

Related Issue(s):

How

AI Disclosure

Hybrid approach = Written by Me + Copilot (Claude Opus 4.6, GPT 5.3) + Audited by me

Testing Instructions

npm run wp-env:test -- start --xdebug=coverage    

npm run test:php -- tests/php/Unit/Modules/Search

Screenshots

Additional Info

Checklist

  • I have read the Contribution Guidelines.
  • I have read the Development Guidelines.
  • I have added necessary tests to cover my changes.
  • I have updated the project documentation as needed.
  • My code has detailed inline documentation.
  • My code is tested to the best of my abilities.
  • My code passes all lints, tests, and checks.
Open WordPress Playground Preview

Copilot AI review requested due to automatic review settings May 8, 2026 06:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds PHPUnit unit tests for the Search module’s WordPress integration points and for the Algolia Index wrapper, helping validate hook registration and several “remote post” mapping behaviors.

Changes:

  • Add SearchTest covering hook registration and selected behaviors for permalinks, author fields, term links, and rendered block rewriting.
  • Add IndexTest covering error returns when prerequisites are missing and verifying Index::get_index() caching behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
tests/php/Unit/Modules/Search/SearchTest.php Introduces unit tests for OneSearch\Modules\Search\Search behaviors (hooks, mapping, block output).
tests/php/Unit/Modules/Search/IndexTest.php Introduces unit tests for OneSearch\Modules\Search\Index error handling and caching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/php/Unit/Modules/Search/SearchTest.php Outdated
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
Comment thread tests/php/Unit/Modules/Search/SearchTest.php Outdated
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
Comment thread tests/php/Unit/Modules/Search/IndexTest.php

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread tests/php/Unit/Modules/Search/SettingsTest.php
Comment thread tests/php/Unit/Modules/Search/WatcherTest.php Outdated

@sabbir1991 sabbir1991 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review the coverage HTML again and check the functions are fully covered which test you have written

Comment thread tests/php/Unit/Modules/Search/IndexTest.php
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
@Kallyan01 Kallyan01 requested a review from sabbir1991 May 12, 2026 13:13
sabbir1991
sabbir1991 previously approved these changes May 12, 2026

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great first pass! 🙌 Especially a fan of daab8dc before I even asked ;-)

Just some minor cleanup and trying to cover the few areas the coverage report missed. We'll be refactoring soon though, so if something I asked for isn't easily testable, lmk and maybe we'll skip it.

Comment thread tests/php/Unit/Modules/Search/IndexTest.php Outdated
Comment thread tests/php/Unit/Modules/Search/IndexTest.php
Comment thread tests/php/Unit/Modules/Search/SearchTest.php Outdated
Comment thread tests/php/Unit/Modules/Search/SearchTest.php Outdated
Comment thread tests/php/Unit/Modules/Search/SearchTest.php
Comment thread tests/php/Unit/Modules/Search/SettingsTest.php
Comment thread tests/php/Unit/Modules/Search/SettingsTest.php
Comment thread tests/php/Unit/Modules/Search/WatcherTest.php
@Kallyan01 Kallyan01 requested a review from justlevine May 21, 2026 20:27
Comment thread tests/php/Unit/Modules/Search/IndexTest.php

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good!

I'm wondering if it's worth it for you to do one more pass through the coverage report, and see if there's some more low-hanging you can catch here, e.g. from a brand site, w. author info, composite scores, > 100 posts, etc. Still outcome based, nothing that requires elaborate stubbing.

Alternatively, we can skip them now add the coverage to the areas that we refactor, the missing areas aren't particularly important for back-compat, just catching unintentional breaks...

@Kallyan01 can you take a look at the report and let me know which - if any - you think are worth doing now vs later?

@Kallyan01

Copy link
Copy Markdown
Collaborator Author

These look good!

I'm wondering if it's worth it for you to do one more pass through the coverage report, and see if there's some more low-hanging you can catch here, e.g. from a brand site, w. author info, composite scores, > 100 posts, etc. Still outcome based, nothing that requires elaborate stubbing.

Alternatively, we can skip them now add the coverage to the areas that we refactor, the missing areas aren't particularly important for back-compat, just catching unintentional breaks...

@Kallyan01 can you take a look at the report and let me know which - if any - you think are worth doing now vs later?

Sure, I've started looking into it, I'll let you know if I find something.

@Kallyan01

Copy link
Copy Markdown
Collaborator Author

@justlevine I think the key public methods are already well covered. If needed, we can add additional tests to cover the logic implemented in the private methods as well, like compute_algolia_score, extract_algolia_highlights, build_post_from_record, split_content_into_chunks, etc. Let me know your thoughts.

@Kallyan01 Kallyan01 requested a review from Utsav-Ladani June 9, 2026 07:37

@Utsav-Ladani Utsav-Ladani left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI checks are failing due to GH token issue else LGTM.

Comment thread tests/php/TestCase.php
@Kallyan01

Copy link
Copy Markdown
Collaborator Author

@justlevine I think the key public methods are already well covered. If needed, we can add additional tests to cover the logic implemented in the private methods as well, like compute_algolia_score, extract_algolia_highlights, build_post_from_record, split_content_into_chunks, etc. Let me know your thoughts.

cc @justlevine

Comment on lines +32 to +75

/**
* Ensures register_hooks adds expected actions.
*/
public function test_register_hooks_adds_expected_actions(): void {
$settings = new Search_Settings();
$settings->register_hooks();

$this->assertNotFalse( has_action( 'admin_init', [ $settings, 'register_settings' ] ) );
$this->assertNotFalse( has_action( 'rest_api_init', [ $settings, 'register_settings' ] ) );
}

/**
* Ensures register_hooks listens to site_type, shared_sites option updates.
*/
public function test_register_hooks_listens_to_site_type_changes(): void {
$settings = new Search_Settings();
$settings->register_hooks();

$this->assertNotFalse(
has_action( 'update_option_' . Settings::OPTION_SITE_TYPE, [ $settings, 'on_site_type_change' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Settings::OPTION_GOVERNING_SHARED_SITES, [ $settings, 'on_shared_sites_change' ] )
);
}

/**
* Ensures register_hooks sets up cache purge hooks.
*/
public function test_register_hooks_sets_up_cache_purge_actions(): void {
$settings = new Search_Settings();
$settings->register_hooks();

$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_ALGOLIA_CREDENTIALS, [ $settings, 'purge_cache_on_update' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_INDEXABLE_SITES, [ $settings, 'purge_cache_on_update' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_SEARCH_SETTINGS, [ $settings, 'purge_cache_on_update' ] )
);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the action is only conditionally enqueued, we never need to test that add_action() or add_filter() work. Checking if specific functions are hooked is fragile and unnecessary.

Suggested change
/**
* Ensures register_hooks adds expected actions.
*/
public function test_register_hooks_adds_expected_actions(): void {
$settings = new Search_Settings();
$settings->register_hooks();
$this->assertNotFalse( has_action( 'admin_init', [ $settings, 'register_settings' ] ) );
$this->assertNotFalse( has_action( 'rest_api_init', [ $settings, 'register_settings' ] ) );
}
/**
* Ensures register_hooks listens to site_type, shared_sites option updates.
*/
public function test_register_hooks_listens_to_site_type_changes(): void {
$settings = new Search_Settings();
$settings->register_hooks();
$this->assertNotFalse(
has_action( 'update_option_' . Settings::OPTION_SITE_TYPE, [ $settings, 'on_site_type_change' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Settings::OPTION_GOVERNING_SHARED_SITES, [ $settings, 'on_shared_sites_change' ] )
);
}
/**
* Ensures register_hooks sets up cache purge hooks.
*/
public function test_register_hooks_sets_up_cache_purge_actions(): void {
$settings = new Search_Settings();
$settings->register_hooks();
$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_ALGOLIA_CREDENTIALS, [ $settings, 'purge_cache_on_update' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_INDEXABLE_SITES, [ $settings, 'purge_cache_on_update' ] )
);
$this->assertNotFalse(
has_action( 'update_option_' . Search_Settings::OPTION_GOVERNING_SEARCH_SETTINGS, [ $settings, 'purge_cache_on_update' ] )
);
}
/**
* Ensures register_hooks adds expected actions.
*/
public function test_class_instantiation(): void {
$settings = new Search_Settings();
$settings->register_hooks();
// If we made it here without erroring, we're good.
$this->assertTrue( true );
}

Comment on lines +35 to +43
/**
* Ensures register_hooks adds transition_post_status action.
*/
public function test_register_hooks_adds_transition_post_status_action(): void {
$watcher = new Watcher();
$watcher->register_hooks();

$this->assertNotFalse( has_action( 'transition_post_status', [ $watcher, 'on_post_transition' ] ) );
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Ensures register_hooks adds transition_post_status action.
*/
public function test_register_hooks_adds_transition_post_status_action(): void {
$watcher = new Watcher();
$watcher->register_hooks();
$this->assertNotFalse( has_action( 'transition_post_status', [ $watcher, 'on_post_transition' ] ) );
}
/**
* Ensures the class is initialized correctly
*/
public function test_class_instantiation(): void {
$watcher = new Watcher();
$watcher->register_hooks();
// If we made it this far, we're good.
$this->assertTrue( true );
}

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last two open comments, and this should be good to merge.

If needed, we can add additional tests to cover the logic implemented in the private methods as well, like compute_algolia_score, extract_algolia_highlights, build_post_from_record, split_content_into_chunks, etc. Let me know your thoughts.

I think we've got enough in place for now, especially as its not fully clear to me which of those implementation details will be necessary in the future. We can backfill them as we update Algolia to v4 and our wrappers accordingly. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants