public function ParagraphsLibraryItemTest::testNoConversionSideEffects

Check that conversion to and from library items does not have side effects.

File

paragraphs/modules/paragraphs_library/tests/src/Functional/ParagraphsLibraryItemTest.php, line 133

Class

ParagraphsLibraryItemTest
Tests the functionality of the Paragraphs Library.

Namespace

Drupal\Tests\paragraphs_library\Functional

Code

public function testNoConversionSideEffects() {

  // Create a text paragraph.
  $text_paragraph = Paragraph::create([
    'type' => 'text',
    'field_text' => [
      'value' => 'Test text 1',
      'format' => 'plain_text',
    ],
  ]);
  $text_paragraph
    ->save();

  // Create a container that contains the text paragraph.
  $container_paragraph = Paragraph::create([
    'type' => 'paragraphs_container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph,
    ],
  ]);
  $container_paragraph
    ->save();

  // Add a node with the paragraphs.
  $node = Node::create([
    'type' => 'paragraphed_test',
    'title' => 'Test Node',
    'field_paragraphs' => [
      $container_paragraph,
    ],
  ]);
  $node
    ->save();

  // Enable conversion to library item.
  ParagraphsType::load('paragraphs_container')
    ->setThirdPartySetting('paragraphs_library', 'allow_library_conversion', TRUE)
    ->save();

  // Convert the container to a library item.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'Promote to library');
  $this
    ->submitForm([], 'Save');

  // Check that the child text paragraph is present in the node.
  $this
    ->assertSession()
    ->pageTextContains('Test text 1');
  $node = $this
    ->drupalGetNodeByTitle('Test Node');

  /** @var \Drupal\paragraphs_library\LibraryItemInterface $library_item */
  $library_item = $node
    ->get('field_paragraphs')->entity
    ->get('field_reusable_paragraph')->entity;

  // Remove the child text paragraph from the library item.
  $this
    ->drupalGet('/admin/content/paragraphs/' . $library_item
    ->id() . '/edit');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Label', 'Test Library Item');
  $this
    ->getSession()
    ->getPage()
    ->findButton('paragraphs_0_subform_paragraphs_container_paragraphs_0_remove')
    ->press();
  $this
    ->submitForm([], 'Save');

  // Check that the child text paragraph is no longer present in the
  // library item or the node.
  $this
    ->drupalGet('/admin/content/paragraphs/' . $library_item
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains('Test text 1');
  $this
    ->drupalGet('/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains('Test text 1');

  // View the second-to-last revision.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/revisions');
  $this
    ->getSession()
    ->getPage()
    ->find('css', '.node-revision-table')
    ->find('xpath', '(//tbody//tr)[2]//a')
    ->click();
  $revision_url = $this
    ->getSession()
    ->getCurrentUrl();
  $this
    ->assertStringContainsString('/node/' . $node
    ->id() . '/revisions/', $revision_url);
  $this
    ->assertStringContainsString('view', $revision_url);

  // Check that the child text paragraph is still present in this revision.
  $this
    ->assertSession()
    ->pageTextContains('Test text 1');

  // Add a new text paragraph to the library item.
  $this
    ->drupalGet('/admin/content/paragraphs/' . $library_item
    ->id() . '/edit');
  $this
    ->submitForm([], 'Add text');
  $this
    ->getSession()
    ->getPage()
    ->fillField('field_text', 'Test text 2');
  $this
    ->submitForm([], 'Save');

  // Check that the child text paragraph is present in the library item and
  // the node.
  $this
    ->drupalGet('/admin/content/paragraphs/' . $library_item
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Test text 2');
  $this
    ->drupalGet('/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Test text 2');

  // Convert the library item in the node back to a container paragraph and
  // delete it.
  $this
    ->drupalGet('/node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'Unlink from library');
  $this
    ->getSession()
    ->getPage()
    ->findButton('field_paragraphs_0_subform_paragraphs_container_paragraphs_0_remove')
    ->press();
  $this
    ->submitForm([], 'Save');

  // Check that the child text paragraph is no longer present in the node but
  // still present in the library item.
  $this
    ->drupalGet('/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextNotContains('Test text 2');
  $this
    ->drupalGet('/admin/content/paragraphs/' . $library_item
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Test text 2');
}