You are here

class BlockStyleBaseTest in Block Style Plugins 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Plugin/BlockStyleBaseTest.php \Drupal\Tests\block_style_plugins\Unit\Plugin\BlockStyleBaseTest

@coversDefaultClass \Drupal\block_style_plugins\Plugin\BlockStyleBase @group block_style_plugins

Hierarchy

Expanded class hierarchy of BlockStyleBaseTest

File

tests/src/Unit/Plugin/BlockStyleBaseTest.php, line 17

Namespace

Drupal\Tests\block_style_plugins\Unit\Plugin
View source
class BlockStyleBaseTest extends UnitTestCase {

  /**
   * Mocked entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Mocked form state.
   *
   * @var \Drupal\Core\Form\FormStateInterface
   */
  protected $formState;

  /**
   * Mocked Block Plugin.
   *
   * @var \Drupal\Core\Block\BlockPluginInterface
   */
  protected $blockPlugin;

  /**
   * Instance of the BlockStyleBase plugin.
   *
   * @var \Drupal\block_style_plugins\Plugin\BlockStyleBase
   */
  protected $plugin;

  /**
   * Create the setup for constants and configFactory stub.
   */
  protected function setUp() : void {
    parent::setUp();

    // Stub the Entity Type Manager.
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);

    // Form state double.
    $this->formState = $this
      ->prophesize(FormStateInterface::class);

    // Block plugin.
    $this->blockPlugin = $this
      ->prophesize(BlockPluginInterface::class);
    $this->blockPlugin
      ->getBaseId()
      ->willReturn('block_content');
    $this->blockPlugin
      ->getDerivativeId()
      ->willReturn('uuid-1234');
    $this->blockPlugin
      ->getPluginId()
      ->willReturn('basic_block');
    $configuration = [];
    $plugin_id = 'block_style_plugins';
    $plugin_definition['provider'] = 'block_style_plugins';
    $this->plugin = new MockBlockStyleBase($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager
      ->reveal());

