Version 1
[yaffs-website] / web / modules / contrib / paragraphs / modules / paragraphs_type_permissions / src / Tests / ParagraphsTypePermissionsTest.php
1 <?php
2
3 namespace Drupal\paragraphs_type_permissions\Tests;
4
5 use Drupal\Core\Entity\Entity;
6 use Drupal\field_ui\Tests\FieldUiTestTrait;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\user\Entity\Role;
9
10 /**
11  * Tests the paragraphs type permissions.
12  *
13  * @group paragraphs
14  */
15 class ParagraphsTypePermissionsTest extends WebTestBase {
16
17   use FieldUiTestTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = array(
25     'node',
26     'paragraphs_demo',
27     'field',
28     'image',
29     'field_ui',
30     'paragraphs_type_permissions',
31   );
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38   }
39
40   /**
41    * Tests paragraphs type permissions for anonymous and authenticated users.
42    */
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',
49       'administer nodes',
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',
58     ));
59     $this->drupalLogin($admin_user);
60
61     // Enable the publish/unpublish checkbox fields.
62     $paragraph_types = [
63       'image_text',
64       'images',
65       'text',
66     ];
67     foreach ($paragraph_types as $paragraph_type) {
68       entity_get_form_display('paragraph', $paragraph_type, 'default')
69         ->setComponent('status', [
70           'type' => 'boolean_checkbox'
71         ])
72         ->save();
73     }
74
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'));
80
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,
84     ], t('Upload'));
85     $images = $this->drupalGetTestFiles('image')[1];
86     $this->drupalPostForm(NULL, [
87       'files[field_paragraphs_demo_1_subform_field_images_demo_0][]' => $images->uri,
88     ], t('Upload'));
89     $edit = [
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',
93     ];
94     $this->drupalPostForm(NULL, $edit, 'Save and publish');
95
96     // Get the node to edit it later.
97     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
98
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=';
102
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');
108
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');
116
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');
124
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');
130     $edit = [
131       'field_paragraphs_demo[0][subform][status][value]' => FALSE,
132     ];
133     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
134
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');
140
141     $this->drupalLogout();
142
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();
150
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();
158
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');
165
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');
173
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.');
186   }
187
188 }