public function ParagraphsLockablePluginTest::testLockedParagraphInstance

Tests the lockable functionality with admin and other role on paragraphs.

File

paragraphs_collection/tests/src/Functional/ParagraphsLockablePluginTest.php, line 29

Class

ParagraphsLockablePluginTest
Tests the lockable plugin.

Namespace

Drupal\Tests\paragraphs_collection\Functional

Code

public function testLockedParagraphInstance() {

  // Create an article with paragraphs field.
  $contentTypeId = 'paragraphed_lock_test';
  $this
    ->addParagraphedContentType($contentTypeId, 'paragraphs');
  $permissions = [
    'administer site configuration',
    'administer lockable paragraph',
    'bypass node access',
    'administer content types',
    'edit behavior plugin settings',
  ];
  $this
    ->loginAsAdmin($permissions);

  // Add a text paragraphs type with a text field.
  $paragraphType = 'text_test';
  $fieldName = 'text';
  $this
    ->addParagraphsType($paragraphType);
  $bundlePath = 'admin/structure/paragraphs_type/' . $paragraphType;
  $this
    ->fieldUIAddExistingField('admin/structure/paragraphs_type/' . $paragraphType, 'paragraphs_text');
  $this
    ->drupalGet($bundlePath);
  $this
    ->assertSession()
    ->fieldExists('behavior_plugins[lockable][enabled]');
  $edit = [
    'behavior_plugins[lockable][enabled]' => TRUE,
  ];
  $this
    ->submitForm($edit, t('Save'));

  // Check that the bundle now has lockable enabled.
  $this
    ->drupalGet($bundlePath);
  $this
    ->assertSession()
    ->checkboxChecked('edit-behavior-plugins-lockable-enabled');

  // Create a paragraphed content.
  $this
    ->drupalGet('node/add/' . $contentTypeId);
  $this
    ->submitForm([], 'paragraphs_' . $paragraphType . '_add_more');

  // Add title and body text to the node and save it.
  $edit = [
    'title[0][value]' => 'Test article',
    'paragraphs[0][subform][paragraphs_' . $fieldName . '][0][value]' => 'This is some text',
    'paragraphs[0][behavior_plugins][lockable][locked]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $nodeUrl = $this
    ->getUrl();
  $this
    ->drupalGet($nodeUrl . '/edit');
  $this
    ->assertSession()
    ->pageTextNotContains('You are not allowed to edit or remove this Paragraph.');

  // Check that a new user without our permission cannot edit.
  $account = $this
    ->drupalCreateUser([
    'bypass node access',
  ]);
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet($nodeUrl . '/edit');
  $this
    ->assertSession()
    ->pageTextContains('You are not allowed to edit or remove this Paragraph.');

  // Check that a new non admin user who does have the permission can edit.
  $account = $this
    ->drupalCreateUser([
    'bypass node access',
    'administer lockable paragraph',
  ]);
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet($nodeUrl . '/edit');
  $this
    ->assertSession()
    ->pageTextNotContains('You are not allowed to edit or remove this Paragraph.');
}