Commit c5a3011f authored by Ben Mullins's avatar Ben Mullins
Browse files

Issue #3349293 by Wim Leers, phenaproxima, smustgrave, longwave, bnjmnm,...

Issue #3349293 by Wim Leers, phenaproxima, smustgrave, longwave, bnjmnm, borisson_: Make assertions using ConfigEntityValidationTestBase::assertValidationErrors() clearer
parent 0b49065c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ protected function setUp(): void {
   */
  public function testInvalidPluginId(): void {
    $this->entity->set('plugin', 'non_existent');
    $this->assertValidationErrors(["The 'non_existent' plugin does not exist."]);
    $this->assertValidationErrors(['plugin' => "The 'non_existent' plugin does not exist."]);
  }

}
+5 −5
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ public function testInvalidDependencies(): void {
    $dependencies['config'] = [];
    $this->entity->set('dependencies', $dependencies);

    $this->assertValidationErrors(['This text editor requires a text format.']);
    $this->assertValidationErrors(['' => 'This text editor requires a text format.']);

    // Things look sort-of like `filter.format.*` should fail validation
    // because they don't exist.
@@ -56,9 +56,9 @@ public function testInvalidDependencies(): void {
    ];
    $this->entity->set('dependencies', $dependencies);
    $this->assertValidationErrors([
      'This text editor requires a text format.',
      "The 'filter.format' config does not exist.",
      "The 'filter.format.' config does not exist.",
      '' => 'This text editor requires a text format.',
      'dependencies.config.0' => "The 'filter.format' config does not exist.",
      'dependencies.config.1' => "The 'filter.format.' config does not exist.",
    ]);
  }

@@ -67,7 +67,7 @@ public function testInvalidDependencies(): void {
   */
  public function testInvalidPluginId(): void {
    $this->entity->setEditor('non_existent');
    $this->assertValidationErrors(["The 'non_existent' plugin does not exist."]);
    $this->assertValidationErrors(['editor' => "The 'non_existent' plugin does not exist."]);
  }

}
+4 −4
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public function testInvalidDependencies(): void {
    $dependencies['config'] = [];
    $this->entity->set('dependencies', $dependencies);

    $this->assertValidationErrors(['This field requires a field storage.']);
    $this->assertValidationErrors(['' => 'This field requires a field storage.']);

    // Things look sort-of like `field.storage.*.*` should fail validation
    // because they don't exist.
@@ -47,9 +47,9 @@ public function testInvalidDependencies(): void {
    ];
    $this->entity->set('dependencies', $dependencies);
    $this->assertValidationErrors([
      "The 'field.storage.fake' config does not exist.",
      "The 'field.storage.' config does not exist.",
      "The 'field.storage.user.' config does not exist.",
      'dependencies.config.0' => "The 'field.storage.fake' config does not exist.",
      'dependencies.config.1' => "The 'field.storage.' config does not exist.",
      'dependencies.config.2' => "The 'field.storage.user.' config does not exist.",
    ]);
  }

