You are here

class BlockFormAlterTest in Block Style Plugins 8.2

@coversDefaultClass \Drupal\block_style_plugins\BlockFormAlter @group block_style_plugins

Hierarchy

Expanded class hierarchy of BlockFormAlterTest

File

tests/src/Unit/BlockFormAlterTest.php, line 22

Namespace

Drupal\Tests\block_style_plugins\Unit
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockFormAlterTest::$blockPlugin protected property Mocked Block Plugin.
BlockFormAlterTest::$blockStyleManager protected property Instance of the Block Style Manager service.
BlockFormAlterTest::$classInstance protected property Instance of the BlockFormAlter.
BlockFormAlterTest::$entityRepository protected property Mocked entity repository service.
BlockFormAlterTest::$entityTypeManager protected property Mocked entity type manager service.
BlockFormAlterTest::$formState protected property Mocked form state.
BlockFormAlterTest::$plugin protected property Instance of the MockBlockStyleBase plugin.
BlockFormAlterTest::allowStylesProvider public function Provider for testAllowStyles()
BlockFormAlterTest::excludeProvider public function Provider for testExclude()
BlockFormAlterTest::includeOnlyProvider public function Provider for testIncludeOnly()
BlockFormAlterTest::setUp protected function Create the setup for constants and configFactory stub. Overrides UnitTestCase::setUp
BlockFormAlterTest::testAllowStyles public function Tests the allowStyles method.
BlockFormAlterTest::testAllowStylesDerivatives public function Tests the allowStyles method with Derivatives.
BlockFormAlterTest::testAllowStylesInlineBlocks public function Tests the allowStyles method with Layout Builder's Inline Blocks.
BlockFormAlterTest::testCreate public function Tests the create method.
BlockFormAlterTest::testExclude public function Tests the exclude method.
BlockFormAlterTest::testFormAlter public function Tests the formAlter() method.
BlockFormAlterTest::testGetBlockContentBundle public function Tests the getBlockContentBundle method.
BlockFormAlterTest::testIncludeOnly public function Tests the includeOnly 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.