Skip to content

Instantly share code, notes, and snippets.

@jhedstrom
Created July 30, 2015 20:01
Show Gist options
  • Save jhedstrom/e4149dea0b981721f32b to your computer and use it in GitHub Desktop.
Save jhedstrom/e4149dea0b981721f32b to your computer and use it in GitHub Desktop.
Organic Groups Behat step-definition examples
<?php
/**
* @Given /^I am logged in as a user in the "(?P<group>[^"]*)" group$/
*/
public function iAmLoggedInAsAUserInTheGroup($group) {
// Create user.
$account = (object) array(
'pass' => $this->getDrupal()->random->name(),
'name' => $this->getDrupal()->random->name(),
);
$account->mail = "{$account->name}@example.com";
// Set the group.
$account->og_user_node[LANGUAGE_NONE][0]['target_id'] = $this->getGroupNidFromTitle($group);
$account->og_user_node[LANGUAGE_NONE][0]['state'] = OG_STATE_ACTIVE;
$this->getDriver()->userCreate($account);
$this->users[$account->name] = $this->user = $account;
// Log in.
$this->login();
}
/**
* @Given /^I am logged in as an "(?P<role>[^"]*)" in the "(?P<group>[^"]*)" state group$/
*/
public function iAmLoggedInAsATypeUserInStateGroup($role, $group) {
// Create user.
$account = (object) array(
'pass' => $this->getDrupal()->random->name(),
'name' => $this->getDrupal()->random->name(),
);
$account->mail = "{$account->name}@example.com";
// Set the group.
$group_id = $this->getGroupNidFromTitle($group, 'state_office');
$account->og_user_node1[LANGUAGE_NONE][0]['target_id'] = $group_id;
$account->og_user_node1[LANGUAGE_NONE][0]['state'] = OG_STATE_ACTIVE;
// User should be in the state offices group as well.
$state_office_id = $this->getGroupNidFromTitle('State Offices');
$account->og_user_node[LANGUAGE_NONE][1]['target_id'] = $state_office_id;
$account->og_user_node[LANGUAGE_NONE][1]['state'] = OG_STATE_ACTIVE;
$this->getDriver()->userCreate($account);
$this->users[$account->name] = $this->user = $account;
// Set user as group admin.
$roles = array_flip(og_roles('node', 'state_office', $group_id));
if (!isset($roles[$role])) {
throw new \Exception(sprintf('No group role with the name %s exists!', $role));
}
og_role_grant('node', $group_id, $account->uid, $roles[$role]);
// If user is an admin, they should get the site role 'state office editor'.
if ($role == 'administrator member') {
$this->getDriver()->userAddRole($account, 'state office editor');
}
// Log in.
$this->login();
}
/**
* Given a group title, find the nid.
*
* @param string $group
* The group title to search for.
* @param string $type
* The node type to use. Defaults to `site_group`.
*
* @return integer
* The node ID of the group.
*/
protected function getGroupNidFromTitle($group, $type = 'site_group') {
// Find group node.
$group_nid = db_query("SELECT nid FROM {node} WHERE type = :type AND title = :title", array(':type' => $type, ':title' => $group))->fetchField();
if (!$group_nid) {
throw new \Exception(sprintf('No group "%s" found!', $group));
}
return $group_nid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment