comparison cake/tests/cases/libs/view/theme.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * ThemeViewTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', array('Theme', 'Controller'));
21
22 if (!class_exists('ErrorHandler')) {
23 App::import('Core', array('Error'));
24 }
25 if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
26 define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
27 }
28
29 /**
30 * ThemePostsController class
31 *
32 * @package cake
33 * @subpackage cake.tests.cases.libs.view
34 */
35 class ThemePostsController extends Controller {
36
37 /**
38 * name property
39 *
40 * @var string 'ThemePosts'
41 * @access public
42 */
43 var $name = 'ThemePosts';
44
45 /**
46 * index method
47 *
48 * @access public
49 * @return void
50 */
51 function index() {
52 $this->set('testData', 'Some test data');
53 $test2 = 'more data';
54 $test3 = 'even more data';
55 $this->set(compact('test2', 'test3'));
56 }
57 }
58
59 /**
60 * ThemeViewTestErrorHandler class
61 *
62 * @package cake
63 * @subpackage cake.tests.cases.libs.view
64 */
65 class ThemeViewTestErrorHandler extends ErrorHandler {
66
67 /**
68 * stop method
69 *
70 * @access public
71 * @return void
72 */
73 function _stop() {
74 return;
75 }
76 }
77
78 /**
79 * TestThemeView class
80 *
81 * @package cake
82 * @subpackage cake.tests.cases.libs.view
83 */
84 class TestThemeView extends ThemeView {
85
86 /**
87 * renderElement method
88 *
89 * @param mixed $name
90 * @param array $params
91 * @access public
92 * @return void
93 */
94 function renderElement($name, $params = array()) {
95 return $name;
96 }
97
98 /**
99 * getViewFileName method
100 *
101 * @param mixed $name
102 * @access public
103 * @return void
104 */
105 function getViewFileName($name = null) {
106 return $this->_getViewFileName($name);
107 }
108
109 /**
110 * getLayoutFileName method
111 *
112 * @param mixed $name
113 * @access public
114 * @return void
115 */
116 function getLayoutFileName($name = null) {
117 return $this->_getLayoutFileName($name);
118 }
119
120 /**
121 * cakeError method
122 *
123 * @param mixed $method
124 * @param mixed $messages
125 * @access public
126 * @return void
127 */
128 function cakeError($method, $messages) {
129 $error =& new ThemeViewTestErrorHandler($method, $messages);
130 return $error;
131 }
132 }
133
134 /**
135 * ThemeViewTest class
136 *
137 * @package cake
138 * @subpackage cake.tests.cases.libs
139 */
140 class ThemeViewTest extends CakeTestCase {
141
142 /**
143 * setUp method
144 *
145 * @access public
146 * @return void
147 */
148 function setUp() {
149 Router::reload();
150 $this->Controller =& new Controller();
151 $this->PostsController =& new ThemePostsController();
152 $this->PostsController->viewPath = 'posts';
153 $this->PostsController->index();
154 $this->ThemeView =& new ThemeView($this->PostsController);
155 }
156
157 /**
158 * tearDown method
159 *
160 * @access public
161 * @return void
162 */
163 function tearDown() {
164 unset($this->ThemeView);
165 unset($this->PostsController);
166 unset($this->Controller);
167 ClassRegistry::flush();
168 }
169 /**
170 * test that the theme view can be constructed without going into the registry
171 *
172 * @return void
173 */
174 function testConstructionNoRegister() {
175 ClassRegistry::flush();
176 $controller = null;
177 $Theme =& new ThemeView($controller, false);
178 $ThemeTwo =& ClassRegistry::getObject('view');
179 $this->assertFalse($ThemeTwo);
180 }
181
182 /**
183 * startTest
184 *
185 * @access public
186 * @return void
187 */
188 function startTest() {
189 App::build(array(
190 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
191 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
192 ));
193 }
194
195 /**
196 * endTest
197 *
198 * @access public
199 * @return void
200 */
201 function endTest() {
202 App::build();
203 }
204
205 /**
206 * testPluginGetTemplate method
207 *
208 * @access public
209 * @return void
210 */
211 function testPluginThemedGetTemplate() {
212 $this->Controller->plugin = 'test_plugin';
213 $this->Controller->name = 'TestPlugin';
214 $this->Controller->viewPath = 'tests';
215 $this->Controller->action = 'index';
216 $this->Controller->theme = 'test_theme';
217
218 $ThemeView =& new TestThemeView($this->Controller);
219 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'tests' . DS .'index.ctp';
220 $result = $ThemeView->getViewFileName('index');
221 $this->assertEqual($result, $expected);
222
223 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'layouts' . DS .'plugin_default.ctp';
224 $result = $ThemeView->getLayoutFileName('plugin_default');
225 $this->assertEqual($result, $expected);
226
227 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
228 $result = $ThemeView->getLayoutFileName('default');
229 $this->assertEqual($result, $expected);
230 }
231
232 /**
233 * testGetTemplate method
234 *
235 * @access public
236 * @return void
237 */
238 function testGetTemplate() {
239 $this->Controller->plugin = null;
240 $this->Controller->name = 'Pages';
241 $this->Controller->viewPath = 'pages';
242 $this->Controller->action = 'display';
243 $this->Controller->params['pass'] = array('home');
244
245 $ThemeView =& new TestThemeView($this->Controller);
246 $ThemeView->theme = 'test_theme';
247 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp';
248 $result = $ThemeView->getViewFileName('home');
249 $this->assertEqual($result, $expected);
250
251 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'posts' . DS .'index.ctp';
252 $result = $ThemeView->getViewFileName('/posts/index');
253 $this->assertEqual($result, $expected);
254
255 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
256 $result = $ThemeView->getLayoutFileName();
257 $this->assertEqual($result, $expected);
258
259 $ThemeView->layoutPath = 'rss';
260 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp';
261 $result = $ThemeView->getLayoutFileName();
262 $this->assertEqual($result, $expected);
263
264 $ThemeView->layoutPath = 'email' . DS . 'html';
265 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp';
266 $result = $ThemeView->getLayoutFileName();
267 $this->assertEqual($result, $expected);
268 }
269
270 /**
271 * testMissingView method
272 *
273 * @access public
274 * @return void
275 */
276 function testMissingView() {
277 $this->Controller->plugin = null;
278 $this->Controller->name = 'Pages';
279 $this->Controller->viewPath = 'pages';
280 $this->Controller->action = 'display';
281 $this->Controller->theme = 'my_theme';
282
283 $this->Controller->params['pass'] = array('home');
284
285 restore_error_handler();
286 $View =& new TestThemeView($this->Controller);
287 ob_start();
288 $result = $View->getViewFileName('does_not_exist');
289 $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
290 set_error_handler('simpleTestErrorHandler');
291 $this->assertPattern("/PagesController::/", $expected);
292 $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
293 }
294
295 /**
296 * testMissingLayout method
297 *
298 * @access public
299 * @return void
300 */
301 function testMissingLayout() {
302 $this->Controller->plugin = null;
303 $this->Controller->name = 'Posts';
304 $this->Controller->viewPath = 'posts';
305 $this->Controller->layout = 'whatever';
306 $this->Controller->theme = 'my_theme';
307
308 restore_error_handler();
309 $View =& new TestThemeView($this->Controller);
310 ob_start();
311 $result = $View->getLayoutFileName();
312 $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
313 set_error_handler('simpleTestErrorHandler');
314 $this->assertPattern("/Missing Layout/", $expected);
315 $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
316 }
317
318 /**
319 * test memory leaks that existed in _paths at one point.
320 *
321 * @return void
322 */
323 function testMemoryLeakInPaths() {
324 if ($this->skipIf(!function_exists('memory_get_usage'), 'No memory measurement function, cannot test for possible memory leak. %s')) {
325 return;
326 }
327 $this->Controller->plugin = null;
328 $this->Controller->name = 'Posts';
329 $this->Controller->viewPath = 'posts';
330 $this->Controller->layout = 'whatever';
331 $this->Controller->theme = 'test_theme';
332
333 $View =& new ThemeView($this->Controller);
334 $View->element('test_element');
335
336 $start = memory_get_usage();
337 for ($i = 0; $i < 10; $i++) {
338 $View->element('test_element');
339 }
340 $end = memory_get_usage();
341 $this->assertWithinMargin($start, $end, 3500);
342 }
343 }