contact.test 10.7 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
/**
 * Test the sitewide contact form.
 */
class ContactSitewideTestCase extends DrupalWebTestCase {

9
10
11
12
13
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
14
15
      'name' => t('Site-wide contact form'),
      'description' => t('Tests site-wide contact form functionality.'),
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
      'group' => t('Contact'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('contact');
  }

  /**
   * Test configuration options and site-wide contact form.
   */
  function testSiteWideContact() {
    // Create and login administative user.
    $admin_user = $this->drupalCreateUser(array('administer site-wide contact form', 'administer permissions'));
    $this->drupalLogin($admin_user);

    // Set settings.
    $edit = array();
    $edit['contact_form_information'] = $this->randomName(100);
    $edit['contact_hourly_threshold'] = 3;
    $edit['contact_default_status'] = TRUE;
    $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));

    // Delete old categories to ensure that new categories are used.
    $this->deleteCategories();

    // Add categories.
    // Test invalid recipients.
    $invalid_recipients = array('invalid', 'invalid@', /*'invalid@site', 'invalid@site.',*/ '@site.', '@site.com');
    foreach ($invalid_recipients as $invalid_recipient) {
      $this->addCategory($this->randomName(16), $invalid_recipient, '', FALSE);
51
      $this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)), t('Caught invalid recipient (' . $invalid_recipient . ').'));
52
53
    }

54
55
56
57
58
    // Test validation of empty category and recipients fields.
    $this->addCategory($category = '', '', '', TRUE);
    $this->assertText(t('Category field is required.'), t('Caught empty category field'));
    $this->assertText(t('Recipients field is required.'), t('Caught empty recipients field.'));

59
    // Create valid categories.
60
    $recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
61
62
63
64
65
66
67
68
69
    $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
    $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));

    $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
    $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));

    $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
    $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));

70
    // Clear flood table in preparation for flood test and allow other checks to complete.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    $this->assertTrue(db_query('DELETE FROM {flood}'), t('Flood table emptied.'));

    // Check to see that anonymous user cannot see contact page without permission.
    $this->setPermission('anonymous user', array('access site-wide contact form' => FALSE));
    $this->drupalLogout();

    $this->drupalGet('contact');
    $this->assertResponse(403, t('Access denied to anonymous user without permission.'));

    // Give anonymous user permission and see that page is viewable.
    $this->drupalLogin($admin_user);
    $this->setPermission('anonymous user', array('access site-wide contact form' => TRUE));
    $this->drupalLogout();

    $this->drupalGet('contact');
    $this->assertResponse(200, t('Access granted to anonymous user with permission.'));

    // Check that "Additional information" is displayed on the page.
    $this->assertText($edit['contact_form_information'], t('Contact information displayed.'));

    // Submit contact form with invalid values.
    $categories = $this->getCategories();
    $this->submitContact('', $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
    $this->assertText(t('Your name field is required.'), t('Name required.'));

    $this->submitContact($this->randomName(16), '', $this->randomName(16), $categories[0], $this->randomName(64));
    $this->assertText(t('Your e-mail address field is required.'), t('E-mail required.'));

    $this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
    $this->assertText(t('You must enter a valid e-mail address.'), t('Valid e-mail required.'));

    $this->submitContact($this->randomName(16), $recipients[0], '', $categories[0], $this->randomName(64));
    $this->assertText(t('Subject field is required.'), t('Subject required.'));

    $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], '');
    $this->assertText(t('Message field is required.'), t('Message required.'));

    // Submit contact form with correct values and check flood interval.
    for ($i = 0; $i < $edit['contact_hourly_threshold']; $i++) {
      $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
      $this->assertText(t('Your message has been sent.'), t('Message sent.'));
    }
    // Submit contact form one over limit.
    $this->drupalGet('contact');
    $this->assertRaw(t('You cannot send more than %number messages per hour. Please try again later.', array('%number' => $edit['contact_hourly_threshold'])), t('Message threshold reached.'));

    // Delete created categories.
    $this->drupalLogin($admin_user);
    $this->deleteCategories();
  }

  /**
   * Add a category.
   *
   * @param string $category Name of category.
   * @param string $recipients List of recipient e-mail addresses.
   * @param string $reply Auto-reply text.
   * @param boolean $selected Defautly selected.
   */
  function addCategory($category, $recipients, $reply, $selected) {
    $edit = array();
    $edit['category'] = $category;
    $edit['recipients'] = $recipients;
    $edit['reply'] = $reply;
    $edit['selected'] = ($selected ? '1' : '0');
    $this->drupalPost('admin/build/contact/add', $edit, t('Save'));
  }

  /**
   * Submit contact form.
   *
   * @param string $name Name.
   * @param string $mail E-mail address.
   * @param string $subject Subject.
   * @param integer $cid Category id.
   * @param string $message Message.
   */
  function submitContact($name, $mail, $subject, $cid, $message) {
    $edit = array();
    $edit['name'] = $name;
    $edit['mail'] = $mail;
    $edit['subject'] = $subject;
    $edit['cid'] = $cid;
    $edit['message'] = $message;
    $this->drupalPost('contact', $edit, t('Send e-mail'));
  }

  /**
   * Delete all categories.
   */
  function deleteCategories() {
    $categories = $this->getCategories();
    foreach ($categories as $category) {
      $category_name = db_result(db_query('SELECT category FROM {contact} WHERE cid = %d', array($category)));
165
      $this->drupalPost('admin/build/contact/delete/' . $category, array(), t('Delete'));
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
      $this->assertRaw(t('Category %category has been deleted.', array('%category' => $category_name)), t('Category deleted sucessfully.'));
    }
  }

  /**
   * Get list category ids.
   *
   * @return array Category ids.
   */
  function getCategories() {
    $result = db_query('SELECT cid FROM {contact}');
    $categories = array();
    while ($category = db_result($result)) {
      $categories[] = $category;
    }
    return $categories;
  }

  /**
   * Set permission.
   *
   * @param string $role User role to set permissions for.
   * @param array $permissions Key-value array of permissions to set.
   */
  function setPermission($role, $permissions) {
    // Get role id (rid) for specified role.
    $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array($role)));
    if ($rid === FALSE) {
194
      $this->fail(t(' [permission] Role "' . $role . '" not found.'));
195
196
197
198
199
    }

    // Create edit array from permission.
    $edit = array();
    foreach ($permissions as $name => $value) {
200
      $edit[$rid . '[' . $name . ']'] = $value;
201
202
203
204
205
206
    }

    $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
    $this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
  }
}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

