public function ParagraphsContentModerationTest::testModeratedContentNestedParagraphs

Tests the moderated content that includes nested paragraphs.

File

paragraphs/modules/paragraphs_library/tests/src/FunctionalJavascript/ParagraphsContentModerationTest.php, line 479

Class

ParagraphsContentModerationTest
Tests Paragraphs, Paragraphs Library and Content Moderation integration.

Namespace

Drupal\Tests\paragraphs_library\FunctionalJavascript

Code

public function testModeratedContentNestedParagraphs() {
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->addParagraphsType('rich_paragraph');
  $this
    ->addFieldtoParagraphType('rich_paragraph', 'field_intermediate_text', 'text');
  $this
    ->addFieldtoParagraphType('rich_paragraph', 'field_nested_paragraphs', 'entity_reference', [
    'target_type' => 'paragraphs_library_item',
  ]);
  $display = \Drupal::service('entity_display.repository')
    ->getViewDisplay('paragraph', 'rich_paragraph');
  $display
    ->setComponent('field_nested_paragraphs', [
    'type' => 'entity_reference_entity_view',
  ])
    ->save();

  // Create a child library item.
  $this
    ->drupalGet('/admin/content/paragraphs/add/default');
  $page
    ->fillField('label[0][value]', 'Child library item');
  $dropbutton_paragraphs = $assert_session
    ->elementExists('css', '#edit-paragraphs-add-more .dropbutton-arrow');
  $dropbutton_paragraphs
    ->click();
  $add_text_paragraph = $assert_session
    ->elementExists('css', '#paragraphs-text-add-more');
  $add_text_paragraph
    ->press();
  $textfield = $assert_session
    ->waitForElement('css', 'input[name="paragraphs[0][subform][field_text][0][value]"]');
  $this
    ->assertNotNull($textfield);
  $page
    ->fillField('paragraphs[0][subform][field_text][0][value]', 'This is the low-level text.');

  // This is published initially.
  $page
    ->selectFieldOption('moderation_state[0][state]', 'published');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Child initial revision.');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Paragraph Child library item has been created.');
  $child_library_item_id = $this
    ->getLastEntityOfType('paragraphs_library_item');

  // Create a rich library item that references the previous one.
  $this
    ->drupalGet('/admin/content/paragraphs/add/default');
  $page
    ->fillField('label[0][value]', 'Rich library item');
  $dropbutton_paragraphs = $assert_session
    ->elementExists('css', '#edit-paragraphs-add-more .dropbutton-arrow');
  $dropbutton_paragraphs
    ->click();
  $add_rich_paragraph = $assert_session
    ->elementExists('css', '#paragraphs-rich-paragraph-add-more');
  $add_rich_paragraph
    ->press();
  $textfield = $assert_session
    ->waitForElement('css', 'input[name="paragraphs[0][subform][field_intermediate_text][0][value]"]');
  $this
    ->assertNotNull($textfield);
  $page
    ->fillField('paragraphs[0][subform][field_intermediate_text][0][value]', 'First level text - draft');
  $paragraphs_field = $assert_session
    ->waitForElement('css', 'input[name="paragraphs[0][subform][field_nested_paragraphs][0][target_id]"]');
  $this
    ->assertNotNull($paragraphs_field);
  $page
    ->fillField('paragraphs[0][subform][field_nested_paragraphs][0][target_id]', "Child library item ({$child_library_item_id})");

  // Let's make this initially a draft.
  $page
    ->selectFieldOption('moderation_state[0][state]', 'draft');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Rich item initial revision.');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Paragraph Rich library item has been created.');
  $rich_library_item_id = $this
    ->getLastEntityOfType('paragraphs_library_item');

  // Create a host node that uses both a rich item and a child one.
  $this
    ->drupalGet('/node/add/paragraphed_moderated_test');
  $page
    ->fillField('title[0][value]', 'Host page 2');
  $add_from_library_button = $assert_session
    ->elementExists('css', 'input[name="field_paragraphs_from_library_add_more"]');
  $add_from_library_button
    ->press();
  $button = $assert_session
    ->waitForButton('Select reusable paragraph');
  $this
    ->assertNotNull($button);
  $button
    ->press();
  $modal = $assert_session
    ->waitForElement('css', '.ui-dialog');
  $this
    ->assertNotNull($modal);
  $session
    ->switchToIFrame('entity_browser_iframe_paragraphs_library_items');
  $assert_session
    ->pageTextContains('Child library item');
  $assert_session
    ->pageTextContains('Rich library item');
  $table = $assert_session
    ->elementExists('css', 'table.views-table');
  $rich_item_row = $this
    ->getTableRowWithText($table, 'Rich library item');
  $rich_item_checkbox = $assert_session
    ->elementExists('css', 'input[type="radio"]', $rich_item_row);
  $rich_item_checkbox
    ->click();
  $page
    ->pressButton('Select reusable paragraph');
  $session
    ->wait(1000);
  $session
    ->switchToIFrame();
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Save the node as published.
  $page
    ->selectFieldOption('moderation_state[0][state]', 'published');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Node initial revision');
  $page
    ->pressButton('Save');
  $host_node_id = $this
    ->getLastEntityOfType('node');

  // Visitor users don't see the paragraphs.
  $this
    ->drupalLogin($this->visitorUser);
  $this
    ->drupalGet("/node/{$host_node_id}");
  $assert_session
    ->pageTextNotContains('This is the low-level text.');
  $assert_session
    ->pageTextNotContains('First level text - draft');
  $this
    ->drupalLogin($this->adminUser);

  // Make the rich paragraph published.
  $this
    ->drupalGet("/admin/content/paragraphs/{$rich_library_item_id}/edit");
  $page
    ->fillField('paragraphs[0][subform][field_intermediate_text][0][value]', 'First level text - published');
  $page
    ->selectFieldOption('moderation_state[0][state]', 'published');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Rich item first published revision.');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Paragraph Rich library item has been updated.');
  $this
    ->drupalLogin($this->visitorUser);
  $this
    ->drupalGet("/node/{$host_node_id}");
  $assert_session
    ->pageTextContains('This is the low-level text.');
  $assert_session
    ->pageTextContains('First level text - published');
  $this
    ->drupalLogin($this->adminUser);

  // Make some draft modifications at the child paragraph.
  $this
    ->drupalGet("/admin/content/paragraphs/{$child_library_item_id}/edit");
  $page
    ->fillField('paragraphs[0][subform][field_text][0][value]', 'The low-level text has been modified (pending approval).');
  $page
    ->selectFieldOption('moderation_state[0][state]', 'draft');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Child item unapproved changes.');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Paragraph Child library item has been updated.');
  $this
    ->drupalLogin($this->visitorUser);
  $this
    ->drupalGet("/node/{$host_node_id}");
  $assert_session
    ->pageTextContains('This is the low-level text.');
  $assert_session
    ->pageTextContains('First level text - published');
  $assert_session
    ->pageTextNotContains('The low-level text has been modified (pending approval).');
  $this
    ->drupalLogin($this->adminUser);

  // Publish the child paragraph.
  $this
    ->drupalGet("/admin/content/paragraphs/{$child_library_item_id}/edit");
  $page
    ->fillField('paragraphs[0][subform][field_text][0][value]', 'The low-level text has been modified (approved!).');
  $page
    ->selectFieldOption('moderation_state[0][state]', 'published');
  $page
    ->find('css', 'a[href="#edit-revision-information"]')
    ->click();
  $page
    ->fillField('revision_log[0][value]', 'Child item approved changes.');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Paragraph Child library item has been updated.');
  $this
    ->drupalLogin($this->visitorUser);
  $this
    ->drupalGet("/node/{$host_node_id}");
  $assert_session
    ->pageTextContains('The low-level text has been modified (approved!).');
  $assert_session
    ->pageTextContains('First level text - published');
  $assert_session
    ->pageTextNotContains('This is the low-level text.');
  $this
    ->drupalLogin($this->adminUser);

  // Revert the child paragraph to its initial version.
  $this
    ->drupalGet("/admin/content/paragraphs/{$child_library_item_id}/revisions");
  $table = $assert_session
    ->elementExists('css', 'table');
  $target_row = $this
    ->getTableRowWithText($table, 'Child initial revision');
  $target_row
    ->clickLink('Revert');
  $assert_session
    ->pageTextContains('Are you sure you want to revert');
  $page
    ->pressButton('Revert');
  $assert_session
    ->pageTextContains(' has been reverted to the revision from ');
  $this
    ->drupalLogin($this->visitorUser);
  $this
    ->drupalGet("/node/{$host_node_id}");
  $assert_session
    ->pageTextContains('This is the low-level text.');
  $assert_session
    ->pageTextContains('First level text - published');
}