You are here

BlockStyleBaseTest.php in Block Style Plugins 8.2

Same filename and directory in other branches
  1. 8 tests/src/Unit/Plugin/BlockStyleBaseTest.php

File

tests/src/Unit/Plugin/BlockStyleBaseTest.php
View source
<?php

namespace Drupal\Tests\block_style_plugins\Unit\Plugin;

use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Block\BlockPluginInterface;

/**
 * @coversDefaultClass \Drupal\block_style_plugins\Plugin\BlockStyleBase
 * @group block_style_plugins
 */
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);
  }

}

Classes

Namesort descending Description
BlockStyleBaseTest @coversDefaultClass \Drupal\block_style_plugins\Plugin\BlockStyleBase @group block_style_plugins