Projet

Général

Profil

Paste
Télécharger (25,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / tests / Og2Tests.test @ 5136ce55

1
<?php
2

    
3
/**
4
 * @file simpletest for Ldap Authorization OG Module, for og 7.x-2.x
5
 *
6
 * Manual testing to accompany simpletests:
7
 *  - logon with og authorization disabled and make sure nothing happens
8
 *  - logon with og authorization enabled and make sure admin and member group memberships granted
9
 *  - change mappings so no roles granted
10
 *  - logon and make sure memberships revoked
11
 */
12

    
13
drupal_load('module', 'ldap_test');
14
module_load_include('php', 'ldap_test', 'LdapTestCase.class');
15
require_once(drupal_get_path('module', 'ldap_authorization_og') . '/LdapAuthorizationConsumerOG.class.php');
16

    
17
class LdapAuthorizationOg2Tests extends LdapTestCase {
18

    
19
  public $groupEntityType = 'node';
20
  public $groupBundle = 'group';
21
  public $groupType = 'node';
22
  public $group_content_type = NULL;
23
  public $group_nodes = array();
24
  public $user1;
25
  public $consumerType = 'og_group';
26
  public $module_name = 'ldap_authorization_og';
27
  protected $ldap_test_data;
28
  public $customOgRoles = array(
29
    'dungeon-master' => array('entity_type' => 'node', 'bundle_type' => 'group'),
30
    'time-keeper' => array('entity_type' => 'node', 'bundle_type' => 'group'),
31
    );
32

    
33
  public static function getInfo() {
34
    return array(
35
      'group' => 'LDAP Authorization',
36
      'name' => 'OG 7.x-2.x Tests.',
37
      'description' => 'Test ldap authorization og 2.',
38
    );
39
  }
40

    
41
  function __construct($test_id = NULL) {
42
    parent::__construct($test_id);
43
  }
44

    
45
  function setUp($addl_modules = array()) {
46
    parent::setUp(array('ldap_authentication', 'ldap_authorization', 'ldap_authorization_og'));
47
    variable_set('ldap_simpletest', 2);
48

    
49
    if (ldap_authorization_og_og_version() != 2) {
50
      debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
51
      return;
52
    }
53

    
54
    $this->user1 = $this->drupalCreateUser();
55
    $this->groups = array();
56
    $this->prepTestData(LDAP_TEST_LDAP_NAME, array('activedirectory1'));
57

    
58

    
59

    
60
    // Create group and group content node types.
61
    $this->groupBundle = $this->drupalCreateContentType(array(
62
      'type' => 'group',
63
      'name' => 'OG Group',
64
      ))->type;
65

    
66
    og_create_field(OG_GROUP_FIELD, $this->groupEntityType, $this->groupBundle);
67
    og_create_field(OG_AUDIENCE_FIELD, $this->groupEntityType,  $this->groupBundle);
68

    
69
    // create og group for each group in group csv
70

    
71
    $this->testFunctions->populateFakeLdapServerData(LDAP_TEST_LDAP_NAME, 'activedirectory1');
72
    $this->testFunctions->getCsvLdapData(LDAP_TEST_LDAP_NAME);
73
    foreach ($this->testFunctions->csvTables['groups'] as $guid => $group) {
74
      $label = $group['cn'];
75
      $settings = array();
76
      $settings['type'] = $this->groupBundle;
77
      $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
78
      $settings['uid'] = $this->user1->uid;
79
      $settings['title'] = $label;
80
      $settings['type'] = 'group';
81
      $this->group_nodes[$label] = $this->drupalCreateNode($settings);
82
    }
83

    
84
  }
85

    
86
  public function createCustomRoles() {
87

    
88
    foreach ($this->customOgRoles as $og_role_name => $og_role) {
89
      $role = new stdClass;
90
      $role->gid = 0;
91
      $role->group_type = $og_role['entity_type'];
92
      $role->group_bundle = $og_role['bundle_type'];
93
      $role->name = $og_role_name;
94
      $status = og_role_save($role);
95
    }
96

    
97
  }
98

    
99
  /**
100
   * get test data in convenient format, so tests are easier to read and write
101
   */
102
  public function getTestData($debug = FALSE) {
103
    $group_nodes = array();
104
    $group_nids = array();
105
    $group_entity_ids = array();
106
    $roles = array();
107
    $roles_by_name = array();
108
    $consumer_ids = array();
109
    foreach (array('gryffindor', 'students', 'faculty', 'users', 'hufflepuff', 'slytherin') as $i => $group_name) {
110
      list($group_nodes[$group_name], $group_entity_ids[$group_name]) =  ldap_authorization_og2_get_group_from_name($this->groupEntityType, $group_name);
111
      $nid = $group_nodes[$group_name]->nid;
112
      $group_nids[$group_name] = $nid;
113
      $roles[$group_name] = og_roles($this->groupEntityType, $this->groupBundle, $nid, FALSE, TRUE);
114
      $roles_by_name[$group_name] = array_flip( $roles[$group_name] );
115
      foreach ($roles[$group_name] as $rid => $role_name) {
116
        $consumer_ids[$group_name][$role_name] = ldap_authorization_og_authorization_id($nid, $rid, 'node');
117
        $consumer_ids[$group_name][$rid] = ldap_authorization_og_authorization_id($nid, $rid, 'node');
118
      }
119
    }
120
    if ($debug) {
121
      debug("group_nids"); debug($group_nids); debug("group_entity_ids"); debug($group_entity_ids); debug("roles"); debug($roles); debug("roles_by_name"); debug($roles_by_name);
122
    }
123
    return array($group_nodes, $group_nids, $group_entity_ids, $roles_by_name, $consumer_ids);
124
  }
125

    
126
  /**
127
   * just make sure install succeeds and
128
   */
129
  function testBasicFunctionsAndApi() {
130

    
131
    if (ldap_authorization_og_og_version() != 2) {
132
      debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
133
      return;
134
    }
135

    
136
    $this->createCustomRoles();
137
    $all_roles = og_roles($this->groupEntityType, $this->groupBundle, 0, FALSE, TRUE);
138

    
139
    $this->ldapTestId = $this->module_name . ': setup success';
140
    // just to give warning if setup doesn't succeed.  may want to take these out at some point.
141
    $setup_success = (
142
        module_exists('ldap_authentication') &&
143
        module_exists('ldap_servers') &&
144
        module_exists('ldap_user') &&
145
        module_exists('ldap_authorization') &&
146
        module_exists('ldap_authorization_og') &&
147
        (variable_get('ldap_simpletest', 0) == 2)
148
      );
149
    $this->assertTrue($setup_success, ' ldap_authorizations og setup successful', $this->ldapTestId);
150

    
151
    $this->ldapTestId = $this->module_name . ': cron test';
152
    $this->assertTrue(drupal_cron_run(), t('Cron can run with ldap authorization og enabled.'), $this->ldapTestId);
153

    
154
    /***
155
     * I. some basic tests to make sure og module's apis are working before testing ldap_authorization_og
156
     * if these aren't working as expected, no ldap authorization og functionality will work.
157
     */
158

    
159
    $web_user = $this->drupalCreateUser();
160
    $this->ldapTestId = $this->module_name . ': og2 functions';
161
    list($group_nodes, $group_nids, $group_entity_ids, $roles_by_name, $consumer_ids) = $this->getTestData(TRUE);
162

    
163

    
164
    /**
165
     * II.0 basic granting tests to make sure og_role_grant, ldap_authorization_og_rid_from_role_name,
166
     *   and ldap_authorization_og2_get_group functions work
167
     *   og_is_member($group_type, $gid, $entity_type = 'user', $entity = NULL, $states = array(OG_STATE_ACTIVE))
168
     */
169

    
170
    $values = array(
171
      'entity_type' => 'user',
172
      'entity' => $web_user->uid,
173
      'field_name' => FALSE,
174
      'state' => OG_STATE_ACTIVE,
175
    );
176
    $og_gryffindor_membership = og_group($this->groupType, $group_nids['gryffindor'], $values);
177
    $og_faculty_membership = og_group($this->groupType, $group_nids['faculty'], $values);
178

    
179
    og_role_grant($this->groupType, $group_nids['gryffindor'], $web_user->uid, $roles_by_name['gryffindor'][OG_AUTHENTICATED_ROLE]);
180
    og_role_grant($this->groupType, $group_nids['faculty'],    $web_user->uid, $roles_by_name['faculty'][OG_ADMINISTRATOR_ROLE]);
181
    og_role_grant($this->groupType, $group_nids['faculty'],    $web_user->uid, $roles_by_name['faculty']['dungeon-master']);
182
    og_role_grant($this->groupType, $group_nids['faculty'],    $web_user->uid, $roles_by_name['faculty'][OG_AUTHENTICATED_ROLE]);
183

    
184
    $web_user = user_load($web_user->uid, TRUE); // need to reload because of issue with og_group and og_role_grant
185
    $ids = array($web_user->uid);
186
    $user_entity = entity_load('user', $ids);
187

    
188
    $this->assertTrue(og_is_member($this->groupType, $group_nids['gryffindor'], 'user', $web_user),
189
       'User is member of Group gryffindor without LDAP (based on og_is_member() function)', $this->ldapTestId);
190

    
191
    $this->assertTrue(og_is_member($this->groupType, $group_nids['faculty'], 'user', $web_user),
192
       'User is member of Group faculty without LDAP (based on og_is_member() function)', $this->ldapTestId);
193

    
194
    $this->assertTrue(ldap_authorization_og2_has_role($this->groupType, $group_nids['gryffindor'], $web_user->uid, OG_AUTHENTICATED_ROLE),
195
      'User is member of Group gryffindor without LDAP (based on dap_authorization_og_has_role() function)', $this->ldapTestId);
196

    
197
    $this->assertTrue(ldap_authorization_og2_has_role($this->groupType, $group_nids['faculty'], $web_user->uid, OG_AUTHENTICATED_ROLE),
198
      'User is member of Group faculty without LDAP (based on ldap_authorization_og2_has_role() function)', $this->ldapTestId);
199

    
200
    $this->assertTrue(ldap_authorization_og2_has_role($this->groupType, $group_nids['faculty'], $web_user->uid, OG_ADMINISTRATOR_ROLE),
201
      'User is administrator member of Group faculty without LDAP (based on dap_authorization_og_has_role() function)', $this->ldapTestId);
202

    
203
    /***
204
     * II.A. construct ldapauthorization og object and test methods.
205
     * (unit tests for methods and class without any ldap user context).
206
     */
207
    //
208
    $this->ldapTestId = $this->module_name . ': LdapAuthorizationConsumerOG class';
209
    $og_auth = new LdapAuthorizationConsumerOG('og_group');
210
    $this->assertTrue(is_object($og_auth), 'Successfully instantiated LdapAuthorizationConsumerOG', $this->ldapTestId);
211
    $this->assertTrue($og_auth->consumerType == 'og_group',
212
      'LdapAuthorizationConsumerOG ConsumerType set properly', $this->ldapTestId);
213

    
214
    $this->assertTrue($og_auth->hasAuthorization($web_user, ldap_authorization_og_authorization_id($group_nids['faculty'], $roles_by_name['faculty'][OG_ADMINISTRATOR_ROLE], 'node')),
215
      'hasAuthorization() method works for non LDAP provisioned og authorization, faculty admin role', $this->ldapTestId);
216

    
217

    
218
    $should_haves = array(
219
      $consumer_ids['gryffindor'][OG_AUTHENTICATED_ROLE] => 'gryffindor member',
220
      $consumer_ids['faculty'][OG_AUTHENTICATED_ROLE] =>  'faculty member',
221
      $consumer_ids['faculty'][OG_ADMINISTRATOR_ROLE] => 'faculty admin',
222
      $consumer_ids['faculty']['dungeon-master'] => 'faculty dungeon master',
223
    );
224

    
225
    foreach ($should_haves as $consumer_id => $descriptor) {
226
      $this->assertTrue(ldap_authorization_og2_has_consumer_id($consumer_id, $web_user->uid),
227
         "LdapAuthorizationConsumerOG usersAuthorizations() for $descriptor - $consumer_id", $this->ldapTestId);
228
    }
229

    
230
    $ldap_entry = NULL;
231
    $user_data = array();
232
    $web_user = user_load($web_user->uid, TRUE);
233

    
234
    $this->assertTrue(ldap_authorization_og2_has_consumer_id($consumer_ids['faculty']['dungeon-master'], $web_user->uid),
235
      "LdapAuthorizationConsumerOG has faculty member role BEFORE authorizationRevoke() test revoke on member role " . $consumer_ids['faculty']['dungeon-master'], $this->ldapTestId);
236

    
237
    $web_user = user_load($web_user->uid, TRUE);
238
    $consumers = array($consumer_ids['faculty']['dungeon-master'] => $og_auth->emptyConsumer);
239
    $og_auth->authorizationRevoke($web_user, $user_data, $consumers, $ldap_entry, TRUE);
240
    $result = ldap_authorization_og2_has_consumer_id($consumer_ids['faculty']['dungeon-master'], $web_user->uid);
241
    $this->assertFalse($result,
242
      "LdapAuthorizationConsumerOG authorizationRevoke() test revoke on member role " . $consumer_ids['faculty']['dungeon-master'], $this->ldapTestId);
243

    
244
    $web_user = user_load($web_user->uid, TRUE);
245
    $consumers =  array($consumer_ids['faculty']['dungeon-master'] => $og_auth->emptyConsumer);
246
    $og_auth->authorizationRevoke($web_user, $user_data, $consumers, $ldap_entry, TRUE);
247
    $this->assertFalse(ldap_authorization_og2_has_consumer_id($consumer_ids['faculty']['dungeon-master'], $web_user->uid),
248
      "LdapAuthorizationConsumerOG authorizationRevoke() test revoke on custom member role role " . $consumer_ids['faculty']['dungeon-master'], $this->ldapTestId);
249

    
250
    $web_user = user_load($web_user->uid, TRUE);
251
    $initial_user_authorizations = $og_auth->usersAuthorizations($web_user, TRUE, TRUE);
252
    debug("initial_user_authorizations authorizations:"); debug($initial_user_authorizations);
253
    debug("initial_user data:"); debug($web_user->data);
254
    $og_auth->authorizationGrant($web_user, $user_data, array($consumer_ids['students'][OG_AUTHENTICATED_ROLE] =>  $og_auth->emptyConsumer), $ldap_entry, TRUE);
255
    $success = ldap_authorization_og2_has_consumer_id($consumer_ids['students'][OG_AUTHENTICATED_ROLE], $web_user->uid);
256
    $this->assertTrue($success, "LdapAuthorizationConsumerOG authorizationGrant() test grant on member role " . $consumer_ids['students'][OG_AUTHENTICATED_ROLE], $this->ldapTestId);
257
    if (!$success) {
258
      debug(array($user_data, array($consumer_ids['students'][OG_AUTHENTICATED_ROLE] => $og_auth->emptyConsumer)));
259
      debug("user authorizations:"); debug($og_auth->usersAuthorizations($web_user, TRUE));
260
    }
261
    $web_user = user_load($web_user->uid, TRUE);
262
    $result = $og_auth->authorizationRevoke($web_user, $user_data, array('node:454:44334'  => $og_auth->emptyConsumer), $ldap_entry, TRUE);
263
    $this->assertFalse($result,
264
      'LdapAuthorizationConsumerOG authorizationRevoke() test revoke of bogus authorization', $this->ldapTestId);
265

    
266
    $web_user = user_load($web_user->uid, TRUE);
267
    $result = $og_auth->authorizationGrant($web_user, $user_data, array('node:454:44334' => $og_auth->emptyConsumer), $ldap_entry, TRUE);
268
    $this->assertFalse($result,
269
      'LdapAuthorizationConsumerOG authorizationGrant() test grant of bogus authorization', $this->ldapTestId);
270

    
271
    $web_user = user_load($web_user->uid, TRUE);
272
    $result = $og_auth->authorizationRevoke($web_user, $user_data, array('bogusformat'), $ldap_entry, TRUE);
273
    $this->assertFalse($result,
274
      'LdapAuthorizationConsumerOG authorizationRevoke()  test revoke malformed params', $this->ldapTestId);
275

    
276
    $web_user = user_load($web_user->uid, TRUE);
277
    $result = $og_auth->authorizationGrant($web_user, $user_data, array('bogusformat'), $ldap_entry, TRUE);
278
    $this->assertFalse($result,
279
      'LdapAuthorizationConsumerOG authorizationGrant() test grant malformed params', $this->ldapTestId);
280

    
281
    /***
282
     * II.B. Also test function in ldap_authorization_og.module
283
     */
284

    
285
    list($students_group, $group_entity_id) = ldap_authorization_og2_get_group_from_name('node', 'students');
286
    //debug("ldap_authorization_og2_get_group_from_name: students_group"); debug($students_group);
287
    $this->assertTrue($students_group->title == 'students', 'ldap_authorization_og2_get_group_from_name() function works', $this->ldapTestId);
288

    
289
    $test = ldap_authorization_og2_has_role($this->groupType, $group_nids['gryffindor'], $web_user->uid, OG_ADMINISTRATOR_ROLE);
290
    $this->assertTrue($test, 'ldap_authorization_og2_has_role() function works', $this->ldapTestId);
291

    
292
    $test = ldap_authorization_og2_has_role($this->groupType,  $group_nids['students'], $web_user->uid, OG_ADMINISTRATOR_ROLE);
293
    $this->assertTrue($test === FALSE, 'ldap_authorization_og2_has_role() function fails with FALSE', $this->ldapTestId);
294

    
295
  }
296

    
297

    
298
  /**
299
 * authorization configuration flags tests clumped together
300
 */
301

    
302
function testFlags() {
303

    
304
  $sid = 'activedirectory1';
305
  $this->prepTestData(
306
    LDAP_TEST_LDAP_NAME,
307
    array($sid),
308
    'provisionToDrupal',
309
    'default',
310
    'og_group2'
311
    );
312

    
313
  $og_group_consumer = ldap_authorization_get_consumers('og_group', TRUE, TRUE);
314
  /**
315
   * LDAP_authorz.Flags.status=0: Disable ldap_authorization_drupal_role configuration and make sure no authorizations performed
316
   */
317

    
318
  list($props_set_display, $props_set_correctly) = $this->checkConsumerConfSetup('og_group2');
319
  $this->assertTrue(
320
    $props_set_correctly,
321
    'Authorization Configuration set correctly in test setup',
322
    'LDAP_authorz.Flags.setup.0'
323
  );
324
  if (!$props_set_correctly) {
325
    debug('LDAP_authorz.Flags.setup.0 properties not set correctly'); debug($props_set_display);
326
  }
327

    
328
  $this->consumerAdminConf['og_group']->useFirstAttrAsGroupId = 0;
329
  $this->consumerAdminConf['og_group']->status = 0;
330
  $this->consumerAdminConf['og_group']->save();
331

    
332
  $user = $this->drupalCreateUser(array());
333
  $hpotter = $this->testFunctions->drupalLdapUpdateUser(array('name' => 'hpotter', 'mail' =>  'hpotter@hogwarts.edu'), TRUE, $user);
334

    
335
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'query');  // just see if the correct ones are derived.
336
  $groups1 = $new_authorizations['og_group'];
337
 // debug('ldap_authorizations_user_authorizations disabled:'); debug($new_authorizations);  debug($notifications);
338
  $this->assertTrue(
339
    count($new_authorizations['og_group']) == 0,
340
    'disabled consumer configuration disallows authorizations.',
341
    'LDAP_authorz.Flags.status.0'
342
  );
343

    
344
  list($group_nodes, $group_nids, $group_entity_ids, $roles_by_name, $consumer_ids) = $this->getTestData(TRUE);
345

    
346
  $this->consumerAdminConf['og_group']->status = 1;
347
  $this->consumerAdminConf['og_group']->save();
348
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'query', 'og_group');  // just see if the correct ones are derived.
349
  //debug('ldap_authorizations_user_authorizations enabled: '); debug($new_authorizations);  debug($notifications);
350

    
351
  $correct_groups = !empty($new_authorizations['og_group'][$consumer_ids['students'][OG_AUTHENTICATED_ROLE]]) &&
352
    !empty($new_authorizations['og_group'][$consumer_ids['gryffindor'][OG_AUTHENTICATED_ROLE]]);
353
  $this->assertTrue($correct_groups, 'enabled consumer configuration allows authorizations.', 'LDAP_authorz.Flags.status.1');
354
  if (!$correct_groups) {
355
    debug('LDAP_authorz.Flags.enable.1 roles with enabled'); debug($new_authorizations);
356
  }
357

    
358

    
359
  /**
360
   * LDAP_authorz.onlyLdapAuthenticated=1: create normal user and
361
   * apply authorization query.  should return no roles
362
   */
363
  $this->consumerAdminConf['og_group']->onlyApplyToLdapAuthenticated = 1;
364
  $this->consumerAdminConf['og_group']->status = 1;
365
  $this->consumerAdminConf['og_group']->save();
366

    
367
  $user = $this->drupalCreateUser(array());
368
  $hgrainger = $this->testFunctions->drupalLdapUpdateUser(array('name' => 'hgrainger', 'mail' =>  'hgrainger@hogwarts.edu'), TRUE, $user);
369

    
370
  // remove old authmap in case it exists so test will work
371
  db_delete('authmap')
372
    ->condition('uid', $user->uid)
373
    ->condition('module', 'ldap_user')
374
    ->execute();
375

    
376
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hgrainger, 'query');  // just see if the correct ones are derived.
377
  $success = (isset($new_authorizations['og_group']) && count($new_authorizations['og_group'] ) == 0);
