Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\contact\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\contact\Entity\ContactForm;
|
Chris@0
|
6 use Drupal\Core\Mail\MailFormatHelper;
|
Chris@0
|
7 use Drupal\Core\Test\AssertMailTrait;
|
Chris@0
|
8 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@4
|
10 use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
|
Chris@0
|
11 use Drupal\user\RoleInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Tests site-wide contact form functionality.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @see \Drupal\Tests\contact\Functional\ContactStorageTest
|
Chris@0
|
17 *
|
Chris@0
|
18 * @group contact
|
Chris@0
|
19 */
|
Chris@0
|
20 class ContactSitewideTest extends BrowserTestBase {
|
Chris@0
|
21
|
Chris@0
|
22 use FieldUiTestTrait;
|
Chris@0
|
23 use AssertMailTrait;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Modules to enable.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var array
|
Chris@0
|
29 */
|
Chris@0
|
30 public static $modules = ['text', 'contact', 'field_ui', 'contact_test', 'block', 'error_service_test', 'dblog'];
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * {@inheritdoc}
|
Chris@0
|
34 */
|
Chris@0
|
35 protected function setUp() {
|
Chris@0
|
36 parent::setUp();
|
Chris@0
|
37 $this->drupalPlaceBlock('system_breadcrumb_block');
|
Chris@0
|
38 $this->drupalPlaceBlock('local_actions_block');
|
Chris@0
|
39 $this->drupalPlaceBlock('page_title_block');
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Tests configuration options and the site-wide contact form.
|
Chris@0
|
44 */
|
Chris@0
|
45 public function testSiteWideContact() {
|
Chris@0
|
46 // Tests name and email fields for authenticated and anonymous users.
|
Chris@0
|
47 $this->drupalLogin($this->drupalCreateUser(['access site-wide contact form']));
|
Chris@0
|
48 $this->drupalGet('contact');
|
Chris@0
|
49
|
Chris@0
|
50 // Ensure that there is no textfield for name.
|
Chris@0
|
51 $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'name']));
|
Chris@0
|
52
|
Chris@0
|
53 // Ensure that there is no textfield for email.
|
Chris@0
|
54 $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'mail']));
|
Chris@0
|
55
|
Chris@0
|
56 // Logout and retrieve the page as an anonymous user
|
Chris@0
|
57 $this->drupalLogout();
|
Chris@0
|
58 user_role_grant_permissions('anonymous', ['access site-wide contact form']);
|
Chris@0
|
59 $this->drupalGet('contact');
|
Chris@0
|
60
|
Chris@0
|
61 // Ensure that there is textfield for name.
|
Chris@0
|
62 $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'name']));
|
Chris@0
|
63
|
Chris@0
|
64 // Ensure that there is textfield for email.
|
Chris@0
|
65 $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'mail']));
|
Chris@0
|
66
|
Chris@0
|
67 // Create and log in administrative user.
|
Chris@0
|
68 $admin_user = $this->drupalCreateUser([
|
Chris@0
|
69 'access site-wide contact form',
|
Chris@0
|
70 'administer contact forms',
|
Chris@0
|
71 'administer users',
|
Chris@0
|
72 'administer account settings',
|
Chris@0
|
73 'administer contact_message display',
|
Chris@0
|
74 'administer contact_message fields',
|
Chris@0
|
75 'administer contact_message form display',
|
Chris@0
|
76 ]);
|
Chris@0
|
77 $this->drupalLogin($admin_user);
|
Chris@0
|
78
|
Chris@0
|
79 // Check the presence of expected cache tags.
|
Chris@0
|
80 $this->drupalGet('contact');
|
Chris@0
|
81 $this->assertCacheTag('config:contact.settings');
|
Chris@0
|
82
|
Chris@0
|
83 $flood_limit = 3;
|
Chris@0
|
84 $this->config('contact.settings')
|
Chris@0
|
85 ->set('flood.limit', $flood_limit)
|
Chris@0
|
86 ->set('flood.interval', 600)
|
Chris@0
|
87 ->save();
|
Chris@0
|
88
|
Chris@0
|
89 // Set settings.
|
Chris@0
|
90 $edit = [];
|
Chris@0
|
91 $edit['contact_default_status'] = TRUE;
|
Chris@0
|
92 $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
|
Chris@0
|
93 $this->assertText(t('The configuration options have been saved.'));
|
Chris@0
|
94
|
Chris@0
|
95 $this->drupalGet('admin/structure/contact');
|
Chris@0
|
96 // Default form exists.
|
Chris@0
|
97 $this->assertLinkByHref('admin/structure/contact/manage/feedback/delete');
|
Chris@0
|
98 // User form could not be changed or deleted.
|
Chris@0
|
99 // Cannot use ::assertNoLinkByHref as it does partial url matching and with
|
Chris@0
|
100 // field_ui enabled admin/structure/contact/manage/personal/fields exists.
|
Chris@0
|
101 // @todo: See https://www.drupal.org/node/2031223 for the above.
|
Chris@0
|
102 $edit_link = $this->xpath('//a[@href=:href]', [
|
Chris@4
|
103 ':href' => \Drupal::url('entity.contact_form.edit_form', ['contact_form' => 'personal']),
|
Chris@0
|
104 ]);
|
Chris@0
|
105 $this->assertTrue(empty($edit_link), format_string('No link containing href %href found.',
|
Chris@0
|
106 ['%href' => 'admin/structure/contact/manage/personal']
|
Chris@0
|
107 ));
|
Chris@0
|
108 $this->assertNoLinkByHref('admin/structure/contact/manage/personal/delete');
|
Chris@0
|
109
|
Chris@0
|
110 $this->drupalGet('admin/structure/contact/manage/personal');
|
Chris@0
|
111 $this->assertResponse(403);
|
Chris@0
|
112
|
Chris@0
|
113 // Delete old forms to ensure that new forms are used.
|
Chris@0
|
114 $this->deleteContactForms();
|
Chris@0
|
115 $this->drupalGet('admin/structure/contact');
|
Chris@0
|
116 $this->assertText('Personal', 'Personal form was not deleted');
|
Chris@0
|
117 $this->assertNoLinkByHref('admin/structure/contact/manage/feedback');
|
Chris@0
|
118
|
Chris@0
|
119 // Ensure that the contact form won't be shown without forms.
|
Chris@0
|
120 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
|
Chris@0
|
121 $this->drupalLogout();
|
Chris@0
|
122 $this->drupalGet('contact');
|
Chris@0
|
123 $this->assertResponse(404);
|
Chris@0
|
124
|
Chris@0
|
125 $this->drupalLogin($admin_user);
|
Chris@0
|
126 $this->drupalGet('contact');
|
Chris@0
|
127 $this->assertResponse(200);
|
Chris@0
|
128 $this->assertText(t('The contact form has not been configured.'));
|
Chris@0
|
129 // Test access personal form via site-wide contact page.
|
Chris@0
|
130 $this->drupalGet('contact/personal');
|
Chris@0
|
131 $this->assertResponse(403);
|
Chris@0
|
132
|
Chris@0
|
133 // Add forms.
|
Chris@0
|
134 // Test invalid recipients.
|
Chris@0
|
135 $invalid_recipients = ['invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com'];
|
Chris@0
|
136 foreach ($invalid_recipients as $invalid_recipient) {
|
Chris@0
|
137 $this->addContactForm($this->randomMachineName(16), $this->randomMachineName(16), $invalid_recipient, '', FALSE);
|
Chris@0
|
138 $this->assertRaw(t('%recipient is an invalid email address.', ['%recipient' => $invalid_recipient]));
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 // Test validation of empty form and recipients fields.
|
Chris@0
|
142 $this->addContactForm('', '', '', '', TRUE);
|
Chris@0
|
143 $this->assertText(t('Label field is required.'));
|
Chris@0
|
144 $this->assertText(t('Machine-readable name field is required.'));
|
Chris@0
|
145 $this->assertText(t('Recipients field is required.'));
|
Chris@0
|
146
|
Chris@0
|
147 // Test validation of max_length machine name.
|
Chris@0
|
148 $recipients = ['simpletest&@example.com', 'simpletest2@example.com', 'simpletest3@example.com'];
|
Chris@0
|
149 $max_length = EntityTypeInterface::BUNDLE_MAX_LENGTH;
|
Chris@0
|
150 $max_length_exceeded = $max_length + 1;
|
Chris@4
|
151 $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length_exceeded)), $label = $this->randomMachineName($max_length_exceeded), implode(',', [$recipients[0]]), '', TRUE);
|
Chris@0
|
152 $this->assertText(format_string('Machine-readable name cannot be longer than @max characters but is currently @exceeded characters long.', ['@max' => $max_length, '@exceeded' => $max_length_exceeded]));
|
Chris@4
|
153 $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length)), $label = $this->randomMachineName($max_length), implode(',', [$recipients[0]]), '', TRUE);
|
Chris@0
|
154 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
|
Chris@0
|
155
|
Chris@0
|
156 // Verify that the creation message contains a link to a contact form.
|
Chris@0
|
157 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']);
|
Chris@0
|
158 $this->assert(isset($view_link), 'The message area contains a link to a contact form.');
|
Chris@0
|
159
|
Chris@0
|
160 // Create first valid form.
|
Chris@4
|
161 $this->addContactForm($id = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0]]), '', TRUE);
|
Chris@0
|
162 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
|
Chris@0
|
163
|
Chris@0
|
164 // Verify that the creation message contains a link to a contact form.
|
Chris@0
|
165 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']);
|
Chris@0
|
166 $this->assert(isset($view_link), 'The message area contains a link to a contact form.');
|
Chris@0
|
167
|
Chris@0
|
168 // Check that the form was created in site default language.
|
Chris@0
|
169 $langcode = $this->config('contact.form.' . $id)->get('langcode');
|
Chris@0
|
170 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
|
Chris@0
|
171 $this->assertEqual($langcode, $default_langcode);
|
Chris@0
|
172
|
Chris@0
|
173 // Make sure the newly created form is included in the list of forms.
|
Chris@0
|
174 $this->assertNoUniqueText($label, 'New form included in forms list.');
|
Chris@0
|
175
|
Chris@0
|
176 // Ensure that the recipient email is escaped on the listing.
|
Chris@0
|
177 $this->drupalGet('admin/structure/contact');
|
Chris@0
|
178 $this->assertEscaped($recipients[0]);
|
Chris@0
|
179
|
Chris@0
|
180 // Test update contact form.
|
Chris@0
|
181 $this->updateContactForm($id, $label = $this->randomMachineName(16), $recipients_str = implode(',', [$recipients[0], $recipients[1]]), $reply = $this->randomMachineName(30), FALSE, 'Your message has been sent.', '/user');
|
Chris@0
|
182 $config = $this->config('contact.form.' . $id)->get();
|
Chris@0
|
183 $this->assertEqual($config['label'], $label);
|
Chris@0
|
184 $this->assertEqual($config['recipients'], [$recipients[0], $recipients[1]]);
|
Chris@0
|
185 $this->assertEqual($config['reply'], $reply);
|
Chris@0
|
186 $this->assertNotEqual($id, $this->config('contact.settings')->get('default_form'));
|
Chris@0
|
187 $this->assertText(t('Contact form @label has been updated.', ['@label' => $label]));
|
Chris@0
|
188 // Ensure the label is displayed on the contact page for this form.
|
Chris@0
|
189 $this->drupalGet('contact/' . $id);
|
Chris@0
|
190 $this->assertText($label);
|
Chris@0
|
191
|
Chris@0
|
192 // Reset the form back to be the default form.
|
Chris@0
|
193 $this->config('contact.settings')->set('default_form', $id)->save();
|
Chris@0
|
194
|
Chris@0
|
195 // Ensure that the contact form is shown without a form selection input.
|
Chris@0
|
196 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
|
Chris@0
|
197 $this->drupalLogout();
|
Chris@0
|
198 $this->drupalGet('contact');
|
Chris@0
|
199 $this->assertText(t('Your email address'));
|
Chris@0
|
200 $this->assertNoText(t('Form'));
|
Chris@0
|
201 $this->drupalLogin($admin_user);
|
Chris@0
|
202
|
Chris@0
|
203 // Add more forms.
|
Chris@4
|
204 $this->addContactForm(mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1]]), '', FALSE);
|
Chris@0
|
205 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
|
Chris@0
|
206
|
Chris@4
|
207 $this->addContactForm($name = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1], $recipients[2]]), '', FALSE);
|
Chris@0
|
208 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
|
Chris@0
|
209
|
Chris@0
|
210 // Try adding a form that already exists.
|
Chris@0
|
211 $this->addContactForm($name, $label, '', '', FALSE);
|
Chris@0
|
212 $this->assertNoText(t('Contact form @label has been added.', ['@label' => $label]));
|
Chris@0
|
213 $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'));
|
Chris@0
|
214
|
Chris@0
|
215 $this->drupalLogout();
|
Chris@0
|
216
|
Chris@0
|
217 // Check to see that anonymous user cannot see contact page without permission.
|
Chris@0
|
218 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
|
Chris@0
|
219 $this->drupalGet('contact');
|
Chris@0
|
220 $this->assertResponse(403);
|
Chris@0
|
221
|
Chris@0
|
222 // Give anonymous user permission and see that page is viewable.
|
Chris@0
|
223 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
|
Chris@0
|
224 $this->drupalGet('contact');
|
Chris@0
|
225 $this->assertResponse(200);
|
Chris@0
|
226
|
Chris@0
|
227 // Submit contact form with invalid values.
|
Chris@0
|
228 $this->submitContact('', $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
|
Chris@0
|
229 $this->assertText(t('Your name field is required.'));
|
Chris@0
|
230
|
Chris@0
|
231 $this->submitContact($this->randomMachineName(16), '', $this->randomMachineName(16), $id, $this->randomMachineName(64));
|
Chris@0
|
232 $this->assertText(t('Your email address field is required.'));
|
Chris@0
|
233
|
Chris@0
|
234 $this->submitContact($this->randomMachineName(16), $invalid_recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
|
Chris@0
|
235 $this->assertRaw(t('The email address %mail is not valid.', ['%mail' => 'invalid']));
|
Chris@0
|
236
|
Chris@0
|
237 $this->submitContact($this->randomMachineName(16), $recipients[0], '', $id, $this->randomMachineName(64));
|
Chris@0
|
238 $this->assertText(t('Subject field is required.'));
|
Chris@0
|
239
|
Chris@0
|
240 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, '');
|
Chris@0
|
241 $this->assertText(t('Message field is required.'));
|
Chris@0
|
242
|
Chris@0
|
243 // Test contact form with no default form selected.
|
Chris@0
|
244 $this->config('contact.settings')
|
Chris@0
|
245 ->set('default_form', '')
|
Chris@0
|
246 ->save();
|
Chris@0
|
247 $this->drupalGet('contact');
|
Chris@0
|
248 $this->assertResponse(404);
|
Chris@0
|
249
|
Chris@0
|
250 // Try to access contact form with non-existing form IDs.
|
Chris@0
|
251 $this->drupalGet('contact/0');
|
Chris@0
|
252 $this->assertResponse(404);
|
Chris@0
|
253 $this->drupalGet('contact/' . $this->randomMachineName());
|
Chris@0
|
254 $this->assertResponse(404);
|
Chris@0
|
255
|
Chris@0
|
256 // Submit contact form with correct values and check flood interval.
|
Chris@0
|
257 for ($i = 0; $i < $flood_limit; $i++) {
|
Chris@0
|
258 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
|
Chris@0
|
259 $this->assertText(t('Your message has been sent.'));
|
Chris@0
|
260 }
|
Chris@0
|
261 // Submit contact form one over limit.
|
Chris@0
|
262 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
|
Chris@0
|
263 $this->assertRaw(t('You cannot send more than %number messages in 10 min. Try again later.', ['%number' => $this->config('contact.settings')->get('flood.limit')]));
|
Chris@0
|
264
|
Chris@0
|
265 // Test listing controller.
|
Chris@0
|
266 $this->drupalLogin($admin_user);
|
Chris@0
|
267
|
Chris@0
|
268 $this->deleteContactForms();
|
Chris@0
|
269
|
Chris@0
|
270 $label = $this->randomMachineName(16);
|
Chris@0
|
271 $recipients = implode(',', [$recipients[0], $recipients[1], $recipients[2]]);
|
Chris@4
|
272 $contact_form = mb_strtolower($this->randomMachineName(16));
|
Chris@0
|
273 $this->addContactForm($contact_form, $label, $recipients, '', FALSE);
|
Chris@0
|
274 $this->drupalGet('admin/structure/contact');
|
Chris@0
|
275 $this->clickLink(t('Edit'));
|
Chris@0
|
276 $this->assertResponse(200);
|
Chris@0
|
277 $this->assertFieldByName('label', $label);
|
Chris@0
|
278
|
Chris@0
|
279 // Test field UI and field integration.
|
Chris@0
|
280 $this->drupalGet('admin/structure/contact');
|
Chris@0
|
281
|
Chris@0
|
282 $view_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and text()=:text]', [
|
Chris@0
|
283 ':href' => \Drupal::url('entity.contact_form.canonical', ['contact_form' => $contact_form]),
|
Chris@0
|
284 ':text' => $label,
|
Chris@0
|
285 ]
|
Chris@0
|
286 );
|
Chris@0
|
287 $this->assertTrue(!empty($view_link), 'Contact listing links to contact form.');
|
Chris@0
|
288
|
Chris@0
|
289 // Find out in which row the form we want to add a field to is.
|
Chris@0
|
290 foreach ($this->xpath('//table/tbody/tr') as $row) {
|
Chris@0
|
291 if ($row->findLink($label)) {
|
Chris@0
|
292 $row->clickLink('Manage fields');
|
Chris@0
|
293 break;
|
Chris@0
|
294 }
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 $this->assertResponse(200);
|
Chris@0
|
298 $this->clickLink(t('Add field'));
|
Chris@0
|
299 $this->assertResponse(200);
|
Chris@0
|
300
|
Chris@0
|
301 // Create a simple textfield.
|
Chris@4
|
302 $field_name = mb_strtolower($this->randomMachineName());
|
Chris@0
|
303 $field_label = $this->randomMachineName();
|
Chris@0
|
304 $this->fieldUIAddNewField(NULL, $field_name, $field_label, 'text');
|
Chris@0
|
305 $field_name = 'field_' . $field_name;
|
Chris@0
|
306
|
Chris@0
|
307 // Check preview field can be ordered.
|
Chris@0
|
308 $this->drupalGet('admin/structure/contact/manage/' . $contact_form . '/form-display');
|
Chris@0
|
309 $this->assertText(t('Preview'));
|
Chris@0
|
310
|
Chris@0
|
311 // Check that the field is displayed.
|
Chris@0
|
312 $this->drupalGet('contact/' . $contact_form);
|
Chris@0
|
313 $this->assertText($field_label);
|
Chris@0
|
314
|
Chris@0
|
315 // Submit the contact form and verify the content.
|
Chris@0
|
316 $edit = [
|
Chris@0
|
317 'subject[0][value]' => $this->randomMachineName(),
|
Chris@0
|
318 'message[0][value]' => $this->randomMachineName(),
|
Chris@0
|
319 $field_name . '[0][value]' => $this->randomMachineName(),
|
Chris@0
|
320 ];
|
Chris@0
|
321 $this->drupalPostForm(NULL, $edit, t('Send message'));
|
Chris@0
|
322 $mails = $this->getMails();
|
Chris@0
|
323 $mail = array_pop($mails);
|
Chris@0
|
324 $this->assertEqual($mail['subject'], t('[@label] @subject', ['@label' => $label, '@subject' => $edit['subject[0][value]']]));
|
Chris@0
|
325 $this->assertTrue(strpos($mail['body'], $field_label));
|
Chris@0
|
326 $this->assertTrue(strpos($mail['body'], $edit[$field_name . '[0][value]']));
|
Chris@0
|
327
|
Chris@0
|
328 // Test messages and redirect.
|
Chris@0
|
329 /** @var \Drupal\contact\ContactFormInterface $form */
|
Chris@0
|
330 $form = ContactForm::load($contact_form);
|
Chris@0
|
331 $form->setMessage('Thanks for your submission.');
|
Chris@0
|
332 $form->setRedirectPath('/user/' . $admin_user->id());
|
Chris@0
|
333 $form->save();
|
Chris@0
|
334 // Check that the field is displayed.
|
Chris@0
|
335 $this->drupalGet('contact/' . $contact_form);
|
Chris@0
|
336
|
Chris@0
|
337 // Submit the contact form and verify the content.
|
Chris@0
|
338 $edit = [
|
Chris@0
|
339 'subject[0][value]' => $this->randomMachineName(),
|
Chris@0
|
340 'message[0][value]' => $this->randomMachineName(),
|
Chris@0
|
341 $field_name . '[0][value]' => $this->randomMachineName(),
|
Chris@0
|
342 ];
|
Chris@0
|
343 $this->drupalPostForm(NULL, $edit, t('Send message'));
|
Chris@0
|
344 $this->assertText('Thanks for your submission.');
|
Chris@0
|
345 $this->assertUrl('user/' . $admin_user->id());
|
Chris@0
|
346
|
Chris@0
|
347 // Test Empty message.
|
Chris@0
|
348 /** @var \Drupal\contact\ContactFormInterface $form */
|
Chris@0
|
349 $form = ContactForm::load($contact_form);
|
Chris@0
|
350 $form->setMessage('');
|
Chris@0
|
351 $form->setRedirectPath('/user/' . $admin_user->id());
|
Chris@0
|
352 $form->save();
|
Chris@0
|
353 $this->drupalGet('admin/structure/contact/manage/' . $contact_form);
|
Chris@0
|
354 // Check that the field is displayed.
|
Chris@0
|
355 $this->drupalGet('contact/' . $contact_form);
|
Chris@0
|
356
|
Chris@0
|
357 // Submit the contact form and verify the content.
|
Chris@0
|
358 $edit = [
|
Chris@0
|
359 'subject[0][value]' => $this->randomMachineName(),
|
Chris@0
|
360 'message[0][value]' => $this->randomMachineName(),
|
Chris@0
|
361 $field_name . '[0][value]' => $this->randomMachineName(),
|
Chris@0
|
362 ];
|
Chris@0
|
363 $this->drupalPostForm(NULL, $edit, t('Send message'));
|
Chris@0
|
364 $result = $this->xpath('//div[@role=:role]', [':role' => 'contentinfo']);
|
Chris@0
|
365 $this->assertEqual(count($result), 0, 'Messages not found.');
|
Chris@0
|
366 $this->assertUrl('user/' . $admin_user->id());
|
Chris@0
|
367
|
Chris@0
|
368 // Test preview and visibility of the message field and label. Submit the
|
Chris@0
|
369 // contact form and verify the content.
|
Chris@0
|
370 $edit = [
|
Chris@0
|
371 'subject[0][value]' => $this->randomMachineName(),
|
Chris@0
|
372 'message[0][value]' => $this->randomMachineName(),
|
Chris@0
|
373 $field_name . '[0][value]' => $this->randomMachineName(),
|
Chris@0
|
374 ];
|
Chris@0
|
375 $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview'));
|
Chris@0
|
376
|
Chris@0
|
377 // Message is now by default displayed twice, once for the form element and
|
Chris@0
|
378 // once for the viewed message.
|
Chris@0
|
379 $page_text = $this->getSession()->getPage()->getText();
|
Chris@0
|
380 $this->assertGreaterThan(1, substr_count($page_text, t('Message')));
|
Chris@0
|
381 $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-above');
|
Chris@0
|
382 $this->assertSession()->pageTextContains($edit['message[0][value]']);
|
Chris@0
|
383
|
Chris@0
|
384 // Hide the message field label.
|
Chris@0
|
385 $display_edit = [
|
Chris@0
|
386 'fields[message][label]' => 'hidden',
|
Chris@0
|
387 ];
|
Chris@0
|
388 $this->drupalPostForm('admin/structure/contact/manage/' . $contact_form . '/display', $display_edit, t('Save'));
|
Chris@0
|
389
|
Chris@0
|
390 $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview'));
|
Chris@0
|
391 // Message should only be displayed once now.
|
Chris@0
|
392 $page_text = $this->getSession()->getPage()->getText();
|
Chris@0
|
393 $this->assertEquals(1, substr_count($page_text, t('Message')));
|
Chris@0
|
394 $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-hidden field__item">');
|
Chris@0
|
395 $this->assertSession()->pageTextContains($edit['message[0][value]']);
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@0
|
398 /**
|
Chris@0
|
399 * Tests auto-reply on the site-wide contact form.
|
Chris@0
|
400 */
|
Chris@0
|
401 public function testAutoReply() {
|
Chris@0
|
402 // Create and log in administrative user.
|
Chris@0
|
403 $admin_user = $this->drupalCreateUser([
|
Chris@0
|
404 'access site-wide contact form',
|
Chris@0
|
405 'administer contact forms',
|
Chris@0
|
406 'administer permissions',
|
Chris@0
|
407 'administer users',
|
Chris@4
|
408 'access site reports',
|
Chris@0
|
409 ]);
|
Chris@0
|
410 $this->drupalLogin($admin_user);
|
Chris@0
|
411
|
Chris@0
|
412 // Set up three forms, 2 with an auto-reply and one without.
|
Chris@0
|
413 $foo_autoreply = $this->randomMachineName(40);
|
Chris@0
|
414 $bar_autoreply = $this->randomMachineName(40);
|
Chris@0
|
415 $this->addContactForm('foo', 'foo', 'foo@example.com', $foo_autoreply, FALSE);
|
Chris@0
|
416 $this->addContactForm('bar', 'bar', 'bar@example.com', $bar_autoreply, FALSE);
|
Chris@0
|
417 $this->addContactForm('no_autoreply', 'no_autoreply', 'bar@example.com', '', FALSE);
|
Chris@0
|
418
|
Chris@0
|
419 // Log the current user out in order to test the name and email fields.
|
Chris@0
|
420 $this->drupalLogout();
|
Chris@0
|
421 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
|
Chris@0
|
422
|
Chris@0
|
423 // Test the auto-reply for form 'foo'.
|
Chris@0
|
424 $email = $this->randomMachineName(32) . '@example.com';
|
Chris@0
|
425 $subject = $this->randomMachineName(64);
|
Chris@0
|
426 $this->submitContact($this->randomMachineName(16), $email, $subject, 'foo', $this->randomString(128));
|
Chris@0
|
427
|
Chris@0
|
428 // We are testing the auto-reply, so there should be one email going to the sender.
|
Chris@0
|
429 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
|
Chris@0
|
430 $this->assertEqual(count($captured_emails), 1);
|
Chris@0
|
431 $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply)));
|
Chris@0
|
432
|
Chris@0
|
433 // Test the auto-reply for form 'bar'.
|
Chris@0
|
434 $email = $this->randomMachineName(32) . '@example.com';
|
Chris@0
|
435 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'bar', $this->randomString(128));
|
Chris@0
|
436
|
Chris@0
|
437 // Auto-reply for form 'bar' should result in one auto-reply email to the sender.
|
Chris@0
|
438 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
|
Chris@0
|
439 $this->assertEqual(count($captured_emails), 1);
|
Chris@0
|
440 $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply)));
|
Chris@0
|
441
|
Chris@0
|
442 // Verify that no auto-reply is sent when the auto-reply field is left blank.
|
Chris@0
|
443 $email = $this->randomMachineName(32) . '@example.com';
|
Chris@0
|
444 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'no_autoreply', $this->randomString(128));
|
Chris@0
|
445 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
|
Chris@0
|
446 $this->assertEqual(count($captured_emails), 0);
|
Chris@0
|
447
|
Chris@0
|
448 // Verify that the current error message doesn't show, that the auto-reply
|
Chris@0
|
449 // doesn't get sent and the correct silent error gets logged.
|
Chris@0
|
450 $email = '';
|
Chris@0
|
451 entity_get_form_display('contact_message', 'foo', 'default')
|
Chris@0
|
452 ->removeComponent('mail')
|
Chris@0
|
453 ->save();
|
Chris@0
|
454 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'foo', $this->randomString(128));
|
Chris@0
|
455 $this->assertNoText('Unable to send email. Contact the site administrator if the problem persists.');
|
Chris@0
|
456 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
|
Chris@0
|
457 $this->assertEqual(count($captured_emails), 0);
|
Chris@0
|
458 $this->drupalLogin($admin_user);
|
Chris@0
|
459 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
460 $this->assertRaw('Error sending auto-reply, missing sender e-mail address in foo');
|
Chris@0
|
461 }
|
Chris@0
|
462
|
Chris@0
|
463 /**
|
Chris@0
|
464 * Adds a form.
|
Chris@0
|
465 *
|
Chris@0
|
466 * @param string $id
|
Chris@0
|
467 * The form machine name.
|
Chris@0
|
468 * @param string $label
|
Chris@0
|
469 * The form label.
|
Chris@0
|
470 * @param string $recipients
|
Chris@0
|
471 * The list of recipient email addresses.
|
Chris@0
|
472 * @param string $reply
|
Chris@0
|
473 * The auto-reply text that is sent to a user upon completing the contact
|
Chris@0
|
474 * form.
|
Chris@0
|
475 * @param bool $selected
|
Chris@0
|
476 * A Boolean indicating whether the form should be selected by default.
|
Chris@0
|
477 * @param string $message
|
Chris@0
|
478 * The message that will be displayed to a user upon completing the contact
|
Chris@0
|
479 * form.
|
Chris@0
|
480 * @param array $third_party_settings
|
Chris@0
|
481 * Array of third party settings to be added to the posted form data.
|
Chris@0
|
482 */
|
Chris@0
|
483 public function addContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $third_party_settings = []) {
|
Chris@0
|
484 $edit = [];
|
Chris@0
|
485 $edit['label'] = $label;
|
Chris@0
|
486 $edit['id'] = $id;
|
Chris@0
|
487 $edit['message'] = $message;
|
Chris@0
|
488 $edit['recipients'] = $recipients;
|
Chris@0
|
489 $edit['reply'] = $reply;
|
Chris@0
|
490 $edit['selected'] = ($selected ? TRUE : FALSE);
|
Chris@0
|
491 $edit += $third_party_settings;
|
Chris@0
|
492 $this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
|
Chris@0
|
493 }
|
Chris@0
|
494
|
Chris@0
|
495 /**
|
Chris@0
|
496 * Updates a form.
|
Chris@0
|
497 *
|
Chris@0
|
498 * @param string $id
|
Chris@0
|
499 * The form machine name.
|
Chris@0
|
500 * @param string $label
|
Chris@0
|
501 * The form label.
|
Chris@0
|
502 * @param string $recipients
|
Chris@0
|
503 * The list of recipient email addresses.
|
Chris@0
|
504 * @param string $reply
|
Chris@0
|
505 * The auto-reply text that is sent to a user upon completing the contact
|
Chris@0
|
506 * form.
|
Chris@0
|
507 * @param bool $selected
|
Chris@0
|
508 * A Boolean indicating whether the form should be selected by default.
|
Chris@0
|
509 * @param string $message
|
Chris@0
|
510 * The message that will be displayed to a user upon completing the contact
|
Chris@0
|
511 * form.
|
Chris@0
|
512 * @param string $redirect
|
Chris@0
|
513 * The path where user will be redirect after this form has been submitted..
|
Chris@0
|
514 */
|
Chris@0
|
515 public function updateContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $redirect = '/') {
|
Chris@0
|
516 $edit = [];
|
Chris@0
|
517 $edit['label'] = $label;
|
Chris@0
|
518 $edit['recipients'] = $recipients;
|
Chris@0
|
519 $edit['reply'] = $reply;
|
Chris@0
|
520 $edit['selected'] = ($selected ? TRUE : FALSE);
|
Chris@0
|
521 $edit['message'] = $message;
|
Chris@0
|
522 $edit['redirect'] = $redirect;
|
Chris@0
|
523 $this->drupalPostForm("admin/structure/contact/manage/$id", $edit, t('Save'));
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 /**
|
Chris@0
|
527 * Submits the contact form.
|
Chris@0
|
528 *
|
Chris@0
|
529 * @param string $name
|
Chris@0
|
530 * The name of the sender.
|
Chris@0
|
531 * @param string $mail
|
Chris@0
|
532 * The email address of the sender.
|
Chris@0
|
533 * @param string $subject
|
Chris@0
|
534 * The subject of the message.
|
Chris@0
|
535 * @param string $id
|
Chris@0
|
536 * The form ID of the message.
|
Chris@0
|
537 * @param string $message
|
Chris@0
|
538 * The message body.
|
Chris@0
|
539 */
|
Chris@0
|
540 public function submitContact($name, $mail, $subject, $id, $message) {
|
Chris@0
|
541 $edit = [];
|
Chris@0
|
542 $edit['name'] = $name;
|
Chris@0
|
543 $edit['mail'] = $mail;
|
Chris@0
|
544 $edit['subject[0][value]'] = $subject;
|
Chris@0
|
545 $edit['message[0][value]'] = $message;
|
Chris@0
|
546 if ($id == $this->config('contact.settings')->get('default_form')) {
|
Chris@0
|
547 $this->drupalPostForm('contact', $edit, t('Send message'));
|
Chris@0
|
548 }
|
Chris@0
|
549 else {
|
Chris@0
|
550 $this->drupalPostForm('contact/' . $id, $edit, t('Send message'));
|
Chris@0
|
551 }
|
Chris@0
|
552 }
|
Chris@0
|
553
|
Chris@0
|
554 /**
|
Chris@0
|
555 * Deletes all forms.
|
Chris@0
|
556 */
|
Chris@0
|
557 public function deleteContactForms() {
|
Chris@0
|
558 $contact_forms = ContactForm::loadMultiple();;
|
Chris@0
|
559 foreach ($contact_forms as $id => $contact_form) {
|
Chris@0
|
560 if ($id == 'personal') {
|
Chris@0
|
561 // Personal form could not be deleted.
|
Chris@0
|
562 $this->drupalGet("admin/structure/contact/manage/$id/delete");
|
Chris@0
|
563 $this->assertResponse(403);
|
Chris@0
|
564 }
|
Chris@0
|
565 else {
|
Chris@0
|
566 $this->drupalPostForm("admin/structure/contact/manage/$id/delete", [], t('Delete'));
|
Chris@0
|
567 $this->assertRaw(t('The contact form %label has been deleted.', ['%label' => $contact_form->label()]));
|
Chris@0
|
568 $this->assertFalse(ContactForm::load($id), format_string('Form %contact_form not found', ['%contact_form' => $contact_form->label()]));
|
Chris@0
|
569 }
|
Chris@0
|
570 }
|
Chris@0
|
571 }
|
Chris@0
|
572
|
Chris@0
|
573 }
|