-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsCurrentLibTestUtility.php
More file actions
589 lines (537 loc) · 15.3 KB
/
NetCommonsCurrentLibTestUtility.php
File metadata and controls
589 lines (537 loc) · 15.3 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
/**
* NetCommonsCakeTestCase
*
* @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
*/
App::uses('AuthComponent', 'Controller/Component');
App::uses('PageLayoutComponent', 'Pages.Controller/Component');
App::uses('GetPageBehavior', 'Pages.Model/Behavior');
App::uses('CurrentLib', 'NetCommons.Lib');
App::uses('Current', 'NetCommons.Utility');
App::uses('SettingMode', 'NetCommons.Lib');
App::uses('File', 'Utility');
App::uses('Folder', 'Utility');
if (!defined('UPLOADS_ROOT')) {
define('UPLOADS_ROOT', NetCommonsCurrentLibTestUtility::getUploadDir());
}
/**
* Fixture test_schema.sqlを読み込むんでテストするためのユーティリティ
*
* Security:
* salt: >
* 2a95829d8ad63ca6cb49baaa52e772270dc174a79e090ee0851505c65b21d3ac9d14287e39a2f30fc4606ff1c9b45c7376b02a0b5a5ef0af82cc9f4d344107ec
* cipherSeed:
* "142354242720272028881904983643200749000"
*
* @package NetCommons\NetCommons\TestSuite
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class NetCommonsCurrentLibTestUtility {
/**
* drop tableの構文文字列
*
* @var string
*/
private static $__dropTablesSql = '';
/**
* ロードしているか否か
*
* @var string
*/
private static $__loadedTables = false;
/**
* drop tableの構文文字列
*
* @var string
*/
private static $__users = [
'1' => [
'id' => '1',
'username' => 'admin',
'password' => 'bf52eec7654a236513801b3d19c0ab557c36f3f29c',
'role_key' => 'system_administrator',
'handlename' => 'System administrator',
'modified' => '2019-03-02 00:00:00',
'UserRoleSetting' => [
'id' => '1',
'role_key' => 'system_administrator',
'origin_role_key' => 'system_administrator',
'use_private_room' => '1',
],
],
'2' => [
'id' => '2',
'username' => 'general_user_1',
'password' => 'bf52eec7654a236513801b3d19c0ab557c36f3f29c',
'role_key' => 'common_user',
'handlename' => 'General user 1',
'modified' => '2019-03-02 00:00:00',
'UserRoleSetting' => [
'id' => '1',
'role_key' => 'common_user',
'origin_role_key' => 'common_user',
'use_private_room' => '1',
],
],
'3' => [
'id' => '3',
'username' => 'general_user_2',
'password' => 'bf52eec7654a236513801b3d19c0ab557c36f3f29c',
'role_key' => 'common_user',
'handlename' => 'General user 2',
'modified' => '2019-03-02 00:00:00',
'UserRoleSetting' => [
'id' => '1',
'role_key' => 'common_user',
'origin_role_key' => 'common_user',
'use_private_room' => '1',
],
],
'4' => [
'id' => '4',
'username' => 'general_user_3',
'password' => 'bf52eec7654a236513801b3d19c0ab557c36f3f29c',
'role_key' => 'common_user',
'handlename' => 'General user 3',
'modified' => '2019-03-02 00:00:00',
'UserRoleSetting' => [
'id' => '1',
'role_key' => 'common_user',
'origin_role_key' => 'common_user',
'use_private_room' => '1',
],
],
'5' => [
'id' => '5',
'username' => 'guest_user_1',
'password' => 'bf52eec7654a236513801b3d19c0ab557c36f3f29c',
'role_key' => 'guest_user',
'handlename' => 'Guest user 1',
'modified' => '2019-03-02 00:00:00',
'UserRoleSetting' => [
'id' => '1',
'role_key' => 'guest_user',
'origin_role_key' => 'common_user',
'use_private_room' => '1',
],
],
];
/**
* テスト名をdebugに出力
*
* @param string $loginTitle ログインタイトル(ログインなし、管理者でログインなどをセットする)
* @param string $method テストメソッド
* @return void
*/
public static function debugLogTestName($loginTitle, $method) {
$pos = strpos($method, 'with');
if ($pos !== false) {
$testName = mb_substr($method, 0, $pos - 1);
$testSubTitle = mb_substr($method, $pos - 1);
} else {
$testName = $method;
$testSubTitle = '';
}
switch ($testName) {
case 'testGetRequest';
$testName = 'GETリクエスト';
break;
case 'testGetRequestAnnouncementPageWithSettingMode':
$testName = 'セッティングモードON(パブリックのお知らせページ)';
break;
case 'testPostRequest';
$testName = 'POSTリクエスト';
break;
case 'testPostRequestFrameAdd';
$testName = 'フレーム追加';
break;
case 'testPostRequestFrameEdit';
$testName = 'フレーム編集';
break;
case 'testPostRequestFrameDelete';
$testName = 'フレーム削除';
break;
}
//ログ出力
CakeLog::debug('');
CakeLog::debug('');
CakeLog::debug('[' . $loginTitle . '] ' . $testName . $testSubTitle);
CakeLog::debug('');
}
/**
* テーブルのロード
*
* @return void
*/
public static function loadTables() {
if (! self::canLoadTables()) {
return;
}
if (! self::$__loadedTables) {
$db = ConnectionManager::getDataSource('test');
$schemaFile = self::getSchemaFile();
$sql = file_get_contents($schemaFile);
$db->rawQuery($sql);
self::$__loadedTables = true;
}
Configure::write('App.base', '');
Router::reload();
}
/**
* テーブルのロードしているか否か
*
* @return void
*/
public static function loadedTables() {
return self::$__loadedTables;
}
/**
* テーブルをロードできるか否か
*
* @return bool
*/
public static function canLoadTables() {
return (bool)getenv('NC3_LOCAL_TEST');
}
/**
* Fixtureのパスを返す
*
* @return string
*/
public static function getFixturePath() {
$path = CakePlugin::path('NetCommons') . 'Test' . DS . 'Fixture' . DS;
return $path;
}
/**
* スキーマファイルのフルパスを返す
*
* @return string
*/
public static function getSchemaFile() {
$schemaFile = self::getFixturePath() . 'CurrentLib' . DS . 'test_schema_current_lib.sql';
return $schemaFile;
}
/**
* tmpディレクトリのパスを返す
*
* @return string
*/
public static function getTmpDir() {
$path = self::getFixturePath() . 'CurrentLib' . DS . 'tmp' . DS;
return $path;
}
/**
* アップロードディレクトリのパスを返す
*
* @return string
*/
public static function getUploadDir() {
$path = self::getFixturePath() . 'CurrentLib' . DS . 'TestUploads' . DS;
return $path;
}
/**
* アップロードディレクトリのパスをセットする
*
* @return string
*/
public static function prepareUploadDir() {
//@var Folder
$Folder = new Folder();
if (! self::clearUploadDir()) {
return false;
}
$options = [
'from' => self::getFixturePath() . 'CurrentLib' . DS . 'Uploads',
'to' => self::getUploadDir(),
];
if (! $Folder->copy($options)) {
return false;
}
return true;
}
/**
* アップロードディレクトリのパスをセットする
*
* @return string
*/
public static function clearUploadDir() {
if (file_exists(self::getUploadDir() . 'files')) {
//@var Folder
$Folder = new Folder();
return $Folder->delete(self::getUploadDir() . 'files');
}
return true;
}
/**
* TemporaryFileのモックとしてFileを使って戻す
*
* @param array $fileInfo アップロードファイル情報
* @return string
*/
public static function getTemporaryFileMock($fileInfo) {
$extension = pathinfo(
$fileInfo['name'],
PATHINFO_EXTENSION
);
$destFileName = Security::hash(mt_rand() . microtime(), 'md5') . '.' . $extension;
$TmpFile = new File($fileInfo['tmp_name']);
$TmpFile->copy(self::getTmpDir() . $destFileName);
unset($TmpFile);
$File = new File(self::getTmpDir() . $destFileName);
$File->temporaryFolder = new Folder(self::getTmpDir());
$File->originalName = $fileInfo['name'];
$File->error = $fileInfo['error'];
return $File;
}
/**
* テーブルの削除
*
* @return void
*/
public static function dropTables() {
if (! self::$__dropTablesSql) {
$db = ConnectionManager::getDataSource('test');
$tables = $db->listSources();
foreach ($tables as $table) {
self::$__dropTablesSql .= 'DROP TABLE IF EXISTS `' . $table . '`;' . "\n";
}
}
if (self::$__dropTablesSql) {
$db = ConnectionManager::getDataSource('test');
//$db->rawQuery(self::$__dropTablesSql);
self::$__loadedTables = false;
}
}
/**
* ログイン
*
* @param string|int $userId ユーザID
* @return void
*/
public static function login($userId) {
$reflectionClass = new ReflectionClass('AuthComponent');
$property = $reflectionClass->getProperty('_user');
$property->setAccessible(true);
$property->setValue(self::$__users[$userId]);
}
/**
* ログアウト
*
* @return void
*/
public static function logout() {
$reflectionClass = new ReflectionClass('AuthComponent');
$property = $reflectionClass->getProperty('_user');
$property->setAccessible(true);
$property->setValue([]);
}
/**
* セッティングモードの変更
*
* @param bool $setting セッティングモード
* @return void
*/
public static function settingMode($setting) {
//if (get_class(new Current()) === 'CurrentLib') {
$reflectionClass = new ReflectionClass('SettingMode');
$property = $reflectionClass->getProperty('__isSettingMode');
$property->setAccessible(true);
$property->setValue($setting);
//} else {
// $reflectionClass = new ReflectionClass('Current');
// $property = $reflectionClass->getProperty('_isSettingMode');
// $property->setAccessible(true);
// $property->setValue($setting);
//}
}
/**
* 旧Currentのリセット
*
* @return void
*/
//private static function __resetOldCurrentUtility() {
// Current::$current = [];
// Current::$originalCurrent = [];
// Current::$permission = [];
//
// $class = new ReflectionClass('Current');
// foreach (['_instance', '_instanceSystem', '_instanceFrame', '_instancePage'] as $prop) {
// $Property = $class->getProperty($prop);
// $Property->setAccessible(true);
// $Property->setValue(null);
// }
//
// $class = new ReflectionClass('CurrentFrame');
// $Property = $class->getProperty('__memoryCache');
// $Property->setAccessible(true);
// $Property->setValue([]);
//
// $class = new ReflectionClass('CurrentFrame');
// $Property = $class->getProperty('__roomIds');
// $Property->setAccessible(true);
// $Property->setValue([]);
//
// $class = new ReflectionClass('CurrentPage');
// $Property = $class->getProperty('__memoryCache');
// $Property->setAccessible(true);
// $Property->setValue([]);
//
// $class = new ReflectionClass('CurrentSystem');
// $Property = $class->getProperty('__memoryCache');
// $Property->setAccessible(true);
// $Property->setValue(null);
//
// $class = new ReflectionClass('GetPageBehavior');
// $Property = $class->getProperty('__memoryPageWithFrame');
// $Property->setAccessible(true);
// $Property->setValue([]);
//}
/**
* Currentライブラリのリセット
*
* @return void
*/
public static function resetCurrentLib() {
//if (get_class(new Current()) === 'CurrentLib') {
CurrentLib::resetInstance();
//} else {
// self::__resetOldCurrentUtility();
//}
$class = new ReflectionClass('PageLayoutComponent');
$Property = $class->getProperty('_page');
$Property->setAccessible(true);
$Property->setValue(null);
}
/**
* コントローラのGETテスト
*
* @param ControllerTestCase $test コントローラテストクラス
* @param string $url テストするURL
* @param array|false $expects 期待値リスト
* @param string|false $exception Exception文字列
* @param string|false $outputDebugTitle 出力するdebugのタイトル
* @return void
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public static function testControllerGetRequest(
ControllerTestCase $test, $url, $expects, $exception, $outputDebugTitle = false) {
if ($expects === false) {
$test->setExpectedException($exception);
}
if ($outputDebugTitle) {
$key = md5(json_encode($url) . json_encode($expects) . json_encode($exception));
CakeLog::debug("=========================================");
}
$test->testAction($url, ['method' => 'GET', 'return' => 'view']);
if ($outputDebugTitle) {
CakeLog::debug('##### ' . var_export($key, true));
CakeLog::debug(__METHOD__ . '(' . __LINE__ . ') beforeFilter - afterFilter = Total');
CakeLog::debug(var_export(($test->controller->endTime - $test->controller->startTime), true));
CakeLog::debug("--------");
CakeLog::debug("");
CakeLog::debug("");
CakeLog::debug("");
CakeLog::debug("");
}
//debug($test->contents);
//debug($test->view);
//debug($test->headers);
if ($expects !== false) {
self::__assertController($test, $expects);
//self::dropTables();
}
}
/**
* コントローラのPOSTテスト
*
* Mockにせずに登録処理を実行するが、saveした結果まではチェックしない。
*
* @param ControllerTestCase $test コントローラテストクラス
* @param string $url テストするURL
* @param array $post POSTの内容
* @param array|false $expects 期待値リスト
* @param string|false $exception Exception文字列
* @return void
*/
public static function testControllerPostRequest(
ControllerTestCase $test, $url, $post, $expects, $exception) {
if ($expects === false) {
$test->setExpectedException($exception);
}
$test->testAction(
$url,
['method' => 'POST', 'return' => 'view', 'data' => $post]
);
//debug($test->contents);
//debug($test->view);
//debug($test->headers);
//debug($test->controller->validationErrors);
if ($expects !== false) {
self::__assertController($test, $expects);
self::dropTables();
}
}
/**
* コントローラのPOSTテスト
*
* Mockにせずに登録処理を実行するが、saveした結果まではチェックしない。
*
* @param ControllerTestCase $test コントローラテストクラス
* @param string $url テストするURL
* @param array $post POSTの内容
* @param array|false $expects 期待値リスト
* @param string|false $exception Exception文字列
* @return void
*/
public static function testJsonControllerPostRequest(
ControllerTestCase $test, $url, $post, $expects, $exception) {
if ($expects === false) {
$test->setExpectedException($exception);
}
$test->testAction(
$url,
['method' => 'POST', 'return' => 'view', 'type' => 'json', 'data' => $post]
);
//debug($test->contents);
//debug($test->view);
//debug($test->headers);
//debug($test->controller->validationErrors);
if ($expects !== false) {
$contents = json_decode($test->contents, true);
foreach ($expects as $key => $value) {
$test->assertEquals($value, Hash::get($contents, $key));
}
self::dropTables();
}
}
/**
* コントローラの検証
*
* @param ControllerTestCase $test コントローラテストクラス
* @param array|false $expects 期待値リスト
* @return void
*/
private static function __assertController(ControllerTestCase $test, $expects) {
$test->contents = str_replace("\n", '', $test->contents);
$test->contents = str_replace("\t", '', $test->contents);
foreach ($expects as $assert => $expect) {
if (strpos($assert, 'headers') !== false) {
list(, $key) = explode('.', $assert);
$test->assertRegExp($expect, $test->headers[$key]);
} elseif ($assert === 'validationErrors') {
$test->assertEquals($expect, $test->controller->validationErrors);
} else {
foreach ($expect as $ex) {
$test->$assert($ex, $test->contents);
}
}
}
}
}