Make WordPress Core

Opened 6 days ago

Last modified 6 days ago

#66553 new enhancement

Tests: Add unit tests for plugins_api()

Reported by: hmbashar Owned by:
Priority: normal Milestone: Awaiting Review
Component: Plugins Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

plugins_api() has no direct unit tests, and 17 of its lines are not covered on Codecov. It builds the arguments, asks WordPress.org for the plugin information and handles the errors.

This ticket adds tests for the default arguments, the plugins_api_args, plugins_api and plugins_api_result filters, the request to WordPress.org, a response that is not JSON, an error in the response, and the request that is made again without SSL when the secure request fails.

Change History (1)

This ticket was mentioned in ​PR #15714 on ​WordPress/wordpress-develop by ​@hmbashar.


6 days ago
#1

Summary

Adds tests for plugins_api(), which had no direct tests (17 lines were not covered on Codecov).

  • The arguments are passed to the filters as an object, with the default locale and WordPress version, and with 24 plugins per page for a query only.
  • The plugins_api_args, plugins_api and plugins_api_result filters: a result of plugins_api is returned without a request and is marked as external, except for an error.
  • The request to api.wordpress.org/plugins/info/1.2/, its action, arguments, timeout and user agent, and the response as an object.
  • A response that is not JSON, an error in the response, and a JSON value that is not an array.
  • A failed secure request is made again without SSL, and the failure is reported, except in a JSON request.

Testing

vendor/bin/phpunit --filter 'Admin_Includes_Plugin_Install_PluginsApi_Test::'
vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Admin_Includes_Plugin_Install_PluginsApi_Test::'
vendor/bin/phpcs tests/phpunit/tests/admin/includes/plugin-install/PluginsApi_Test.php

Mutation check: 24 changes to plugins_api() (defaults, conditions, timeout, user agent, SSL retry, error codes and data, the external flag and the filters) were each caught by a test.

The branch that makes one request when SSL is not supported is not tested, because wp_http_supports() depends on the transports of the server. The two SSL tests are skipped when SSL is not supported.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.

Note: See TracTickets for help on using tickets.