summaryrefslogtreecommitdiff
path: root/modules/profile/profile.test
blob: 83bed25a5840cd8bc1144751f128ee0b970c5652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
165
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
194
195
196
197
198
199
200
201
202
203
204
205
206
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php

/**
 * @file
 * Tests for profile.module.
 */

/**
 * A class for common methods for testing profile fields.
 */
class ProfileTestCase extends DrupalWebTestCase {
  protected $admin_user;
  protected $normal_user;

  function setUp() {
    parent::setUp('profile');
    variable_set('user_register', USER_REGISTER_VISITORS);

    $this->admin_user = $this->drupalCreateUser(array('administer users', 'access user profiles', 'administer blocks'));

    // This is the user whose profile will be edited.
    $this->normal_user = $this->drupalCreateUser();
  }

  /**
   * Create a profile field.
   *
   * @param $type
   *   The field type to be created.
   * @param $category
   *   The category in which the field should be created.
   * @param $edit
   *   Additional parameters to be submitted.
   * @return
   *   The fid of the field that was just created.
   */
  function createProfileField($type = 'textfield', $category = 'simpletest', $edit = array()) {
    $edit['title'] = $title = $this->randomName(8);
    $edit['name'] = $form_name = 'profile_' . $title;
    $edit['category'] = $category;
    $edit['explanation'] = $this->randomName(50);

    $this->drupalPost('admin/config/people/profile/add/' . $type, $edit, t('Save field'));
    $fid = db_query("SELECT fid FROM {profile_field} WHERE title = :title", array(':title' => $title))->fetchField();
    $this->assertTrue($fid, t('New Profile field has been entered in the database'));

    // Check that the new field is appearing on the user edit form.
    $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);

    // Checking field.
    if ($type == 'date') {
      $this->assertField($form_name . '[month]', t('Found month selection field'));
      $this->assertField($form_name . '[day]', t('Found day selection field'));
      $this->assertField($form_name . '[year]', t('Found day selection field'));
    }
    else {
      $this->assertField($form_name , t('Found form named @name', array('@name' => $form_name)));
    }

    // Checking name.
    $this->assertText($title, t('Checking title for field %title', array('%title' => $title)));
    // Checking explanation.
    $this->assertText($edit['explanation'], t('Checking explanation for field %title', array('%title' => $title)));

    return array(
      'fid' => $fid,
      'type' => $type,
      'form_name' => $form_name,
      'title' => $title,
      'category' => $category,
    );
  }

  /**
   * Update a profile field.
   *
   * @param $fid
   *   The fid of the field to be updated.
   * @param $type
   *   The type of field to be updated.
   * @param $edit
   *   Field parameters to be submitted.
   * @return
   *   Array representation of the updated field.
   */
  function updateProfileField($fid, $type = 'textfield', $edit = array()) {

    $form_name = $edit['name'];
    $title = $edit['title'];
    $category = $edit['category'];

    $this->drupalPost('admin/config/people/profile/edit/' . $fid, $edit, t('Save field'));

    // Check that the updated field is appearing on the user edit form.
    $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);

    // Checking field.
    if ($type == 'date') {
      $this->assertField($form_name . '[month]', t('Found month selection field'));
      $this->assertField($form_name . '[day]', t('Found day selection field'));
      $this->assertField($form_name . '[year]', t('Found day selection field'));
    }
    else {
      $this->assertField($form_name , t('Found form named @name', array('@name' => $form_name)));
    }

    // Checking name.
    $this->assertText($title, t('Checking title for field %title', array('%title' => $title)));
    // Checking explanation.
    $this->assertText($edit['explanation'], t('Checking explanation for field %title', array('%title' => $title)));

    return array(
      'fid' => $fid,
      'type' => $type,
      'form_name' => $form_name,
      'title' => $title,
      'category' => $category,
    );
  }

  /**
   * Set the profile field to a random value
   *
   * @param $field
   *   The field that should be set.
   * @param $value
   *   The value for the field, defaults to a random string.
   * @return
   *   The value that has been assigned to
   */
  function setProfileField($field, $value = NULL) {

    if (!isset($value)) {
      $value = $this->randomName();
    }

    $edit = array(
      $field['form_name'] => $value,
    );
    $this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));

    // Check profile page.
    $content = $this->drupalGet('user/' . $this->normal_user->uid);
    $this->assertText($field['title'], t('Found profile field with title %title', array('%title' => $field['title'])));

    if ($field['type'] != 'checkbox') {
      // $value must be cast to a string in order to be found by assertText.
      $this->assertText("$value", t('Found profile field with value %value', array('%value' => $value)));
    }

    return $value;
  }

  /**
   * Delete a profile field.
   *
   * @param $field
   *   The field to be deleted.
   */
  function deleteProfileField($field) {
    $this->drupalPost('admin/config/people/profile/delete/' . $field['fid'], array(), t('Delete'));
    $this->drupalGet('admin/config/people/profile');
    $this->assertNoText($field['title'], t('Checking deleted field %title', array('%title' => $field['title'])));
  }
}

