Add utility class for request parameter access in a testable way
Feature Description
Using filter_input() is recommended over direct access to superglobals like $_GET or $_POST, however it doesn't allow us to test the behavior like that, since we can't override the initial values.
Let's introduce a simple class that should be used to access request parameters (for the beginning GET and POST should be sufficient) using filter_input(), simply wrapping these calls.
In unit tests, the class instances used in consuming code can then be overridden with mocks so that we can mock request data. This gives us best practices and testability at the same time.
See related discussion.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- There should be a dedicated class for calling PHP's
filter_input()in a wrapped way. - The class instance used should be easily replaceable in any PHPUnit test with an alternative implementation that looks at superglobals directly (required to manually write values to those).
Implementation Brief
- Add a new
Core\Util\Inputclass with a publicfiltermethod, which accepts the same parameters asfilter_inputand usesfilter_inputinternally - Add
Inputas a new optional constructor dependency of theContextclass, which defaults to a newInputinstance- Expose the instance via a new public
inputgetter method
- Expose the instance via a new public
- Add a new
Tests\MutableInputclass (in the tests sources) which extendsCore\Util\Inputand overloads thefiltermethod- This class should use
filter_varinternally, passing the respective superglobal variable to the function based on theINPUT_constant and$variable_namepassed as the first and second arguments, and passing the same filter and options constants tofilter_varas well
- This class should use
- Update all direct references to super globals to use
InputviaContextinstead
Regex to find usages in PHP\$_[A-Z]+
Accessing input from a module or other class that uses Context then becomes:
$this->context->input()->filter( INPUT_GET, 'googlesitekit_variable' );
Changelog entry
- N/A
@aaemnnosttv IB looks great.
Let's go with the $this->context->input()->filter( INPUT_GET, 'googlesitekit_variable' ) variant for now. This requires us to provide only one method on both Context (just to expose the Input instance) and Input (filter() method), and it keeps the signature similar to filter_input() which makes it easy to grasp for folks familiar with that function.
IB updated 👍
IB ✅
This is covered in #916 (same PR as for #905).
Ran set of functional regression for setup using the proxy for different users and google accounts confirming the domain and completing setup. Setup Analytics, Search Console, Ad Sense and PageSpeed Insights. Reset Site Kit and setup again. Deactivated, reactivated. All passed.
Positive setup test below:

Passed QA ✅