You are here

function UserRelationshipsApiTestCase::testUserRelationshipsLoadDuplicates in User Relationships 6

File

user_relationships_api/tests/user_relationships_api.api.test, line 102
User Relationships API testcase @author Alex Karshakevich http://drupal.org/user/183217

Class

UserRelationshipsApiTestCase
@file User Relationships API testcase @author Alex Karshakevich http://drupal.org/user/183217

Code

function testUserRelationshipsLoadDuplicates() {
  $u1 = $this
    ->drupalCreateUser();
  $u2 = $this
    ->drupalCreateUser();
  $u3 = $this
    ->drupalCreateUser();
  $u4 = $this
    ->drupalCreateUser();

  //create relationships
  user_relationships_request_relationship($u1, $u2, $this->rtypes['oneway']->rtid, FALSE);
  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
  user_relationships_request_relationship($u1, $u2, $this->rtypes['twoway']->rtid, FALSE);
  user_relationships_request_relationship($u1, $u3, $this->rtypes['twoway']->rtid, FALSE);
  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_api_can_request($u1, $this->rtypes['approval-oneway']));
  user_relationships_request_relationship($u1, $u2, $this->rtypes['approval-oneway']->rtid, FALSE);
  $this
    ->assertTrue(user_relationships_api_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_api_can_request($u1, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_api_can_request($u3, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_api_can_request($u4, $this->rtypes['approval']));
  $this
    ->assertTrue(user_relationships_api_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);
}