function InputTest::testDefaultValueFromNonExistentConfigWithFallback

Tests getting the fallback default value from non-existing configuration.

@covers \Drupal\Core\Recipe\InputConfigurator::getDefaultValue

File

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

Class

InputTest
@group Recipe @covers \Drupal\Core\Recipe\InputConfigurator[[api-linebreak]]

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testDefaultValueFromNonExistentConfigWithFallback() : void {
  $recipe_data = [
    'name' => 'Default value from non-existent config',
    'input' => [
      'capital' => [
        'data_type' => 'string',
        'description' => 'This will use the fallback value.',
        'default' => [
          'source' => 'config',
          'config' => [
            'foo.baz',
            'bar',
          ],
          'fallback' => 'fallback',
        ],
      ],
    ],
  ];
  $recipe = $this->createRecipe($recipe_data);
  // Mock an input collector that will return the default value.
  $collector = $this->createMock(InputCollectorInterface::class);
  $collector->expects($this->atLeastOnce())
    ->method('collectValue')
    ->withAnyParameters()
    ->willReturnArgument(2);
  $recipe->input
    ->collectAll($collector);
  $this->assertSame([
    'capital' => 'fallback',
  ], $recipe->input
    ->getValues());
  // NULL is an allowable fallback value.
  $recipe_data['input']['capital']['default']['fallback'] = NULL;
  $recipe = $this->createRecipe($recipe_data);
  $recipe->input
    ->collectAll($collector);
  $this->assertSame([
    'capital' => NULL,
  ], $recipe->input
    ->getValues());
  // If there's no fallback value at all, we should get an exception.
  unset($recipe_data['input']['capital']['default']['fallback']);
  $recipe = $this->createRecipe($recipe_data);
  $this->expectException(\RuntimeException::class);
  $this->expectExceptionMessage("The 'foo.baz' config object does not exist.");
  $recipe->input
    ->collectAll($collector);
}

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