Verified Commit db2e23fd authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3126654 by idimopoulos, smustgrave, borisson_, mglaman, pfrenssen,...

Issue #3126654 by idimopoulos, smustgrave, borisson_, mglaman, pfrenssen, catch: EntityConstraintViolationList::findByCodes is inconsistent

(cherry picked from commit 66aaf44b)
parent b4533402
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -170,6 +170,20 @@ public function filterByFieldAccess(AccountInterface $account = NULL) {
    return $this->filterByFields($filtered_fields);
  }

  /**
   * {@inheritdoc}
   */
  public function findByCodes(string|array $codes): static {
    $violations = [];
    foreach ($this as $violation) {
      if (in_array($violation->getCode(), $codes, TRUE)) {
        $violations[] = $violation;
      }
    }

    return new static($this->getEntity(), $violations);
  }

  /**
   * {@inheritdoc}
   */
+14 −0
Original line number Diff line number Diff line
@@ -47,6 +47,20 @@ public function getByField($field_name);
   */
  public function getByFields(array $field_names);

  /**
   * Filters this violation list by the given error codes.
   *
   * Copied from Symfony parent class
   * \Symfony\Component\Validator\ConstraintViolationList.
   *
   * @param string|string[] $codes
   *   The codes to find.
   *
   * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
   *   A list of violations of the given fields.
   */
  public function findByCodes(string|array $codes): static;

  /**
   * Filters this violation list by the given fields.
   *
+20 −4
Original line number Diff line number Diff line
@@ -75,6 +75,22 @@ public function testFilterByFieldAccessWithCompositeConstraint() {
    $this->assertEquals(array_values(iterator_to_array($constraint_list)), [$violations[2], $violations[3], $violations[4], $violations[5]]);
  }

  /**
   * @covers ::findByCodes
   */
  public function testFindByCodes() {
    $account = $this->prophesize('\Drupal\Core\Session\AccountInterface')->reveal();
    $entity = $this->setupEntity($account);

    $constraint_list = $this->setupConstraintListWithoutCompositeConstraint($entity);
    $violations = iterator_to_array($constraint_list);

    $codes = ['test-code-violation-name', 'test-code-violation2-name'];
    $actual = $constraint_list->findByCodes($codes);
    $this->assertCount(2, $actual);
    $this->assertEquals(iterator_to_array($actual), $violations);
  }

  /**
   * Builds the entity.
   *
@@ -121,11 +137,11 @@ protected function setupConstraintListWithoutCompositeConstraint(FieldableEntity
    $violations = [];

    // Add two violations to two specific fields.
    $violations[] = new ConstraintViolation('test name violation', '', [], '', 'name', 'invalid');
    $violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid');
    $violations[] = new ConstraintViolation('test name violation', '', [], '', 'name', 'invalid', NULL, 'test-code-violation-name');
    $violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid', NULL, 'test-code-violation2-name');

    $violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid');
    $violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid');
    $violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid', NULL, 'test-code-violation-type');
    $violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid', NULL, 'test-code-violation2-type');

    // Add two entity level specific violations.
    $violations[] = new ConstraintViolation('test entity violation', '', [], '', '', 'invalid');