Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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\field_ui\Tests\FieldUiTestTrait;
6 use Drupal\paragraphs\Tests\Classic\ParagraphsCoreVersionUiTestTrait;
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, ParagraphsCoreVersionUiTestTrait;
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 content types',
50       'administer node fields',
51       'administer node display',
52       'administer paragraphs types',
53       'administer paragraph form display',
54       'create paragraphed_content_demo content',
55       'edit any paragraphed_content_demo content',
56       'bypass paragraphs type content access',
57       'administer node form display',
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, t('Save'));
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
126     $this->drupalLogout();
127     $this->drupalLogin($admin_user);
128
129     // Set edit mode to open.
130     $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/form-display');
131     $this->drupalPostAjaxForm(NULL, [], "field_paragraphs_demo_settings_edit");
132     $edit = ['fields[field_paragraphs_demo][settings_edit_form][settings][edit_mode]' => 'open'];
133     $this->drupalPostForm(NULL, $edit, t('Save'));
134
135     // Unpublish the 'Image + Text' paragraph type.
136     $this->drupalGet('node/' . $node->id() . '/edit');
137     $this->assertFieldChecked('edit-field-paragraphs-demo-0-subform-status-value');
138     $edit = [
139       'field_paragraphs_demo[0][subform][status][value]' => FALSE,
140     ];
141     $this->drupalPostForm(NULL, $edit, t('Save'));
142
143     // Check that 'Image + Text' paragraph is not shown anymore for admin user.
144     $this->assertNoRaw($image_text_tag);
145     $this->assertRaw($images_tag);
146     $this->assertNoText('Paragraph type Image + Text');
147     $this->assertText('Paragraph type Text');
148
149     $this->drupalLogout();
150
151     // Add permissions to anonymous user to view only 'Image + Text' and
152     // 'Text' paragraph contents.
153     /** @var \Drupal\user\RoleInterface $anonymous_role */
154     $anonymous_role = Role::load('anonymous');
155     $anonymous_role->grantPermission('view paragraph content image_text');
156     $anonymous_role->grantPermission('view paragraph content text');
157     $anonymous_role->save();
158
159     // Add permissions to authenticated user to view only 'Image + Text' and
160     // 'Images' paragraph contents.
161     /** @var \Drupal\user\RoleInterface $authenticated_role */
162     $authenticated_role = Role::load('authenticated');
163     $authenticated_role->grantPermission('view paragraph content image_text');
164     $authenticated_role->grantPermission('view paragraph content images');
165     $authenticated_role->save();
166
167     // Check that the anonymous user can only view the 'Text' paragraph.
168     $this->drupalGet('node/' . $node->id());
169     $this->assertNoRaw($image_text_tag);
170     $this->assertNoRaw($images_tag);
171     $this->assertNoText('Paragraph type Image + Text');
172     $this->assertText('Paragraph type Text');
173
174     // Check that the authenticated user can only view the 'Images' paragraph.
175     $this->drupalLogin($authenticated_user);
176     $this->drupalGet('node/' . $node->id());
177     $this->assertNoRaw($image_text_tag);
178     $this->assertRaw($images_tag);
179     $this->assertNoText('Paragraph type Image + Text');
180     $this->assertNoText('Paragraph type Text');
181
182     // Check the authenticated user with edit permission.
183     $authenticated_role->grantPermission('update paragraph content image_text');
184     $authenticated_role->grantPermission('bypass node access');
185     $authenticated_role->save();
186     $this->drupalLogin($authenticated_user);
187     $this->drupalGet('node/' . $node->id() . '/edit');
188     $this->assertRaw('Image + Text');
189     $this->assertText('Paragraph type Image + Text');
190     $this->assertText('You are not allowed to remove this Paragraph.');
191     $this->assertText('Published');
192     $this->assertText('Images');
193     $this->assertText('You are not allowed to edit or remove this Paragraph.');
194   }
195
196 }