1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Tests for Developers Controller |
6
|
|
|
* |
7
|
|
|
* phpMyAdmin Error reporting server |
8
|
|
|
* Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License |
11
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
15
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
16
|
|
|
* |
17
|
|
|
* @see https://www.phpmyadmin.net/ |
18
|
|
|
*/ |
19
|
|
|
namespace App\Test\TestCase\Controller; |
20
|
|
|
|
21
|
|
|
use App\Controller\DevelopersController; |
22
|
|
|
use Cake\ORM\TableRegistry; |
23
|
|
|
use Cake\TestSuite\IntegrationTestCase; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* App\Controller\DevelopersController Test Case |
27
|
|
|
*/ |
28
|
|
|
class DevelopersControllerTest extends IntegrationTestCase |
29
|
|
|
{ |
30
|
|
|
use \phpmock\phpunit\PHPMock; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Fixtures |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $fixtures = [ |
38
|
|
|
'app.developers', |
39
|
|
|
'app.notifications' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public function setUp() |
43
|
|
|
{ |
44
|
|
|
$this->Developers = TableRegistry::get('Developers'); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test login method |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function testLogin() |
53
|
|
|
{ |
54
|
|
|
// Empty session during initiation |
55
|
|
|
$this->session(array()); |
56
|
|
|
|
57
|
|
|
$this->get('developers/login'); |
58
|
|
|
$this->assertRedirectContains('https://github.com/login/oauth/authorize'); |
59
|
|
|
$this->assertRedirectContains('developers%2Fcallback'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test callback method |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function testCallback() |
68
|
|
|
{ |
69
|
|
|
// Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent |
70
|
|
|
// so that we don't actually hit the Github Api |
71
|
|
|
$curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec'); |
72
|
|
|
$curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo'); |
73
|
|
|
|
74
|
|
|
$accessTokenResponse = json_encode(array('access_token' => 'abc')); |
75
|
|
|
$emptyAccessTokenResponse = json_encode(array('access_token' => null)); |
76
|
|
|
|
77
|
|
|
$nonSuccessUserResponse = json_encode(array('message' => 'Unauthorized access')); |
78
|
|
|
$userResponse = file_get_contents(TESTS . 'Fixture' . DS . 'user_response.json'); |
79
|
|
|
|
80
|
|
|
// Github unsuccessful response followed by successful response |
81
|
|
|
$curlExecMock->expects($this->exactly(10))->willReturnOnConsecutiveCalls( |
82
|
|
|
$emptyAccessTokenResponse, |
83
|
|
|
$emptyAccessTokenResponse, |
84
|
|
|
$accessTokenResponse, $nonSuccessUserResponse, |
85
|
|
|
$accessTokenResponse, $userResponse, null, |
86
|
|
|
$accessTokenResponse, $userResponse, null |
87
|
|
|
); |
88
|
|
|
$curlGetInfoMock->expects($this->exactly(5))->willReturnOnConsecutiveCalls( |
89
|
|
|
401, |
90
|
|
|
200, 404, |
91
|
|
|
200, 204 |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
// Case 1.1 Test no access_token in Github response (with last_page not set in session) |
95
|
|
|
// So, empty the session |
96
|
|
|
$this->session(array()); |
97
|
|
|
|
98
|
|
|
$this->get('developers/callback/?code=123123123'); |
99
|
|
|
$this->assertRedirect(['controller' => '', 'action' => 'index']); |
100
|
|
|
|
101
|
|
|
// Case 1.2 Test no access_token in Github response (with last_page set in session) |
102
|
|
|
$this->session( |
103
|
|
|
array( |
104
|
|
|
'last_page' => array( |
105
|
|
|
'controller' => 'notifications', |
106
|
|
|
'action' => 'index' |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->get('developers/callback/?code=123123123'); |
112
|
|
|
$this->assertRedirect(['controller' => '', 'action' => 'index']); |
113
|
|
|
|
114
|
|
|
// Case 2. Non successful response code from Github |
115
|
|
|
$this->session( |
116
|
|
|
array( |
117
|
|
|
'last_page' => array( |
118
|
|
|
'controller' => 'reports', |
119
|
|
|
'action' => 'index' |
120
|
|
|
) |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
$this->get('developers/callback/?code=123123123'); |
124
|
|
|
$this->assertRedirect(['controller' => '', 'action' => 'index']); |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
// Case 3. Successful response code (new user), check whether session variables are init |
128
|
|
|
$this->get('developers/callback/?code=123123123'); |
129
|
|
|
$this->assertSession(3, 'Developer.id'); |
130
|
|
|
$this->assertSession(true, 'read_only'); |
131
|
|
|
$this->assertSession('abc', 'access_token'); |
132
|
|
|
|
133
|
|
|
$developer = $this->Developers->get(3); |
134
|
|
|
$this->assertEquals('abc', $developer->access_token); |
135
|
|
|
$this->assertEquals('[email protected]', $developer->email); |
136
|
|
|
|
137
|
|
|
// Case 4. Successful response code (returning user) |
138
|
|
|
// check whether session variables are init |
139
|
|
|
$this->session(array('last_page' => null)); |
140
|
|
|
|
141
|
|
|
$this->get('developers/callback/?code=123123123'); |
142
|
|
|
$this->assertSession(3, 'Developer.id'); |
143
|
|
|
$this->assertSession(false, 'read_only'); |
144
|
|
|
$this->assertSession('abc', 'access_token'); |
145
|
|
|
|
146
|
|
|
$developer = $this->Developers->get(3); |
147
|
|
|
$this->assertEquals(1, $developer->has_commit_access); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Test logout method |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function testLogout() |
156
|
|
|
{ |
157
|
|
|
$this->session(array('Developer.id' => 1)); |
158
|
|
|
|
159
|
|
|
$this->get('developers/logout'); |
160
|
|
|
$this->assertSession(null, 'Developer.id'); |
161
|
|
|
$this->assertRedirect(['controller' => '', 'action' => 'index']); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: