From 9194056668678b4bebcbb5c82776fd7a3baf36d4 Mon Sep 17 00:00:00 2001
From: "Bradley M. Froehle" <brad.froehle@gmail.com>
Date: Thu, 30 Jun 2011 19:18:48 -0700
Subject: [PATCH] Move CAS server responses into the CAS Server module.

---
 cas.test                    |    3 +-
 cas_server.module           |   20 +++++++++
 cas_server.response.inc     |   90 +++++++++++++++++++++++++++++++++++++++++++
 tests/cas_test.info         |    4 ++
 tests/cas_test.module       |   20 ---------
 tests/cas_test.response.inc |   90 -------------------------------------------
 6 files changed, 116 insertions(+), 111 deletions(-)
 create mode 100644 cas_server.response.inc
 delete mode 100644 tests/cas_test.response.inc

diff --git a/cas.test b/cas.test
index b8fbf7c..cfe0fd2 100644
--- a/cas.test
+++ b/cas.test
@@ -14,7 +14,8 @@ class CasTestHelper extends DrupalWebTestCase {
    * Creates an administrative user and downloads phpCAS.
    */
   function setUp($modules = array()) {
-    $modules = array_merge(array('cas', 'cas_test'), $modules);
+    // cas_test requires the CAS Server module.
+    $modules = array_merge(array('cas', 'cas_test', 'cas_server'), $modules);
     parent::setUp($modules);
 
     // Tests will fail unless clean URLs are enabled, due to an incompatibility
diff --git a/cas_server.module b/cas_server.module
index 8eeb1c7..7bb7713 100644
--- a/cas_server.module
+++ b/cas_server.module
@@ -47,6 +47,26 @@ function cas_server_menu() {
   return $items;
 }
 
+/**
+ * Implements hook_theme().
+ */
+function cas_server_theme() {
+  return array(
+    'cas_service_validate_success' => array(
+      'variables' => array('name' => NULL, 'attributes' => NULL),
+      'file' => 'cas_server.response.inc',
+    ),
+    'cas_service_validate_attributes' => array(
+      'variables' => array('attributes' => NULL, 'style' => 'jasig'),
+      'file' => 'cas_server.response.inc',
+    ),
+    'cas_service_validate_failure' => array(
+      'variables' => array('ticket' => NULL, 'error_code' => NULL),
+      'file' => 'cas_server.response.inc',
+    ),
+  );
+}
+
 function cas_server_service_return() {
   global $user;
   $service = $_COOKIE[CAS_LOGIN_COOKIE];
diff --git a/cas_server.response.inc b/cas_server.response.inc
new file mode 100644
index 0000000..f6815f8
--- /dev/null
+++ b/cas_server.response.inc
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * @file
+ * Response callbacks for the CAS Server module.
+ */
+
+/**
+ * Returns a CAS 2.0 service response for a validation success.
+ *
+ * @param $variables
+ *   An associative array containing the keys:
+ *   - 'name': CAS username.
+ *   - 'attributes': (optional) CAS attributes.
+ */
+function theme_cas_service_validate_success($variables) {
+  $cas_name = $variables['name'];
+  $output .= "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
+  $output .= "<cas:authenticationSuccess>\n";
+  $output .= "<cas:user>$cas_name</cas:user>\n";
+  $output .= theme('cas_service_validate_attributes', $variables);
+  $output .= "</cas:authenticationSuccess>\n";
+  $output .= "</cas:serviceResponse>\n";
+
+  return $output;
+}
+
+/**
+ * Returns CAS attributes as part of a CAS 2.0 service response.
+ *
+ * @param $variables
+ *   An asociative array containing the keys 'attributes' and 'style', where
+ *   the value of 'style' must be one of:
+ *     - 'jasig' (default)
+ *     - 'rubycas'
+ *     - 'name-value'
+ */
+function theme_cas_service_validate_attributes($variables) {
+  $attributes = $variables['attributes'];
+  $style = $variables['style'];
+
+  $output = '';
+  switch($style) {
+    case 'jasig':
+    default:
+      $output .= "<cas:attributes>\n";
+      $output .= "<cas:attraStyle>Jasig</cas:attraStyle>\n";
+      foreach ($attributes as $name => $values) {
+        foreach ((array) $values as $value) {
+          $output .= strtr("<cas:!name>!value</cas:!name>\n", array('!name' => $name, '!value' => $value));
+        }
+      }
+      $output .= "</cas:attributes>\n";
+    case 'rubycas':
+      $output .= "<cas:attraStyle>RubyCAS</cas:attraStyle>\n";
+      foreach ($attributes as $name => $values) {
+        foreach ((array) $values as $value) {
+          $output .= strtr("<cas:!name>!value</cas:!name>\n", array('!name' => $name, '!value' => $value));
+        }
+      }
+    case 'name-value':
+      $output .= "<cas:attribute name='attraStyle' value='Name-Value' />\n";
+      foreach ($attributes as $name => $values) {
+        foreach ((array) $values as $value) {
+          $output .= strtr("<cas:attribute name='!name' value='!value' />\n", array('!name' => $name, '!value' => $value));
+        }
+      }
+  }
+
+  return $output;
+}
+
+
+/**
+ * Returns a CAS 2.0 service response for a validation failure.
+ *
+ * @param $variables
+ *  An associative array containing the keys 'ticket' and 'error_code'.
+ */
+function theme_cas_service_validate_failure($variables) {
+  $ticket = $variables['ticket'];
+  $error_code = $variables['error_code'];
+  $output = "<cas:serviceReponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
+  $output .= "<cas:authenticationFailure code=\"$error_code\">\n";
+  $output .= "Ticket $ticket not recognized.\n";
+  $output .= "</cas:authenticationFailure>\n";
+  $output .= "</cas:serviceResponse>\n";
+
+  return $output;
+}
diff --git a/tests/cas_test.info b/tests/cas_test.info
index 10bf06a..31b7ba6 100644
--- a/tests/cas_test.info
+++ b/tests/cas_test.info
@@ -3,3 +3,7 @@ description = "Support module for CAS testing."
 package = Testing
 core = 7.x
 hidden = TRUE
+
+; Normally one should not enable both CAS and CAS Server. However, to reduce
+; code duplication we use several themes defined by that module.
+dependencies[] = cas_server
diff --git a/tests/cas_test.module b/tests/cas_test.module
index 341c001..78b7b9c 100644
--- a/tests/cas_test.module
+++ b/tests/cas_test.module
@@ -6,26 +6,6 @@
  */
 
 /**
- * Implements hook_theme().
- */
-function cas_test_theme() {
-  return array(
-    'cas_service_validate_success' => array(
-      'variables' => array('name' => NULL, 'attributes' => NULL),
-      'file' => 'cas_test.response.inc',
-    ),
-    'cas_service_validate_attributes' => array(
-      'variables' => array('attributes' => NULL, 'style' => 'jasig'),
-      'file' => 'cas_test.response.inc',
-    ),
-    'cas_service_validate_failure' => array(
-      'variables' => array('ticket' => NULL, 'error_code' => NULL),
-      'file' => 'cas_test.response.inc',
-    ),
-  );
-}
-
-/**
  *
  */
 function cas_test_cas_phpcas_alter() {
diff --git a/tests/cas_test.response.inc b/tests/cas_test.response.inc
deleted file mode 100644
index cfbee14..0000000
--- a/tests/cas_test.response.inc
+++ /dev/null
@@ -1,90 +0,0 @@
-<?php
-
-/**
- * @file
- * Response callbacks for the cas_test module.
- */
-
-/**
- * Returns a CAS 2.0 service response for a validation success.
- *
- * @param $variables
- *   An associative array containing the keys:
- *   - 'name': CAS username.
- *   - 'attributes': (optional) CAS attributes.
- */
-function theme_cas_service_validate_success($variables) {
-  $cas_name = $variables['name'];
-  $output .= "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
-  $output .= "<cas:authenticationSuccess>\n";
-  $output .= "<cas:user>$cas_name</cas:user>\n";
-  $output .= theme('cas_service_validate_attributes', $variables);
-  $output .= "</cas:authenticationSuccess>\n";
-  $output .= "</cas:serviceResponse>\n";
-
-  return $output;
-}
-
-/**
- * Returns CAS attributes as part of a CAS 2.0 service response.
- *
- * @param $variables
- *   An asociative array containing the keys 'attributes' and 'style', where
- *   the value of 'style' must be one of:
- *     - 'jasig' (default)
- *     - 'rubycas'
- *     - 'name-value'
- */
-function theme_cas_service_validate_attributes($variables) {
-  $attributes = $variables['attributes'];
-  $style = $variables['style'];
-
-  $output = '';
-  switch($style) {
-    case 'jasig':
-    default:
-      $output .= "<cas:attributes>\n";
-      $output .= "<cas:attraStyle>Jasig</cas:attraStyle>\n";
-      foreach ($attributes as $name => $values) {
-        foreach ((array) $values as $value) {
-          $output .= strtr("<cas:!name>!value</cas:!name>\n", array('!name' => $name, '!value' => $value));
-        }
-      }
-      $output .= "</cas:attributes>\n";
-    case 'rubycas':
-      $output .= "<cas:attraStyle>RubyCAS</cas:attraStyle>\n";
-      foreach ($attributes as $name => $values) {
-        foreach ((array) $values as $value) {
-          $output .= strtr("<cas:!name>!value</cas:!name>\n", array('!name' => $name, '!value' => $value));
-        }
-      }
-    case 'name-value':
-      $output .= "<cas:attribute name='attraStyle' value='Name-Value' />\n";
-      foreach ($attributes as $name => $values) {
-        foreach ((array) $values as $value) {
-          $output .= strtr("<cas:attribute name='!name' value='!value' />\n", array('!name' => $name, '!value' => $value));
-        }
-      }
-  }
-
-  return $output;
-}
-
-
-/**
- * Returns a CAS 2.0 service response for a validation failure.
- *
- * @param $variables
- *  An associative array containing the keys 'ticket' and 'error_code'.
- */
-function theme_cas_service_validate_failure($variables) {
-  $ticket = $variables['ticket'];
-  $error_code = $variables['error_code'];
-  $output = "<cas:serviceReponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
-  $output .= "<cas:authenticationFailure code=\"$error_code\">\n";
-  $output .= "Ticket $ticket not recognized.\n";
-  $output .= "</cas:authenticationFailure>\n";
-  $output .= "</cas:serviceResponse>\n";
-
-  return $output;
-}
-- 
1.7.5.4

