You are here

function PrivatemsgTestCase::testAutocomplete in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgTestCase::testAutocomplete()
  2. 7 privatemsg.test \PrivatemsgTestCase::testAutocomplete()

Test autocomplete.

File

./privatemsg.test, line 854
Test file for privatemsg.module

Class

PrivatemsgTestCase

Code

function testAutocomplete() {
  $current = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $user1 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $user2 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $user3 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $this
    ->drupalLogin($current);

  // Use specific names to be able to test for specific name combinations.
  user_save($current, array(
    'name' => 'whatever',
  ));
  user_save($user1, array(
    'name' => 'aaaa',
  ));
  user_save($user2, array(
    'name' => 'aaab',
  ));
  user_save($user3, array(
    'name' => 'bbbb',
  ));
  $json = $this
    ->drupalGet('messages/autocomplete/aa');
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual(count($autocomplete), 2, t('Autocomplete object contains two suggestions.'));
  $this
    ->assertEqual($autocomplete['aaaa, '], 'aaaa');
  $this
    ->assertEqual($autocomplete['aaab, '], 'aaab');
  $json = $this
    ->drupalGet('messages/autocomplete/bb');
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
  $this
    ->assertEqual($autocomplete['bbbb, '], 'bbbb');
  $json = $this
    ->drupalGet('messages/autocomplete/cc');
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual(count($autocomplete), 0, t('Autocomplete object contains no suggestions.'));
  $json = $this
    ->drupalGet('messages/autocomplete/aaaa, a');
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
  $this
    ->assertEqual($autocomplete['aaaa, aaab, '], 'aaab');

  // Test XSS protection, create a username and check that the suggestion is
  // safe.
  $user4 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $user4 = user_save($user4, array(
    'name' => "<script>alert('XSS')</script>",
  ));
  $json = $this
    ->drupalGet('messages/autocomplete/<sc');
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
  $this
    ->assertEqual($autocomplete[strip_tags($user4->name) . ', '], strip_tags($user4->name));
}