function EntityMethodConfigActionsTest::testSetNestedProperty

Test setting a nested property on a config entity.

File

core/tests/Drupal/KernelTests/Core/Recipe/EntityMethodConfigActionsTest.php, line 190

Class

EntityMethodConfigActionsTest
@group Recipe

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testSetNestedProperty() : void {
  $this->container
    ->get(ThemeInstallerInterface::class)
    ->install([
    'claro',
  ]);
  $block = $this->placeBlock('local_tasks_block', [
    'theme' => 'claro',
  ]);
  $this->configActionManager
    ->applyAction('setProperties', $block->getConfigDependencyName(), [
    'settings.label' => 'Magic!',
  ]);
  $settings = Block::load($block->id())
    ->get('settings');
  $this->assertSame('Magic!', $settings['label']);
  // If the property is not nested, it should still work.
  $settings['label'] = 'Mundane';
  $this->configActionManager
    ->applyAction('setProperties', $block->getConfigDependencyName(), [
    'settings' => $settings,
  ]);
  $settings = Block::load($block->id())
    ->get('settings');
  $this->assertSame('Mundane', $settings['label']);
  // We can use this to set a scalar property normally.
  $this->configActionManager
    ->applyAction('setProperties', $block->getConfigDependencyName(), [
    'region' => 'highlighted',
  ]);
  $this->assertSame('highlighted', Block::load($block->id())
    ->getRegion());
  // We should get an exception if we try to set a nested value on a property
  // that isn't an array.
  $this->expectException(ConfigActionException::class);
  $this->expectExceptionMessage('The setProperties config action can only set nested values on arrays.');
  $this->configActionManager
    ->applyAction('setProperties', $block->getConfigDependencyName(), [
    'theme.name' => 'stark',
  ]);
}

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