You are here

public function ParagraphsLockablePluginTest::testLockedParagraphInstance in Paragraphs Collection 8

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

File

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
    ->assertFieldByName('behavior_plugins[lockable][enabled]');
  $edit = [
    'behavior_plugins[lockable][enabled]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

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

  // Create a paragraphed content.
  $this
    ->drupalGet('node/add/' . $contentTypeId);
  $this
    ->drupalPostForm(NULL, [], '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
    ->drupalPostForm(NULL, $edit, 'Save');
  $nodeUrl = $this
    ->getUrl();
  $this
    ->drupalGet($nodeUrl . '/edit');
  $this
    ->assertNoText('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
    ->assertText('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
    ->assertNoText('You are not allowed to edit or remove this Paragraph.');
}