Projet

Général

Profil

Paste
Télécharger (5,45 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / session_test.module @ 76597ebf

1
<?php
2

    
3
/**
4
 * Implements hook_menu().
5
 */
6
function session_test_menu() {
7
  $items['session-test/get'] = array(
8
    'title' => 'Session value',
9
    'page callback' => '_session_test_get',
10
    'access arguments' => array('access content'),
11
    'type' => MENU_CALLBACK,
12
  );
13
  $items['session-test/id'] = array(
14
    'title' => 'Session ID',
15
    'page callback' => '_session_test_id',
16
    'access arguments' => array('access content'),
17
    'type' => MENU_CALLBACK,
18
  );
19
  $items['session-test/id-from-cookie'] = array(
20
    'title' => 'Session ID from cookie',
21
    'page callback' => '_session_test_id_from_cookie',
22
    'access arguments' => array('access content'),
23
    'type' => MENU_CALLBACK,
24
  );
25
  $items['session-test/set/%'] = array(
26
    'title' => 'Set session value',
27
    'page callback' => '_session_test_set',
28
    'page arguments' => array(2),
29
    'access arguments' => array('access content'),
30
    'type' => MENU_CALLBACK,
31
  );
32
  $items['session-test/no-set/%'] = array(
33
    'title' => 'Set session value but do not save session',
34
    'page callback' => '_session_test_no_set',
35
    'page arguments' => array(2),
36
    'access arguments' => array('access content'),
37
    'type' => MENU_CALLBACK,
38
  );
39
  $items['session-test/set-message'] = array(
40
    'title' => 'Set message',
41
    'page callback' => '_session_test_set_message',
42
    'access arguments' => array('access content'),
43
    'type' => MENU_CALLBACK,
44
  );
45
  $items['session-test/set-message-but-dont-save'] = array(
46
    'title' => 'Set message but do not save session',
47
    'page callback' => '_session_test_set_message_but_dont_save',
48
    'access arguments' => array('access content'),
49
    'type' => MENU_CALLBACK,
50
  );
51
  $items['session-test/set-not-started'] = array(
52
    'title' => 'Set message when session is not started',
53
    'page callback' => '_session_test_set_not_started',
54
    'access arguments' => array('access content'),
55
    'type' => MENU_CALLBACK,
56
  );
57
  $items['session-test/is-logged-in'] = array(
58
    'title' => 'Check if user is logged in',
59
    'page callback' => '_session_test_is_logged_in',
60
    'access callback' => 'user_is_logged_in',
61
    'type' => MENU_CALLBACK,
62
  );
63

    
64
  return $items;
65
}
66

    
67
/**
68
 * Implements hook_boot().
69
 */
70
function session_test_boot() {
71
  header('X-Session-Empty: ' . intval(empty($_SESSION)));
72
}
73

    
74
/**
75
 * Page callback, prints the stored session value to the screen.
76
 */
77
function _session_test_get() {
78
  if (!empty($_SESSION['session_test_value'])) {
79
    return t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value']));
80
  }
81
  else {
82
    return "";
83
  }
84
}
85

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

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

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

    
112
  drupal_session_commit();
113

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

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

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

    
135
/**
136
 * Menu callback, sets a message but call drupal_save_session(FALSE).
137
 */
138
function _session_test_set_message_but_dont_save() {
139
  drupal_save_session(FALSE);
140
  _session_test_set_message();
141
}
142

    
143
/**
144
 * Menu callback, stores a value in $_SESSION['session_test_value'] without
145
 * having started the session in advance.
146
 */
147
function _session_test_set_not_started() {
148
  if (!drupal_session_will_start()) {
149
    $_SESSION['session_test_value'] = t('Session was not started');
150
  }
151
}
152

    
153
/**
154
 * Implements hook_user().
155
 */
156
function session_test_user_login($edit = array(), $user = NULL) {
157
  if ($user->name == 'session_test_user') {
158
    // Exit so we can verify that the session was regenerated
159
    // before hook_user() was called.
160
    exit;
161
  }
162
}
163

    
164
/**
165
 * Implements hook_form_FORM_ID_alter().
166
 */
167
function session_test_form_user_login_alter(&$form) {
168
  $form['#https'] = TRUE;
169
}
170

    
171
/**
172
 * Implements hook_drupal_goto_alter().
173
 *
174
 * Force the redirection to go to a non-secure page after being on a secure
175
 * page through https.php.
176
 */
177
function session_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
178
  global $base_insecure_url, $is_https_mock;
179
  // Alter the redirect to use HTTP when using a mock HTTPS request through
180
  // https.php because form submissions would otherwise redirect to a
181
  // non-existent HTTPS site.
182
  if (!empty($is_https_mock)) {
183
    $path = $base_insecure_url . '/' . $path;
184
  }
185
}
186

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