Skip to content

Tests: add unit test coverage for ElasticPressIo and ElasticPressIoTemplateManager - #4370

Open
noruzzamans wants to merge 1 commit into
10up:developfrom
noruzzamans:test/add-elasticpress-io-unit-coverage
Open

noruzzamans wants to merge 1 commit into
10up:developfrom
noruzzamans:test/add-elasticpress-io-unit-coverage

Conversation

@noruzzamans

Copy link
Copy Markdown

Summary

This PR introduces comprehensive automated unit test coverage for ElasticPressIo and the ElasticPressIoTemplateManager trait in 10up/ElasticPress.

These components manage the lifecycle of ElasticPress.io search template registration, remote template synchronization, service availability checks, and caching. Prior to this PR, neither component had dedicated unit tests in tests/php/.

This PR is 100% test-only and introduces zero production code changes, ensuring zero risk of regressions while significantly strengthening the test harness.

Changes

1. tests/php/TestElasticPressIo.php (New File)

Dedicated unit test suite for ElasticPress\ElasticPressIo:

  • test_non_epio_environment_returns_empty():
    • Sets IS_EPIO_ENVIRONMENT=0 to simulate a non-ElasticPress.io environment.
    • Verifies that get_endpoint_status(), get_endpoint_messages(), and get_endpoint_available_services() safely return empty arrays ([]).
    • Verifies that is_service_available('synonyms') returns false without making any unexpected HTTP requests.
  • test_get_endpoint_status_from_transient():
    • Sets IS_EPIO_ENVIRONMENT=1 and seeds set_transient( ElasticPressIo::STATUS_TRANSIENT_NAME, ... ) with structured endpoint responses (operational messages, available services with boolean and string flags).
    • Verifies that status, messages, and available services are accurately deserialized from transient cache.
    • Verifies is_service_available() logic across multiple scenarios: active services return true, explicitly disabled services return false, empty string flags return false, and non-existent services return false.

2. tests/php/TestElasticPressIoTemplateManager.php (New File)

Dedicated unit test suite for the ElasticPress\ElasticPressIoTemplateManager trait using an anonymous class test double:

  • test_get_hook_prefix():
    • Verifies that get_hook_prefix() correctly converts hyphenated feature slugs into underscore-delimited WordPress hook prefixes (e.g. test-feature-slug -> ep_test_feature_slug, woocommerce-orders-autosuggest -> ep_woocommerce_orders_autosuggest).
  • test_epio_save_search_template():
    • Verifies that epio_save_search_template() triggers the {hook_prefix}_template_saved action hook and passes the exact search template JSON body.
  • test_epio_delete_search_template():
    • Verifies that epio_delete_search_template() triggers the {hook_prefix}_template_deleted action hook upon template deletion.
  • test_epio_get_search_template():
    • Case 1 (Remote Error): Intercepts the request using ep_intercept_remote_request / ep_do_intercept_request to simulate a WP_Error and verifies that epio_get_search_template() safely bubbles up the WP_Error.
    • Case 2 (Success): Simulates an HTTP 200 response with a JSON payload and verifies that the retrieved template body is returned intact.
  • test_after_update_feature():
    • Verifies feature slug isolation: feature update events for mismatched slugs do not trigger any save or delete actions.
    • Verifies that activating the feature (['active' => true]) calls epio_save_search_template() and fires the save hook.
    • Verifies that deactivating the feature (['active' => false]) calls epio_delete_search_template() and fires the delete hook.

Testing

1. PHPUnit Execution

./vendor/bin/phpunit tests/php/TestElasticPressIo.php
./vendor/bin/phpunit tests/php/TestElasticPressIoTemplateManager.php

Result: All 7 tests and 25 assertions pass (OK (7 tests, 25 assertions) in < 0.15s).

2. Coding Standards (10up PHPCS Standard)

./vendor/bin/phpcs tests/php/TestElasticPressIo.php tests/php/TestElasticPressIoTemplateManager.php

Result: 0 errors, 0 warnings (100% compliant with 10up PHPCS standards).

@noruzzamans
noruzzamans marked this pull request as ready for review September 13, 2026 06:15

This branch has not been deployed

No deployments
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.

1 participant