function ConfigActionTest::testEntityMethod

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php \Drupal\KernelTests\Core\Config\Action\ConfigActionTest::testEntityMethod()

See also

\Drupal\Core\Config\Action\Plugin\ConfigAction\EntityMethod

File

core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php, line 58

Class

ConfigActionTest
Tests the config action system.

Namespace

Drupal\KernelTests\Core\Config\Action

Code

public function testEntityMethod() : void {
  $this->installConfig('config_test');
  $storage = \Drupal::entityTypeManager()->getStorage('config_test');
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Default', $config_test_entity->getProtectedProperty());
  /** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
  $manager = $this->container
    ->get('plugin.manager.config_action');
  // Call a method action.
  $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', 'config_test.dynamic.dotted.default', 'Test value');
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value', $config_test_entity->getProtectedProperty());
  $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', 'config_test.dynamic.dotted.default', 'Test value 2');
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value 2', $config_test_entity->getProtectedProperty());
  $manager->applyAction('entity_method:config_test.dynamic:concatProtectedProperty', 'config_test.dynamic.dotted.default', [
    'Test value ',
    '3',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value 3', $config_test_entity->getProtectedProperty());
  $manager->applyAction('entity_method:config_test.dynamic:concatProtectedPropertyOptional', 'config_test.dynamic.dotted.default', [
    'Test value ',
    '4',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value 4', $config_test_entity->getProtectedProperty());
  // Test calling an action that has 2 arguments but one is optional with an
  // array value.
  $manager->applyAction('entity_method:config_test.dynamic:concatProtectedPropertyOptional', 'config_test.dynamic.dotted.default', [
    'Test value 5',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value 5', $config_test_entity->getProtectedProperty());
  // Test calling an action that has 2 arguments but one is optional with a
  // non array value.
  $manager->applyAction('entity_method:config_test.dynamic:concatProtectedPropertyOptional', 'config_test.dynamic.dotted.default', 'Test value 6');
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Test value 6', $config_test_entity->getProtectedProperty());
  // Test calling an action that expects no arguments.
  $manager->applyAction('entity_method:config_test.dynamic:defaultProtectedProperty', 'config_test.dynamic.dotted.default', []);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame('Set by method', $config_test_entity->getProtectedProperty());
  $manager->applyAction('entity_method:config_test.dynamic:addToArray', 'config_test.dynamic.dotted.default', 'foo');
  $manager->applyAction('entity_method:config_test.dynamic:addToArray', 'config_test.dynamic.dotted.default', 'bar');
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame([
    'foo',
    'bar',
  ], $config_test_entity->getArrayProperty());
  $manager->applyAction('entity_method:config_test.dynamic:addToArray', 'config_test.dynamic.dotted.default', [
    'a',
    'b',
    'c',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame([
    'foo',
    'bar',
    [
      'a',
      'b',
      'c',
    ],
  ], $config_test_entity->getArrayProperty());
  $manager->applyAction('entity_method:config_test.dynamic:setArray', 'config_test.dynamic.dotted.default', [
    'a',
    'b',
    'c',
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame([
    'a',
    'b',
    'c',
  ], $config_test_entity->getArrayProperty());
  $manager->applyAction('entity_method:config_test.dynamic:setArray', 'config_test.dynamic.dotted.default', [
    [
      'a',
      'b',
      'c',
    ],
    [
      'a',
    ],
  ]);
  /** @var \Drupal\config_test\Entity\ConfigTest $config_test_entity */
  $config_test_entity = $storage->load('dotted.default');
  $this->assertSame([
    [
      'a',
      'b',
      'c',
    ],
    [
      'a',
    ],
  ], $config_test_entity->getArrayProperty());
  $config_test_entity->delete();
  try {
    $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', 'config_test.dynamic.dotted.default', 'Test value');
    $this->fail('Expected exception not thrown');
  } catch (ConfigActionException $e) {
    $this->assertSame('Entity config_test.dynamic.dotted.default does not exist', $e->getMessage());
  }
  // Test custom and default admin labels.
  $this->assertSame('Test configuration append', (string) $manager->getDefinition('entity_method:config_test.dynamic:append')['admin_label']);
  $this->assertSame('Set default name', (string) $manager->getDefinition('entity_method:config_test.dynamic:defaultProtectedProperty')['admin_label']);
}

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