View source
<?php
namespace Drupal\Tests\block_style_plugins\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\block_style_plugins\Plugin\BlockStyleManager;
use Drupal\block_style_plugins\SectionFormAlter;
use Drupal\layout_builder\Form\ConfigureSectionForm;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\Tests\block_style_plugins\Unit\Plugin\MockBlockStyleBase;
class SectionFormAlterTest extends UnitTestCase {
protected $entityTypeManager;
protected $blockStyleManager;
protected $formState;
protected $section;
protected $plugin;
protected $classInstance;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->formState = $this
->prophesize(FormStateInterface::class);
$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());
$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());
$translator = $this
->getStringTranslationStub();
$this->classInstance
->setStringTranslation($translator);
}
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);
}
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());
$this
->assertArrayHasKey('block_styles', $form);
$styles = $this->plugin
->getConfiguration();
$expected_styles = [
'sample_class' => '',
'sample_checkbox' => '',
'test_style' => TRUE,
];
$this
->assertEquals($expected_styles, $styles);
$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']);
$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]);
}
}