site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Add utility class for request parameter access in a testable way

Open felixarntz opened this issue 7 years ago • 5 comments

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\Input class with a public filter method, which accepts the same parameters as filter_input and uses filter_input internally
  • Add Input as a new optional constructor dependency of the Context class, which defaults to a new Input instance
    • Expose the instance via a new public input getter method
  • Add a new Tests\MutableInput class (in the tests sources) which extends Core\Util\Input and overloads the filter method
    • This class should use filter_var internally, passing the respective superglobal variable to the function based on the INPUT_ constant and $variable_name passed as the first and second arguments, and passing the same filter and options constants to filter_var as well
  • Update all direct references to super globals to use Input via Context instead
    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

felixarntz avatar Nov 20 '19 13:11 felixarntz

@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.

felixarntz avatar Nov 27 '19 07:11 felixarntz

IB updated 👍

aaemnnosttv avatar Nov 27 '19 07:11 aaemnnosttv

IB ✅

felixarntz avatar Nov 27 '19 08:11 felixarntz

This is covered in #916 (same PR as for #905).

felixarntz avatar Nov 27 '19 08:11 felixarntz

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: image

Passed QA ✅

cole10up avatar Dec 12 '19 06:12 cole10up