Test support for extensions
In CiviCRM core test class we have a function createLogggedInnser to emulated having a logged in user
When running extension tests a) this function is not available and b) the UFClass is Drupal not Tests - which causes problems running tests.
We have a function like this in our test class.
/**
* Emulate a logged in user since certain functions use that.
* value to store a record in the DB (like activity)
* CRM-8180
*
* @return int
* Contact ID of the created user.
*/
public function imitateAdminUser() {
$result = $this->callAPISuccess('UFMatch', 'get', array(
'uf_id' => 1,
'sequential' => 1,
));
if (empty($result['id'])) {
$contact = $this->callAPISuccess('Contact', 'create', array(
'first_name' => 'Super',
'last_name' => 'Duper',
'contact_type' => 'Individual',
'api.UFMatch.create' => array('uf_id' => 1, 'uf_name' => 'Wizard'),
));
$contactID = $contact['id'];
}
else {
$contactID = $result['values'][0]['contact_id'];
}
$session = CRM_Core_Session::singleton();
$session->set('userID', $contactID);
CRM_Core_Config::singleton()->userPermissionClass = new CRM_Core_Permission_UnitTests();
CRM_Core_Config::singleton()->userPermissionClass->permissions = array(
'edit all contacts',
'Access CiviCRM',
'Administer CiviCRM',
);
return $contactID;
}
I wonder about either
- providing some civix support or
- adding a new test trait \Civi\Test\UserTrait with
- createLoggedInUser
- something like the above
- move some of the stuff from the unavailable permission trait
ping @totten