| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* SessionHelperTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.View.Helper
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Controller', 'Controller');
|
|
|
20 |
App::uses('View', 'View');
|
|
|
21 |
App::uses('SessionHelper', 'View/Helper');
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* SessionHelperTest class
|
|
|
25 |
*
|
|
|
26 |
* @package Cake.Test.Case.View.Helper
|
|
|
27 |
*/
|
|
|
28 |
class SessionHelperTest extends CakeTestCase {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* setUp method
|
|
|
32 |
*
|
|
|
33 |
* @return void
|
|
|
34 |
*/
|
|
|
35 |
public function setUp() {
|
|
|
36 |
parent::setUp();
|
|
|
37 |
$controller = null;
|
|
|
38 |
$this->View = new View($controller);
|
|
|
39 |
$this->Session = new SessionHelper($this->View);
|
|
|
40 |
CakeSession::start();
|
|
|
41 |
|
|
|
42 |
if (!CakeSession::started()) {
|
|
|
43 |
CakeSession::start();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
$_SESSION = array(
|
|
|
47 |
'test' => 'info',
|
|
|
48 |
'Message' => array(
|
|
|
49 |
'flash' => array(
|
|
|
50 |
'element' => 'default',
|
|
|
51 |
'params' => array(),
|
|
|
52 |
'message' => 'This is a calling'
|
|
|
53 |
),
|
|
|
54 |
'notification' => array(
|
|
|
55 |
'element' => 'session_helper',
|
|
|
56 |
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
|
|
|
57 |
'message' => 'This is a test of the emergency broadcasting system',
|
|
|
58 |
),
|
|
|
59 |
'classy' => array(
|
|
|
60 |
'element' => 'default',
|
|
|
61 |
'params' => array('class' => 'positive'),
|
|
|
62 |
'message' => 'Recorded'
|
|
|
63 |
),
|
|
|
64 |
'bare' => array(
|
|
|
65 |
'element' => null,
|
|
|
66 |
'message' => 'Bare message',
|
|
|
67 |
'params' => array(),
|
|
|
68 |
),
|
|
|
69 |
),
|
|
|
70 |
'Deeply' => array('nested' => array('key' => 'value')),
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* tearDown method
|
|
|
76 |
*
|
|
|
77 |
* @return void
|
|
|
78 |
*/
|
|
|
79 |
public function tearDown() {
|
|
|
80 |
$_SESSION = array();
|
|
|
81 |
unset($this->View, $this->Session);
|
|
|
82 |
CakePlugin::unload();
|
|
|
83 |
parent::tearDown();
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* testRead method
|
|
|
88 |
*
|
|
|
89 |
* @return void
|
|
|
90 |
*/
|
|
|
91 |
public function testRead() {
|
|
|
92 |
$result = $this->Session->read('Deeply.nested.key');
|
|
|
93 |
$this->assertEquals('value', $result);
|
|
|
94 |
|
|
|
95 |
$result = $this->Session->read('test');
|
|
|
96 |
$this->assertEquals('info', $result);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* testCheck method
|
|
|
101 |
*
|
|
|
102 |
* @return void
|
|
|
103 |
*/
|
|
|
104 |
public function testCheck() {
|
|
|
105 |
$this->assertTrue($this->Session->check('test'));
|
|
|
106 |
|
|
|
107 |
$this->assertTrue($this->Session->check('Message.flash.element'));
|
|
|
108 |
|
|
|
109 |
$this->assertFalse($this->Session->check('Does.not.exist'));
|
|
|
110 |
|
|
|
111 |
$this->assertFalse($this->Session->check('Nope'));
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* testFlash method
|
|
|
116 |
*
|
|
|
117 |
* @return void
|
|
|
118 |
*/
|
|
|
119 |
public function testFlash() {
|
|
|
120 |
$result = $this->Session->flash('flash');
|
|
|
121 |
$expected = '<div id="flashMessage" class="message">This is a calling</div>';
|
|
|
122 |
$this->assertEquals($expected, $result);
|
|
|
123 |
$this->assertFalse($this->Session->check('Message.flash'));
|
|
|
124 |
|
|
|
125 |
$expected = '<div id="classyMessage" class="positive">Recorded</div>';
|
|
|
126 |
$result = $this->Session->flash('classy');
|
|
|
127 |
$this->assertEquals($expected, $result);
|
|
|
128 |
|
|
|
129 |
App::build(array(
|
|
|
130 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
131 |
));
|
|
|
132 |
$result = $this->Session->flash('notification');
|
|
|
133 |
$result = str_replace("\r\n", "\n", $result);
|
|
|
134 |
$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>";
|
|
|
135 |
$this->assertEquals($expected, $result);
|
|
|
136 |
$this->assertFalse($this->Session->check('Message.notification'));
|
|
|
137 |
|
|
|
138 |
$result = $this->Session->flash('bare');
|
|
|
139 |
$expected = 'Bare message';
|
|
|
140 |
$this->assertEquals($expected, $result);
|
|
|
141 |
$this->assertFalse($this->Session->check('Message.bare'));
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* test flash() with the attributes.
|
|
|
146 |
*
|
|
|
147 |
* @return void
|
|
|
148 |
*/
|
|
|
149 |
public function testFlashAttributes() {
|
|
|
150 |
$result = $this->Session->flash('flash', array('params' => array('class' => 'test-message')));
|
|
|
151 |
$expected = '<div id="flashMessage" class="test-message">This is a calling</div>';
|
|
|
152 |
$this->assertEquals($expected, $result);
|
|
|
153 |
$this->assertFalse($this->Session->check('Message.flash'));
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* test setting the element from the attrs.
|
|
|
158 |
*
|
|
|
159 |
* @return void
|
|
|
160 |
*/
|
|
|
161 |
public function testFlashElementInAttrs() {
|
|
|
162 |
App::build(array(
|
|
|
163 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
164 |
));
|
|
|
165 |
$result = $this->Session->flash('flash', array(
|
|
|
166 |
'element' => 'session_helper',
|
|
|
167 |
'params' => array('title' => 'Notice!', 'name' => 'Alert!')
|
|
|
168 |
));
|
|
|
169 |
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
|
|
|
170 |
$this->assertTextEquals($expected, $result);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* test using elements in plugins.
|
|
|
175 |
*
|
|
|
176 |
* @return void
|
|
|
177 |
*/
|
|
|
178 |
public function testFlashWithPluginElement() {
|
|
|
179 |
App::build(array(
|
|
|
180 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
|
|
181 |
));
|
|
|
182 |
CakePlugin::load('TestPlugin');
|
|
|
183 |
|
|
|
184 |
$result = $this->Session->flash('flash', array(
|
|
|
185 |
'element' => 'plugin_element',
|
|
|
186 |
'params' => array('plugin' => 'TestPlugin')
|
|
|
187 |
));
|
|
|
188 |
$expected = 'this is the plugin element using params[plugin]';
|
|
|
189 |
$this->assertEquals($expected, $result);
|
|
|
190 |
}
|
|
|
191 |
}
|