    // Create a translation stub for the t() method.
    $translator = $this
      ->getStringTranslationStub();
    $this->plugin
      ->setStringTranslation($translator);
  }

  /**
   * Tests the create method.
   *
   * @see ::create()
   */
  public function testCreate() {
    $configuration = [];
    $plugin_id = 'block_style_plugins';
    $plugin_definition['provider'] = 'block_style_plugins';
    $container = $this
      ->prophesize(ContainerInterface::class);
    $container
      ->get('entity_type.manager')
      ->willReturn($this->entityTypeManager
      ->reveal());
    $instance = MockBlockStyleBase::create($container
      ->reveal(), $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\Plugin\\BlockStyleInterface', $instance);
  }

  /**
   * Tests the defaultConfiguration method.
   *
   * @see ::defaultConfiguration()
   */
  public function testDefaultConfiguration() {
    $expected = [
      'sample_class' => '',
      'sample_checkbox' => FALSE,
    ];
    $default = $this->plugin
      ->defaultConfiguration();
    $this
      ->assertEquals($expected, $default);
  }

  /**
   * Tests the buildConfigurationForm method.
   *
   * @see ::buildConfigurationForm()
   */
  public function testBuildConfigurationForm() {
    $form = [];
    $return = $this->plugin
      ->buildConfigurationForm($form, $this->formState
      ->reveal());
    $this
      ->assertEquals([], $return);
  }

  /**
   * Tests the build method.
   *
   * @see ::build()
   * @TODO Create a provider so that more combinations can be tested.
   */
  public function testBuild() {
    $block = $this
      ->prophesize(ConfigEntityInterface::class);
    $storage = $this
      ->prophesize(EntityStorageInterface::class);
    $storage
      ->load(1)
      ->willReturn($block
      ->reveal());
    $this->entityTypeManager
      ->getStorage('block')
      ->willReturn($storage
      ->reveal());

    // 1. No element ID is passed through the variables.
    $variables = [];
    $return = $this->plugin
      ->build($variables);
    $this
      ->assertEquals($variables, $return);

    // 2. No styles attached to the block.
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn(FALSE);
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $return = $this->plugin
      ->build($variables);
    $this
      ->assertEquals($variables, $return);

    // 3. Return the third party styles set in the plugin.
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn([
      'class1',
      'class2',
    ]);
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $expected = [
      'elements' => [
        '#id' => 1,
      ],
      'configuration' => [
        'block_styles' => [
          'block_style_plugins' => [
            'class1',
            'class2',
          ],
        ],
      ],
      'attributes' => [
        'class' => [
          'class1',
          'class2',
        ],
      ],
    ];
    $return = $this->plugin
      ->build($variables);
    $this
      ->assertEquals($expected, $return);

    // 4. Multiple values will each be set as a class.
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn([
      [
        'class1',
        'class2',
      ],
    ]);
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $expected = [
      'elements' => [
        '#id' => 1,
      ],
      'configuration' => [
        'block_styles' => [
          'block_style_plugins' => [
            [
              'class1',
              'class2',
            ],
          ],
        ],
      ],
      'attributes' => [
        'class' => [
          'class1',
          'class2',
        ],
      ],
    ];
    $return = $this->plugin
      ->build($variables);
    $this
      ->assertEquals($expected, $return);

    // 5. Don't set a class for integers.
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn([
      'class1',
      1,
      'class2',
      0,
    ]);
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $expected = [
      'elements' => [
        '#id' => 1,
      ],
      'configuration' => [
        'block_styles' => [
          'block_style_plugins' => [
            'class1',
            1,
            'class2',
            0,
          ],
        ],
      ],
      'attributes' => [
        'class' => [
          'class1',
          'class2',
        ],
      ],
    ];
    $return = $this->plugin
      ->build($variables);
    $this
      ->assertEquals($expected, $return);

    // 6. Auto classes are disabled.
    $configuration = [];
    $plugin_id = 'block_style_plugins';
    $plugin_definition = [
      'provider' => 'block_style_plugins',
      'disable_auto_classes' => TRUE,
    ];
    $plugin = new MockBlockStyleBase($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager
      ->reveal());
    $translator = $this
      ->getStringTranslationStub();
    $plugin
      ->setStringTranslation($translator);
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
      ->willReturn([
      'class1',
      1,
      'class2',
      0,
    ]);
    $variables = [
      'elements' => [
        '#id' => 1,
      ],
    ];
    $expected = [
      'elements' => [
        '#id' => 1,
      ],
      'configuration' => [
        'block_styles' => [
          'block_style_plugins' => [
            'class1',
            1,
            'class2',
            0,
          ],
        ],
      ],
    ];
    $return = $plugin
      ->build($variables);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Tests the getConfiguration method.
   *
   * @see ::getConfiguration()
   */
  public function testGetConfiguration() {
    $expected = [
      'sample_class' => '',
      'sample_checkbox' => FALSE,
    ];
    $this->plugin
      ->setConfiguration([]);
    $return = $this->plugin
      ->getConfiguration();
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Tests the setConfiguration method.
   *
   * @see ::setConfiguration()
   */
  public function testSetConfiguration() {
    $expected = [
      'sample_class' => '',
      'sample_checkbox' => FALSE,
      'new_key' => 'new_val',
    ];
    $new_styles = [
      'new_key' => 'new_val',
    ];
    $this->plugin
      ->setConfiguration($new_styles);
    $return = $this->plugin
      ->getConfiguration();
    $this
      ->assertEquals($expected, $return);

    // Overwrite styles.
    $expected = [
      'sample_class' => 'class_name',
      'sample_checkbox' => TRUE,
    ];
    $this->plugin
      ->setConfiguration($expected);
    $return = $this->plugin
      ->getConfiguration();
    $this
      ->assertEquals($expected, $return);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockStyleBaseTest::$blockPlugin protected property Mocked Block Plugin.
BlockStyleBaseTest::$entityTypeManager protected property Mocked entity type manager service.
BlockStyleBaseTest::$formState protected property Mocked form state.
BlockStyleBaseTest::$plugin protected property Instance of the BlockStyleBase plugin.
BlockStyleBaseTest::setUp protected function Create the setup for constants and configFactory stub. Overrides UnitTestCase::setUp
BlockStyleBaseTest::testBuild public function Tests the build method.
BlockStyleBaseTest::testBuildConfigurationForm public function Tests the buildConfigurationForm method.
BlockStyleBaseTest::testCreate public function Tests the create method.
BlockStyleBaseTest::testDefaultConfiguration public function Tests the defaultConfiguration method.
BlockStyleBaseTest::testGetConfiguration public function Tests the getConfiguration method.
BlockStyleBaseTest::testSetConfiguration public function Tests the setConfiguration method.
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.