You are here

BlockFormAlterTest.php in Block Style Plugins 8.2

File

tests/src/Unit/BlockFormAlterTest.php
View source
<?php

namespace Drupal\Tests\block_style_plugins\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\block_style_plugins\BlockFormAlter;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\block_style_plugins\Plugin\BlockStyleManager;
use Drupal\Tests\block_style_plugins\Unit\Plugin\MockBlockStyleBase;

/**
 * @coversDefaultClass \Drupal\block_style_plugins\BlockFormAlter
 * @group block_style_plugins
 */
class BlockFormAlterTest extends UnitTestCase {

  /**
   * Mocked entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepository
   */
  protected $entityRepository;

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

  /**
   * Instance of the Block Style Manager service.
   *
   * @var \Drupal\block_style_plugins\Plugin\BlockStyleManager
   */
  protected $blockStyleManager;

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

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

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

  /**
   * Instance of the BlockFormAlter.
   *
   * @var \Drupal\block_style_plugins\BlockFormAlter
   */
  protected $classInstance;

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

    // Stub the Entity Repository Service.
    $this->entityRepository = $this
      ->prophesize(EntityRepositoryInterface::class);

    // 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());

    // Stub the Block Style Manager service.
    $this->blockStyleManager = $this
      ->prophesize(BlockStyleManager::class);
    $this->blockStyleManager
      ->getBlockDefinitions()
      ->willReturn([
      $plugin_id => $plugin_definition,
    ]);
    $this->blockStyleManager
      ->createInstance('block_style_plugins')
      ->willReturn($this->plugin);
    $this->classInstance = new BlockFormAlter($this->blockStyleManager
      ->reveal());

    // Use reflection to alter the protected entityRepository property.
    $reflectionObject = new \ReflectionObject($this->classInstance);
    $property = $reflectionObject
      ->getProperty('entityRepository');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($this->classInstance, $this->entityRepository
      ->reveal());

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

