You are here

function UserRelationshipsTestCase::testRelationshipAPI in User Relationships 7

Test various relationship APi functions and params.

This test case concentrates on parts which aren't covered by others tests already.

File

./user_relationships.test, line 184
User Relationships API tests @author Alex Karshakevich http://drupal.org/user/183217

Class

UserRelationshipsTestCase
Test cases für UR API.

Code

function testRelationshipAPI() {
  $this
    ->createDefaultRelationshipTypes();
  $permissions = array(
    'can have ' . $this->rtypes['oneway']->machine_name . ' relationships',
    'can request ' . $this->rtypes['oneway']->machine_name . ' relationships',
    'can have ' . $this->rtypes['twoway']->machine_name . ' relationships',
    'can request ' . $this->rtypes['twoway']->machine_name . ' relationships',
    'can have ' . $this->rtypes['approval_oneway']->machine_name . ' relationships',
    'can request ' . $this->rtypes['approval_oneway']->machine_name . ' relationships',
    'can have ' . $this->rtypes['approval']->machine_name . ' relationships',
    'can request ' . $this->rtypes['approval']->machine_name . ' relationships',
    'can have ' . $this->rtypes['approval_reciprocal']->machine_name . ' relationships',
    'can request ' . $this->rtypes['approval_reciprocal']->machine_name . ' relationships',
  );
  $u1 = $this
    ->drupalCreateUser($permissions);
  $u2 = $this
    ->drupalCreateUser($permissions);

  // Test the exclude_rtids param for loading relationships. Create two
  // relationships of a different type and then try to exclude on rtid.
  user_relationships_request_relationship($u1, $u2, $this->rtypes['oneway']);
  user_relationships_request_relationship($u1, $u2, $this->rtypes['twoway']);

  // First, load without exclude param, check that both relationships are
  // listed.
  $relationships = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ));
  $this
    ->assertEqual(2, count($relationships));

  // Second, exclude the twoway relationship type.
  $relationships = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
    'exclude_rtids' => array(
      $this->rtypes['twoway']->rtid,
    ),
  ));
  $this
    ->assertEqual(1, count($relationships));
  $this
    ->assertEqual($this->rtypes['oneway']->rtid, array_shift($relationships)->rtid);

  // Include user info.
  $relationships = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'include_user_info' => TRUE,
  ));
  $this
    ->assertEqual(2, count($relationships));

  // Assert returned user info.
  foreach ($relationships as $relationship) {

    // Requester.
    $this
      ->assertEqual($u1->name, $relationship->requester->name);
    $this
      ->assertEqual($u1->mail, $relationship->requester->mail);
    $this
      ->assertEqual($u1->uid, $relationship->requester->uid);

    // Requestee.
    $this
      ->assertEqual($u2->name, $relationship->requestee->name);
    $this
      ->assertEqual($u2->mail, $relationship->requestee->mail);
    $this
      ->assertEqual($u2->uid, $relationship->requestee->uid);
  }

  // Order relationships by rtid, default direction (ASC).
  $relationships = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'order' => 'ur.rtid',
  ));
  $this
    ->assertEqual(2, count($relationships));
  $this
    ->assertEqual($this->rtypes['oneway']->rtid, array_shift($relationships)->rtid, t('First relationship is of type oneway'));
  $this
    ->assertEqual($this->rtypes['twoway']->rtid, array_shift($relationships)->rtid, t('Second relationship is of type twoway'));

  // Order relationships by rtid, DESC.
  $relationships = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'order' => array(
      'ur.rtid',
      'DESC',
    ),
  ));
  $this
    ->assertEqual(2, count($relationships));
  $this
    ->assertEqual($this->rtypes['twoway']->rtid, array_shift($relationships)->rtid, t('First relationship is of type twoway'));
  $this
    ->assertEqual($this->rtypes['oneway']->rtid, array_shift($relationships)->rtid, t('Second relationship is of type oneway'));

  // Create a relationship which is not approved automatically.
  $relationship = user_relationships_request_relationship($u1, $u2, $this->rtypes['approval']);
  $this
    ->assertFalse($relationship->approved);
  user_relationships_save_relationship($relationship, 'approve');
  $this
    ->assertTrue($relationship->approved);
  $relationships = user_relationships_load(array(
    'user' => $u2->uid,
    'rtid' => array(
      $this->rtypes['approval']->rtid,
    ),
  ));
  debug($relationships);
  debug(db_query('SELECT * FROM {user_relationships} WHERE rtid = :rtid', array(
    ':rtid' => $this->rtypes['approval']->rtid,
  ))
    ->fetchAll());
  $relationship = array_shift($relationships);
  $this
    ->assertTrue($relationship->approved);
}