function ProfileTestAutocomplete::testAutocomplete

7.x profile.test ProfileTestAutocomplete::testAutocomplete()

Tests profile field autocompletion and access.

File

drupal-7.x/modules/profile/profile.test, line 330
Tests for profile.module.

Class

ProfileTestAutocomplete
Test profile field autocompletion and access.

Code

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, 'Autocomplete found.');
  $this->assertRaw('misc/autocomplete.js', 'Autocomplete JavaScript found.');
  $this->assertRaw('class="form-text form-autocomplete"', '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, 'Autocomplete path allowed to user with permission.');
  $this->assertRaw($field['value'], '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, '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, 'Autocomplete path denied to user without permission.');
}