| 1 |
<?php
|
| 2 |
// $Id: profile.test,v 1.21 2009/10/05 01:18:25 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* A class for common methods for testing profile fields.
|
| 6 |
*/
|
| 7 |
class ProfileTestCase extends DrupalWebTestCase {
|
| 8 |
protected $admin_user;
|
| 9 |
protected $normal_user;
|
| 10 |
|
| 11 |
function setUp() {
|
| 12 |
parent::setUp('profile');
|
| 13 |
variable_set('user_register', 1);
|
| 14 |
|
| 15 |
$this->admin_user = $this->drupalCreateUser(array('administer users', 'access user profiles'));
|
| 16 |
|
| 17 |
// This is the user whose profile will be edited.
|
| 18 |
$this->normal_user = $this->drupalCreateUser();
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Create a profile field.
|
| 23 |
*
|
| 24 |
* @param $type
|
| 25 |
* The field type to be created.
|
| 26 |
* @param $category
|
| 27 |
* The category in which the field should be created.
|
| 28 |
* @param $edit
|
| 29 |
* Additional parameters to be submitted.
|
| 30 |
* @return
|
| 31 |
* The fid of the field that was just created.
|
| 32 |
*/
|
| 33 |
function createProfileField($type = 'textfield', $category = 'simpletest', $edit = array()) {
|
| 34 |
$edit['title'] = $title = $this->randomName(8);
|
| 35 |
$edit['name'] = $form_name = 'profile_' . $title;
|
| 36 |
$edit['category'] = $category;
|
| 37 |
$edit['explanation'] = $this->randomName(50);
|
| 38 |
|
| 39 |
$this->drupalPost('admin/config/people/profile/add/' . $type, $edit, t('Save field'));
|
| 40 |
$fid = db_query("SELECT fid FROM {profile_field} WHERE title = :title", array(':title' => $title))->fetchField();
|
| 41 |
$this->assertTrue($fid, t('New Profile field has been entered in the database'));
|
| 42 |
|
| 43 |
// Check that the new field is appearing on the user edit form.
|
| 44 |
$this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
|
| 45 |
|
| 46 |
// Checking field.
|
| 47 |
if ($type == 'date') {
|
| 48 |
$this->assertField($form_name . '[month]', t('Found month selection field'));
|
| 49 |
$this->assertField($form_name . '[day]', t('Found day selection field'));
|
| 50 |
$this->assertField($form_name . '[year]', t('Found day selection field'));
|
| 51 |
}
|
| 52 |
else {
|
| 53 |
$this->assertField($form_name , t('Found form named @name', array('@name' => $form_name)));
|
| 54 |
}
|
| 55 |
|
| 56 |
// Checking name.
|
| 57 |
$this->assertText($title, t('Checking title for field %title', array('%title' => $title)));
|
| 58 |
// Checking explanation.
|
| 59 |
$this->assertText($edit['explanation'], t('Checking explanation for field %title', array('%title' => $title)));
|
| 60 |
|
| 61 |
return array(
|
| 62 |
'fid' => $fid,
|
| 63 |
'type' => $type,
|
| 64 |
'form_name' => $form_name,
|
| 65 |
'title' => $title,
|
| 66 |
'category' => $category,
|
| 67 |
);
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Set the profile field to a random value
|
| 72 |
*
|
| 73 |
* @param $field
|
| 74 |
* The field that should be set.
|
| 75 |
* @param $value
|
| 76 |
* The value for the field, defaults to a random string.
|
| 77 |
* @return
|
| 78 |
* The value that has been assigned to
|
| 79 |
*/
|
| 80 |
function setProfileField($field, $value = NULL) {
|
| 81 |
|
| 82 |
if (!isset($value)) {
|
| 83 |
$value = $this->randomName();
|
| 84 |
}
|
| 85 |
|
| 86 |
$edit = array(
|
| 87 |
$field['form_name'] => $value,
|
| 88 |
);
|
| 89 |
$this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));
|
| 90 |
|
| 91 |
// Check profile page.
|
| 92 |
$content = $this->drupalGet('user/' . $this->normal_user->uid);
|
| 93 |
$this->assertText($field['title'], t('Found profile field with title %title', array('%title' => $field['title'])));
|
| 94 |
|
| 95 |
if ($field['type'] != 'checkbox') {
|
| 96 |
// $value must be cast to a string in order to be found by assertText.
|
| 97 |
$this->assertText("$value", t('Found profile field with value %value', array('%value' => $value)));
|
| 98 |
}
|
| 99 |
|
| 100 |
return $value;
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* Delete a profile field.
|
| 105 |
*
|
| 106 |
* @param $field
|
| 107 |
* The field to be deleted.
|
| 108 |
*/
|
| 109 |
function deleteProfileField($field) {
|
| 110 |
$this->drupalPost('admin/config/people/profile/delete/' . $field['fid'], array(), t('Delete'));
|
| 111 |
$this->drupalGet('admin/config/people/profile');
|
| 112 |
$this->assertNoText($field['title'], t('Checking deleted field %title', array('%title' => $field['title'])));
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
class ProfileTestFields extends ProfileTestCase {
|
| 117 |
public static function getInfo() {
|
| 118 |
return array(
|
| 119 |
'name' => 'Test single fields',
|
| 120 |
'description' => 'Testing profile module with add/edit/delete textfield, textarea, list, checkbox, and url fields into profile page',
|
| 121 |
'group' => 'Profile'
|
| 122 |
);
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Test each of the field types. List selection and date fields are tested
|
| 127 |
* separately because they need some special handling.
|
| 128 |
*/
|
| 129 |
function testProfileFields() {
|
| 130 |
$this->drupalLogin($this->admin_user);
|
| 131 |
|
| 132 |
// Set test values for every field type.
|
| 133 |
$field_types = array(
|
| 134 |
'textfield' => $this->randomName(),
|
| 135 |
'textarea' => $this->randomName(),
|
| 136 |
'list' => $this->randomName(),
|
| 137 |
'checkbox' => 1,
|
| 138 |
// An underscore is an invalid character in a domain name. The method randomName can
|
| 139 |
// return an underscore.
|
| 140 |
'url' => 'http://www.' . str_replace('_', '', $this->randomName(10)) . '.org',
|
| 141 |
);
|
| 142 |
|
| 143 |
// For each field type, create a field, give it a value and delete the field.
|
| 144 |
foreach ($field_types as $type => $value) {
|
| 145 |
$field = $this->createProfileField($type);
|
| 146 |
$this->setProfileField($field, $value);
|
| 147 |
$this->deleteProfileField($field);
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
class ProfileTestSelect extends ProfileTestCase {
|
| 153 |
public static function getInfo() {
|
| 154 |
return array(
|
| 155 |
'name' => 'Test select field',
|
| 156 |
'description' => 'Testing profile module with add/edit/delete a select field',
|
| 157 |
'group' => 'Profile'
|
| 158 |
);
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Create a list selection field, give it a value, and delete the field.
|
| 163 |
*/
|
| 164 |
function testProfileSelectionField() {
|
| 165 |
$this->drupalLogin($this->admin_user);
|
| 166 |
|
| 167 |
$edit = array(
|
| 168 |
'options' => implode("\n", range(1, 10)),
|
| 169 |
);
|
| 170 |
$field = $this->createProfileField('selection', 'simpletest', $edit);
|
| 171 |
|
| 172 |
$this->setProfileField($field, rand(1, 10));
|
| 173 |
|
| 174 |
$this->deleteProfileField($field);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
class ProfileTestDate extends ProfileTestCase {
|
| 179 |
public static function getInfo() {
|
| 180 |
return array(
|
| 181 |
'name' => 'Test date field',
|
| 182 |
'description' => 'Testing profile module with add/edit/delete a date field',
|
| 183 |
'group' => 'Profile'
|
| 184 |
);
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Create a date field, give it a value, and delete the field.
|
| 189 |
*/
|
| 190 |
function testProfileDateField() {
|
| 191 |
$this->drupalLogin($this->admin_user);
|
| 192 |
|
| 193 |
variable_set('date_format_short', 'm/d/Y - H:i');
|
| 194 |
$field = $this->createProfileField('date');
|
| 195 |
|
| 196 |
// Set date to January 09, 1983
|
| 197 |
$edit = array(
|
| 198 |
$field['form_name'] . '[month]' => 1,
|
| 199 |
$field['form_name'] . '[day]' => 9,
|
| 200 |
$field['form_name'] . '[year]' => 1983,
|
| 201 |
);
|
| 202 |
|
| 203 |
$this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));
|
| 204 |
|
| 205 |
// Check profile page.
|
| 206 |
$this->drupalGet('user/' . $this->normal_user->uid);
|
| 207 |
$this->assertText($field['title'], t('Found profile field with title %title', array('%title' => $field['title'])));
|
| 208 |
|
| 209 |
$this->assertText('01/09/1983', t('Found date profile field.'));
|
| 210 |
|
| 211 |
$this->deleteProfileField($field);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
class ProfileTestWeights extends ProfileTestCase {
|
| 216 |
public static function getInfo() {
|
| 217 |
return array(
|
| 218 |
'name' => 'Test field weights',
|
| 219 |
'description' => 'Testing profile modules weigting of fields',
|
| 220 |
'group' => 'Profile'
|
| 221 |
);
|
| 222 |
}
|
| 223 |
|
| 224 |
function testProfileFieldWeights() {
|
| 225 |
$this->drupalLogin($this->admin_user);
|
| 226 |
|
| 227 |
$category = $this->randomName();
|
| 228 |
$field1 = $this->createProfileField('textfield', $category, array('weight' => 1));
|
| 229 |
$field2 = $this->createProfileField('textfield', $category, array('weight' => -1));
|
| 230 |
|
| 231 |
$this->setProfileField($field1, $this->randomName(8));
|
| 232 |
$this->setProfileField($field2, $this->randomName(8));
|
| 233 |
|
| 234 |
$profile_edit = $this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
|
| 235 |
$this->assertTrue(strpos($profile_edit, $field1['title']) > strpos($profile_edit, $field2['title']), t('Profile field weights are respected on the user edit form.'));
|
| 236 |
|
| 237 |
$profile_page = $this->drupalGet('user/' . $this->normal_user->uid);
|
| 238 |
$this->assertTrue(strpos($profile_page, $field1['title']) > strpos($profile_page, $field2['title']), t('Profile field weights are respected on the user profile page.'));
|
| 239 |
}
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Test profile field autocompletion and access.
|
| 244 |
*/
|
| 245 |
class ProfileTestAutocomplete extends ProfileTestCase {
|
| 246 |
public static function getInfo() {
|
| 247 |
return array(
|
| 248 |
'name' => 'Autocompletion',
|
| 249 |
'description' => 'Test profile fields with autocompletion.',
|
| 250 |
'group' => 'Profile'
|
| 251 |
);
|
| 252 |
}
|
| 253 |
|
| 254 |
/**
|
| 255 |
* Tests profile field autocompletion and access.
|
| 256 |
*/
|
| 257 |
function testAutocomplete() {
|
| 258 |
$this->drupalLogin($this->admin_user);
|
| 259 |
|
| 260 |
// Create a new profile field with autocompletion enabled.
|
| 261 |
$category = $this->randomName();
|
| 262 |
$field = $this->createProfileField('textfield', $category, array('weight' => 1, 'autocomplete' => 1));
|
| 263 |
|
| 264 |
// Enter profile field value.
|
| 265 |
$field['value'] = $this->randomName();
|
| 266 |
$this->setProfileField($field, $field['value']);
|
| 267 |
|
| 268 |
// Set some html for what we want to see in the page output later.
|
| 269 |
$autocomplete_html = '<input class="autocomplete" type="hidden" id="' . drupal_html_id('edit-' . $field['form_name'] . '-autocomplete') . '" value="' . url('profile/autocomplete/' . $field['fid'], array('absolute' => TRUE)) . '" disabled="disabled" />';
|
| 270 |
$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" />';
|
| 271 |
|
| 272 |
// Check that autocompletion html is found on the user's profile edit page.
|
| 273 |
$this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
|
| 274 |
$this->assertRaw($autocomplete_html, t('Autocomplete found.'));
|
| 275 |
$this->assertRaw('misc/autocomplete.js', t('Autocomplete JavaScript found.'));
|
| 276 |
$this->assertRaw('class="form-text form-autocomplete"', t('Autocomplete form element class found.'));
|
| 277 |
|
| 278 |
// Check the autocompletion path using the first letter of our user's profile
|
| 279 |
// field value to make sure access is allowed and a valid result if found.
|
| 280 |
$this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
|
| 281 |
$this->assertResponse(200, t('Autocomplete path allowed to user with permission.'));
|
| 282 |
$this->assertRaw($field['value'], t('Autocomplete value found.'));
|
| 283 |
|
| 284 |
// Logout and login with a user without the 'access user profiles' permission.
|
| 285 |
$this->drupalLogout();
|
| 286 |
$this->drupalLogin($this->normal_user);
|
| 287 |
|
| 288 |
// Check that autocompletion html is not found on the user's profile edit page.
|
| 289 |
$this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
|
| 290 |
$this->assertNoRaw($autocomplete_html, t('Autocomplete not found.'));
|
| 291 |
|
| 292 |
// User should be denied access to the profile autocomplete path.
|
| 293 |
$this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
|
| 294 |
$this->assertResponse(403, t('Autocomplete path denied to user without permission.'));
|
| 295 |
}
|
| 296 |
}
|
| 297 |
|
| 298 |
class ProfileBlockTestCase extends DrupalWebTestCase {
|
| 299 |
public static function getInfo() {
|
| 300 |
return array(
|
| 301 |
'name' => 'Block availability',
|
| 302 |
'description' => 'Check if the author-information block is available.',
|
| 303 |
'group' => 'Profile',
|
| 304 |
);
|
| 305 |
}
|
| 306 |
|
| 307 |
function setUp() {
|
| 308 |
parent::setUp('profile');
|
| 309 |
|
| 310 |
// Create and login user
|
| 311 |
$admin_user = $this->drupalCreateUser(array('administer blocks'));
|
| 312 |
$this->drupalLogin($admin_user);
|
| 313 |
}
|
| 314 |
|
| 315 |
function testAuthorInformationBlock() {
|
| 316 |
// Set block title to confirm that the interface is availble.
|
| 317 |
$this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array('title' => $this->randomName(8)), t('Save block'));
|
| 318 |
$this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
|
| 319 |
|
| 320 |
// Set the block to a region to confirm block is availble.
|
| 321 |
$edit = array();
|
| 322 |
$edit['profile_author-information[region]'] = 'footer';
|
| 323 |
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
|
| 324 |
$this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
|
| 325 |
}
|
| 326 |
}
|
| 327 |
|
| 328 |
/**
|
| 329 |
* TODO:
|
| 330 |
* - Test field visibility
|
| 331 |
* - Test profile browsing
|
| 332 |
* - Test required fields
|
| 333 |
* - Test fields on registration form
|
| 334 |
* - Test updating fields
|
| 335 |
*/
|