You are here

public function ParagraphsLibraryTest::testLibraryItemRevisions in Paragraphs 8

Test paragraphs library item revisions.

File

modules/paragraphs_library/tests/src/Functional/ParagraphsLibraryTest.php, line 593

Class

ParagraphsLibraryTest
Tests paragraphs library functionality.

Namespace

Drupal\Tests\paragraphs_library\Functional

Code

public function testLibraryItemRevisions() {
  $this
    ->loginAsAdmin([
    'create paragraphed_test content',
    'edit any paragraphed_test content',
    'administer paragraphs library',
  ]);
  $this
    ->addParagraphsType('test_content');
  $this
    ->addParagraphsType('nested_paragraph');
  $this
    ->fieldUIAddNewField('admin/structure/paragraphs_type/test_content', 'paragraphs_text', 'Test content', 'text_long', [], []);

  // Add nested paragraph field.
  $this
    ->fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'err_field', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
    'settings[target_type]' => 'paragraph',
    'cardinality' => '-1',
  ], []);

  // Add nested paragraph directly in library.
  $this
    ->drupalGet('admin/content/paragraphs/add/default');
  $this
    ->submitForm([], 'paragraphs_nested_paragraph_add_more');
  $this
    ->submitForm([], 'paragraphs_0_subform_field_err_field_test_content_add_more');
  $edit = [
    'label[0][value]' => 'Test revisions nested original',
    'paragraphs[0][subform][field_err_field][0][subform][field_paragraphs_text][0][value]' => 'Example text for revision original.',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Paragraph Test revisions nested original has been created.');

  // Check revisions tab.
  $this
    ->clickLink('Test revisions nested original');
  $this
    ->clickLink('Revisions');
  $this
    ->assertSession()
    ->titleEquals('Revisions for Test revisions nested original | Drupal');

  // Edit library item, check that new revision is enabled as default.
  $this
    ->clickLink('Edit');
  $this
    ->assertSession()
    ->checkboxChecked('edit-revision');
  $edit = [
    'label[0][value]' => 'Test revisions nested first change',
    'paragraphs[0][subform][field_err_field][0][subform][field_paragraphs_text][0][value]' => 'Example text for revision first change.',
  ];
  $this
    ->submitForm($edit, 'Save');

  // Check previous revision.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('paragraphs_library_item');
  $date_formatter = \Drupal::service('date.formatter');
  $this
    ->clickLink('Test revisions nested first change');
  $this
    ->clickLink('Revisions');
  $this
    ->assertSession()
    ->titleEquals('Revisions for Test revisions nested first change | Drupal');
  $revision = $storage
    ->loadRevision(1);
  $this
    ->assertSession()
    ->pageTextContains('Test revisions nested original by ' . $this->admin_user
    ->getAccountName());
  $this
    ->assertSession()
    ->pageTextContains($date_formatter
    ->format($revision
    ->getChangedTime(), 'short') . ': ' . $revision
    ->label());
  $this
    ->clickLink($date_formatter
    ->format($revision
    ->getChangedTime(), 'short'), 1);
  $this
    ->assertSession()
    ->pageTextContains('Test revisions nested original');
  $this
    ->assertSession()
    ->pageTextContains('Example text for revision original');
  $this
    ->clickLink('Revisions');

  // Test reverting revision.
  $this
    ->clickLink('Revert');
  $this
    ->assertSession()
    ->responseContains('Are you sure you want to revert revision from ' . $date_formatter
    ->format($revision
    ->getChangedTime()) . '?');
  $this
    ->submitForm([], 'Revert');
  $this
    ->assertSession()
    ->pageTextContains('Test revisions nested original has been reverted to the revision from ' . $date_formatter
    ->format($revision
    ->getChangedTime()) . '.');

  // Check current revision.
  $current_revision = $storage
    ->loadRevision(3);
  $this
    ->clickLink($date_formatter
    ->format($current_revision
    ->getChangedTime(), 'short'));
  $this
    ->assertSession()
    ->pageTextContains('Example text for revision original');
  $this
    ->clickLink('Revisions');

  // Test deleting revision.
  $revision_for_deleting = $storage
    ->loadRevision(2);
  $this
    ->clickLink('Delete');
  $this
    ->assertSession()
    ->responseContains('Are you sure you want to delete revision from ' . $date_formatter
    ->format($revision_for_deleting
    ->getChangedTime()));
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->pageTextContains('Revision from ' . $date_formatter
    ->format($revision_for_deleting
    ->getChangedTime()) . ' has been deleted.');
}