3 namespace Drupal\paragraphs_type_permissions\Tests;
5 use Drupal\Core\Entity\Entity;
6 use Drupal\field_ui\Tests\FieldUiTestTrait;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\user\Entity\Role;
11 * Tests the paragraphs type permissions.
15 class ParagraphsTypePermissionsTest extends WebTestBase {
24 public static $modules = array(
30 'paragraphs_type_permissions',
36 protected function setUp() {
41 * Tests paragraphs type permissions for anonymous and authenticated users.
43 public function testAnonymousParagraphsTypePermissions() {
44 // Create an authenticated user without special permissions for test.
45 $authenticated_user = $this->drupalCreateUser();
46 // Create an admin user for test.
47 $admin_user = $this->drupalCreateUser(array(
48 'administer site configuration',
50 'administer content types',
51 'administer node fields',
52 'administer node display',
53 'administer paragraphs types',
54 'administer paragraph form display',
55 'create paragraphed_content_demo content',
56 'edit any paragraphed_content_demo content',
57 'bypass paragraphs type content access',
59 $this->drupalLogin($admin_user);
61 // Enable the publish/unpublish checkbox fields.
67 foreach ($paragraph_types as $paragraph_type) {
68 entity_get_form_display('paragraph', $paragraph_type, 'default')
69 ->setComponent('status', [
70 'type' => 'boolean_checkbox'
75 // Create a node with some paragraph types.
76 $this->drupalGet('node/add/paragraphed_content_demo');
77 $this->drupalPostForm(NULL, NULL, t('Add Image + Text'));
78 $this->drupalPostForm(NULL, NULL, t('Add Images'));
79 $this->drupalPostForm(NULL, NULL, t('Add Text'));
81 $image_text = $this->drupalGetTestFiles('image')[0];
82 $this->drupalPostForm(NULL, [
83 'files[field_paragraphs_demo_0_subform_field_image_demo_0]' => $image_text->uri,
85 $images = $this->drupalGetTestFiles('image')[1];
86 $this->drupalPostForm(NULL, [
87 'files[field_paragraphs_demo_1_subform_field_images_demo_0][]' => $images->uri,
90 'title[0][value]' => 'paragraph node title',
91 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Paragraph type Image + Text',
92 'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Paragraph type Text',
94 $this->drupalPostForm(NULL, $edit, 'Save and publish');
96 // Get the node to edit it later.
97 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
99 // Get the images data to check for their presence.
100 $image_text_tag = '/files/styles/large/public/image-test_0.png?itok=';
101 $images_tag = '/files/styles/medium/public/image-test_0_0.png?itok=';
103 // Check that all paragraphs are shown for admin user.
104 $this->assertRaw($image_text_tag);
105 $this->assertRaw($images_tag);
106 $this->assertText('Paragraph type Image + Text');
107 $this->assertText('Paragraph type Text');
109 // Logout, check that no paragraphs are shown for anonymous user.
110 $this->drupalLogout();
111 $this->drupalGet('node/' . $node->id());
112 $this->assertNoRaw($image_text_tag);
113 $this->assertNoRaw($images_tag);
114 $this->assertNoText('Paragraph type Image + Text');
115 $this->assertNoText('Paragraph type Text');
117 // Login as authenticated user, check that no paragraphs are shown for him.
118 $this->drupalLogin($authenticated_user);
119 $this->drupalGet('node/' . $node->id());
120 $this->assertNoRaw($image_text_tag);
121 $this->assertNoRaw($images_tag);
122 $this->assertNoText('Paragraph type Image + Text');
123 $this->assertNoText('Paragraph type Text');
125 // Login as admin again to unpublish the 'Image + Text' paragraph type.
126 $this->drupalLogout();
127 $this->drupalLogin($admin_user);
128 $this->drupalGet('node/' . $node->id() . '/edit');
129 $this->assertFieldChecked('edit-field-paragraphs-demo-0-subform-status-value');
131 'field_paragraphs_demo[0][subform][status][value]' => FALSE,
133 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
135 // Check that 'Image + Text' paragraph is not shown anymore for admin user.
136 $this->assertNoRaw($image_text_tag);
137 $this->assertRaw($images_tag);
138 $this->assertNoText('Paragraph type Image + Text');
139 $this->assertText('Paragraph type Text');
141 $this->drupalLogout();
143 // Add permissions to anonymous user to view only 'Image + Text' and
144 // 'Text' paragraph contents.
145 /** @var \Drupal\user\RoleInterface $anonymous_role */
146 $anonymous_role = Role::load('anonymous');
147 $anonymous_role->grantPermission('view paragraph content image_text');
148 $anonymous_role->grantPermission('view paragraph content text');
149 $anonymous_role->save();
151 // Add permissions to authenticated user to view only 'Image + Text' and
152 // 'Images' paragraph contents.
153 /** @var \Drupal\user\RoleInterface $authenticated_role */
154 $authenticated_role = Role::load('authenticated');
155 $authenticated_role->grantPermission('view paragraph content image_text');
156 $authenticated_role->grantPermission('view paragraph content images');
157 $authenticated_role->save();
159 // Check that the anonymous user can only view the 'Text' paragraph.
160 $this->drupalGet('node/' . $node->id());
161 $this->assertNoRaw($image_text_tag);
162 $this->assertNoRaw($images_tag);
163 $this->assertNoText('Paragraph type Image + Text');
164 $this->assertText('Paragraph type Text');
166 // Check that the authenticated user can only view the 'Images' paragraph.
167 $this->drupalLogin($authenticated_user);
168 $this->drupalGet('node/' . $node->id());
169 $this->assertNoRaw($image_text_tag);
170 $this->assertRaw($images_tag);
171 $this->assertNoText('Paragraph type Image + Text');
172 $this->assertNoText('Paragraph type Text');
174 // Check the authenticated user with edit permission.
175 $authenticated_role->grantPermission('update paragraph content image_text');
176 $authenticated_role->grantPermission('bypass node access');
177 $authenticated_role->save();
178 $this->drupalLogin($authenticated_user);
179 $this->drupalGet('node/' . $node->id() . '/edit');
180 $this->assertRaw('Image + Text');
181 $this->assertText('Paragraph type Image + Text');
182 $this->assertText('You are not allowed to remove this Paragraph.');
183 $this->assertText('Published');
184 $this->assertText('Images');
185 $this->assertText('You are not allowed to edit or remove this Paragraph.');