public function ParagraphsTypePermissionsTest::testAnonymousParagraphsTypePermissions in Paragraphs 8
Tests paragraphs type permissions for anonymous and authenticated users.
File
- modules/
paragraphs_type_permissions/ tests/ src/ Functional/ ParagraphsTypePermissionsTest.php, line 111
Class
- ParagraphsTypePermissionsTest
- Tests the paragraphs type permissions.
Namespace
Drupal\Tests\paragraphs_type_permissions\FunctionalCode
public function testAnonymousParagraphsTypePermissions() {
// Create an authenticated user without special permissions for test.
$authenticated_user = $this
->drupalCreateUser();
// Create an admin user for test.
$admin_user = $this
->drupalCreateUser(array(
'administer site configuration',
'administer content types',
'administer node fields',
'administer node display',
'administer paragraphs types',
'administer paragraph form display',
'create paragraphed_content_demo content',
'edit any paragraphed_content_demo content',
'bypass paragraphs type content access',
'administer node form display',
));
$this
->drupalLogin($admin_user);
// Enable the publish/unpublish checkbox fields.
$paragraph_types = [
'text_image',
'images',
'text',
];
foreach ($paragraph_types as $paragraph_type) {
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('paragraph', $paragraph_type);
$form_display
->setComponent('status', [
'type' => 'boolean_checkbox',
])
->save();
}
// Create a node with some Paragraph types.
$this
->drupalGet('node/add/paragraphed_content_demo');
$this
->submitForm([], 'Add text_image');
$this
->submitForm([], 'Add images');
$this
->submitForm([], 'Add text');
$image_text = $this
->getTestFiles('image')[0];
$this
->submitForm([
'files[field_paragraphs_demo_0_subform_field_image_demo_0]' => $image_text->uri,
], 'Upload');
$images = $this
->getTestFiles('image')[1];
$this
->submitForm([
'files[field_paragraphs_demo_1_subform_field_images_demo_0][]' => $images->uri,
], 'Upload');
$edit = [
'title[0][value]' => 'paragraph node title',
'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Paragraph type Image + Text',
'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Paragraph type Text',
];
$this
->submitForm($edit, 'Save');
// Get the node to edit it later.
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
// Get the images data to check for their presence.
$image_text_tag = '/files/styles/large/public/' . date('Y-m') . '/image-test.png?itok=';
$images_tag = '/files/styles/medium/public/' . date('Y-m') . '/image-test_0.png?itok=';
// Check that all paragraphs are shown for admin user.
$this
->assertSession()
->responseContains($image_text_tag);
$this
->assertSession()
->responseContains($images_tag);
$this
->assertSession()
->pageTextContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextContains('Paragraph type Text');
// Logout, check that no paragraphs are shown for anonymous user.
$this
->drupalLogout();
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains($image_text_tag);
$this
->assertSession()
->responseNotContains($images_tag);
$this
->assertSession()
->pageTextNotContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextNotContains('Paragraph type Text');
// Login as authenticated user, check that no paragraphs are shown for him.
$this
->drupalLogin($authenticated_user);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains($image_text_tag);
$this
->assertSession()
->responseNotContains($images_tag);
$this
->assertSession()
->pageTextNotContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextNotContains('Paragraph type Text');
// Login as admin
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
// Set edit mode to open.
$this
->drupalGet('admin/structure/types/manage/paragraphed_content_demo/form-display');
$this
->submitForm([], "field_paragraphs_demo_settings_edit");
$edit = [
'fields[field_paragraphs_demo][settings_edit_form][settings][edit_mode]' => 'open',
];
$this
->submitForm($edit, 'Save');
// Unpublish the 'Image + Text' paragraph type.
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-field-paragraphs-demo-0-subform-status-value');
$edit = [
'field_paragraphs_demo[0][subform][status][value]' => FALSE,
];
$this
->submitForm($edit, 'Save');
// Check that 'Image + Text' paragraph is not shown anymore for admin user.
$this
->assertSession()
->responseNotContains($image_text_tag);
$this
->assertSession()
->responseContains($images_tag);
$this
->assertSession()
->pageTextNotContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextContains('Paragraph type Text');
$this
->drupalLogout();
// Add permissions to anonymous user to view only 'Image + Text' and
// 'Text' paragraph contents.
/** @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load('anonymous');
$anonymous_role
->grantPermission('view paragraph content text_image');
$anonymous_role
->grantPermission('view paragraph content text');
$anonymous_role
->save();
// Add permissions to authenticated user to view only 'Image + Text' and
// 'Images' paragraph contents.
/** @var \Drupal\user\RoleInterface $authenticated_role */
$authenticated_role = Role::load('authenticated');
$authenticated_role
->grantPermission('view paragraph content text_image');
$authenticated_role
->grantPermission('view paragraph content images');
$authenticated_role
->save();
// Check that the anonymous user can only view the 'Text' paragraph.
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains($image_text_tag);
$this
->assertSession()
->responseNotContains($images_tag);
$this
->assertSession()
->pageTextNotContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextContains('Paragraph type Text');
// Check that the authenticated user can only view the 'Images' paragraph.
$this
->drupalLogin($authenticated_user);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains($image_text_tag);
$this
->assertSession()
->responseContains($images_tag);
$this
->assertSession()
->pageTextNotContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextNotContains('Paragraph type Text');
// Check the authenticated user with edit permission.
$authenticated_role
->grantPermission('update paragraph content text_image');
$authenticated_role
->grantPermission('bypass node access');
$authenticated_role
->save();
$this
->drupalLogin($authenticated_user);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->responseContains('Image + Text');
$this
->assertSession()
->pageTextContains('Paragraph type Image + Text');
$this
->assertSession()
->pageTextContains('You are not allowed to remove this Paragraph.');
$this
->assertSession()
->pageTextContains('Published');
$this
->assertSession()
->pageTextContains('Images');
$this
->assertSession()
->pageTextContains('You are not allowed to edit or remove this Paragraph.');
$this
->assertSession()
->responseContains('paragraphs-collapsed-description"><div class="paragraphs-content-wrapper"><span class="summary-content">image-test_0.png<');
$this
->assertSession()
->responseNotContains('paragraphs-collapsed-description"><div class="paragraphs-content-wrapper"><span class="summary-content">Paragraph type Text<');
// Check that the paragraph is collapsed by asserting the content summary.
$authenticated_role
->grantPermission('view paragraph content text');
$authenticated_role
->save();
$this
->drupalLogin($authenticated_user);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->pageTextContains('You are not allowed to edit or remove this Paragraph.');
$this
->assertSession()
->responseContains('paragraphs-collapsed-description"><div class="paragraphs-content-wrapper"><span class="summary-content">image-test_0.png<');
$this
->assertSession()
->responseContains('paragraphs-collapsed-description"><div class="paragraphs-content-wrapper"><span class="summary-content">Paragraph type Text<');
}