You are here

function UserRelationshipsTestCase::testUserRelationshipsLoadDuplicates in User Relationships 7

File

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

Class

UserRelationshipsTestCase
Test cases für UR API.

Code

function testUserRelationshipsLoadDuplicates() {
  $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',
  );
  $u1 = $this
    ->drupalCreateUser($permissions);
  $u2 = $this
    ->drupalCreateUser($permissions);
  $u3 = $this
    ->drupalCreateUser($permissions);
  $u4 = $this
    ->drupalCreateUser($permissions);

  // Create relationships.
  $this
    ->assertTrue((bool) user_relationships_request_relationship($u1, $u2, $this->rtypes['oneway']->rtid, FALSE));
  $this
    ->assertTrue((bool) user_relationships_request_relationship($u1, $u3, $this->rtypes['oneway']->rtid, FALSE));

  // user_relationships_request_relationship($u3, $u1, $this->rtypes['oneway']->rtid, FALSE);//this is a no-op
  $this
    ->assertTrue((bool) user_relationships_request_relationship($u1, $u2, $this->rtypes['twoway']->rtid, FALSE));
  $this
    ->assertTrue((bool) user_relationships_request_relationship($u1, $u3, $this->rtypes['twoway']->rtid, FALSE));
  $this
    ->assertTrue((bool) user_relationships_request_relationship($u4, $u1, $this->rtypes['twoway']->rtid, FALSE));

  //test between
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 2);

  //sort by rtid and here reverse relationships will show up
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'sort' => 'rtid',
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 1);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 1);
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u2->uid,
    ),
  ), array(
    'sort' => 'rtid',
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 1);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 2);

  //test user
  $result = user_relationships_load(array(
    'user' => $u1->uid,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'user' => $u1->uid,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'user' => $u1->uid,
  ), array(
    'sort' => 'rtid',
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 2);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 3);
  $result = user_relationships_load(array(
    'user' => $u1->uid,
  ), array(
    'sort' => 'rtid',
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 2);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 6);

  //test rtid
  $result = user_relationships_load(array(
    'rtid' => $this->rtypes['twoway']->rtid,
  ));
  $this
    ->assertEqual(count($result), 3);
  $result = user_relationships_load(array(
    'rtid' => $this->rtypes['twoway']->rtid,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 3);
  $result = user_relationships_load(array(
    'rtid' => $this->rtypes['twoway']->rtid,
  ), array(
    'sort' => 'rtid',
  ));
  $this
    ->assertFalse(isset($result[$this->rtypes['oneway']->rtid]));
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 3);
  $result = user_relationships_load(array(
    'rtid' => $this->rtypes['twoway']->rtid,
  ), array(
    'sort' => 'rtid',
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertFalse(isset($result[$this->rtypes['oneway']->rtid]));
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 6);

  //test approved

  //load pending records
  $this
    ->assertTrue(user_relationships_can_request($u1, $this->rtypes['approval_oneway']));
  user_relationships_request_relationship($u1, $u2, $this->rtypes['approval_oneway']->rtid, FALSE);
  $this
    ->assertTrue(user_relationships_can_request($u3, $this->rtypes['approval_oneway']));
  user_relationships_request_relationship($u3, $u1, $this->rtypes['approval_oneway']->rtid, FALSE);
  $result = user_relationships_load(array(
    'approved' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'approved' => 1,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'approved' => 1,
  ), array(
    'sort' => 'rtid',
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 2);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 3);
  $this
    ->assertFalse(isset($result[$this->rtypes['approval_oneway']->rtid]));
  $result = user_relationships_load(array(
    'approved' => 1,
  ), array(
    'sort' => 'rtid',
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result[$this->rtypes['oneway']->rtid]), 2);
  $this
    ->assertEqual(count($result[$this->rtypes['twoway']->rtid]), 6);
  $this
    ->assertFalse(isset($result[$this->rtypes['approval_oneway']->rtid]));

  //test pending
  $result = user_relationships_load(array(
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'approved' => 0,
  ), array(
    'sort' => 'rtid',
  ));
  $this
    ->assertEqual(count($result), 1);
  $this
    ->assertEqual(count($result[$this->rtypes['approval_oneway']->rtid]), 2);
  $result = user_relationships_load(array(
    'approved' => 0,
  ), array(
    'sort' => 'rtid',
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 1);
  $this
    ->assertEqual(count($result[$this->rtypes['approval_oneway']->rtid]), 2);

  //test user and pending
  $result = user_relationships_load(array(
    'user' => $u1->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'user' => $u1->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'requester_id' => $u1->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 1);
  $result = user_relationships_load(array(
    'requester_id' => $u1->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 1);

  //test user and approved
  $result = user_relationships_load(array(
    'user' => $u1->uid,
    'approved' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'user' => $u1->uid,
    'approved' => 1,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'requester_id' => $u1->uid,
    'approved' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'requester_id' => $u1->uid,
    'approved' => 1,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 5);
  $result = user_relationships_load(array(
    'requestee_id' => $u1->uid,
    'approved' => 1,
  ));
  $this
    ->assertEqual(count($result), 3);
  $result = user_relationships_load(array(
    'requestee_id' => $u1->uid,
    'approved' => 1,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 3);

  //add more pending records
  $this
    ->assertTrue(user_relationships_can_request($u1, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_can_request($u3, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_can_request($u4, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_can_request($u4, $this->rtypes['approval_oneway']));
  user_relationships_request_relationship($u4, $u2, $this->rtypes['approval_oneway']->rtid, FALSE);
  user_relationships_request_relationship($u3, $u4, $this->rtypes['approval_oneway']->rtid, FALSE);
  user_relationships_request_relationship($u1, $u2, $this->rtypes['approval']->rtid, FALSE);
  user_relationships_request_relationship($u4, $u2, $this->rtypes['approval']->rtid, FALSE);
  user_relationships_request_relationship($u3, $u4, $this->rtypes['approval']->rtid, FALSE);
  user_relationships_request_relationship($u3, $u1, $this->rtypes['approval']->rtid, FALSE);

  //test pending and requester
  $result = user_relationships_load(array(
    'requester_id' => $u3->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'requester_id' => $u3->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);

  //test pending and requestee
  $result = user_relationships_load(array(
    'requestee_id' => $u2->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'requestee_id' => $u2->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);

  //test pending and user
  $result = user_relationships_load(array(
    'user' => $u3->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'user' => $u3->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'user' => $u2->uid,
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'user' => $u2->uid,
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);

  //test between and pending
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u1->uid,
    ),
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u1->uid,
    ),
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u4->uid,
    ),
    'approved' => 0,
  ));
  $this
    ->assertEqual(count($result), 2);
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u4->uid,
    ),
    'approved' => 0,
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 2);

  //#520392 test just between
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u1->uid,
    ),
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u1->uid,
    ),
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u3->uid,
    ),
  ));
  $this
    ->assertEqual(count($result), 4);
  $result = user_relationships_load(array(
    'between' => array(
      $u1->uid,
      $u3->uid,
    ),
  ), array(
    'include_twoway_reverse' => 1,
  ));
  $this
    ->assertEqual(count($result), 4);

  // #1211060: Make sure that exactly one reverse relationship is created when
  // a twoway relationship is approved.
  // Make sure only an unapproved relationship from u4 to u2 exists.
  $unapproved_relationship = user_relationships_load(array(
    'requestee_id' => $u2->uid,
    'requester_id' => $u4->uid,
    'rtid' => $this->rtypes['approval']->rtid,
  ));
  $unapproved_relationship = reset($unapproved_relationship);
  $this
    ->assertFalse($unapproved_relationship->approved);
  $this
    ->assertFalse(user_relationships_load(array(
    'requester_id' => $u2->uid,
    'requestee_id' => $u4->uid,
    'rtid' => $this->rtypes['approval']->rtid,
  )));

  // Approve the relationship.
  user_relationships_save_relationship($unapproved_relationship, 'approve');

  // Make sure the relationship was approved and exists both ways.
  $relationship = user_relationships_load(array(
    'requestee_id' => $u2->uid,
    'requester_id' => $u4->uid,
    'rtid' => $this->rtypes['approval']->rtid,
  ));
  $relationship = reset($relationship);
  $this
    ->assertTrue($relationship->approved);
  $relationship = user_relationships_load(array(
    'requester_id' => $u2->uid,
    'requestee_id' => $u4->uid,
    'rtid' => $this->rtypes['approval']->rtid,
  ));
  $relationship = reset($relationship);
  $this
    ->assertTrue($relationship->approved);

  // Update the relationship again, make sure that only a single entry for
  // both directions exists.
  user_relationships_save_relationship($relationship, 'update');
  $arguments_array = array(
    array(
      ':requestee_id' => $u2->uid,
      ':requester_id' => $u4->uid,
      ':rtid' => $this->rtypes['approval']->rtid,
    ),
    array(
      ':requester_id' => $u2->uid,
      ':requestee_id' => $u4->uid,
      ':rtid' => $this->rtypes['approval']->rtid,
    ),
  );
  foreach ($arguments_array as $arguments) {
    $this
      ->assertEqual(1, db_query('SELECT COUNT(*) FROM {user_relationships} WHERE requester_id = :requester_id AND requestee_id = :requestee_id AND rtid = :rtid', $arguments)
      ->fetchField());
  }
}