function InputTest::testInputIsValidated

Tests input validation.

File

core/tests/Drupal/KernelTests/Core/Recipe/InputTest.php, line 89

Class

InputTest
Tests Input.

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testInputIsValidated() : void {
  $collector = $this->createMock(InputCollectorInterface::class);
  $collector->expects($this->atLeastOnce())
    ->method('collectValue')
    ->willReturnCallback(function (string $name) {
    return match ($name) {  'create_node_type.node_type' => 'test',
      'input_test.owner' => 'hack',
    
    };
  });
  try {
    $this->recipe->input
      ->collectAll($collector);
    $this->fail('Expected an exception due to validation failure, but none was thrown.');
  } catch (ValidationFailedException $e) {
    $value = $e->getValue();
    $this->assertInstanceOf(TypedDataInterface::class, $value);
    $this->assertSame('hack', $value->getValue());
    $this->assertSame("I don't think you should be owning sites.", (string) $e->getViolations()
      ->get(0)
      ->getMessage());
  }
}

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