From 444991c292f3480a3be48be028a2709803320765 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.install |   11 ++++++++
 cas.module  |   59 +++++++++++++++++++++++++++++++++++-----------
 cas.test    |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+), 14 deletions(-)

diff --git a/cas.install b/cas.install
index b3d7af7..8c481be 100644
--- a/cas.install
+++ b/cas.install
@@ -337,3 +337,14 @@ function cas_update_6304() {
     menu_link_save($link);
   }
 }
+
+/**
+ * Transform numeric block deltas to string block deltas.
+ */
+function cas_update_7000(&$sandbox) {
+  $renamed_deltas = array(
+    'cas' => array('0' => 'login'),
+  );
+  $moved_deltas = array();
+  update_fix_d7_block_deltas($sandbox, $renamed_deltas, $moved_deltas);
+}
diff --git a/cas.module b/cas.module
index 2b886b1..b8001cc 100644
--- a/cas.module
+++ b/cas.module
@@ -663,24 +663,55 @@ function cas_logout() {
   drupal_goto($logout_url, $options);
 }
 
+/**
+ * Implements hook_block_info().
+ */
+function cas_block_info() {
+  $blocks['login']['info'] = t('CAS login');
+  // Not worth caching.
+  $blocks['login']['cache'] = DRUPAL_NO_CACHE;
 
-function cas_login_form($form_state) {
-  $form['submit'] = array('#type' => 'submit', '#value' => t('Log in via CAS'));
-  if (module_exists('persistent_login') && variable_get('cas_allow_rememberme', 0)) {
-    $form['remember'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Remember me'),
-      '#default_value' => 0,
-    );
-  }
-  return $form;
+  return $blocks;
 }
 
-function cas_login_form_submit($form, &$form_state) {
-  if ($form_state['values']['remember']) {
-    $_SESSION['cas_remember'] = 1;
+/**
+ * Implements hook_block_view().
+ */
+function cas_block_view($delta = '') {
+  global $user;
+
+  $block = array();
+
+  switch ($delta) {
+    case 'login':
+      // For usability's sake, avoid showing two login forms on one page.
+      if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
+        $block['subject'] = t('User login');
+        $block['content'] = drupal_get_form('cas_login_block');
+      }
+      return $block;
   }
-  $form_state['redirect'] = 'cas';
+}
+
+/**
+ * Login form for the CAS login block.
+ */
+function cas_login_block($form) {
+  $form['#action'] = url('cas', array('query' => drupal_get_destination()));
+  $form['#id'] = 'cas-login-form';
+
+  $form['cas_login_redirection_message'] = array(
+    '#type' => 'item',
+    '#markup' => t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)),
+    '#weight' => -1,
+  );
+  $form['actions'] = array('#type' => 'actions');
+  $form['actions']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT)),
+  );
+
+  return $form;
 }
 
 /**
diff --git a/cas.test b/cas.test
index d9d5716..5104db0 100644
--- a/cas.test
+++ b/cas.test
@@ -724,3 +724,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(
+      'blocks[user_login][region]' => '-1',
+      'blocks[cas_login][region]' => 'sidebar_first',
+    );
+    $this->drupalPost('admin/structure/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-login'));
+    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);
+  }
+}
-- 
1.7.4.1

