Modify Drupal\Component\Plugin\Context\ContextInterface to allow for unit testing.

Created on 26 November 2014, over 10 years ago
Updated 23 April 2025, 3 months ago

Problem/Motivation

I'm unable to get full coverage for Drupal\Component\Plugin\Context\Context in #2378311: Expand unit testing for Drupal\Component\Plugin\Context\Context β†’ , because the validate() method depends on a static factory method.

It would be much easier to test behavior, and better in general, to depend on an optionally-injected instance of Symfony\Component\Validator\ValidatorInterface.

Here's the method as it exists now:

  public function validate() {
    $validator = Validation::createValidatorBuilder()
      ->getValidator();
    return $validator->validateValue($this->getContextValue(), $this->getConstraints());
  }

I propose a change like this:

  public function validate(ValidatorInterface $validator = NULL) {
    if (NULL === $validator) {
      $validator = Validation::createValidatorBuilder()->getValidator();
    }
    return $validator->validateValue($this->getContextValue(), $this->getConstraints());
  }

This way the behavior of the method is isolated from the implementation of Validation::createValidatorBuilder(), but still allows users of this interface to let Context build a validator for them.

Proposed resolution

Context::validate() is part of Drupal\Component\Plugin\Context\ContextInterface, so this change would be to the interface, not just Context. Context would then implement this change.

Remaining tasks

User interface changes

API changes

Change ContextInterface::validate()'s signature to this:

  /**
   * Validates the set context value.
   *
   * @param Symfony\Component\Validator\ValidatorInterface $validator
   *   (optional) A validator object. If none is supplied, one will be created.
   *
   * @return \Symfony\Component\Validator\ConstraintViolationListInterface
   *   A list of constraint violations. If the list is empty, validation
   *   succeeded.
   */
  public function validate(ValidatorInterface $validator = NULL);
πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

plugin system

Created by

πŸ‡ΊπŸ‡ΈUnited States mile23 Seattle, WA

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024