/**
 * Test the personal contact form.
 */
class ContactPersonalTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Personal contact form'),
      'description' => t('Tests personal contact form functionality.'),
      'group' => t('Contact'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('contact');
  }

  /**
   * Test personal contact form.
   */
  function testPersonalContact() {
    $admin_user = $this->drupalCreateUser(array('administer site-wide contact form'));
    $this->drupalLogin($admin_user);

    // Enable the personal contact form.
    $edit = array();
    $edit['contact_default_status'] = TRUE;
    $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));

    // Reload variables.
    $this->drupalLogout();

    // Create web users and attempt to use personal contact forms with default set to true.
    $web_user1 = $this->drupalCreateUser(array());
    $web_user2 = $this->drupalCreateUser(array());

    $this->drupalLogin($web_user1);

    $this->drupalGet('user/' . $web_user2->uid . '/contact');
    $this->assertResponse(200, t('Access to personal contact form granted.'));

    $edit = array();
    $edit['subject'] = $this->randomName(16);
    $edit['message'] = $this->randomName(64);
    $this->drupalPost(NULL, $edit, t('Send e-mail'));
    $this->assertText(t('The message has been sent.'), t('Message sent.'));

    $this->drupalLogout();

    $this->drupalLogin($admin_user);

    // Disable the personal contact form.
    $edit = array();
    $edit['contact_default_status'] = FALSE;
    $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));

    // Reload variables.
    $this->drupalLogout();

    // Create web users and attempt to use personal contact forms with default set to false.
    $web_user3 = $this->drupalCreateUser(array());
    $web_user4 = $this->drupalCreateUser(array());

    $this->drupalLogin($web_user3);

    $this->drupalGet('user/' . $web_user4->uid . '/contact');
    $this->assertResponse(403, t('Access to personal contact form denied.'));
  }
284
}