Projet

Général

Profil

Paste
Télécharger (27,1 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / cas / cas.test @ c12e7e6a

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for cas.module.
6
 */
7

    
8
class CasTestHelper extends DrupalWebTestCase {
9
  protected $admin_user;
10

    
11
  /**
12
   * Helper class for CAS tests.
13
   *
14
   * Creates an administrative user and downloads phpCAS.
15
   */
16
  function setUp() {
17
    // Install modules needed for this test. This could have been passed in as
18
    // either a single array argument or a variable number of string arguments.
19
    // @todo Remove this compatibility layer in Drupal 8, and only accept
20
    // $modules as a single array argument.
21
    $modules = func_get_args();
22
    if (isset($modules[0]) && is_array($modules[0])) {
23
      $modules = $modules[0];
24
    }
25

    
26
    // cas_test requires the CAS Server module.
27
    $modules = array_merge(array('cas', 'cas_test', 'cas_server'), $modules);
28
    parent::setUp($modules);
29

    
30
    // Tests will fail unless clean URLs are enabled, due to an incompatibility
31
    // in phpCAS.
32
    variable_set('clean_url', TRUE);
33

    
34
    // Create admin user.
35
    $this->admin_user = $this->drupalCreateUser(array('administer users', 'administer cas'));
36

    
37
    // Download and extract in PHPCAS.
38
    $this->downloadExtractPhpCas('1.2.2');
39
  }
40

    
41
  /**
42
   * Download and extract phpCAS.
43
   *
44
   * Sets the 'cas_library_dir' variable to the directory where phpCAS
45
   * is downloaded.
46
   *
47
   * @param $version
48
   *   The phpCAS version number to download and extract.
49
   */
50
  function downloadExtractPhpCas($version) {
51
    // Find the most URL of the most recent phpCAS version.
52
    $directory = 'CAS-' . $version;
53
    $filename = 'CAS-' . $version . '.tgz';
54
    $url = 'http://downloads.jasig.org/cas-clients/php/' . $version . '/' . $filename;
55

    
56
    // Avoid downloading the file dozens of times
57
    $simpletest_cache = $this->originalFileDirectory . '/simpletest/cas';
58
    if (!file_exists($simpletest_cache)) {
59
      mkdir($simpletest_cache);
60
    }
61

    
62
    // Local archive name.
63
    $local_archive = $simpletest_cache . '/' . $filename;
64
    $cas_library_dir = $simpletest_cache . '/' . $directory;
65

    
66
    // Begin single threaded code.
67
    if (function_exists('sem_get')) {
68
      $semaphore = sem_get(ftok(__FILE__, 1));
69
      sem_acquire($semaphore);
70
    }
71

    
72
    // Download and extact the archive, but only in one thread.
73
    if (!file_exists($local_archive)) {
74
      $local_archive = system_retrieve_file($url, $local_archive, FALSE, FILE_EXISTS_REPLACE);
75
    }
76
    if (!file_exists($cas_library_dir)) {
77
      // Extract the files.
78
      $archiver = archiver_get_archiver($local_archive);
79
      $archiver->extract($simpletest_cache);
80
      // Work around issue https://issues.jasig.org/browse/PHPCAS-105.
81
      // @see http://drupal.org/node/1120034
82
      switch ($version) {
83
        case '1.1.3':
84
          system_retrieve_file('http://drupal.org/files/issues/CAS-1.1.3-CAS-client.txt', $cas_library_dir . '/CAS/client.php', FALSE, FILE_EXISTS_REPLACE);
85
          break;
86
        case '1.2.1':
87
          system_retrieve_file('http://drupal.org/files/issues/CAS-1.2.1-CAS-client_0.txt', $cas_library_dir . '/CAS/client.php', FALSE, FILE_EXISTS_REPLACE);
88
          break;
89
      }
90
    }
91
    if (function_exists('sem_get')) {
92
      sem_release($semaphore);
93
    }
94
    // End single threaded code.
95

    
96
    // Verify that files were successfully extracted.
97
    $this->assertTrue(file_exists($cas_library_dir . '/CAS.php'), t('CAS.php found in @cas_library_dir.', array('@cas_library_dir' => $cas_library_dir)));
98

    
99
    // Set the CAS library directory.
100
    variable_set('cas_library_dir', $cas_library_dir);
101
  }
102

    
103
  /**
104
   * Create a CAS user with the specified username.
105
   *
106
   * @param $cas_name
107
   *   The CAS username. If omitted, a CAS username will be automatically
108
   *   generated.
109
   * @param $permissions
110
   *   An array of permissions to assign to the created user.
111
   *
112
   * @return
113
   *   A user account object. The CAS username is present in the cas_name
114
   *   field.
115
   */
116
  function casCreateUser($cas_name = NULL, $permissions = array('access comments', 'access content', 'post comments', 'skip comment approval')) {
117
    // Create user.
118
    $account = $this->drupalCreateUser($permissions);
119
    $pass_raw = $account->pass_raw;
120

    
121
    // Add CAS username.
122
    if (empty($cas_name)) {
123
      $cas_name = $this->randomName();
124
    }
125
    $edit['cas_name'] = $cas_name;
126
    $account = user_save($account, $edit);
127

    
128
    // Restore password.
129
    $account->pass_raw = $pass_raw;
130
    return $account;
131
  }
132

    
133
  /**
134
   * Log in a CAS user with the internal browser.
135
   *
136
   * @param $account
137
   *   A user object with a valid CAS username field, or the CAS username as a
138
   *   string.
139
   * @param $attributes
140
   *   Additional attributes for the CAS user.
141
   */
142
  function casLogin($account, $attributes = array()) {
143
    if ($this->loggedInUser) {
144
      $this->drupalLogout();
145
    }
146

    
147
    // Log in the user.
148
    $cas_name = is_object($account) ? $account->cas_name : $account;
149
    $cas_user = array('name' => $cas_name, 'attributes' => $attributes);
150
    variable_set('cas_test_cas_user', $cas_user);
151
    $this->drupalGet('cas');
152

    
153
    $pass = $this->assertLink(t('Log out'), 0, t('CAS user %cas_name successfully logged in.', array('%cas_name' => $cas_name)), t('User login'));
154
    if ($pass) {
155
      $this->loggedInUser = cas_user_load_by_name($cas_name, TRUE);
156
    }
157
  }
158

    
159
  /**
160
   * Assert that the user has logged in.
161
   *
162
   * @return
163
   *  TRUE if the assertion succeeded, FALSE otherwise.
164
   */
165
  function assertLoggedIn($account) {
166
    $pass = $this->assertLink(t('Log out'), 0, t('CAS user %cas_name successfully logged in.', array('%cas_name' => $account->cas_name)), t('User login'));
167
    if ($pass) {
168
      $this->loggedInUser = $account;
169
    }
170
    return $pass;
171
  }
172

    
173
  /**
174
   * Assert that the user has been logged out.
175
   *
176
   * @return
177
   *  TRUE if the assertion succeeded, FALSE otherwise.
178
   */
179
  function assertLoggedOut() {
180
    $this->drupalGet('user');
181
    $pass = $this->assertField('name', t('Username field found.'), t('Logout'));
182
    $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout'));
183
    if ($pass) {
184
      $this->loggedInUser = FALSE;
185
    }
186
    return $pass;
187
  }
188

    
189
  /**
190
   * Assert the value of the token.
191
   *
192
   * @param $token
193
   *   A token to evaluate for the current CAS user.
194
   * @param $value
195
   *   The expected value after the token is evaluated.
196
   * @param $message
197
   *   The message to display along with the assertion.
198
   *
199
   * @return
200
   *  TRUE if the assertion succeeded, FALSE otherwise.
201
   */
202
  function assertToken($token, $value, $message = '') {
203
    $options = array(
204
      'query' => array(
205
        'token' => $token,
206
        'name' => $this->loggedInUser->cas_name,
207
      ),
208
    );
209
    $path = 'cas_test/token';
210
    $out = $this->drupalGet($path, $options);
211
    return $this->assertEqual($out, $value, $message, 'Token');
212
  }
213

    
214
}
215

    
216
class CasUserAdminTestCase extends CasTestHelper {
217

    
218
  public static function getInfo() {
219
    return array(
220
      'name' => 'User administration',
221
      'description' => 'Test CAS user administration.',
222
      'group' => 'Central Authentication Service'
223
    );
224
  }
225

    
226
  /**
227
   * Registers, modifies, and deletes a CAS user using User API hooks.
228
   */
229
  function testCASUserHooks() {
230
    // Create a test account.
231
    $account = $this->drupalCreateUser();
232
    $uid = $account->uid;
233

    
234
    // Add a CAS username.
235
    $cas_name = $this->randomName();
236
    $edit = array('cas_name' => $cas_name);
237
    $account = user_save($account, $edit);
238
    $this->assertEqual($cas_name, $account->cas_name, t('CAS username %cas_name successfully created.', array('%cas_name' => $cas_name)));
239

    
240
    // Reload the account and ensure the CAS name is still present.
241
    $account = user_load($uid);
242
    $this->assertEqual($cas_name, $account->cas_name, t('CAS username %cas_name successfully saved.', array('%cas_name' => $cas_name)));
243

    
244
    // Load the account by the CAS username.
245
    $account = cas_user_load_by_name($cas_name);
246
    $this->assertEqual($uid, $account->uid, t('Loaded the correct account with CAS username %cas_name.', array('%cas_name' => $cas_name)));
247

    
248
    // Change the CAS username.
249
    $cas_new_name = $this->randomName();
250
    $account = user_load($uid);
251
    $edit = array('cas_name' => $cas_new_name);
252
    user_save($account, $edit);
253
    $account = user_load($uid);
254
    $this->assertEqual($cas_new_name, $account->cas_name, t('CAS username %cas_name successfully updated.', array('%cas_name' => $cas_new_name)));
255
    $this->assertEqual(count($account->cas_names), 1, t('Only one CAS username is present.'));
256
    $account = cas_user_load_by_name($cas_name);
257
    $this->assertFalse($account, t('Could not load account using old CAS username.'));
258

    
259
    // Remove the CAS username.
260
    $account = user_load($uid);
261
    $edit = array('cas_name' => NULL);
262
    user_save($account, $edit);
263
    $account = user_load($uid);
264
    $this->assertFalse($account->cas_name, t('CAS username successfully deleted.'));
265
    $this->assertEqual(count($account->cas_names), 0, t('No CAS usernames are present.'));
266

    
267
    // Attempt to load by a non-existant CAS username.
268
    $account = cas_user_load_by_name($cas_new_name);
269
    $this->assertFalse($account, t('Could not load account with non-existent CAS username.'));
270

    
271
    // Verify that all CAS usernames have been removed from {cas_user}.
272
    $cas_uid_count = db_select('cas_user')
273
      ->condition('cas_name', array($cas_name, $cas_new_name), 'IN')
274
      ->countQuery()
275
      ->execute()
276
      ->fetchField();
277
    $this->assertEqual($cas_uid_count, 0, t('CAS usernames successfully removed from {cas_user}.'));
278
  }
279
  /**
280
   * Tests adding a user with a CAS username in the administrative interface.
281
   */
282
  function testUserAdd() {
283
    $this->drupalLogin($this->admin_user);
284

    
285
    // Register a user with a CAS username.
286
    $cas_name = $this->randomName();
287
    $edit = array(
288
      'name' => $this->randomName(),
289
      'mail' => $this->randomName() . '@example.com',
290
      'cas_name' => $cas_name,
291
      'pass[pass1]' => $pass = $this->randomString(),
292
      'pass[pass2]' => $pass,
293
      'notify' => FALSE,
294
    );
295
    $this->drupalPost('admin/people/create', $edit, t('Create new account'));
296
    $this->assertText(t('Created a new user account for @name. No e-mail has been sent.', array('@name' => $edit['name'])), 'User created');
297

    
298
    $this->drupalGet('admin/people');
299
    $this->assertText($edit['name'], 'User found in list of users');
300
    $this->assertText($edit['cas_name'], 'CAS username found in list of users');
301

    
302
    // Verify that duplicate CAS usernames are not allowed.
303
    $edit = array(
304
      'name' => $this->randomName(),
305
      'mail' => $this->randomName() . '@example.com',
306
      'cas_name' => $cas_name,
307
      'pass[pass1]' => $pass = $this->randomString(),
308
      'pass[pass2]' => $pass,
309
      'notify' => FALSE,
310
    );
311
    $this->drupalPost('admin/people/create', $edit, t('Create new account'));
312
    $this->assertText(t('The CAS username is already in use on this site.'), 'CAS username already in use.');
313
  }
314

    
315
  /**
316
   * Tests adding a CAS user in the administrative interface.
317
   */
318
  function testCasUserAdd() {
319
    $this->drupalLogin($this->admin_user);
320

    
321
    // Add a CAS user.
322
    $edit = array(
323
      'cas_name' => $this->randomName(),
324
    );
325
    $this->drupalPost('admin/people/cas/create', $edit, t('Create new account'));
326
    $this->assertText(t('Created a new user account for @name. No e-mail has been sent.', array('@name' => $edit['cas_name'])), 'User created');
327

    
328
    // Verify the user shows up in the list of all users.
329
    $this->drupalGet('admin/people');
330
    $this->assertNoUniqueText($edit['cas_name'], 'User and CAS username found in list of users');
331

    
332
    // Attempt to add the user again and see that it fails.
333
    $this->drupalPost('admin/people/cas/create', $edit, t('Create new account'));
334
    $this->assertText(t('The CAS username is already in use on this site.'), 'CAS username already in use.');
335
  }
336
}
337

    
338
/**
339
 * Test case to test user editing behavior.
340
 */
341
class CasUserTestCase extends CasTestHelper {
342

    
343
  public static function getInfo() {
344
    return array(
345
      'name' => 'User behavior',
346
      'description' => 'Test CAS user behavior, including auto-registration and user editing.',
347
      'group' => 'Central Authentication Service',
348
    );
349
  }
350

    
351
  /**
352
   * Tests automatically registering a user on login.
353
   */
354
  function testCasAutoRegister() {
355
    $cas_name = $this->randomName();
356
    variable_set('cas_test_cas_user', array('name' => $cas_name));
357

    
358
    // Test that the user is not automatically registered.
359
    variable_set('cas_user_register', FALSE);
360
    $this->drupalGet('cas');
361
    $this->assertRaw(t('No account found for %cas_name.', array('%cas_name' => $cas_name)));
362

    
363
    // Test that the user is automatically registered.
364
    variable_set('cas_user_register', TRUE);
365
    $this->drupalGet('cas');
366
    $this->loggedInUser = cas_user_load_by_name($cas_name, TRUE);
367
    $this->assertRaw(t('Logged in via CAS as %cas_username.', array('%cas_username' => $cas_name)));
368
    $this->drupalLogout();
369

    
370
    // Create a new user.
371
    $user2 = $this->drupalCreateUser();
372
    $cas_name = $user2->name;
373
    variable_set('cas_test_cas_user', array('name' => $cas_name));
374

    
375
    // Test that CAS does not hijack an existing username.
376
    $this->drupalGet('cas');
377
    $this->assertRaw(t('A new account could not be created for %cas_name. The username is already in use on this site.', array('%cas_name' => $cas_name)));
378
  }
379

    
380
  function testUserEdit() {
381
    $cas_name = $this->randomName();
382
    $account = $this->casCreateUser($cas_name, array('change own username'));
383

    
384
    $this->casLogin($cas_name);
385

    
386
    // Standard user page.
387
    variable_set('cas_hide_email', FALSE);
388
    variable_set('cas_hide_password', FALSE);
389
    $this->drupalGet("user/$account->uid/edit");
390
    $this->assertField('mail', 'E-mail field is present.');
391
    $this->assertField('current_pass', 'Current password field is present.');
392
    $this->assertField('pass[pass1]', 'Existing password field 1 is present.');
393
    $this->assertField('pass[pass2]', 'Existing password field 2 is present.');
394
    $edit = array(
395
      'mail' => $mail = $this->randomName() . '@example.com',
396
      'current_pass' => $account->pass_raw,
397
    );
398
    $this->drupalPost("user/$account->uid/edit", $edit, t('Save'));
399
    $this->assertFieldByName('mail', $mail);
400

    
401
    // Hide the password, ensure edits can be made without providing
402
    // the current password.
403
    variable_set('cas_hide_password', TRUE);
404
    $this->drupalGet("user/$account->uid/edit");
405
    $this->assertNoField('current_pass', 'Current password field is not present.');
406
    $this->assertNoField('pass[pass1]', 'Existing password field 1 is not present.');
407
    $this->assertNoField('pass[pass2]', 'Existing password field 2 is not present.');
408
    $edit = array(
409
      'mail' => $mail = $this->randomName() . '@example.com',
410
    );
411
    $this->drupalPost("user/$account->uid/edit", $edit, t('Save'));
412
    $this->assertFieldByName('mail', $mail);
413

    
414
    // Hide the e-mail field as well, ensure that it is not visible.
415
    variable_set('cas_hide_email', TRUE);
416
    $this->drupalGet("user/$account->uid/edit");
417
    $this->assertNoField('mail', 'E-mail field is not present.');
418
  }
419

    
420
  function testNameToken() {
421
    $account = $this->casCreateUser();
422
    $this->casLogin($account);
423

    
424
    $this->assertToken('[cas:name]', $account->cas_name);
425
  }
426
}
427

    
428
/**
429
 * Test case to test user logout behavior.
430
 */
431
class CasLogoutRedirectionTestCase extends CasTestHelper {
432

    
433
  public static function getInfo() {
434
    return array(
435
      'name' => 'Logout redirection',
436
      'description' => 'Test CAS user logout redirection.',
437
      'group' => 'Central Authentication Service',
438
    );
439
  }
440

    
441
  /**
442
   * Test redirection on user logout.
443
   */
444
  function testLogoutRedirection() {
445
    $account = $this->casCreateUser();
446

    
447
    $this->casLogin($account);
448
    $this->drupalGet('caslogout');
449
    $this->assertText('Logged out. No redirection provided.');
450
    $this->assertLoggedOut();
451

    
452
    // Verify the destination parameter may be passed on logout, i.e.,
453
    // caslogout?destination=node
454
    $destination = 'node';
455
    $this->casLogin($account);
456
    $this->drupalGet('caslogout', array('query' => array('destination' => $destination)));
457
    $this->assertText(t('Logged out. Continue to @url.', array('@url' => url($destination, array('absolute' => TRUE)))));
458
    $this->assertLoggedOut();
459

    
460
    // Verify that remote destination parameters are not allowed.
461
    $destination = 'http://example.com/?query=yes#fragment';
462
    $this->casLogin($account);
463
    $this->drupalGet('caslogout', array('query' => array('destination' => $destination)));
464
    $this->assertText(t('Logged out. No redirection provided.'));
465
    $this->assertLoggedOut();
466

    
467
    // Verify 'cas_logout_destination' works for a variety of destinations,
468
    // including remote destinations.
469
    $destinations = array('<front>', 'http://example.com/?query=yes#fragment', 'node/1');
470
    foreach ($destinations as $destination) {
471
      variable_set('cas_logout_destination', $destination);
472
      $this->casLogin($account);
473
      $this->drupalGet('caslogout');
474
      $this->assertText(t('Logged out. Continue to @url.', array('@url' => url($destination, array('absolute' => TRUE)))));
475
      $this->assertLoggedOut();
476
    }
477

    
478
    // Verify 'cas_logout_destination' can be overwritten by passing the
479
    // destination query string.
480
    variable_set('cas_logout_destination', 'http://example.com/');
481
    $destination = 'node/1';
482
    $this->casLogin($account);
483
    $this->drupalGet('caslogout', array('query' => array('destination' => $destination)));
484
    $this->assertText(t('Logged out. Continue to @url.', array('@url' => url($destination, array('absolute' => TRUE)))));
485
    $this->assertLoggedOut();
486
  }
487
}
488

    
489
/**
490
 * Test case to test user login behavior.
491
 */
492
class CasLoginRedirectionTestCase extends CasTestHelper {
493

    
494
  public static function getInfo() {
495
    return array(
496
      'name' => 'Login redirection',
497
      'description' => 'Test CAS user login redirection.',
498
      'group' => 'Central Authentication Service',
499
    );
500
  }
501

    
502
  /**
503
   * Verify login redirection for an existing user.
504
   */
505
  function testExistingUserLoginRedirection() {
506
    $node1 = $this->drupalCreateNode();
507
    $node2 = $this->drupalCreateNode();
508
    $node3 = $this->drupalCreateNode();
509

    
510
    // Create a CAS user.
511
    $account = $this->casCreateUser();
512
    $cas_name = $account->cas_name;
513
    variable_set('cas_test_cas_user', array('name' => $cas_name));
514

    
515
    // Test going to 'cas'
516
    $this->drupalGet('cas');
517
    $this->assertLoggedIn($account);
518
    $this->assertUrl('');
519
    $this->drupalLogout();
520

    
521
    // Test going to 'cas?destination=node/$node1->nid'
522
    $destination = "node/$node1->nid";
523
    $this->drupalGet('cas', array('query' => array('destination' => $destination)));
524
    $this->assertLoggedIn($account);
525
    $this->assertUrl($destination);
526
    $this->drupalLogout();
527

    
528
    // Use the login block on $node2.
529
    $destination = "node/$node2->nid";
530
    variable_set('cas_login_form', CAS_ADD_LINK);
531
    $edit = array('cas_identifier' => TRUE);
532
    $this->drupalPost($destination, $edit, t('Log in'));
533
    $this->assertLoggedIn($account);
534
    $this->assertUrl($destination);
535
    $this->drupalLogout();
536

    
537
    // Use the regular login page, without a destination.
538
    $edit = array('cas_identifier' => TRUE);
539
    $this->drupalPost('user/login', $edit, t('Log in'));
540
    $this->assertLoggedIn($account);
541
    $this->assertUrl('user');
542
    $this->drupalLogout();
543

    
544
    // Use the regular login page, with a destination.
545
    $destination = "node/$node3->nid";
546
    $edit = array('cas_identifier' => TRUE);
547
    $this->drupalPost('user/login', $edit, t('Log in'), array('query' => array('destination' => $destination)));
548
    $this->assertLoggedIn($account);
549
    $this->assertUrl($destination);
550
    $this->drupalLogout();
551

    
552
    // External destinations are not allowed.
553
    $destination = '';
554
    $this->drupalGet('cas', array('query' => array('destination' => 'http://example.com/node/3')));
555
    $this->assertLoggedIn($account);
556
    $this->assertUrl($destination);
557
    $this->drupalLogout();
558
  }
559

    
560
  /**
561
   * Verify login redirection for a new user.
562
   */
563
  function testNewUserLoginRedirection() {
564
    // Initial login without a destination goes to front page.
565
    $cas_name = $this->randomName();
566
    $this->casLogin($cas_name);
567
    $this->assertUrl('');
568
    $this->drupalLogout();
569

    
570
    // Initial login with redirection goes to specified destination.
571
    $node = $this->drupalCreateNode();
572
    variable_set('cas_first_login_destination', "node/$node->nid");
573
    $cas_name = $this->randomName();
574
    $account = $this->casLogin($cas_name);
575
    $this->assertUrl("node/$node->nid");
576
    $this->drupalLogout();
577

    
578
    // The second login should not be redirected.
579
    $this->casLogin($cas_name);
580
    $this->assertUrl('');
581
    $this->drupalLogout();
582

    
583
    // Initial login with a admin-created account goes to the specified
584
    // destination.
585
    $account = $this->casCreateUser();
586
    $this->casLogin($account);
587
    $this->assertUrl("node/$node->nid");
588
    $this->drupalLogout();
589

    
590
    // The second login should not be redirected.
591
    $this->casLogin($account);
592
    $this->assertUrl('');
593
    $this->drupalLogout();
594
  }
595
}
596

    
597
/**
598
 * Test CAS Single Sign-Out.
599
 */
600
class CasSingleSignOutTestCase extends CasTestHelper {
601

    
602
  public static function getInfo() {
603
    return array(
604
      'name' => 'Single Sign-Out',
605
      'description' => 'Test CAS Single Sign-Out.',
606
      'group' => 'Central Authentication Service',
607
    );
608
  }
609

    
610
  function testSingleSignOut() {
611
    // Create a user, and log in.
612
    $cas_name = $this->randomName();
613
    $account = $this->casCreateUser($cas_name);
614
    $this->casLogin($account);
615

    
616
    cas_test_single_sign_out($cas_name);
617
    $this->assertLoggedOut();
618

    
619
    // @todo: Add additional tests for other methods of logging in (especially
620
    //   methods coming from cas_pages).
621
  }
622
}
623

    
624
/**
625
 * Test case for CAS gateway feature.
626
 */
627
class CasGatewayTestCase extends CasTestHelper {
628

    
629
  public static function getInfo() {
630
    return array(
631
      'name' => 'CAS Gateway',
632
      'description' => 'Test CAS Gateway ("Check to see if user is already logged in") feature.',
633
      'group' => 'Central Authentication Service',
634
    );
635
  }
636

    
637
  function setUp() {
638
    parent::setUp();
639
    variable_set('cas_check_first', TRUE);
640
  }
641

    
642
  /**
643
   * Test the CAS Gateway functionality of the user is not logged in.
644
   */
645
  function testCasGatewayLoggedOut() {
646
    $this->drupalGet('');
647
    $this->assertTrue($this->redirect_count > 1, 'Polled CAS server on first request.');
648
    $this->drupalGet('');
649
    $this->assertEqual($this->redirect_count, 0, 'Did not poll CAS server on second request.');
650
    $this->drupalGet('node');
651
    $this->assertEqual($this->redirect_count, 0, 'Did not poll CAS server on third request.');
652
  }
653

    
654
  /**
655
   * Test the CAS Gateway functionality of the user is logged in.
656
   */
657
  function testCasGatewayLoggedIn() {
658
    // Create a user.
659
    $cas_name = $this->randomName();
660
    $account = $this->casCreateUser($cas_name);
661
    variable_set('cas_test_cas_user', array('name' => $cas_name));
662

    
663
    $this->drupalGet('node');
664
    $this->assertLoggedIn($account);
665
  }
666
}
667

    
668
/**
669
 * Test case for CAS force login feature.
670
 */
671
class CasRequiredLoginTestCase extends CasTestHelper {
672

    
673
  public static function getInfo() {
674
    return array(
675
      'name' => 'Required Login',
676
      'description' => 'Test CAS required login redirection.',
677
      'group' => 'Central Authentication Service',
678
    );
679
  }
680

    
681
  /**
682
   * Test redirection forced by cas_access and cas_pages variables.
683
   */
684
  function testCasPages() {
685
    $node1 = $this->drupalCreateNode();
686
    $node2 = $this->drupalCreateNode();
687
    $account = $this->casCreateUser();
688
    variable_set('cas_test_cas_user', array('name' => $account->cas_name));
689

    
690
    $this->drupalGet("node/$node2->nid");
691

    
692
    // Enable required login for $node.
693
    variable_set('cas_access', 0);
694
    variable_set('cas_pages', "node/$node1->nid\nnode/$node2->nid");
695

    
696
    // Visit the node and verify we are logged in.
697
    $this->drupalGet("node/$node2->nid");
698
    $this->assertLoggedIn($account);
699
    $this->assertUrl("node/$node2->nid");
700
    $this->drupalLogout();
701

    
702
    // Invert the access restrictions. Verify we can get the access the node
703
    // without restriction.
704
    variable_set('cas_access', 1);
705
    $this->drupalGet("node/$node1->nid");
706
    $this->assertField('name', t('Username field found.'), t('Logout'));
707
    $this->assertField('pass', t('Password field found.'), t('Logout'));
708

    
709
    // Verify that accessing any other page redirects to the login page.
710
    variable_del('cas_test_cas_user');
711
    $this->drupalGet('node');
712
    $this->assertText('No CAS name provided.');
713
  }
714

    
715
  /**
716
   * Test redirection prevented by cas_exclude.
717
   */
718
  function testCasExclude() {
719
    $node = $this->drupalCreateNode();
720
    $account = $this->casCreateUser();
721
    variable_set('cas_test_cas_user', array('name' => $account->cas_name));
722

    
723
    variable_set('cas_check_first', TRUE);
724
    variable_set('cas_exclude', "node/$node->nid");
725

    
726
    // Visit an excluded page and ensure we did not try to log in.
727
    $this->drupalGet("node/$node->nid");
728
    $this->assertField('name', t('Username field found.'), t('Logout'));
729
    $this->assertField('pass', t('Password field found.'), t('Logout'));
730

    
731
    // Visit another page and ensure we logged in.
732
    $this->drupalGet('node');
733
    $this->assertLoggedIn($account);
734
    $this->assertUrl('node');
735
  }
736
}
737

    
738
/**
739
 * Tests the visibility and functionality of the CAS login block.
740
 */
741
class CasLoginBlockTestCase extends CasTestHelper {
742
  public static function getInfo() {
743
    return array(
744
      'name' => 'CAS login block',
745
      'description' => 'Tests the CAS login block.',
746
      'group' => 'Central Authentication Service',
747
    );
748
  }
749

    
750
  function setUp() {
751
    parent::setUp();
752

    
753
    // Enable the CAS login block.
754
    $admin_user = $this->drupalCreateUser(array('administer blocks'));
755
    $this->drupalLogin($admin_user);
756
    $edit = array(
757
      'blocks[user_login][region]' => '-1',
758
      'blocks[cas_login][region]' => 'sidebar_first',
759
    );
760
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
761
    $this->drupalLogout();
762
  }
763

    
764
  /**
765
   * Asserts that the CAS login block is shown or not shown.
766
   *
767
   * @param $visible
768
   *   Whether or not the CAS login block is expected to be shown.
769
   *
770
   * @return
771
   *  TRUE if the assertion succeeded, FALSE otherwise.
772
   */
773
  function assertCasLoginBlock($visible) {
774
    $xpath = '//div[@id=block-cas-0]/*';
775
    $xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-cas-login'));
776
    if ($visible) {
777
      return $this->assertFieldByXPath($xpath, NULL, t('CAS login block found.'));
778
    }
779
    else {
780
      return $this->assertNoFieldByXPath($xpath, NULL, t('CAS login block not found.'));
781
    }
782
  }
783

    
784
  /**
785
   * Tests the visibility and functionality of the CAS login block.
786
   */
787
  function testCasLoginBlock() {
788
    $account = $this->casCreateUser();
789
    variable_set('cas_test_cas_user', array('name' => $account->cas_name));
790

    
791
    // Verify that the block is shown on some pages, but not on others.
792
    $this->drupalGet('');
793
    $this->assertCasLoginBlock(TRUE);
794

    
795
    $this->drupalGet('user');
796
    $this->assertCasLoginBlock(FALSE);
797

    
798
    $this->drupalGet('user/1');
799
    $this->assertCasLoginBlock(TRUE);
800

    
801
    // Log in using the login block, and verify redirection works.
802
    $edit = array();
803
    $submit = t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT));
804

    
805
    $this->drupalPost('', $edit, $submit);
806
    $this->assertLoggedIn($account);
807
    $this->assertUrl('node');
808

    
809
    // Block should not be shown to logged in users.
810
    $this->assertCasLoginBlock(FALSE);
811
  }
812
}