You are here

public function LayoutBuilderMigrationTest::testToSection in Panelizer 8.5

@covers ::toSection

File

tests/src/Kernel/LayoutBuilderMigrationTest.php, line 130

Class

LayoutBuilderMigrationTest
@coversDefaultClass \Drupal\panelizer\LayoutBuilderMigration

Namespace

Drupal\Tests\panelizer\Kernel

Code

public function testToSection() {

  /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $display */
  $display = $this->container
    ->get('panelizer')
    ->getDefaultPanelsDisplay('default', 'node', 'page', 'default');
  $this
    ->assertInstanceOf(PanelsDisplayVariant::class, $display);
  $configuration = $display
    ->getConfiguration();
  $this
    ->assertNotEmpty($configuration['blocks']);

  // Set the layout plugin ID to an outdated one to ensure it is updated.
  $configuration['layout'] = 'twocol';

  // Normally this would not be kosher, but in this case the method really
  // does deserve private static visibility and should be tested by using
  // reflection to pry it open. It is too important to the migration to NOT
  // have a dedicated test.
  $migration = LayoutBuilderMigration::create($this->container);
  $method = new \ReflectionMethod($migration, 'toSection');
  $method
    ->setAccessible(TRUE);

  /** @var \Drupal\layout_builder\Section $section */
  $section = $method
    ->invokeArgs($migration, [
    &$configuration,
    'node',
    'page',
  ]);
  $this
    ->assertInstanceOf(Section::class, $section);
  $this
    ->assertSame('layout_twocol', $section
    ->getLayoutId());
  $this
    ->assertSame('layout_twocol', $configuration['layout']);
  $this
    ->assertSame($display
    ->getLayout()
    ->getConfiguration(), $section
    ->getLayoutSettings());
  foreach ($configuration['blocks'] as $uuid => $block) {
    $component = $section
      ->getComponent($uuid);
    $this
      ->assertSame($uuid, $component
      ->getUuid());
    $this
      ->assertSame($block['region'], $component
      ->getRegion());
    $this
      ->assertSame($block['weight'], $component
      ->getWeight());
    if (strpos($block['id'], 'entity_field:') === 0) {
      list(, , $field_name) = explode(':', $block['id']);
      $this
        ->assertSame("field_block:node:page:{$field_name}", $component
        ->getPluginId());

      // If the 'entity' context is mapped to Panelizer's entity context,
      // assert that mapping has been deleted, since it's not necessary (and
      // in fact causes errors) with Layout Builder.
      if (isset($block['context_mapping']['entity']) && $block['context_mapping']['entity'] === '@panelizer.entity_context:entity') {
        $component_configuration = $component
          ->get('configuration');
        $this
          ->assertSame('layout_builder.entity', $component_configuration['context_mapping']['entity']);
      }
    }
    else {
      $this
        ->assertSame($block['id'], $component
        ->getPluginId());
    }
    if ($block['id'] === 'context_block') {
      $plugin = $component
        ->getPlugin();
      $this
        ->assertSame('42', $plugin
        ->getContextValue('value'));
      $this
        ->assertSame('Foxtrot', $plugin
        ->getContextValue('letter'));
    }
  }
}