|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Test; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Core\Configure; |
|
15
|
|
|
use Cake\Network\Request; |
|
16
|
|
|
use Cake\Network\Response; |
|
17
|
|
|
use Cake\Utility\Hash; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ConfigTrait |
|
21
|
|
|
* |
|
22
|
|
|
* @package CakeDC\Api\Test |
|
23
|
|
|
*/ |
|
24
|
|
|
trait ConfigTrait |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Configure public auth access |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function _publicAccess() |
|
31
|
|
|
{ |
|
32
|
|
|
$config = Configure::read('Test.Api'); |
|
33
|
|
|
$config['Auth'] = [ |
|
34
|
|
|
'allow' => '*' |
|
35
|
|
|
]; |
|
36
|
|
|
Configure::write('Test.Api', $config); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Configure token auth access |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function _authAccess() |
|
43
|
|
|
{ |
|
44
|
|
|
$config = (array)Configure::read('Test.Api'); |
|
45
|
|
|
$auth = [ |
|
46
|
|
|
'authorize' => [ |
|
47
|
|
|
'CakeDC/Api.Crud' => [] |
|
48
|
|
|
], |
|
49
|
|
|
'authenticate' => [ |
|
50
|
|
|
'all' => [ |
|
51
|
|
|
'finder' => 'active', |
|
52
|
|
|
], |
|
53
|
|
|
'CakeDC/Api.Form' => [ |
|
54
|
|
|
'userModel' => 'CakeDC/Users.Users' |
|
55
|
|
|
] |
|
56
|
|
|
], |
|
57
|
|
|
]; |
|
58
|
|
|
$path = 'Service.default.Action.default.Auth'; |
|
59
|
|
|
$config = Hash::insert($config, $path, $auth); |
|
60
|
|
|
Configure::write('Test.Api', $config); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Configure token auth access |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function _tokenAccess() |
|
67
|
|
|
{ |
|
68
|
|
|
$config = (array)Configure::read('Test.Api'); |
|
69
|
|
|
$config['Auth'] = [ |
|
70
|
|
|
'Crud' => [ |
|
71
|
|
|
'default' => 'allow' |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$auth = [ |
|
76
|
|
|
'authorize' => [ |
|
77
|
|
|
'CakeDC/Api.Crud' => [] |
|
78
|
|
|
], |
|
79
|
|
|
'authenticate' => [ |
|
80
|
|
|
'all' => [ |
|
81
|
|
|
'finder' => 'auth', |
|
82
|
|
|
], |
|
83
|
|
|
'CakeDC/Api.Token' => [ |
|
84
|
|
|
'require_ssl' => false, |
|
85
|
|
|
'table' => 'CakeDC/Users.Users', |
|
86
|
|
|
] |
|
87
|
|
|
], |
|
88
|
|
|
]; |
|
89
|
|
|
$path = 'Service.default.Action.default.Auth'; |
|
90
|
|
|
$config = Hash::insert($config, $path, $auth); |
|
91
|
|
|
Configure::write('Test.Api', $config); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Insert api options into specific path. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path Setting path. |
|
98
|
|
|
* @param mixed $options An options. |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function _addSettingByPath($path, $options) |
|
102
|
|
|
{ |
|
103
|
|
|
$config = (array)Configure::read('Test.Api'); |
|
104
|
|
|
$config = Hash::insert($config, $path, $options); |
|
105
|
|
|
Configure::write('Test.Api', $config); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Add default extensions into configuration. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array|string $extension |
|
112
|
|
|
* @param bool $overwrite Owerwrite flag. |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function _loadDefaultExtensions($extension, $overwrite = false) |
|
115
|
|
|
{ |
|
116
|
|
|
$config = (array)Configure::read('Test.Api'); |
|
117
|
|
|
$path = 'Service.default.Action.default.Extension'; |
|
118
|
|
|
$default = (array)Hash::get($config, $path); |
|
119
|
|
|
$config = Hash::insert($config, $path, ($overwrite ? $extension : array_merge($default, (array)$extension))); |
|
|
|
|
|
|
120
|
|
|
Configure::write('Test.Api', $config); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Performs controller initialization. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $requestOptions Request options. |
|
127
|
|
|
* @param string $method Http method. |
|
128
|
|
|
* @param array $options Options. |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function _initializeRequest($requestOptions = [], $method = 'GET', $options = []) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$_SERVER['REQUEST_METHOD'] = $method; |
|
134
|
|
|
if (empty($requestOptions['params'])) { |
|
135
|
|
|
$requestOptions['params'] = []; |
|
136
|
|
|
} |
|
137
|
|
|
if (empty($requestOptions['params']['service'])) { |
|
138
|
|
|
$requestOptions['params']['service'] = 'articles'; |
|
139
|
|
|
} |
|
140
|
|
|
if (empty($requestOptions['params']['pass'])) { |
|
141
|
|
|
$requestOptions['params']['pass'] = []; |
|
142
|
|
|
} |
|
143
|
|
|
$this->request = new Request($requestOptions); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
if (empty($options['response'])) { |
|
146
|
|
|
$this->response = new Response(); |
|
|
|
|
|
|
147
|
|
|
} else { |
|
148
|
|
|
$this->response = $options['response']; |
|
149
|
|
|
} |
|
150
|
|
|
$this->Controller = $this->createMock('Cake\Controller\Controller', ['redirect']); |
|
|
|
|
|
|
151
|
|
|
$this->Controller->request = $this->request; |
|
152
|
|
|
$this->Controller->response = $this->response; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.