378
  $this->assertTrue($success, ' only apply to ldap authenticated grants no roles for non ldap user.', 'LDAP_authorz.onlyLdapAuthenticated.1');
379
  if (!$success) {
380
    debug('LDAP_authorz.onlyLdapAuthenticated.1');
381
    debug($new_authorizations);
382
    debug($this->testFunctions->ldapUserIsAuthmapped('hgrainger'));
383
    debug($notifications);
384
  }
385

    
386

    
387
  /**
388
   * LDAP_authorz.Flags.synchOnLogon - execute logon and check that no roles are applied if disabled
389
   */
390

    
391
  $this->consumerAdminConf['og_group']->synchOnLogon = 0;
392
  $this->consumerAdminConf['og_group']->save();
393
  $edit = array(
394
    'name' => 'hgrainger',
395
    'pass' => 'goodpwd',
396
  );
397
  $this->drupalPost('user', $edit, t('Log in'));
398
  $this->assertText(
399
    t('Member for'),
400
    'New Ldap user with good password authenticated.',
401
    'LDAP_authorz.Flags.synchOnLogon.0'
402
  );
403
  $this->assertTrue(
404
    $this->testFunctions->ldapUserIsAuthmapped('hgrainger'),
405
    'Ldap user properly authmapped.',
406
    'LDAP_authorz.Flags.synchOnLogon.0'
407
  );
