You are here

function OgGroupApi::testOgGroupField in Organic groups 7

Test OG group field behaviour.

File

./og.test, line 326

Class

OgGroupApi
Test the Organic groups API and CRUD handling.

Code

function testOgGroupField() {

  // Add OG group field to the entity_test's "main" bundle.
  og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

  // Create user.
  $web_user = $this
    ->drupalCreateUser();

  // Create an entity.
  $property = OG_GROUP_FIELD;
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $web_user->uid,
  ));
  $entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
  $entity
    ->save();

  // Assert no group was created.
  $group = og_get_group('entity_test', $entity->pid);
  $this
    ->assertTrue(empty($group->gid), t('Group was not created.'));

  // Assert group was created, and was already saved, and its state is active
  $entity->{$property}[LANGUAGE_NONE][0]['value'] = 1;
  $entity
    ->save();
  $group = og_get_group('entity_test', $entity->pid);
  $this
    ->assertTrue(!empty($group->gid), t('Group was created.'));
  $this
    ->assertTrue($group->state == OG_STATE_ACTIVE, t('Group state is set to active.'));

  // Assert the user is registered to the new group.
  $this
    ->assertTrue(og_is_member($group->gid, 'user', $web_user), t('User is registered to the new group.'));

  // Assert group's state was set to pending.
  $entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
  $entity
    ->save();
  $group = og_get_group('entity_test', $entity->pid, FALSE, array(
    OG_STATE_ACTIVE,
    OG_STATE_PENDING,
  ), TRUE);
  $this
    ->assertTrue($group->state == OG_STATE_PENDING, t('Group state was set to pending.'));
  $gid = $group->gid;

  // Delete the entity, and assert the group was deleted.
  $entity
    ->delete();
  $group = og_get_group('entity_test', $entity->pid, FALSE, array(
    OG_STATE_ACTIVE,
    OG_STATE_PENDING,
  ));
  $this
    ->assertFalse($group, t('Group was deleted.'));

  // Assert user no longer belongs to group.
  $this
    ->assertFalse(og_is_member($gid), t('User is no longer registered to the new group.'));
}