You are here

class LayoutClassesPluginDefinitionFormatTest in Layout Section Classes 8

@coversDefaultClass \Drupal\layout_section_classes\ClassyLayout @group layout_section_classes

Hierarchy

Expanded class hierarchy of LayoutClassesPluginDefinitionFormatTest

File

tests/src/Unit/LayoutClassesPluginDefinitionFormatTest.php, line 16

Namespace

Drupal\Tests\layout_section_classes\Unit
View source
class LayoutClassesPluginDefinitionFormatTest extends UnitTestCase {

  /**
   * @covers ::build
   */
  public function testClassesBuild() {
    $configuration = [
      'additional' => [
        'classes' => [
          'foo' => [
            'class_1',
            'class_2',
          ],
          'bar' => 'class_3',
          'baz' => '',
          'qux' => [
            '',
          ],
        ],
      ],
    ];
    $plugin = new ClassyLayout($configuration, 'foo_plugin', new LayoutDefinition([]));
    $build = $plugin
      ->build([]);
    $this
      ->assertEquals([
      'class_1',
      'class_2',
      'class_3',
    ], $build['#attributes']['class']);
  }

  /**
   * Test the plugin definition parsing into a form.
   *
   * @covers ::buildConfigurationForm
   * @dataProvider definitionFormGenerationTestCases
   */
  public function testDefinitionFormGeneration($definition, $configuration, $expected_form) {
    $plugin = new ClassyLayout($configuration, 'foo_plugin', $definition);
    $plugin
      ->setStringTranslation($this
      ->prophesize(TranslationManager::class)
      ->reveal());
    $form = $plugin
      ->buildConfigurationForm([], new FormState());
    array_walk_recursive($form, function (&$value) {
      if ($value instanceof TranslatableMarkup) {
        $value = $value
          ->getUntranslatedString();
      }
    });
    unset($form['label']);
    $this
      ->assertEquals($expected_form, $form);
  }

  /**
   * Test cases for ::testDefinitionParsing.
   */
  public function definitionFormGenerationTestCases() {
    return [
      'Standard plugin' => [
        new LayoutDefinition([
          'label' => 'Two column',
          'classes' => [
            'style' => [
              'label' => 'Style',
              'description' => 'Select the style that will be applied to this region.',
              'multiple' => FALSE,
              'required' => FALSE,
              'options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
            ],
            'spacing' => [
              'label' => 'Spacing',
              'multiple' => FALSE,
              'required' => TRUE,
              'default' => 'section--bottom-l section--top-l',
              'options' => [
                'section--bottom-l section--top-l' => 'Standard',
                '' => 'Tight',
              ],
            ],
          ],
        ]),
        [
          'additional' => [
            'classes' => [
              'style' => [],
              'spacing' => [],
            ],
          ],
        ],
        [
          'classes' => [
            '#type' => 'container',
            '#tree' => TRUE,
            'style' => [
              '#title' => 'Style',
              '#type' => 'select',
              '#multiple' => FALSE,
              '#options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
              '#required' => FALSE,
              '#default_value' => [],
              '#description' => 'Select the style that will be applied to this region.',
              '#empty_option' => '- Select -',
            ],
            'spacing' => [
              '#title' => 'Spacing',
              '#type' => 'select',
              '#multiple' => FALSE,
              '#options' => [
                'section--bottom-l section--top-l' => 'Standard',
                '' => 'Tight',
              ],
              '#required' => TRUE,
              '#default_value' => [],
              '#description' => '',
            ],
          ],
        ],
      ],
      'Minimal plugin' => [
        new LayoutDefinition([
          'label' => 'Two column',
          'classes' => [
            'style' => [
              'options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
            ],
          ],
        ]),
        [],
        [
          'classes' => [
            '#type' => 'container',
            '#tree' => TRUE,
            'style' => [
              '#title' => 'Classes',
              '#type' => 'select',
              '#multiple' => FALSE,
              '#options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
              '#required' => FALSE,
              '#default_value' => NULL,
              '#description' => '',
              '#empty_option' => '- Select -',
            ],
          ],
        ],
      ],
      'Mixed string array configuration' => [
        new LayoutDefinition([
          'label' => 'Two column',
          'classes' => [
            'style' => [
              'multiple' => FALSE,
              'options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
            ],
            'style_2' => [
              'multiple' => TRUE,
              'options' => [
                'foo' => 'Foo',
                'bar' => 'Bar',
              ],
            ],
          ],
        ]),
        [
          'additional' => [
            'classes' => [
              'style' => 'background--primary-light',
              'style_2' => [
                'foo',
                'bar',
              ],
            ],
          ],
        ],
        [
          'classes' => [
            '#type' => 'container',
            '#tree' => TRUE,
            'style' => [
              '#title' => 'Classes',
              '#type' => 'select',
              '#multiple' => FALSE,
              '#options' => [
                'background--primary-light' => 'Light background',
                'background--wave-dark background--primary-light' => 'Wave background',
              ],
              '#required' => FALSE,
              '#default_value' => 'background--primary-light',
              '#description' => '',
              '#empty_option' => '- Select -',
            ],
            'style_2' => [
              '#title' => 'Classes',
              '#type' => 'select',
              '#multiple' => TRUE,
              '#options' => [
                'foo' => 'Foo',
                'bar' => 'Bar',
              ],
              '#required' => FALSE,
              '#default_value' => [
                'foo',
                'bar',
              ],
              '#description' => '',
              '#empty_option' => '- Select -',
            ],
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutClassesPluginDefinitionFormatTest::definitionFormGenerationTestCases public function Test cases for ::testDefinitionParsing.
LayoutClassesPluginDefinitionFormatTest::testClassesBuild public function @covers ::build
LayoutClassesPluginDefinitionFormatTest::testDefinitionFormGeneration public function Test the plugin definition parsing into a form.
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