class ProfileTestFields extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Test single fields',
      'description' => 'Testing profile module with add/edit/delete textfield, textarea, list, checkbox, and url fields into profile page',
      'group' => 'Profile'
    );
  }

  /**
   * Test each of the field types. List selection and date fields are tested
   * separately because they need some special handling.
   */
  function testProfileFields() {
    $this->drupalLogin($this->admin_user);

    // Set test values for every field type.
    $field_types = array(
      'textfield' => $this->randomName(),
      'textarea' => $this->randomName(),
      'list' => $this->randomName(),
      'checkbox' => 1,
      // An underscore is an invalid character in a domain name. The method randomName can
      // return an underscore.
      'url' => 'http://www.' . str_replace('_', '', $this->randomName(10)) . '.org',
    );

    // For each field type, create a field, give it a value, update the field,
    // and delete the field.
    foreach ($field_types as $type => $value) {
      $field = $this->createProfileField($type);
      $this->setProfileField($field, $value);
      $edit = array(
        'name' => $field['form_name'],
        'title' => $this->randomName(),
        'category' => $field['category'],
        'explanation' => $this->randomName(),
      );
      $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
      $this->deleteProfileField($field);
    }
  }
}

class ProfileTestSelect extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Test select field',
      'description' => 'Testing profile module with add/edit/delete a select field',
      'group' => 'Profile'
    );
  }

  /**
   * Create a list selection field, give it a value, update and delete the field.
   */
  function testProfileSelectionField() {
    $this->drupalLogin($this->admin_user);

    $edit = array(
      'options' => implode("\n", range(1, 10)),
    );
    $field = $this->createProfileField('selection', 'simpletest', $edit);

    $this->setProfileField($field, rand(1, 10));

    $edit = array(
      'name' => $field['form_name'],
      'title' => $this->randomName(),
      'category' => $field['category'],
      'explanation' => $this->randomName(),
    );
    $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
    $this->deleteProfileField($field);
  }
}

class ProfileTestDate extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Test date field',
      'description' => 'Testing profile module with add/edit/delete a date field',
      'group' => 'Profile'
    );
  }

  /**
   * Create a date field, give it a value, update and delete the field.
   */
  function testProfileDateField() {
    $this->drupalLogin($this->admin_user);

    variable_set('date_format_short', 'm/d/Y - H:i');
    $field = $this->createProfileField('date');

    // Set date to January 09, 1983
    $edit = array(
      $field['form_name'] . '[month]' => 1,
      $field['form_name'] . '[day]' => 9,
      $field['form_name'] . '[year]' => 1983,
    );

    $this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));

    // Check profile page.
    $this->drupalGet('user/' . $this->normal_user->uid);
    $this->assertText($field['title'], t('Found profile field with title %title', array('%title' => $field['title'])));

    $this->assertText('01/09/1983', t('Found date profile field.'));

    $edit = array(
      'name' => $field['form_name'],
      'title' => $this->randomName(),
      'category' => $field['category'],
      'explanation' => $this->randomName(),
    );
    $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
    $this->deleteProfileField($field);
  }
}

class ProfileTestWeights extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Test field weights',
      'description' => 'Testing profile modules weigting of fields',
      'group' => 'Profile'
    );
  }

  function testProfileFieldWeights() {
    $this->drupalLogin($this->admin_user);

    $category = $this->randomName();
    $field1 = $this->createProfileField('textfield', $category, array('weight' => 1));
    $field2 = $this->createProfileField('textfield', $category, array('weight' => -1));

    $this->setProfileField($field1, $this->randomName(8));
    $this->setProfileField($field2, $this->randomName(8));

    $profile_edit = $this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
    $this->assertTrue(strpos($profile_edit, $field1['title']) > strpos($profile_edit, $field2['title']), t('Profile field weights are respected on the user edit form.'));

    $profile_page = $this->drupalGet('user/' . $this->normal_user->uid);
    $this->assertTrue(strpos($profile_page, $field1['title']) > strpos($profile_page, $field2['title']), t('Profile field weights are respected on the user profile page.'));
  }
}

/**
 * Test profile field autocompletion and access.
 */
