You are here

class ConfigActionsTransformTest in Config Actions 8

test the ConfigActionsTransform class

@coversDefaultClass \Drupal\config_actions\ConfigActionsTransform @group config_actions

Hierarchy

Expanded class hierarchy of ConfigActionsTransformTest

File

tests/src/Unit/ConfigActionsTransformTest.php, line 15

Namespace

Drupal\Tests\config_actions\Unit
View source
class ConfigActionsTransformTest extends UnitTestCase {

  /**
   * @covers ::replace
   */
  public function testReplace() {
    $source = Yaml::decode(file_get_contents(dirname(__FILE__) . "/node.type.page.yml"));
    $replace = [
      "needs_review" => "approver",
      "workbench" => "workflow",
      "@bundle@" => "page",
    ];
    $output = ConfigActionsTransform::replace($source, $replace);
    $new_source = $source;
    $new_source['dependencies']['module'][1] = 'workflow_moderation';

    // 'workbench' only replaced with 'workflow' in values
    $new_source['third_party_settings']['workbench_moderation']['allowed_moderation_states'][2] = 'approver';
    $new_source['type'] = 'page';
    self::assertEquals($new_source, $output);
    $items = [
      "workbench_moderation",
      "state_needs_review",
      "unaltered",
    ];
    $output = ConfigActionsTransform::replace($items, $replace);
    self::assertEquals([
      "workflow_moderation",
      "state_approver",
      "unaltered",
    ], $output);

    // Now test replacing 'workbench' with 'workflow' in keys.
    $replace_keys = [
      'workbench' => 'workflow',
    ];
    $output = ConfigActionsTransform::replace($source, $replace, $replace_keys);
    $value = $new_source['third_party_settings']['workbench_moderation'];
    $new_source['third_party_settings']['workflow_moderation'] = $value;
    unset($new_source['third_party_settings']['workbench_moderation']);
    self::assertEquals($new_source, $output);
  }

  /**
   * @covers ::add
   */
  public function testAdd() {
    $source = Yaml::decode(file_get_contents(dirname(__FILE__) . "/node.type.page.yml"));
    $path = [
      "third_party_settings",
      "workbench_moderation",
      "test",
    ];
    $value = [
      "myvalue" => 123,
    ];
    $output = ConfigActionsTransform::add($source, $path, $value);
    $source['third_party_settings']['workbench_moderation']['test'] = $value;
    self::assertEquals($source, $output);
  }

  /**
   * @covers ::read
   */
  public function testRead() {
    $source = Yaml::decode(file_get_contents(dirname(__FILE__) . "/node.type.page.yml"));
    $path = [
      "dependencies",
      "module",
    ];
    $output = ConfigActionsTransform::read($source, $path);
    self::assertEquals([
      'menu_ui',
      'workbench_moderation',
    ], $output);
  }

  /**
   * @covers ::change
   */
  public function testChange() {
    $source = Yaml::decode(file_get_contents(dirname(__FILE__) . "/node.type.page.yml"));
    $path = [
      "third_party_settings",
      "workbench_moderation",
      "enabled",
    ];
    $output = ConfigActionsTransform::change($source, $path, FALSE);
    $source['third_party_settings']['workbench_moderation']['enabled'] = FALSE;
    self::assertEquals($source, $output);
    $output = ConfigActionsTransform::change($source, $path, 123);
    $source['third_party_settings']['workbench_moderation']['enabled'] = 123;
    self::assertEquals($source, $output);
    $output = ConfigActionsTransform::change($source, $path, 'test');
    $source['third_party_settings']['workbench_moderation']['enabled'] = 'test';
    self::assertEquals($source, $output);
    $output = ConfigActionsTransform::change($source, $path, [
      'my_key' => 'test',
    ]);
    $source['third_party_settings']['workbench_moderation']['enabled'] = [
      'my_key' => 'test',
    ];
    self::assertEquals($source, $output);
  }

  /**
   * @covers ::delete
   */
  public function testDelete() {
    $source = Yaml::decode(file_get_contents(dirname(__FILE__) . "/node.type.page.yml"));
    $output = ConfigActionsTransform::delete($source, [
      "dependencies",
      "module",
    ], true);
    unset($source['dependencies']);
    self::assertEquals($source, $output);
    $output = ConfigActionsTransform::delete($source, [
      'description',
    ], true);
    $new_source = $source;
    unset($new_source['description']);
    self::assertEquals($new_source, $output);

    // Test clearing string value without pruning.
    $output = ConfigActionsTransform::delete($source, [
      'description',
    ]);
    $new_source = $source;
    $new_source['description'] = '';
    self::assertEquals($new_source, $output);
  }

  /**
   * @covers ::parseWildcards
   */
  public function testParseWildcards() {
    $replace = [
      '@name@' => 'old name',
      '@type@' => 'old type',
      '@other@' => 'existing',
    ];

    // Directly testing the code used in ConfigActionsPluginBase to merge
    // existing replacements with parsed key wildcards.
    $replace = array_merge($replace, ConfigActionsTransform::parseWildcards('@name@.@type@', 'hello.world'));
    $this
      ->assertEquals('hello', $replace['@name@']);
    $this
      ->assertEquals('world', $replace['@type@']);
    $this
      ->assertEquals('existing', $replace['@other@']);
    $result = ConfigActionsTransform::parseWildcards('blog.@name@.@type@', 'hello.world');
    $this
      ->assertEquals([], $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigActionsTransformTest::testAdd public function @covers ::add
ConfigActionsTransformTest::testChange public function @covers ::change
ConfigActionsTransformTest::testDelete public function @covers ::delete
ConfigActionsTransformTest::testParseWildcards public function @covers ::parseWildcards
ConfigActionsTransformTest::testRead public function @covers ::read
ConfigActionsTransformTest::testReplace public function @covers ::replace
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340