function ComponentRenderTest::testRenderNestedPropValidationIncludesCallingTemplate

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php \Drupal\KernelTests\Components\ComponentRenderTest::testRenderNestedPropValidationIncludesCallingTemplate()

Ensures nested invalid props report the calling parent template.

File

core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php, line 165

Class

ComponentRenderTest
Tests the correct rendering of components.

Namespace

Drupal\KernelTests\Components

Code

public function testRenderNestedPropValidationIncludesCallingTemplate() : void {
  // Omit ctaText so my-banner still validates, but nested my-cta does not.
  $build = [
    '#type' => 'component',
    '#component' => 'sdc_test:my-banner',
    '#props' => [
      'heading' => 'I am a banner',
      'ctaHref' => 'https://www.example.org',
      'ctaTarget' => '',
    ],
    '#slots' => [
      'banner_body' => [
        '#plain_text' => 'Banner body',
      ],
    ],
  ];
  try {
    $this->renderComponentRenderArray($build);
    $this->fail('Invalid nested prop did not cause an exception');
  } catch (\Throwable $e) {
    // Find the "InvalidComponentException":
    while ($e && !$e instanceof InvalidComponentException) {
      $e = $e->getPrevious();
    }
    $this->assertInstanceOf(InvalidComponentException::class, $e);
    // Check, that the error message contains the error calling template, as
    // well as the component, whose props actually fail validation:
    $this->assertStringContainsString('Component validation failed in Twig file [sdc_test:my-banner] for component [sdc_test:my-cta]:', $e->getMessage());
  }
}

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