View source
<?php
require_once drupal_get_path('module', 'og') . '/tests/og_testcase.php';
class OgSubscribe extends OgTestCase {
public static function getInfo() {
return array(
'name' => t('Organic groups membership tests'),
'description' => t("Tests membership functionality and asserts correct behavior with all the different selective settings (e.g. open, moderated, etc'). Note: requires Views"),
'group' => t('Organic groups'),
);
}
function setUp() {
parent::setUp('og', 'og_access');
$this->web_admin = $this
->drupalCreateUser(array(
'administer nodes',
'administer content types',
'access administration pages',
'administer site configuration',
'administer organic groups',
));
$this
->drupalLogin($this->web_admin);
$og_group_type = $this
->drupalCreateContentType();
variable_set('og_content_type_usage_' . $og_group_type->name, 'group');
menu_rebuild();
$this->selective = array(
'open' => OG_OPEN,
'moderated' => OG_MODERATED,
'invite' => OG_INVITE_ONLY,
'closed' => OG_CLOSED,
);
$this->selective_labels = array_flip($this->selective);
$this->nodes = array();
foreach ($this->selective as $key => $selective) {
$this->nodes[$key] = node_load($this
->addOgGroup($og_group_type->name, $selective));
}
$this->web_user = $this
->drupalCreateUser(array(
'access content',
));
}
function testWebUserSubscribeOg() {
$this
->drupalLogin($this->web_user);
$cases = array();
$cases['open'] = array(
'join_button' => t('Join'),
'request_response' => t('You are now a member of %title.', array(
'%title' => $this->nodes['open']->title,
)),
);
$cases['moderated'] = array(
'join_button' => t('Request membership'),
'request_response' => t('Membership request to the %title group awaits approval by an administrator.', array(
'%title' => $this->nodes['moderated']->title,
)),
);
foreach ($this->selective as $selective_id => $value) {
if (array_key_exists($selective_id, $cases)) {
$this
->_testWebUserSubscribe($selective_id, $cases[$selective_id]);
$this
->_testWebUserUnsubscribeOg($selective_id);
}
else {
$this
->drupalGet('og/subscribe/' . $this->nodes[$selective_id]->nid);
$this
->assertResponse(403, t('User got a 403 page while trying to access @selective group subscription.', array(
'@selective' => $selective_id,
)));
}
}
}
protected function _testWebUserSubscribe($selective, $text) {
$node = $this->nodes[$selective];
$this
->drupalGet('og/subscribe/' . $node->nid);
$this
->assertRaw(t('Are you sure you want to join the group %title?', array(
'%title' => $node->title,
)), t('Confirmation of subscribe to @selective group text found.', array(
'@selective' => $selective,
)));
$this
->drupalPost(NULL, array(), $text['join_button']);
$this
->assertRaw($text['request_response'], t('Subscribed to @selective group text found.', array(
'@selective' => $selective,
)));
if ($selective == 'open') {
$this
->assertOgMember($this->web_user, $node);
}
else {
$this
->assertNotOgMember($this->web_user, $node);
$this
->assertOgPendingMember($this->web_user, $node);
}
}
protected function _testWebUserUnsubscribeOg($selective) {
$this
->drupalGet('og/unsubscribe/' . $this->nodes[$selective]->nid . '/' . $this->web_user->uid);
$this
->assertRaw(t('Are you sure you want to leave the group %title?', array(
'%title' => $this->nodes[$selective]->title,
)), t('Unsubscribe @selective group text found.', array(
'@selective' => $selective,
)));
$this
->drupalPost(NULL, array(), t('Leave'));
$this
->assertRaw(t('You left the group %group.', array(
'%group' => $this->nodes[$selective]->title,
)), t('Confirmation of unsubscribing @selective group text found.', array(
'@selective' => $selective,
)));
$this
->assertNotOgMember($this->web_user, $this->nodes[$selective]);
}
protected function assertOgMember($account, $group) {
$vars = array(
'%user' => theme('username', $account),
'%group' => $group->title,
'@selective' => $this->selective_labels[$group->og_selective],
);
$message = t('%user is a member of @selective group %group', $vars);
return $this
->assertTrue($this
->_checkOgMembershipStatus($account, $group), $message);
}
protected function assertNotOgMember($account, $group) {
$vars = array(
'%user' => theme('username', $account),
'%group' => $group->title,
'@selective' => $this->selective_labels[$group->og_selective],
);
$message = t('%user is not a member of @selective group %group', $vars);
return $this
->assertFalse($this
->_checkOgMembershipStatus($account, $group), $message);
}
protected function _checkOgMembershipStatus($account, $group) {
return array_key_exists($group->nid, og_get_subscriptions($account->uid, 1, TRUE));
}
protected function assertOgPendingMember($account, $group) {
$vars = array(
'%user' => theme('username', $account),
'%group' => $group->title,
'@selective' => $this->selective_labels[$group->og_selective],
);
$message = t('%user is a pending member of @selective group %group', $vars);
return $this
->assertTrue(og_is_pending_member($group->nid, $account->uid), $message);
}
}