You are here

function OgGroupApi::testOgGetGroupIds in Organic groups 7

Test og_get_group_ids().

Create a few groups of different entities. and check we get their IDs.

File

./og.test, line 202

Class

OgGroupApi
Test the Organic groups API and CRUD handling.

Code

function testOgGetGroupIds() {

  // List keyed by the group ID and their entity type, ID as value.
  $list = array(
    1 => array(
      'entity_test',
      10,
    ),
    2 => array(
      'entity_test',
      20,
    ),
    3 => array(
      'entity_test',
      30,
    ),
    4 => array(
      'entity_test',
      40,
      OG_STATE_PENDING,
    ),
    5 => array(
      'entity_test',
      50,
      OG_STATE_PENDING,
    ),
    6 => array(
      'node',
      10,
    ),
  );
  foreach ($list as $value) {
    $values = array(
      'entity_type' => $value[0],
      'etid' => $value[1],
      'state' => !empty($value[2]) ? $value[2] : OG_STATE_ACTIVE,
      'created' => time(),
      'label' => $this
        ->randomString(),
    );
    entity_create('group', $values)
      ->save();
  }
  $gids = og_get_group_ids('entity_test');
  $expected_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($gids, $expected_gids, t('All active groups of the same entity were returned.'));
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('group', array());
  $this
    ->assertFalse($gids, t('No groups were returned, as no entity ID was specified.'));
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test', FALSE, array(
    OG_STATE_PENDING,
  ));
  $expected_gids = array(
    40 => 4,
    50 => 5,
  );
  $this
    ->assertEqual($gids, $expected_gids, t('All pending groups of the same entity were returned.'));

  // Ask for a group that is pending, but result should include only active.
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    40,
  ));
  $expected_gids = array(
    10 => 1,
    20 => 2,
  );
  $this
    ->assertEqual($gids, $expected_gids, t('Specific active groups were returned.'));

  // Check the state conditions filters results.
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    40,
  ), array(
    OG_STATE_PENDING,
  ));
  $expected_gids = array(
    40 => 4,
  );
  $this
    ->assertEqual($gids, $expected_gids, t('Specific pending groups were returned.'));

  // Entity type that doesn't exist.
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test_non_exist', array(
    1,
  ));
  $this
    ->assertFalse($gids, t('Non existent entity type was not returned.'));

  // Entity Id that doesn't exist.
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test', array(
    100,
  ));
  $this
    ->assertFalse($gids, t('Non existent entity ID was not returned.'));

  // Test caching is correct and resets properly when the state changes.
  // We can check the cache itself, by getting it (not by reference) and
  // making sure it has the correct values.
  drupal_static_reset('og_get_group_ids');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    30,
  ));
  $this
    ->assertEqual(count($gids), 3, t('All active groups loaded.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
  ));
  $this
    ->assertEqual(count($gids), 2, t('All active groups re-loaded from cache.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
  ), array(
    OG_STATE_ACTIVE,
  ), TRUE);
  $this
    ->assertEqual(count($gids), 2, t('All requested groups loaded after soft-reset.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'All requested groups are cached after soft-reset.');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    30,
    40,
  ));
  $this
    ->assertEqual(count($gids), 3, t('Only active groups loaded.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'Only active groups cached.');
  $cache = drupal_static('og_get_group_ids', array());
  $this
    ->assertEqual($cache['__info']['entity_test']['states'], array(
    OG_STATE_ACTIVE,
  ), 'Cache states is set to active.');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    30,
    40,
  ), array(
    OG_STATE_PENDING,
  ));
  $this
    ->assertEqual(count($gids), 1, t('Cache was soft reset as state was changed, and only pending group was loaded.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    40 => 4,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'Only requested pending group was cached.');
  $this
    ->assertEqual($cache['__info']['entity_test']['states'], array(
    OG_STATE_PENDING,
  ), 'Cache states was changed to pending.');
  $cache = drupal_static('og_get_group_ids', array());
  $this
    ->assertFalse($cache['__info']['entity_test']['query all'], '"query all" is FALSE in cache.');
  $gids = og_get_group_ids('entity_test', FALSE);
  $this
    ->assertEqual(count($gids), 3, t('All active groups loaded from cache after query all.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
  $this
    ->assertTrue($cache['__info']['entity_test']['query all'], '"query all" was set to TRUE in cache.');
  $gids = og_get_group_ids('entity_test', array(
    10,
    20,
    40,
  ));
  $this
    ->assertEqual(count($gids), 2, t('All requested active groups loaded from cache after query all.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = array(
    10 => 1,
    20 => 2,
    30 => 3,
  );
  $this
    ->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are still cached from previous call.');
  $this
    ->assertTrue($cache['__info']['entity_test']['query all'], '"query all" is still set to TRUE in cache.');

  // Get all groups (i.e. get by "group" entity type).
  $cache = drupal_static('og_get_group_ids', array());
  $this
    ->assertTrue(empty($cache['group']), 'Cache of "group" entity is empty.');
  $gids = og_get_group_ids('group', FALSE, array(
    OG_STATE_ACTIVE,
    OG_STATE_PENDING,
  ));
  $this
    ->assertEqual(count($gids), 6, t('All active and pending groups loaded.'));
  $cache = drupal_static('og_get_group_ids', array());
  $cache_gids = drupal_map_assoc(array(
    1,
    2,
    3,
    4,
    5,
    6,
  ));
  $this
    ->assertEqual($cache['group'], $cache_gids, 'All active and pending groups are cached.');
  $this
    ->assertTrue($cache['__info']['group']['query all'], '"query all" is set to TRUE in cache.');
}