function ConfigTargetTest::testSingleTarget

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Form/ConfigTargetTest.php \Drupal\Tests\Core\Form\ConfigTargetTest::testSingleTarget()

File

core/tests/Drupal/Tests/Core/Form/ConfigTargetTest.php, line 246

Class

ConfigTargetTest
@coversDefaultClass \Drupal\Core\Form\ConfigTarget @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testSingleTarget() : void {
    $config_target = new ConfigTarget('foo.settings', 'something', fromConfig: fn(bool $something): string => $something ? 'Yes' : 'No', toConfig: fn(string $form_value): ToConfig|bool => match ($form_value) {    'Yes' => TRUE,
        '<test:noop>' => ToConfig::NoOp,
        '<test:delete>' => ToConfig::DeleteKey,
        default => FALSE,
    
    });
    // Assert the logic in the callables works as expected.
    $this->assertSame("Yes", ($config_target->fromConfig)(TRUE));
    $this->assertSame("No", ($config_target->fromConfig)(FALSE));
    $this->assertTrue(($config_target->toConfig)("Yes"));
    $this->assertFalse(($config_target->toConfig)("No"));
    $this->assertFalse(($config_target->toConfig)("some random string"));
    $this->assertSame(ToConfig::NoOp, ($config_target->toConfig)("<test:noop>"));
    $this->assertSame(ToConfig::DeleteKey, ($config_target->toConfig)("<test:delete>"));
    // Now simulate how this will be used in the form, and ensure it results in
    // the expected Config::set() calls.
    $config = $this->prophesize(Config::class);
    $config->getName()
        ->willReturn('foo.settings');
    // First to transform the stored config value to the form value.
    $config->get('something')
        ->willReturn(TRUE);
    $this->assertSame("Yes", $config_target->getValue($config->reveal()));
    // Then to transform the modified form value back to config.
    $config->set('something', TRUE)
        ->shouldBeCalledTimes(1);
    $config_target->setValue($config->reveal(), 'Yes', $this->prophesize(FormStateInterface::class)
        ->reveal());
    // Repeat, but for the other possible value.
    $config->get('something')
        ->willReturn(FALSE);
    $this->assertSame("No", $config_target->getValue($config->reveal()));
    $config->set('something', FALSE)
        ->shouldBeCalledTimes(1);
    $config_target->setValue($config->reveal(), 'No', $this->prophesize(FormStateInterface::class)
        ->reveal());
    // Test `ToConfig::NoOp`: nothing should happen to the Config.
    $config = $this->prophesize(Config::class);
    $config->getName()
        ->willReturn('foo.settings');
    $config->set('something', Argument::any())
        ->shouldBeCalledTimes(0);
    $config->clear('something', Argument::any())
        ->shouldBeCalledTimes(0);
    $config_target->setValue($config->reveal(), '<test:noop>', $this->prophesize(FormStateInterface::class)
        ->reveal());
    // Test `ToConfig::DeleteKey`: Config::clear() should be called.
    $config = $this->prophesize(Config::class);
    $config->getName()
        ->willReturn('foo.settings');
    $config->clear('something')
        ->shouldBeCalledTimes(1);
    $config_target->setValue($config->reveal(), '<test:delete>', $this->prophesize(FormStateInterface::class)
        ->reveal());
}

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