function ConfigEntityAdapterTest::testValidate

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityAdapterTest::testValidate()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityAdapterTest::testValidate()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityAdapterTest::testValidate()

@covers ::validate

File

core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php, line 64

Class

ConfigEntityAdapterTest
Tests entity adapter for configuration entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidate() : void {
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $violations = $adapter->validate();
    $this->assertEmpty($violations);
    $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',
    ]);
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $violations = $adapter->validate();
    $this->assertCount(2, $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.