From 790d0eedc63b12a55099a9cdd710fa31b785313a Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <brad.froehle@gmail.com>
Date: Tue, 26 Apr 2011 11:36:23 -0700
Subject: [PATCH] Issue #1139118: Login redirection does not work with CAS login block.

---
 cas.module |   40 +++++++++++++++++++------------
 cas.test   |   76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 15 deletions(-)

diff --git a/cas.module b/cas.module
index f60771c..5c80ded 100644
--- a/cas.module
+++ b/cas.module
@@ -612,13 +612,14 @@ function cas_logout() {
 }
 
 /**
- * Provides login blog that only shows up when the user logs in.
+ * Implements hook_block().
+ *
+ * Provides CAS login block for anonymous users.
  */
 function cas_block($op = 'list', $delta = 0, $edit = array()) {
   global $user;
   if ($op == 'list') {
-    $blocks[0]['info'] = t('CAS User login');
-
+    $blocks[0]['info'] = t('CAS login');
     return $blocks;
   }
   elseif ($op == 'view') {
@@ -628,21 +629,27 @@ function cas_block($op = 'list', $delta = 0, $edit = array()) {
       case 0:
         // For usability's sake, avoid showing two login forms on one page.
         if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
-          $edit = $_POST['edit'];
-
-          $output = "<div class=\"user-login-block\">\n";
-          $output .= drupal_get_form('cas_login_form');
-          $output .= "</div>\n";
-          $block['subject'] = t('User Login');
-          $block['content'] = $output;
+          $block['subject'] = t('User login');
+          $block['content'] = drupal_get_form('cas_login_block');
         }
         return $block;
     }
   }
 }
 
-function cas_login_form($form_state) {
-  $form['submit'] = array('#type' => 'submit', '#value' => t('Log in via CAS'));
+/**
+ * Login form for the CAS login block.
+ */
+function cas_login_block($form_state) {
+  $form['cas.return_to'] = array('#type' => 'hidden', '#value' => user_login_destination());
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT)),
+  );
+  $form['cas_login_redirection_message'] = array(
+    '#value' => '<div class="form-item cas-login-redirection-message">' . t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)) . '</div>',
+    '#weight' => -1,
+  );
   if (module_exists('persistent_login') && variable_get('cas_allow_rememberme', 0)) {
     $form['remember'] = array(
       '#type' => 'checkbox',
@@ -653,11 +660,14 @@ function cas_login_form($form_state) {
   return $form;
 }
 
-function cas_login_form_submit($form, &$form_state) {
-  if ($form_state['values']['remember']) {
+/**
+ * Submit handler for cas_login_block().
+ */
+function cas_login_block_submit($form, &$form_state) {
+  if (!empty($form_state['values']['remember'])) {
     $_SESSION['cas_remember'] = 1;
   }
-  $form_state['redirect'] = 'cas';
+  $form_state['redirect'] = url('cas', array('query' => $form_state['values']['cas.return_to'], 'absolute' => TRUE));
 }
 
 /**
diff --git a/cas.test b/cas.test
index d759465..f8570e1 100644
--- a/cas.test
+++ b/cas.test
@@ -757,3 +757,79 @@ class CasRequiredLoginTestCase extends CasTestHelper {
     $this->assertUrl('node');
   }
 }
+
+/**
+ * Tests the visibility and functionality of the CAS login block.
+ */
+class CasLoginBlockTestCase extends CasTestHelper {
+  public static function getInfo() {
+    return array(
+      'name' => 'CAS login block',
+      'description' => 'Tests the CAS login block.',
+      'group' => 'Central Authentication Service',
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+
+    // Enable the CAS login block.
+    $admin_user = $this->drupalCreateUser(array('administer blocks'));
+    $this->drupalLogin($admin_user);
+    $edit = array(
+      'user_0[region]' => '-1',
+      'cas_0[region]' => 'left',
+    );
+    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
+    $this->drupalLogout();
+  }
+
+  /**
+   * Asserts that the CAS login block is shown or not shown.
+   *
+   * @param $visible
+   *   Whether or not the CAS login block is expected to be shown.
+   *
+   * @return
+   *  TRUE if the assertion succeeded, FALSE otherwise.
+   */
+  function assertCasLoginBlock($visible) {
+    $xpath = '//div[@id=block-cas-0]/*';
+    $xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-cas-0'));
+    if ($visible) {
+      return $this->assertFieldByXPath($xpath, NULL, t('CAS login block found.'));
+    }
+    else {
+      return $this->assertNoFieldByXPath($xpath, NULL, t('CAS login block not found.'));
+    }
+  }
+
+  /**
+   * Tests the visibility and functionality of the CAS login block.
+   */
+  function testCasLoginBlock() {
+    $account = $this->casCreateUser();
+    variable_set('cas_test_cas_user', array('name' => $account->cas_name));
+
+    // Verify that the block is shown on some pages, but not on others.
+    $this->drupalGet('');
+    $this->assertCasLoginBlock(TRUE);
+
+    $this->drupalGet('user');
+    $this->assertCasLoginBlock(FALSE);
+
+    $this->drupalGet('user/1');
+    $this->assertCasLoginBlock(TRUE);
+
+    // Log in using the login block, and verify redirection works.
+    $edit = array();
+    $submit = t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT));
+
+    $this->drupalPost('', $edit, $submit);
+    $this->assertLoggedIn($account);
+    $this->assertUrl('node');
+
+    // Block should not be shown to logged in users.
+    $this->assertCasLoginBlock(FALSE);
+  }
+}
\ No newline at end of file
-- 
1.7.4.1

