You are here

function OgGroupApi::testOgCrud in Organic groups 7

Test CRUD of group entities.

File

./og.test, line 146

Class

OgGroupApi
Test the Organic groups API and CRUD handling.

Code

function testOgCrud() {
  $node = entity_create('node', array(
    'type' => 'article',
    'title' => $this
      ->randomName(),
    'uid' => 1,
  ));
  entity_save('node', $node);
  $group = og_get_group('node', $node->nid, TRUE);

  // Assert is new property.
  $this
    ->assertTrue($group->is_new, t('New group has "is new" property.'));

  // Assert default state.
  $this
    ->assertTrue($group->state == OG_STATE_ACTIVE, t('Default state property is active.'));

  // Assert default creation time.
  $this
    ->assertTrue($group->created, t('Group creating time was added to group.'));

  // Assert group ID not set.
  $this
    ->assertTrue(empty($group->gid), t('Group ID not set for unsaved group.'));

  // Save the group.
  $group
    ->save();

  // Assert group ID was set.
  $this
    ->assertTrue(!empty($group->gid), t('Group ID was set for saved group.'));

  // Set a new state for the group.
  $group = og_get_group('node', $node->nid);
  $group->state = OG_STATE_PENDING;
  $group
    ->save();

  // Assert group isn't loaded, when state is pending and state isn't
  // specifically stated.
  drupal_static_reset('og_get_group_ids');
  $group = og_get_group('node', $node->nid);
  $this
    ->assertFalse($group, t('Pending state group was not loaded, as it was not requested.'));

  // Reload group to make sure state was updated.
  $group = og_get_group('node', $node->nid, FALSE, array(
    OG_STATE_PENDING,
  ), TRUE);
  $this
    ->assertTrue($group->state == OG_STATE_PENDING, t('Group was updated.'));
  $group
    ->delete();
  $group = og_get_group('node', $node->nid, FALSE, array(), TRUE);
  $this
    ->assertFalse($group, t('Group was deleted.'));
}