408

    
409
  $hgrainger = user_load_by_name('hgrainger');
410
  $this->drupalGet('user/logout');
411

    
412
  $this->consumerAdminConf['og_group']->synchOnLogon = 1;
413
  $this->consumerAdminConf['og_group']->save();
414
  $edit = array(
415
    'name' => 'hgrainger',
416
    'pass' => 'goodpwd',
417
  );
418
  $this->drupalPost('user', $edit, t('Log in'));
419
  $this->assertText(t('Member for'), 'New Ldap user with good password authenticated.',
420
    'LDAP_authorz.Flags.synchOnLogon=1');
421
  $hgrainger = user_load_by_name('hgrainger');
422
  $this->drupalGet('user/logout');
423

    
424
  // create a couple roles for next 2 tests
425
  $troublemaker = new stdClass();
426
  $troublemaker->name = 'troublemaker';
427
  user_role_save($troublemaker);
428
  $troublemaker = user_role_load_by_name('troublemaker');
429

    
430
  $superadmin = new stdClass();
431
  $superadmin->name = 'superadmin';
432
  user_role_save($superadmin);
433
  $superadmin = user_role_load_by_name('superadmin');
434

    
435
   /**
436
   * LDAP_authorz.Flags.revokeLdapProvisioned: test flag for
437
   *   removing manually granted roles
438
   *
439
   *   $this->revokeLdapProvisioned == 1 : Revoke !consumer_namePlural previously granted by LDAP Authorization but no longer valid.
440
   *
441
   *   grant roles via ldap and some not vai ldap manually,
442
   *   then alter ldap so they are no longer valid,
443
   *   then logon again and make sure the ldap provided roles are revoked and the drupal ones are not revoked
444
   *
445
   */
