You are here

class SectionFormAlterTest in Block Style Plugins 8.2

@coversDefaultClass \Drupal\block_style_plugins\SectionFormAlter @group block_style_plugins

Hierarchy

Expanded class hierarchy of SectionFormAlterTest

File

tests/src/Unit/SectionFormAlterTest.php, line 20

Namespace

Drupal\Tests\block_style_plugins\Unit
View source
class SectionFormAlterTest extends UnitTestCase {

  /**
   * 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 Section.
   *
   * @var \Drupal\layout_builder\Section
   */
  protected $section;

  /**
   * 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 Type Manager.
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);

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

    // Block plugin.
    $this->section = $this
      ->prophesize(Section::class);
    $this->section
      ->getLayoutId()
      ->willReturn('layout_onecol');
    $configuration = [];
    $plugin_id = 'block_style_plugins';
    $plugin_definition['provider'] = 'block_style_plugins';
    $plugin_definition['type'] = 'section';
    $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
      ->getSectionDefinitions()
      ->willReturn([
      $plugin_id => $plugin_definition,
    ]);
    $this->blockStyleManager
      ->createInstance('block_style_plugins')
      ->willReturn($this->plugin);
    $this->classInstance = new SectionFormAlter($this->blockStyleManager
      ->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 = SectionFormAlter::create($container
      ->reveal());
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\SectionFormAlter', $instance);
  }

  /**
   * Tests the formAlter() method.
   *
   * @see ::formAlter()
   */
  public function testFormAlter() {
    $this->section
      ->getThirdPartySetting('block_style_plugins', 'block_style_plugins', [])
      ->willReturn([
      'test_style' => TRUE,
    ]);
    $sectionStorage = $this
      ->prophesize(SectionStorageInterface::class);
    $sectionStorage
      ->getSections()
      ->willReturn([
      $this->section
        ->reveal(),
    ]);
    $sectionForm = $this
      ->prophesize(ConfigureSectionForm::class);
    $sectionForm
      ->getSectionStorage()
      ->willReturn($sectionStorage
      ->reveal());
    $this->formState
      ->getFormObject()
      ->willReturn($sectionForm
      ->reveal());
    $this->formState
      ->getBuildInfo()
      ->willReturn([
      'args' => [
        1 => 0,
      ],
    ]);
    $form = [
      '#attributes' => [
        'data-layout-builder-target-highlight-id' => 'section-update-1',
      ],
    ];
    $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\\SectionFormAlter', $form['#validate'][0][0]);
    $this
      ->assertEquals('validateForm', $form['#validate'][0][1]);
    $this
      ->assertInstanceOf('Drupal\\block_style_plugins\\SectionFormAlter', $form['#submit'][0][0]);
    $this
      ->assertEquals('submitForm', $form['#submit'][0][1]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SectionFormAlterTest::$blockStyleManager protected property Instance of the Block Style Manager service.
SectionFormAlterTest::$classInstance protected property Instance of the BlockFormAlter.
SectionFormAlterTest::$entityTypeManager protected property Mocked entity type manager service.
SectionFormAlterTest::$formState protected property Mocked form state.
SectionFormAlterTest::$plugin protected property Instance of the MockBlockStyleBase plugin.
SectionFormAlterTest::$section protected property Mocked Section.
SectionFormAlterTest::setUp protected function Create the setup for constants and configFactory stub. Overrides UnitTestCase::setUp
SectionFormAlterTest::testCreate public function Tests the create method.
SectionFormAlterTest::testFormAlter public function Tests the formAlter() method.
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.