summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/session_test.module
blob: 61aa3ae9573353edce852c457e4ba0d82847c9b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// $Id$

/**
 * Implements hook_menu().
 */
function session_test_menu() {
  $items['session-test/get'] = array(
    'title' => 'Session value',
    'page callback' => '_session_test_get',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/id'] = array(
    'title' => 'Session ID',
    'page callback' => '_session_test_id',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/id-from-cookie'] = array(
    'title' => 'Session ID from cookie',
    'page callback' => '_session_test_id_from_cookie',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/set/%'] = array(
    'title' => 'Set session value',
    'page callback' => '_session_test_set',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/no-set/%'] = array(
    'title' => 'Set session value but do not save session',
    'page callback' => '_session_test_no_set',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/set-message'] = array(
    'title' => 'Set message',
    'page callback' => '_session_test_set_message',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/set-message-but-dont-save'] = array(
    'title' => 'Set message but do not save session',
    'page callback' => '_session_test_set_message_but_dont_save',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/set-not-started'] = array(
    'title' => 'Set message when session is not started',
    'page callback' => '_session_test_set_not_started',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['session-test/is-logged-in'] = array(
    'title' => 'Check if user is logged in',
    'page callback' => '_session_test_is_logged_in',
    'access callback' => 'user_is_logged_in',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_boot().
 */
function session_test_boot() {
  header('X-Session-Empty: ' . intval(empty($_SESSION)));
}

/**
 * Page callback, prints the stored session value to the screen.
 */
function _session_test_get() {
  if (!empty($_SESSION['session_test_value'])) {
    return t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value']));
  }
  else {
    return "";
  }
}

/**
 * Page callback, stores a value in $_SESSION['session_test_value'].
 */
function _session_test_set($value) {
  $_SESSION['session_test_value'] = $value;
  return t('The current value of the stored session variable has been set to %val', array('%val' => $value));
}

/**
 * Menu callback: turns off session saving and then tries to save a value
 * anyway.
 */
function _session_test_no_set($value) {
  drupal_save_session(FALSE);
  _session_test_set($value);
  return t('session saving was disabled, and then %val was set', array('%val' => $value));
}

/**
 * Menu callback: print the current session ID.
 */
function _session_test_id() {
  // Set a value in $_SESSION, so that drupal_session_commit() will start
  // a session.
  $_SESSION['test'] = 'test';

  drupal_session_commit();

  return 'session_id:' . session_id() . "\n";
}

/**
 * Menu callback: print the current session ID as read from the cookie.
 */
function _session_test_id_from_cookie() {
  return 'session_id:' . $_COOKIE[session_name()] . "\n";
}

/**
 * Menu callback, sets a message to me displayed on the following page.
 */
function _session_test_set_message() {
  drupal_set_message(t('This is a dummy message.'));
  print t('A message was set.');
  // Do not return anything, so the current request does not result in a themed
  // page with messages. The message will be displayed in the following request
  // instead.
}

/**
 * Menu callback, sets a message but call drupal_save_session(FALSE).
 */
function _session_test_set_message_but_dont_save() {
  drupal_save_session(FALSE);
  _session_test_set_message();
}

/**
 * Menu callback, stores a value in $_SESSION['session_test_value'] without
 * having started the session in advance.
 */
function _session_test_set_not_started() {
  if (!drupal_session_will_start()) {
    $_SESSION['session_test_value'] = t('Session was not started');
  }
}

/**
 * Implements hook_user().
 */
function session_test_user_login($edit = array(), $user = NULL) {
  if ($user->name == 'session_test_user') {
    // Exit so we can verify that the session was regenerated
    // before hook_user() was called.
    exit;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function session_test_form_user_login_alter(&$form) {
  $form['#https'] = TRUE;
}

/**
 * Implements hook_drupal_goto_alter().
 *
 * Force the redirection to go to a non-secure page after being on a secure
 * page through https.php.
 */
function session_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  global $base_insecure_url, $is_https_mock;
  // Alter the redirect to use HTTP when using a mock HTTPS request through
  // https.php because form submissions would otherwise redirect to a
  // non-existent HTTPS site.
  if (!empty($is_https_mock)) {
    $path = $base_insecure_url . '/' . $path;
  }
}

/**
 * Menu callback, only available if current user is logged in.
 */
function _session_test_is_logged_in() {
  return t('User is logged in.');
}