446

    
447
  $this->consumerAdminConf['og_group']->onlyApplyToLdapAuthenticated = 0;
448
  $this->consumerAdminConf['og_group']->revokeLdapProvisioned = 1;
449
  $this->consumerAdminConf['og_group']->createConsumers = 1;
450
  $this->consumerAdminConf['og_group']->save();
451
  // set correct roles manually
452
  $hpotter = user_load_by_name('hpotter');
453
  user_delete($hpotter->uid);
454
  $user = $this->drupalCreateUser(array());
455
  $hpotter = $this->testFunctions->drupalLdapUpdateUser(array('name' => 'hpotter', 'mail' =>  'hpotter@hogwarts.edu'), TRUE, $user);
456
  $edit = array(
457
    'name' => 'hpotter',
458
    'pass' => 'goodpwd',
459
  );
460
  $this->drupalPost('user', $edit, t('Log in'));
461
  $this->assertText(
462
    t('Member for'),
463
    'New Ldap user with good password authenticated.',
464
    'LDAP_authorz.Flags.revokeLdapProvisioned=1'
465
  );
466
  $hpotter = user_load_by_name('hpotter');
467

    
468
  // add an underserved, ldap granted drupal role superadmin
469
  // and an undeserved, non ldap granted role troublemaker
470
  $hpotter = user_load($hpotter->uid, TRUE);
