Verified Commit e764a353 authored by Dave Long's avatar Dave Long
Browse files

fix: #3526908 Fix issues with ConfigEntityValidationTestBase

By: larowlan
By: annmarysruthy
By: smustgrave
By: dcam
(cherry picked from commit fac47b82)
parent 167d02bc
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
   *
   * @var int
   */
  public $weight = 0;
  public int $weight = 0;

  /**
   * The image style to use.
+38 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\config_test\Kernel;

use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests validation of config_test entities.
 */
#[Group('config_test')]
#[RunTestsInSeparateProcesses]
class ConfigTestValidationTest extends ConfigEntityValidationTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['config_test'];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installConfig('config_test');

    $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
      'id' => 'test',
      'label' => 'test',
    ]);
    $this->entity->save();
  }

}
+22 −5
Original line number Diff line number Diff line
@@ -587,14 +587,27 @@ public function testRequiredPropertyValuesMissing(?array $additional_expected_va
      }

      $this->entity = clone $original_entity;

      try {
        $this->entity->set($property, NULL);
      }
      catch (\TypeError) {
        // If setting the property to NULL causes a TypeError, skip this
        // property as validation is already enforced at the language level.
        continue;
      }

      $expected_validation_errors = in_array($property, $properties_with_optional_values, TRUE)
        ? []
        : [$property => 'This value should not be null.'];

      // @see `type: required_label`
      // @see \Symfony\Component\Validator\Constraints\NotBlank
      if (!$this->isFullyValidatable() && $this->entity->getEntityType()->getKey('label') == $property) {
      if (
        !$this->isFullyValidatable()
        && $this->entity->getEntityType()->getKey('label') == $property
        && $this->entity->getTypedData()->get($property)->getDataDefinition()->getDataType() == 'required_label'
      ) {
        $expected_validation_errors = [$property => 'This value should not be blank.'];
      }

@@ -690,15 +703,19 @@ protected function getPropertiesWithOptionalValues(): array {
    // optional, with the exception of `type: langcode` and
    // `type: required_label`.
    if (!$this->isFullyValidatable()) {
      return array_diff($config_entity_properties, [
      $excepted_properties = [
        // @see `type: langcode`
        // @see \Symfony\Component\Validator\Constraints\NotNull
        'langcode',
        'default_langcode',
      ];
      $label_property = $this->entity->getEntityType()->getKey('label');
      if ($label_property && $this->entity->getTypedData()->get($label_property)->getDataDefinition()->getDataType() == 'required_label') {
        // @see `type: required_label`
        // @see \Symfony\Component\Validator\Constraints\NotBlank
        $this->entity->getEntityType()->getKey('label'),
      ]);
        $excepted_properties[] = $label_property;
      }
      return array_diff($config_entity_properties, $excepted_properties);
    }

    // Otherwise, all properties are required except for those marked
+4 −7
Original line number Diff line number Diff line
@@ -72,18 +72,15 @@ public function testValidate(): void {
    $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
      'id' => 'system',
      'label' => 'foobar',
      // Set weight to be a string which should not validate.
      'weight' => 'very heavy',
      // Set style to be an array which should not validate.
      'style' => [],
    ]);
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $violations = $adapter->validate();
    $this->assertCount(2, $violations);
    $this->assertCount(1, $violations);
    $violation = $violations->get(0);
    $this->assertEquals('This value should be a valid number.', $violation->getMessage());
    $this->assertEquals('weight', $violation->getPropertyPath());
    $violation = $violations->get(1);
    $this->assertEquals('This value should be of the correct primitive type.', $violation->getMessage());
    $this->assertEquals('weight', $violation->getPropertyPath());
    $this->assertEquals('style', $violation->getPropertyPath());
  }

  /**