  /**
   * Tests the create method.
   *
   * @see ::create()
   */
  public function testCreate() {
    $container = $this
      ->prophesize(ContainerInterface::class);
    $container
      ->get('plugin.manager.block_style.processor')
      ->willReturn($this->blockStyleManager
      ->reveal());
    $instance = BlockFormAlter::create($container
      ->reveal());
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $instance);
  }

  /**
   * Tests the formAlter() method.
   *
   * @see ::formAlter()
   */
  public function testFormAlter() {
    $block = $this
      ->prophesize(Block::class);
    $block
      ->getPlugin()
      ->willReturn($this->blockPlugin
      ->reveal());
    $block
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins', [])
      ->willReturn([
      'test_style' => TRUE,
    ]);
    $blockForm = $this
      ->prophesize(BlockForm::class);
    $blockForm
      ->getEntity()
      ->willReturn($block
      ->reveal());
    $this->formState
      ->getFormObject()
      ->willReturn($blockForm
      ->reveal());
    $form = [];
    $this->classInstance
      ->alterForm($form, $this->formState
      ->reveal());

    // Check that a block_styles array is set.
    $this
      ->assertArrayHasKey('block_styles', $form);

    // Check that styles were set.
    $styles = $this->plugin
      ->getConfiguration();
    $expected_styles = [
      'sample_class' => '',
      'sample_checkbox' => '',
      'test_style' => TRUE,
    ];
    $this
      ->assertEquals($expected_styles, $styles);

    // Check third party settings.
    $expected_third_party_settings['block_style_plugins']['block_style_plugins'] = [
      '#type' => 'container',
      '#group' => 'block_styles',
    ];
    $this
      ->assertEquals($expected_third_party_settings, $form['third_party_settings']);

    // Check that validation and submit callbacks are set.
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $form['#validate'][0][0]);
    $this
      ->assertEquals('validateForm', $form['#validate'][0][1]);
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $form['#submit'][0][0]);
    $this
      ->assertEquals('submitForm', $form['#submit'][0][1]);
  }

  /**
   * Tests the allowStyles method.
   *
   * @see ::allowStyles()
   *
   * @dataProvider allowStylesProvider
   */
  public function testAllowStyles($type, $plugin, $expected) {
    $plugin_definition = [];
    if ($plugin) {
      $plugin_definition = [
        $type => [
          $plugin,
        ],
      ];
    }
    $return = $this->classInstance
      ->allowStyles('basic_block', $plugin_definition);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Provider for testAllowStyles()
   */
  public function allowStylesProvider() {
    return [
      'No include options are passed' => [
        NULL,
        NULL,
        TRUE,
      ],
      'Include basic_block' => [
        'include',
        'basic_block',
        TRUE,
      ],
      'Include only a sample_block' => [
        'include',
        'wrong_block',
        FALSE,
      ],
      'Include all derivatives of a base_plugin_id' => [
        'include',
        'basic_block:*',
        TRUE,
      ],
      'No exclude options are passed' => [
        NULL,
        NULL,
        TRUE,
      ],
      'Exclude basic_block' => [
        'exclude',
        'basic_block',
        FALSE,
      ],
      'Exclude a block that is not the current one' => [
        'exclude',
        'wrong_block',
        TRUE,
      ],
      'Exclude all derivatives of a base_plugin_id' => [
        'exclude',
        'basic_block:*',
        FALSE,
      ],
    ];
  }

  /**
   * Tests the allowStyles method with Derivatives.
   *
   * @see ::allowStyles()
   */
  public function testAllowStylesDerivatives() {
    $plugin_definition = [
      'exclude' => [
        'system_menu_block:*',
      ],
    ];
    $return = $this->classInstance
      ->allowStyles('system_menu_block:main', $plugin_definition);
    $this
      ->assertFalse($return);
  }

  /**
   * Tests the allowStyles method with Layout Builder's Inline Blocks.
   *
   * @see ::allowStyles()
   */
  public function testAllowStylesInlineBlocks() {
    $plugin_definition = [
      'exclude' => [
        'block_type',
      ],
    ];
    $return = $this->classInstance
      ->allowStyles('inline_block:block_type', $plugin_definition);
    $this
      ->assertFalse($return);
  }

  /**
   * Tests the exclude method.
   *
   * @see ::exclude()
   *
   * @dataProvider excludeProvider
   */
  public function testExclude($plugin_id, $expected) {
    $plugin_definition = [];
    if ($plugin_id) {
      $plugin_definition = [
        'exclude' => [
          $plugin_id,
        ],
      ];
    }
    $return = $this->classInstance
      ->exclude('basic_block', $plugin_definition);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Provider for testExclude()
   */
  public function excludeProvider() {
    return [
      'No exclude options are passed' => [
        FALSE,
        FALSE,
      ],
      'Exclude basic_block' => [
        'basic_block',
        TRUE,
      ],
      'Exclude a block that is not the current one' => [
        'wrong_block',
        FALSE,
      ],
      'Exclude all derivatives of a base_plugin_id' => [
        'basic_block:*',
        TRUE,
      ],
    ];
  }

  /**
   * Tests the includeOnly method.
   *
   * @see ::includeOnly()
   *
   * @dataProvider includeOnlyProvider
   */
  public function testIncludeOnly($plugin_id, $expected) {
    $plugin_definition = [];
    if ($plugin_id) {
      $plugin_definition = [
        'include' => [
          $plugin_id,
        ],
      ];
    }
    $return = $this->classInstance
      ->includeOnly('basic_block', $plugin_definition);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Provider for testIncludeOnly()
   */
  public function includeOnlyProvider() {
    return [
      'No include options are passed' => [
        NULL,
        TRUE,
      ],
      'Include basic_block' => [
        'basic_block',
        TRUE,
      ],
      'Include only a sample_block' => [
        'wrong_block',
        FALSE,
      ],
      'Include all derivatives of a base_plugin_id' => [
        'basic_block:*',
        TRUE,
      ],
    ];
  }

  /**
   * Tests the getBlockContentBundle method.
   *
   * @see ::getBlockContentBundle()
   */
  public function testGetBlockContentBundle() {
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->bundle()
      ->willReturn('basic_custom_block');
    $this->entityRepository
      ->loadEntityByUuid('block_content', 'uuid-1234')
      ->willReturn($entity
      ->reveal());
    $block = $this
      ->prophesize(Block::class);
    $block
      ->getPlugin()
      ->willReturn($this->blockPlugin
      ->reveal());
    $blockForm = $this
      ->prophesize(BlockForm::class);
    $blockForm
      ->getEntity()
      ->willReturn($block
      ->reveal());
    $this->formState
      ->getFormObject()
      ->willReturn($blockForm
      ->reveal());
    $bundle = $this->classInstance
      ->getBlockContentBundle($this->formState
      ->reveal());
    $this
      ->assertEquals('basic_custom_block', $bundle);
  }

}

Classes

Namesort descending Description
BlockFormAlterTest @coversDefaultClass \Drupal\block_style_plugins\BlockFormAlter @group block_style_plugins