471
  $roles = $hpotter->roles;
472
  $roles[$troublemaker->rid] = $troublemaker->name;
473
  $roles[$superadmin->rid] = $superadmin->name;
474

    
475
  $data = array(
476
    'roles' =>  $roles,
477
    'data' => array('ldap_authorizations' =>
478
      array(
479
        'og_group' =>
480
        array(
481
          $superadmin->name =>
482
          array('date_granted' => 1304216778),
483
        ),
484
      ),
485
    ),
486
  );
487
  $hpotter = user_save($hpotter, $data);
488

    
489
  // apply correct authorizations.  should remove the administrator role but not the manually created 'troublemaker' role
490
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'set', 'og_group', 'logon');
491

    
492
  $hpotter = user_load($hpotter->uid, TRUE);
493
  $this->assertTrue(
494
    (!isset($new_authorizations['og_group'][$superadmin->rid])),
495
    ' revoke superadmin ldap granted roles when no longer deserved.',
496
    'LDAP_authorz.Flags.revokeLdapProvisioned=1'
497
  );
498

    
499

    
500
   /**
501
   * LDAP_authorz.Flags.regrantLdapProvisioned
502
   * $this->regrantLdapProvisioned == 1 :
503
   *   Re grant !consumer_namePlural previously granted
504
   *   by LDAP Authorization but removed manually.
505
   *
506
   * - manually remove ldap granted role
507
   * - logon
508
   * - check if regranted
509
   */
