<?php
/**
 * @file
 * Tests for Linkit search plugin user.
 */

/**
 * Test the the user search plugin.
 */
class LinkitsearchPluginUserTestCase extends LinkitsearchPluginTestCase {

  /**
   * Definition.
   */
  public static function getInfo() {
    return array(
      'name' => 'Linkit Search plugin (User)',
      'description' => 'Test the the user search plugin.',
      'group' => 'Linkit'
    );
  }

  function setUp($extra_modules = array()) {
    parent::setUp($extra_modules);

    // Create a basic profile.
    $this->createProfile(array(
      'data' => array(
        'search_plugins' => array(
          'entity:user' => array(
            'enabled' => 1,
            'weight' => 0,
          ),
        ),
        'entity:user' => array(
          'result_description' => '',
        ),
        'insert_plugin' => array(
          'url_method' => LINKIT_URL_METHOD_RAW,
        ),
      )
    ));

    // Users need the 'access user profiles' permission to search for users.
    $this->account = $this->drupalCreateUser(array('access user profiles'));
    $this->drupalLogin($this->account);
  }

  /**
   * Create a user.
   *
   * We can't use drupalCreateUser() as we want to set specific usernames.
   */
  protected function createUser($extra = array()) {
    // Create a user assigned to that role.
    $edit = array();
    $edit += $extra;
    if (!isset($edit['name'])) {
      $edit['name'] = $this->randomName();
    }

    $edit['mail']   = $edit['name'] . '@example.com';
    $edit['pass']   = user_password();
    $edit['status'] = 1;

    $account = user_save(drupal_anonymous_user(), $edit);

    $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
    if (empty($account->uid)) {
      return FALSE;
    }

    // Add the raw password so that we can log in as this user.
    $account->pass_raw = $edit['pass'];
    return $account;
  }

  /**
   * Test that we get results back which is valid.
   */
  public function testBasicResults() {
    // We don't use the drupalCreateUser() as we want to set specific usernames.
    $user_1 = $this->createUser(array('name' => $this->search_string . $this->randomName()));
    $user_2 = $this->createUser(array('name' => $this->search_string . $this->randomName()));
    $user_3 = $this->createUser(array('name' => $this->search_string . $this->randomName()));

    // Call the autocomplete helper method.
    $this->autocompleteCall();

    // Check that the usernames appears in the response.
    $this->assertRaw($user_1->name, 'Username was found in the result array.');
    $this->assertRaw($user_2->name, 'Username was found in the result array.');
    $this->assertRaw($user_3->name, 'Username was found in the result array.');
  }

  /**
   * Test result description.
   *
   * We just test one token.
   */
  public function testDescription() {
    // We don't use the drupalCreateUser() as we want to set specific usernames.
    $user_1 = $this->createUser(array('name' => $this->search_string . $this->randomName()));
    $user_2 = $this->createUser(array('name' => $this->search_string . $this->randomName()));

    // Update the profile with a user result description.
    $this->_profile->data['entity:user']['result_description'] = 'Created [user:created:raw]';
    $this->updateProfile();

    // Call the autocomplete helper method.
    $this->autocompleteCall();

    // Check that the result description appers in the result.
    $this->assertRaw('Created ' . $user_1->created, 'The result description was found in the result array.');
    $this->assertRaw('Created ' . $user_2->created, 'The result description was found in the result array.');
  }
}