forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsControllerBaseTestCase.php
More file actions
291 lines (260 loc) · 7.73 KB
/
NetCommonsControllerBaseTestCase.php
File metadata and controls
291 lines (260 loc) · 7.73 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* NetCommonsControllerTestCase
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
CakeLog::drop('stdout');
CakeLog::drop('stderr');
App::uses('TestAuthGeneral', 'AuthGeneral.TestSuite');
App::uses('Current', 'NetCommons.Utility');
App::uses('CurrentSystem', 'NetCommons.Utility');
App::uses('NetCommonsUrl', 'NetCommons.Utility');
App::uses('NetCommonsTestSuite', 'NetCommons.TestSuite');
App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite');
App::uses('Role', 'Roles.Model');
App::uses('SiteSettingUtil', 'SiteManager.Utility');
App::uses('OriginalKeyBehavior', 'NetCommons.Model/Behavior');
App::uses('AuthComponent', 'Controller/Component');
/**
* NetCommonsControllerTestCase
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\TestSuite
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class NetCommonsControllerBaseTestCase extends ControllerTestCase {
/**
* Plugin name
*
* @var string
*/
public $plugin = null;
/**
* Controller name
*
* @var string
*/
protected $_controller;
/**
* Post data
*
* @var array
*/
public $data = null;
/**
* Fixture merge
*
* @var bool
*/
protected $_isFixtureMerged = true;
/**
* Fixtures
*
* @var array
*/
protected $_fixtures = array(
'plugin.blocks.block',
'plugin.blocks.block_role_permission',
'plugin.blocks.block_setting',
'plugin.blocks.blocks_language',
'plugin.boxes.box',
'plugin.boxes.boxes_page_container',
'plugin.categories.category',
'plugin.data_types.data_type',
'plugin.data_types.data_type_choice',
'plugin.files.upload_file',
'plugin.files.upload_files_content',
'plugin.frames.frame',
'plugin.frames.frame_public_language',
'plugin.frames.frames_language',
'plugin.m17n.language',
'plugin.mails.mail_queue',
'plugin.mails.mail_queue_user',
'plugin.mails.mail_setting',
'plugin.pages.pages_language',
'plugin.pages.page',
'plugin.pages.page_container',
'plugin.plugin_manager.plugin',
'plugin.plugin_manager.plugins_role',
'plugin.plugin_manager.plugins_room',
'plugin.roles.default_role_permission',
'plugin.roles.role',
'plugin.rooms.roles_room',
'plugin.rooms.roles_rooms_user',
'plugin.rooms.room',
'plugin.rooms.room_role',
'plugin.rooms.room_role_permission',
'plugin.rooms.rooms_language',
'plugin.rooms.space',
'plugin.site_manager.site_setting',
'plugin.topics.topic',
'plugin.topics.topic_readable',
'plugin.topics.topic_user_status',
'plugin.user_attributes.user_attribute',
'plugin.user_attributes.user_attribute_choice',
'plugin.user_attributes.user_attribute_setting',
'plugin.user_roles.user_attributes_role',
'plugin.user_roles.user_role_setting',
'plugin.users.user',
'plugin.users.users_language',
);
/**
* Fixtures load
*
* @param string $name The name parameter on PHPUnit_Framework_TestCase::__construct()
* @param array $data The date parameter on PHPUnit_Framework_TestCase::__construct()
* @param string $dataName The dataName parameter on PHPUnit_Framework_TestCase::__construct()
* @return void
*/
public function __construct($name = null, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
if ($this->_isFixtureMerged && isset($this->fixtures)) {
$this->fixtures = array_merge($this->_fixtures, $this->fixtures);
}
if ($this->plugin) {
NetCommonsTestSuite::$plugin = $this->plugin;
}
Configure::write('NetCommons.installed', true);
Configure::write('Config.language', 'ja');
}
/**
* setUp method
*
* @return void
*/
public function setUp() {
NetCommonsCakeTestCase::loadTestPlugin($this, 'NetCommons', 'TestPlugin');
$this->_clear();
parent::setUp();
Configure::write('NetCommons.installed', true);
Configure::write('Config.language', 'ja');
Current::$current['Language'] = [
'id' => '2',
'code' => 'ja',
];
AuthComponent::$sessionKey = false;
if ($this->_controller) {
$this->generateNc(Inflector::camelize($this->_controller));
}
}
/**
* 初期化
*
* @return void
*/
protected function _clear() {
Configure::write('NetCommons.installed', false);
Configure::write('Config.language', null);
CakeSession::write('Auth.User', null);
AuthComponent::$sessionKey = 'Auth.User';
Current::$current = array();
Current::$permission = array();
OriginalKeyBehavior::$isUnitRandomKey = false;
SiteSettingUtil::reset();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
}
/**
* Generates a mocked controller and mocks any classes passed to `$mocks`. By
* default, `_stop()` is stubbed as is sending the response headers, so to not
* interfere with testing.
*
* ### Mocks:
*
* - `methods` Methods to mock on the controller. `_stop()` is mocked by default
* - `models` Models to mock. Models are added to the ClassRegistry so any
* time they are instantiated the mock will be created. Pass as key value pairs
* with the value being specific methods on the model to mock. If `true` or
* no value is passed, the entire model will be mocked.
* - `components` Components to mock. Components are only mocked on this controller
* and not within each other (i.e., components on components)
*
* @param string $controller Controller name
* @param array $mocks List of classes and methods to mock
* @return Controller Mocked controller
*/
public function generateNc($controller, $mocks = array()) {
list($plugin, $controller) = pluginSplit($controller);
if (! $plugin) {
$plugin = Inflector::camelize($this->plugin);
}
if (Hash::check($mocks, 'components.Session')) {
$default = array('components' => array(
//'Auth' => array('user'),
'Security',
));
} else {
$default = array('components' => array(
//'Auth' => array('user'),
'Session',
'Security',
));
}
$this->generate($plugin . '.' . $controller, Hash::merge($default, $mocks));
//throwが実行されるとログイン情報が残っているため、初期化する
TestAuthGeneral::logout($this);
}
/**
* privateおよびprotectedメソッドのテスト
*
* @param Instance $instance インスタンス
* @param string $mockMethod Mockのメソッド
* @param array $params Mockのメソッドのパラメータ
* @return void
*/
protected function _testReflectionMethod($instance, $mockMethod, $params = array()) {
$method = new ReflectionMethod($instance, $mockMethod);
$method->setAccessible(true);
$result = $method->invokeArgs($instance, $params);
return $result;
}
/**
* Viewの結果をスペース等のパースを行う
*
* @param string $view Viewの結果
* @return string
*/
protected function _parseView($view) {
$view = preg_replace('/[>][\s\t]+([^a-z])/u', '>$1', $view);
$view = preg_replace('/[\s\t]+</u', '<', $view);
$view = preg_replace('/[\s\t]+/u', ' ', $view);
$view = str_replace("\n", '', $view);
$view = trim($view);
return $view;
}
/**
* Assert Date time
*
* @param string $result Result data
* @param string $message メッセージ
* @return void
*/
public function assertDatetime($result, $message = null) {
$pattern = '/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/';
$this->assertRegExp($pattern, $result, $message);
}
/**
* リダイレクトの評価
*
* @param string $result 期待値
* @param string $message メッセージ
* @return void
*/
public function assertRedirect($result, $message = null) {
$this->assertNull($this->contents, $message);
$header = $this->controller->response->header();
$this->assertTextContains($result, $header['Location'], $message);
}
}