510
  $this->drupalGet('user/logout');
511
  $this->consumerAdminConf['og_group']->regrantLdapProvisioned = 1;
512
  $this->consumerAdminConf['og_group']->save();
513
  $hpotter = user_load($hpotter->uid, TRUE);
514
  $roles = $hpotter->roles;
515
  unset($roles[$superadmin->rid]);
516
  user_save($hpotter, array('roles' => $roles));
517
  $hpotter = user_load($hpotter->uid, TRUE);
518
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'set', 'og_group', 'logon');
519
  $hpotter = user_load($hpotter->uid, TRUE);
520
  $success = !in_array('administrator', array_values($hpotter->roles));
521

    
522
  $this->assertTrue(
523
    $success,
524
    'regrant Ldap Provisioned roles that were manually revoked',
525
    'LDAP_authorz.Flags.regrantLdapProvisioned=1'
526
  );
527
  if (!$success) {
528
    debug('LDAP_authorz.Flags.regrantLdapProvisioned=1');
529
    debug('hpotter roles'); debug($hpotter->roles);
530
    debug('new_authorizations'); debug($new_authorizations);
531
  }
532

    
533
  /**
534
  * LDAP_authorz.Flags.createConsumers=1
535
  */
536

    
537
  if (!empty($og_group_consumer['allowConsumerObjectCreation']) && $og_group_consumer['allowConsumerObjectCreation']) {
538
    //@todo.  this needs to be finished when creation of og groups is added to ldap authorization og functionality
539

    
540
    //add new mapping to and enable create consumers
541
    $this->prepTestData('hogwarts', array($sid), 'provisionToDrupal', 'default', 'drupal_role_default');
542
    $this->drupalGet('user/logout');
543
    $new_role = 'oompa-loompas';
544
    $this->consumerAdminConf['og_group']->createConsumers = 1;
545
    $this->consumerAdminConf['og_group']->mappings[] = array(
546
      'from' => 'cn=students,ou=groups,dc=hogwarts,dc=edu',
547
      'user_entered' => $new_role,
548
      'normalized' => 'node:' . $new_role . ':' . OG_AUTHENTICATED_ROLE,
549
      'simplified' => $new_role,
550
      'valid' => TRUE,
551
      'error_message' => '',
552
      );
553

    
554
    $this->consumerAdminConf['og_group']->save();
555

    
556
    $edit = array(
557
      'name' => 'hpotter',
558
      'pass' => 'goodpwd',
559
    );
560
    $this->drupalPost('user', $edit, t('Log in'));
561

    
562
    $new_role_created = in_array($new_role, array_values(user_roles()));
563
    $roles_by_name = array_flip(user_roles());
564
    $hpotter = user_load_by_name('hpotter');
565
    $hpotter = user_load($hpotter->uid, TRUE);
566
    $role_granted = isset($hpotter->roles[$roles_by_name[$new_role]]);
567
    debug('roles'); debug(user_roles());
568
    debug('roles by name'); debug($roles_by_name);
569
    debug('hpotter->roles'); debug($hpotter->roles);
570
    debug("$new_role_created AND $role_granted");
571

    
572
    $this->assertTrue(
573
      ($new_role_created && $role_granted),
574
      'create consumers (e.g. roles)',
575
      'LDAP_authorz.Flags.createConsumers=1'
576
    );
577
  }
578

    
579
}
580

    
581
}