You are here

public function PanelizerNodeFunctionalTest::testMigrationToLayoutBuilder in Panelizer 8.5

Tests migration of the entity view display data to Layout Builder.

File

tests/src/Functional/PanelizerNodeFunctionalTest.php, line 186

Class

PanelizerNodeFunctionalTest
Basic functional tests of using Panelizer with nodes.

Namespace

Drupal\Tests\panelizer\Functional

Code

public function testMigrationToLayoutBuilder() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  /** @var \Drupal\panelizer\PanelizerInterface $panelizer */
  $panelizer = $this->container
    ->get('panelizer');

  // Create a node to test with.
  $node = $this
    ->drupalCreateNode([
    'type' => 'page',
  ]);

  // For the first revision, explicitly use whatever layout is the default for
  // the page node type.
  $panelizer
    ->setPanelsDisplay($node, 'full', '__bundle_default__');

  // Get the revision URL so we can visit it later to ensure it was migrated.
  $alpha_revision_url = Url::fromRoute('entity.node.revision', [
    'node' => $node
      ->id(),
    'node_revision' => $node
      ->getRevisionId(),
  ]);

  /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */
  $panels_display = $panelizer
    ->getPanelsDisplay($node, 'full');
  $this
    ->assertInstanceOf(PanelsDisplayVariant::class, $panels_display);

  // Add the block to the custom layout.
  $beta_uuid = $panels_display
    ->addBlock([
    'id' => 'block_content:beta',
    'label' => 'beta title',
    'region' => 'content',
    'weight' => 1,
  ]);
  $panelizer
    ->setPanelsDisplay($node, 'full', NULL, $panels_display);

  // Get the revision URL so we can visit it later to ensure it was migrated.
  $beta_revision_url = Url::fromRoute('entity.node.revision', [
    'node' => $node
      ->id(),
    'node_revision' => $node
      ->getRevisionId(),
  ]);

  // Create a new revision with a different custom block in the layout.
  $panels_display
    ->removeBlock($beta_uuid)
    ->addBlock([
    'id' => 'block_content:charlie',
    'label' => 'charlie title',
    'region' => 'content',
    'weight' => 1,
  ]);
  $panelizer
    ->setPanelsDisplay($node, 'full', NULL, $panels_display);

  // Create a query to count the number of revisions that are created during
  // the migration.

  /** @var \Drupal\Core\Entity\Query\QueryInterface $revision_count_query */
  $revision_count_query = $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->getQuery()
    ->allRevisions()
    ->condition('nid', $node
    ->id())
    ->count();
  $this
    ->assertSame('4', $revision_count_query
    ->execute());
  $this
    ->drupalGet($node
    ->toUrl());
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('charlie title');
  $assert_session
    ->pageTextContains('charlie body');

  // Ensure the previous revisions look right.
  $this
    ->drupalGet($beta_revision_url);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('beta title');
  $assert_session
    ->pageTextContains('beta body');
  $this
    ->drupalGet($alpha_revision_url);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('The context value is 42, brought to you by the letter Juliet.');
  $this->container
    ->get('module_installer')
    ->install([
    'layout_builder',
  ]);
  $this
    ->drupalGet('/admin/structure/types/manage/page/display');
  $page
    ->checkField('Full content');
  $page
    ->checkField('Teaser');
  $page
    ->pressButton('Save');
  $page
    ->clickLink('Full content');
  $assert_session
    ->checkboxChecked('Panelize this view mode');
  $assert_session
    ->checkboxChecked('Allow users to select which display to use');
  $assert_session
    ->checkboxChecked('Allow each content item to have its display customized');
  $assert_session
    ->checkboxNotChecked('Use Layout Builder');
  $assert_session
    ->checkboxNotChecked('Allow each content item to have its layout customized.');
  $page
    ->pressButton('Migrate to Layout Builder');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Hold your horses, cowpoke.');
  $page
    ->pressButton('I understand the risks and have backed up my database. Proceed!');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->checkboxChecked('Use Layout Builder');
  $assert_session
    ->checkboxChecked('Allow content editors to use stored layouts');
  $assert_session
    ->checkboxChecked('Allow each content item to have its layout customized.');
  $assert_session
    ->fieldNotExists('Panelize this view mode');
  $assert_session
    ->fieldNotExists('Allow users to select which display to use');
  $assert_session
    ->fieldNotExists('Allow each content item to have its display customized');
  $page
    ->clickLink('Manage layout');
  $page
    ->pressButton('Save layout');
  $page
    ->clickLink('Teaser');
  $assert_session
    ->checkboxChecked('Panelize this view mode');
  $assert_session
    ->checkboxNotChecked('Allow users to select which display to use');
  $assert_session
    ->fieldNotExists('Allow each content item to have its display customized');
  $assert_session
    ->checkboxNotChecked('Use Layout Builder');
  $assert_session
    ->fieldNotExists('Allow each content item to have its layout customized.');
  $page
    ->pressButton('Migrate to Layout Builder');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Hold your horses, cowpoke.');
  $page
    ->pressButton('I understand the risks and have backed up my database. Proceed!');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->checkboxChecked('Use Layout Builder');
  $assert_session
    ->checkboxNotChecked('Allow content editors to use stored layouts');
  $assert_session
    ->fieldNotExists('Allow each content item to have its layout customized.');
  $assert_session
    ->fieldNotExists('Panelize this view mode');
  $assert_session
    ->fieldNotExists('Allow users to select which display to use');
  $assert_session
    ->fieldNotExists('Allow each content item to have its display customized');
  $page
    ->clickLink('Manage layout');
  $page
    ->pressButton('Save layout');

  // No new revisions should have been created during the migration.
  $this
    ->assertSame('4', $revision_count_query
    ->execute());
  $this
    ->drupalGet($node
    ->toUrl());
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('charlie title');
  $assert_session
    ->pageTextContains('charlie body');
  $assert_session
    ->pageTextContains('The context value is 42, brought to you by the letter Juliet.');

  // Ensure the previous revisions look right.
  $this
    ->drupalGet($beta_revision_url);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('beta title');
  $assert_session
    ->pageTextContains('beta body');
  $assert_session
    ->pageTextContains('The context value is 42, brought to you by the letter Juliet.');
  $this
    ->drupalGet($alpha_revision_url);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('The context value is 42, brought to you by the letter Juliet.');
  $node = $this
    ->drupalCreateNode([
    'type' => 'page',
  ]);
  $this
    ->drupalGet($node
    ->toUrl());
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($node
    ->getTitle());
  $assert_session
    ->pageTextContains($node->body->value);
  $assert_session
    ->pageTextNotContains('Powered by Drupal');
  $assert_session
    ->pageTextNotContains('alpha title');
  $assert_session
    ->pageTextNotContains('alpha body');
  $assert_session
    ->pageTextContains('The context value is 42, brought to you by the letter Juliet.');
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $assert_session
    ->statusCodeEquals(200);
  $page
    ->selectFieldOption('Layout', 'Alpha');
  $page
    ->pressButton('Save');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($node
    ->getTitle());
  $assert_session
    ->pageTextContains($node->body->value);
  $assert_session
    ->pageTextContains('Powered by Drupal');
  $assert_session
    ->pageTextContains('alpha title');
  $assert_session
    ->pageTextContains('alpha body');
  $assert_session
    ->pageTextContains('The context value is 99, brought to you by the letter X-ray.');

  // Ensure that the teaser looks correct, too.
  $this
    ->drupalGet('/node');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($node
    ->getTitle());
  $assert_session
    ->pageTextContains('alpha teaser');
  $assert_session
    ->pageTextContains('alpha body');
  $assert_session
    ->pageTextContains('The context value is 35, brought to you by the letter Tango.');
}