/
SessionHelperTest.php
192 lines (172 loc) · 5.1 KB
/
SessionHelperTest.php
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
<?php
/**
* SessionHelperTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('SessionHelper', 'View/Helper');
/**
* SessionHelperTest class
*
* @package Cake.Test.Case.View.Helper
*/
class SessionHelperTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$controller = null;
$this->View = new View($controller);
$this->Session = new SessionHelper($this->View);
CakeSession::start();
if (!CakeSession::started()) {
CakeSession::start();
}
$_SESSION = array(
'test' => 'info',
'Message' => array(
'flash' => array(
'element' => 'default',
'params' => array(),
'message' => 'This is a calling'
),
'notification' => array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
),
'classy' => array(
'element' => 'default',
'params' => array('class' => 'positive'),
'message' => 'Recorded'
),
'bare' => array(
'element' => null,
'message' => 'Bare message',
'params' => array(),
),
),
'Deeply' => array('nested' => array('key' => 'value')),
);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
$_SESSION = array();
unset($this->View, $this->Session);
CakePlugin::unload();
parent::tearDown();
}
/**
* testRead method
*
* @return void
*/
public function testRead() {
$result = $this->Session->read('Deeply.nested.key');
$this->assertEquals($result, 'value');
$result = $this->Session->read('test');
$this->assertEquals($result, 'info');
}
/**
* testCheck method
*
* @return void
*/
public function testCheck() {
$this->assertTrue($this->Session->check('test'));
$this->assertTrue($this->Session->check('Message.flash.element'));
$this->assertFalse($this->Session->check('Does.not.exist'));
$this->assertFalse($this->Session->check('Nope'));
}
/**
* testFlash method
*
* @return void
*/
public function testFlash() {
$result = $this->Session->flash('flash');
$expected = '<div id="flashMessage" class="message">This is a calling</div>';
$this->assertEquals($expected, $result);
$this->assertFalse($this->Session->check('Message.flash'));
$expected = '<div id="classyMessage" class="positive">Recorded</div>';
$result = $this->Session->flash('classy');
$this->assertEquals($expected, $result);
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
));
$result = $this->Session->flash('notification');
$result = str_replace("\r\n", "\n", $result);
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a test of the emergency broadcasting system</p>\n</div>";
$this->assertEquals($expected, $result);
$this->assertFalse($this->Session->check('Message.notification'));
$result = $this->Session->flash('bare');
$expected = 'Bare message';
$this->assertEquals($expected, $result);
$this->assertFalse($this->Session->check('Message.bare'));
}
/**
* test flash() with the attributes.
*
* @return void
*/
public function testFlashAttributes() {
$result = $this->Session->flash('flash', array('params' => array('class' => 'test-message')));
$expected = '<div id="flashMessage" class="test-message">This is a calling</div>';
$this->assertEquals($expected, $result);
$this->assertFalse($this->Session->check('Message.flash'));
}
/**
* test setting the element from the attrs.
*
* @return void
*/
public function testFlashElementInAttrs() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
));
$result = $this->Session->flash('flash', array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!')
));
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
$this->assertEquals($expected, $result);
}
/**
* test using eleents in plugins.
*
* @return void
*/
public function testFlashWithPluginElement() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin'. DS)
));
CakePlugin::load('TestPlugin');
$result = $this->Session->flash('flash', array(
'element' => 'plugin_element',
'params' => array('plugin' => 'TestPlugin')
));
$expected = 'this is the plugin element using params[plugin]';
$this->assertEquals($expected, $result);
}
}