+52 −21
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ public function providerConfigDependenciesValidation(): array {
          'fun_stuff' => ['star-trek.deep-space-nine'],
        ],
        [
          "'fun_stuff' is not a supported key.",
          'dependencies' => "'fun_stuff' is not a supported key.",
        ],
      ],
      'empty string in config dependencies' => [
@@ -69,16 +69,18 @@ public function providerConfigDependenciesValidation(): array {
          'config' => [''],
        ],
        [
          'dependencies.config.0' => [
            'This value should not be blank.',
            "The '' config does not exist.",
          ],
        ],
      ],
      'non-existent config dependency' => [
        [
          'config' => ['fake_settings'],
        ],
        [
          "The 'fake_settings' config does not exist.",
          'dependencies.config.0' => "The 'fake_settings' config does not exist.",
        ],
      ],
      'empty string in module dependencies' => [
@@ -86,25 +88,29 @@ public function providerConfigDependenciesValidation(): array {
          'module' => [''],
        ],
        [
          'dependencies.module.0' => [
            'This value should not be blank.',
            "Module '' is not installed.",
          ],
        ],
      ],
      'invalid module dependency' => [
        [
          'module' => ['invalid-module-name'],
        ],
        [
          'dependencies.module.0' => [
            'This value is not valid.',
            "Module 'invalid-module-name' is not installed.",
          ],
        ],
      ],
      'non-installed module dependency' => [
        [
          'module' => ['bad_judgment'],
        ],
        [
          "Module 'bad_judgment' is not installed.",
          'dependencies.module.0' => "Module 'bad_judgment' is not installed.",
        ],
      ],
      'empty string in theme dependencies' => [
@@ -112,25 +118,29 @@ public function providerConfigDependenciesValidation(): array {
          'theme' => [''],
        ],
        [
          'dependencies.theme.0' => [
            'This value should not be blank.',
            "Theme '' is not installed.",
          ],
        ],
      ],
      'invalid theme dependency' => [
        [
          'theme' => ['invalid-theme-name'],
        ],
        [
          'dependencies.theme.0' => [
            'This value is not valid.',
            "Theme 'invalid-theme-name' is not installed.",
          ],
        ],
      ],
      'non-installed theme dependency' => [
        [
          'theme' => ['ugly_theme'],
        ],
        [
          "Theme 'ugly_theme' is not installed.",
          'dependencies.theme.0' => "Theme 'ugly_theme' is not installed.",
        ],
      ],
    ];
@@ -141,8 +151,10 @@ public function providerConfigDependenciesValidation(): array {
   *
   * @param array[] $dependencies
   *   The dependencies that should be added to the config entity under test.
   * @param string[] $expected_messages
   *   The expected constraint violation messages.
   * @param array<string, string|string[]> $expected_messages
   *   The expected validation error messages. Keys are property paths, values
   *   are the expected messages: a string if a single message is expected, an
   *   array of strings if multiple are expected.
   *
   * @dataProvider providerConfigDependenciesValidation
   */
@@ -154,7 +166,7 @@ public function testConfigDependenciesValidation(array $dependencies, array $exp

    // Add the dependencies we were given to the dependencies that may already
    // exist in the entity.
    $dependencies = NestedArray::mergeDeep($this->entity->getDependencies(), $dependencies);
    $dependencies = NestedArray::mergeDeep($dependencies, $this->entity->getDependencies());

    $this->entity->set('dependencies', $dependencies);
    $this->assertValidationErrors($expected_messages);
@@ -163,14 +175,23 @@ public function testConfigDependenciesValidation(array $dependencies, array $exp
    $this->entity->set('dependencies', [
      'enforced' => $dependencies,
    ]);
    $this->assertValidationErrors($expected_messages);
    // We now expect validation errors not at `dependencies.module.0`, but at
    // `dependencies.enforced.module.0`. So reuse the same messages, but perform
    // string replacement in the keys.
    $expected_enforced_messages = array_combine(
      str_replace('dependencies', 'dependencies.enforced', array_keys($expected_messages)),
      array_values($expected_messages),
    );
    $this->assertValidationErrors($expected_enforced_messages);
  }

  /**
   * Asserts a set of validation errors is raised when the entity is validated.
   *
   * @param string[] $expected_messages
   *   The expected validation error messages.
   * @param array<string, string|string[]> $expected_messages
   *   The expected validation error messages. Keys are property paths, values
   *   are the expected messages: a string if a single message is expected, an
   *   array of strings if multiple are expected.
   */
  protected function assertValidationErrors(array $expected_messages): void {
    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
@@ -180,7 +201,17 @@ protected function assertValidationErrors(array $expected_messages): void {

    $actual_messages = [];
    foreach ($violations as $violation) {
      $actual_messages[] = (string) $violation->getMessage();
      if (!isset($actual_messages[$violation->getPropertyPath()])) {
        $actual_messages[$violation->getPropertyPath()] = (string) $violation->getMessage();
      }
      else {
        // Transform value from string to array.
        if (is_string($actual_messages[$violation->getPropertyPath()])) {
          $actual_messages[$violation->getPropertyPath()] = (array) $actual_messages[$violation->getPropertyPath()];
        }
        // And append.
        $actual_messages[$violation->getPropertyPath()][] = (string) $violation->getMessage();
      }
    }
    $this->assertSame($expected_messages, $actual_messages);
  }