Line # | Frequency | Source Line |
1 | | <?php
|
2 | | // $Id: contact.test,v 1.10 2008/04/01 23:33:54 boombatower Exp $
|
3 | |
|
4 | | class ContactTestCase extends DrupalTestCase {
|
5 | | /**
|
6 | | * Implementation of getInfo().
|
7 | | */
|
8 | | function getInfo() {
|
9 | | return array(
|
10 | 1 | 'name' => t('Contact functionality'),
|
11 | | 'description' => t('Test site-wide contact form and personal contact form functionality.'),
|
12 | | 'group' => t('Contact Tests'),
|
13 | | );
|
14 | | }
|
15 | |
|
16 | | /**
|
17 | | * Implementation of setUp().
|
18 | | */
|
19 | | function setUp() {
|
20 | | parent::setUp();
|
21 | |
|
22 | | $this->drupalModuleEnable('contact');
|
23 | | }
|
24 | |
|
25 | | /**
|
26 | | * Test configuration options and site-wide contact form.
|
27 | | */
|
28 | | function testSiteWideContact() {
|
29 | | // Create and login administative user.
|
30 | | $admin_user = $this->drupalCreateUser(array('administer site-wide contact form', 'administer permissions'));
|
31 | | $this->drupalLogin($admin_user);
|
32 | |
|
33 | | // Set settings.
|
34 | | $edit = array();
|
35 | | $edit['contact_form_information'] = $this->randomName(100);
|
36 | | $edit['contact_hourly_threshold'] = 3;
|
37 | | $edit['contact_default_status'] = TRUE;
|
38 | | $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
39 | | $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
40 | |
|
41 | | $this->reloadVariables();
|
42 | |
|
43 | | // Delete old categories to ensure that new categories are used.
|
44 | | $this->deleteCategories();
|
45 | |
|
46 | | // Add categories.
|
47 | | // Test invalid recipients.
|
48 | | $invalid_recipients = array('invalid', 'invalid@', /*'invalid@site', 'invalid@site.',*/ '@site.', '@site.com');
|
49 | | foreach ($invalid_recipients as $invalid_recipient) {
|
50 | | $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 | | // Create valid categories.
|
55 | | $recipients = array('simpletest@test.com', 'simpletest2@test.com', 'simpletest3@test.com');
|
56 | | $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
|
57 | | $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
58 | |
|
59 | | $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
|
60 | | $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
61 | |
|
62 | | $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
|
63 | | $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
64 | |
|
65 | | // Clear food table in preparation for flood test and allow other checks to complete.
|
66 | | $this->assertTrue(db_query('DELETE FROM {flood}'), t('Flood table emptied.'));
|
67 | |
|
68 | | // Check to see that anonymous user cannot see contact page without permission.
|
69 | | $this->setPermission('anonymous user', array('access site-wide contact form' => FALSE));
|
70 | | $this->drupalLogout();
|
71 | |
|
72 | | $this->drupalGet('contact');
|
73 | | $this->assertResponse(403, t('Access denied to anonymous user without permission.'));
|
74 | |
|
75 | | // Give anonymous user permission and see that page is viewable.
|
76 | | $this->drupalLogin($admin_user);
|
77 | | $this->setPermission('anonymous user', array('access site-wide contact form' => TRUE));
|
78 | | $this->drupalLogout();
|
79 | |
|
80 | | $this->drupalGet('contact');
|
81 | | $this->assertResponse(200, t('Access granted to anonymous user with permission.'));
|
82 | |
|
83 | | // Check that "Additional information" is displayed on the page.
|
84 | | $this->assertText($edit['contact_form_information'], t('Contact information displayed.'));
|
85 | |
|
86 | | // Submit contact form with invalid values.
|
87 | | $categories = $this->getCategories();
|
88 | | $this->submitContact('', $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
89 | | $this->assertText(t('Your name field is required.'), t('Name required.'));
|
90 | |
|
91 | | $this->submitContact($this->randomName(16), '', $this->randomName(16), $categories[0], $this->randomName(64));
|
92 | | $this->assertText(t('Your e-mail address field is required.'), t('E-mail required.'));
|
93 | |
|
94 | | $this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
95 | | $this->assertText(t('You must enter a valid e-mail address.'), t('Valid e-mail required.'));
|
96 | |
|
97 | | $this->submitContact($this->randomName(16), $recipients[0], '', $categories[0], $this->randomName(64));
|
98 | | $this->assertText(t('Subject field is required.'), t('Subject required.'));
|
99 | |
|
100 | | $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], '');
|
101 | | $this->assertText(t('Message field is required.'), t('Message required.'));
|
102 | |
|
103 | | // Submit contact form with correct values and check flood interval.
|
104 | | for ($i = 0; $i < $edit['contact_hourly_threshold']; $i++) {
|
105 | | $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
106 | | $this->assertText(t('Your message has been sent.'), t('Message sent.'));
|
107 | | }
|
108 | | // Submit contact form one over limit.
|
109 | | $this->drupalGet('contact');
|
110 | | $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.'));
|
111 | |
|
112 | | // Delete created categories.
|
113 | | $this->drupalLogin($admin_user);
|
114 | | $this->deleteCategories();
|
115 | | }
|
116 | |
|
117 | | /**
|
118 | | * Test personal contact form.
|
119 | | */
|
120 | | function testPersonalContact() {
|
121 | | $admin_user = $this->drupalCreateUser(array('administer site-wide contact form'));
|
122 | | $this->drupalLogin($admin_user);
|
123 | |
|
124 | | // Enable the personal contact form.
|
125 | | $edit = array();
|
126 | | $edit['contact_default_status'] = TRUE;
|
127 | | $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
128 | | $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
129 | |
|
130 | | // Reload variables.
|
131 | | $this->drupalLogout();
|
132 | | $this->reloadVariables();
|
133 | |
|
134 | | // Create web users and attempt to use personal contact forms with default set to true.
|
135 | | $web_user1 = $this->drupalCreateUser(array());
|
136 | | $web_user2 = $this->drupalCreateUser(array());
|
137 | |
|
138 | | $this->drupalLogin($web_user1);
|
139 | |
|
140 | | $this->drupalGet('user/'. $web_user2->uid .'/contact');
|
141 | | $this->assertResponse(200, t('Access to personal contact form granted.'));
|
142 | |
|
143 | | $edit = array();
|
144 | | $edit['subject'] = $this->randomName(16);
|
145 | | $edit['message'] = $this->randomName(64);
|
146 | | $this->drupalPost(NULL, $edit, t('Send e-mail'));
|
147 | | $this->assertText(t('The message has been sent.'), t('Message sent.'));
|
148 | |
|
149 | | $this->drupalLogout();
|
150 | |
|
151 | | $this->drupalLogin($admin_user);
|
152 | |
|
153 | | // Disable the personal contact form.
|
154 | | $edit = array();
|
155 | | $edit['contact_default_status'] = FALSE;
|
156 | | $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
157 | | $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
158 | |
|
159 | | // Reload variables.
|
160 | | $this->drupalLogout();
|
161 | | $this->reloadVariables();
|
162 | |
|
163 | | // Create web users and attempt to use personal contact forms with default set to false.
|
164 | | $web_user3 = $this->drupalCreateUser(array());
|
165 | | $web_user4 = $this->drupalCreateUser(array());
|
166 | |
|
167 | | $this->drupalLogin($web_user3);
|
168 | |
|
169 | | $this->drupalGet('user/'. $web_user4->uid .'/contact');
|
170 | | $this->assertResponse(403, t('Access to personal contact form denied.'));
|
171 | | }
|
172 | |
|
173 | | /**
|
174 | | * Add a category.
|
175 | | *
|
176 | | * @param string $category Name of category.
|
177 | | * @param string $recipients List of recipient e-mail addresses.
|
178 | | * @param string $reply Auto-reply text.
|
179 | | * @param boolean $selected Defautly selected.
|
180 | | */
|
181 | | function addCategory($category, $recipients, $reply, $selected) {
|
182 | | $edit = array();
|
183 | | $edit['category'] = $category;
|
184 | | $edit['recipients'] = $recipients;
|
185 | | $edit['reply'] = $reply;
|
186 | | $edit['selected'] = ($selected ? '1' : '0');
|
187 | | $this->drupalPost('admin/build/contact/add', $edit, t('Save'));
|
188 | | }
|
189 | |
|
190 | | /**
|
191 | | * Submit contact form.
|
192 | | *
|
193 | | * @param string $name Name.
|
194 | | * @param string $mail E-mail address.
|
195 | | * @param string $subject Subject.
|
196 | | * @param integer $cid Category id.
|
197 | | * @param string $message Message.
|
198 | | */
|
199 | | function submitContact($name, $mail, $subject, $cid, $message) {
|
200 | | $edit = array();
|
201 | | $edit['name'] = $name;
|
202 | | $edit['mail'] = $mail;
|
203 | | $edit['subject'] = $subject;
|
204 | | $edit['cid'] = $cid;
|
205 | | $edit['message'] = $message;
|
206 | | $this->drupalPost('contact', $edit, t('Send e-mail'));
|
207 | | }
|
208 | |
|
209 | | /**
|
210 | | * Delete all categories.
|
211 | | */
|
212 | | function deleteCategories() {
|
213 | | $categories = $this->getCategories();
|
214 | | foreach ($categories as $category) {
|
215 | | $category_name = db_result(db_query('SELECT category FROM {contact} WHERE cid = %d', array($category)));
|
216 | | $this->drupalPost('admin/build/contact/delete/'. $category, array(), t('Delete'));
|
217 | | $this->assertRaw(t('Category %category has been deleted.', array('%category' => $category_name)), t('Category deleted sucessfully.'));
|
218 | | }
|
219 | | }
|
220 | |
|
221 | | /**
|
222 | | * Get list category ids.
|
223 | | *
|
224 | | * @return array Category ids.
|
225 | | */
|
226 | | function getCategories() {
|
227 | | $result = db_query('SELECT cid FROM {contact}');
|
228 | | $categories = array();
|
229 | | while ($category = db_result($result)) {
|
230 | | $categories[] = $category;
|
231 | | }
|
232 | | return $categories;
|
233 | | }
|
234 | |
|
235 | | /**
|
236 | | * Set permission.
|
237 | | *
|
238 | | * @param string $role User role to set permissions for.
|
239 | | * @param array $permissions Key-value array of permissions to set.
|
240 | | */
|
241 | | function setPermission($role, $permissions) {
|
242 | | // Get role id (rid) for specified role.
|
243 | | $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array($role)));
|
244 | | if ($rid === FALSE) {
|
245 | | $this->fail(t(' [permission] Role "'. $role .'" not found.'));
|
246 | | }
|
247 | |
|
248 | | // Create edit array from permission.
|
249 | | $edit = array();
|
250 | | foreach ($permissions as $name => $value) {
|
251 | | $edit[$rid .'['. $name .']'] = $value;
|
252 | | }
|
253 | |
|
254 | | $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
|
255 | | $this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
|
256 | | }
|
257 | |
|
258 | | /**
|
259 | | * Reload variables table.
|
260 | | */
|
261 | | function reloadVariables() {
|
262 | | global $conf;
|
263 | |
|
264 | | cache_clear_all('variables', 'cache');
|
265 | | $conf = variable_init();
|
266 | | $this->assertTrue($conf, t('Variables reloaded.'));
|
267 | | }
|
268 | | }
|