class ProfileTestAutocomplete extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Autocompletion',
      'description' => 'Test profile fields with autocompletion.',
      'group' => 'Profile'
    );
  }

  /**
   * Tests profile field autocompletion and access.
   */
  function testAutocomplete() {
    $this->drupalLogin($this->admin_user);

    // Create a new profile field with autocompletion enabled.
    $category = $this->randomName();
    $field = $this->createProfileField('textfield', $category, array('weight' => 1, 'autocomplete' => 1));

    // Enter profile field value.
    $field['value'] = $this->randomName();
    $this->setProfileField($field, $field['value']);

    // Set some html for what we want to see in the page output later.
    $autocomplete_html = '<input type="hidden" id="' . drupal_html_id('edit-' . $field['form_name'] . '-autocomplete') . '" value="' . url('profile/autocomplete/' . $field['fid'], array('absolute' => TRUE)) . '" disabled="disabled" class="autocomplete" />';
    $field_html = '<input type="text" maxlength="255" name="' . $field['form_name'] . '" id="' . drupal_html_id('edit-' . $field['form_name']) . '" size="60" value="' . $field['value'] . '" class="form-text form-autocomplete required" />';

    // Check that autocompletion html is found on the user's profile edit page.
    $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
    $this->assertRaw($autocomplete_html, t('Autocomplete found.'));
    $this->assertRaw('misc/autocomplete.js', t('Autocomplete JavaScript found.'));
    $this->assertRaw('class="form-text form-autocomplete"', t('Autocomplete form element class found.'));

    // Check the autocompletion path using the first letter of our user's profile
    // field value to make sure access is allowed and a valid result if found.
    $this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
    $this->assertResponse(200, t('Autocomplete path allowed to user with permission.'));
    $this->assertRaw($field['value'], t('Autocomplete value found.'));

    // Logout and login with a user without the 'access user profiles' permission.
    $this->drupalLogout();
    $this->drupalLogin($this->normal_user);

    // Check that autocompletion html is not found on the user's profile edit page.
    $this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
    $this->assertNoRaw($autocomplete_html, t('Autocomplete not found.'));

    // User should be denied access to the profile autocomplete path.
    $this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
    $this->assertResponse(403, t('Autocomplete path denied to user without permission.'));
  }
}

class ProfileBlockTestCase extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Block availability',
      'description' => 'Check if the Author Information block is available.',
      'group' => 'Profile',
    );
  }

  function setUp() {
    parent::setUp();

    // Login the admin user.
    $this->drupalLogin($this->admin_user);

    // Create two fields.
    $category = $this->randomName();
    $this->field1 = $this->createProfileField('textfield', $category, array('weight' => 0));
    $this->field2 = $this->createProfileField('textfield', $category, array('weight' => 1));

    // Assign values to those fields.
    $this->value1 = $this->setProfileField($this->field1);
    $this->value2 = $this->setProfileField($this->field2);

    // Create a node authored by the normal user.
    $this->node = $this->drupalCreateNode(array(
      'uid' => $this->normal_user->uid,
    ));
  }

  function testAuthorInformationBlock() {
    // Set the block to a region to confirm the block is availble.
    $edit = array();
    $edit['blocks[profile_author-information][region]'] = 'footer';
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));

    // Enable field 1.
    $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
      'profile_block_author_fields[' . $this->field1['form_name'] . ']' => TRUE,
    ), t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));

    // Visit the node and confirm that the field is displayed.
    $this->drupalGet('node/' . $this->node->nid);
    $this->assertRaw($this->value1, t('Field 1 is displayed'));
    $this->assertNoRaw($this->value2, t('Field 2 is not displayed'));

    // Enable only field 2.
    $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
      'profile_block_author_fields[' . $this->field1['form_name'] . ']' => FALSE,
      'profile_block_author_fields[' . $this->field2['form_name'] . ']' => TRUE,
    ), t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));

    // Visit the node and confirm that the field is displayed.
    $this->drupalGet('node/' . $this->node->nid);
    $this->assertNoRaw($this->value1, t('Field 1 is not displayed'));
    $this->assertRaw($this->value2, t('Field 2 is displayed'));

    // Enable both fields.
    $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
      'profile_block_author_fields[' . $this->field1['form_name'] . ']' => TRUE,
      'profile_block_author_fields[' . $this->field2['form_name'] . ']' => TRUE,
    ), t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));

    // Visit the node and confirm that the field is displayed.
    $this->drupalGet('node/' . $this->node->nid);
    $this->assertRaw($this->value1, t('Field 1 is displayed'));
    $this->assertRaw($this->value2, t('Field 2 is displayed'));

    // Enable the link to the user profile.
    $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
      'profile_block_author_fields[user_profile]' => TRUE,
    ), t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));

    // Visit the node and confirm that the user profile link is displayed.
    $this->drupalGet('node/' . $this->node->nid);
    $this->clickLink(t('View full user profile'));
    $this->assertEqual($this->getUrl(), url('user/' . $this->normal_user->uid, array('absolute' => TRUE)));
  }
}

/**
 * Test profile browsing.
 */
class ProfileTestBrowsing extends ProfileTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Profile browsing',
      'description' => 'Test profile browsing.',
      'group' => 'Profile',
    );
  }

  /**
   * Test profile browsing.
   */
  function testProfileBrowsing() {
    $this->drupalLogin($this->admin_user);
    $field = $this->createProfileField('list', 'simpletest', array('page' => '%value'));

    // Set a random value for the profile field.
    $value = $this->setProfileField($field);

    // Check that user is found on the profile browse page.
    $this->drupalGet("profile/{$field['form_name']}/$value");
    $this->assertText($this->normal_user->name);
  }
}

  /**
   * TODO:
   * - Test field visibility
   * - Test required fields
   * - Test fields on registration form
   * - Test updating fields
   */