forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkSocialBehaviorTest.php
More file actions
363 lines (331 loc) · 12.7 KB
/
Copy pathLinkSocialBehaviorTest.php
File metadata and controls
363 lines (331 loc) · 12.7 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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Test\TestCase\Model\Behavior;
use CakeDC\Users\Model\Behavior\LinkSocialBehavior;
use CakeDC\Users\Model\Table\UsersTable;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* App\Model\Behavior\LinkSocialBehavior Test Case
*/
class LinkSocialBehaviorTest extends TestCase
{
/**
* Test subject
*
* @var \App\Model\Behavior\LinkSocialBehavior
*/
public $Behavior;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'plugin.CakeDC/Users.social_accounts',
'plugin.CakeDC/Users.users'
];
/**
* setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->Table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users');
$this->Behavior = new LinkSocialBehavior($this->Table);
}
/**
* tearDown
*
* @return void
*/
public function tearDown()
{
unset($this->Table, $this->Behavior);
parent::tearDown();
}
/**
* Test linkSocialAccount with facebook and not existing social account
*
* @param array $data Test input data
* @param string $userId User id to add social account
* @param array $result Expected result
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return void
* @dataProvider providerFacebookLinkSocialAccount
*/
public function testlinkSocialAccountFacebookProvider($data, $userId, $result)
{
$user = $this->Table->get($userId, [
'contain' => 'SocialAccounts'
]);
$resultUser = $this->Behavior->linkSocialAccount($user, $data);
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\User', $resultUser);
$actual = $resultUser->social_accounts[2];
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\SocialAccount', $actual);
$actual->token_expires = $actual->token_expires->format('Y-m-d H:i:s');
foreach ($result as $property => $value) {
$this->assertEquals($value, $actual->$property);
}
$result = $this->Table->SocialAccounts->exists(['id' => $actual->id]);
$this->assertTrue($result);
}
/**
* Provider for linkSocialAccount with facebook and not existing social account
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return array
*/
public function providerFacebookLinkSocialAccount()
{
$expiresTime = new Time();
$tokenExpires = $expiresTime->setTimestamp(1458423682)->format('Y-m-d H:i:s');
return [
'provider' => [
'data' => [
'id' => '9999911112255', //Reference existe mas provider google
'username' => null,
'full_name' => 'Full name',
'first_name' => 'First name',
'last_name' => 'Last name',
'email' => 'user-1@test.com',
'raw' => [
'id' => '9999911112255',
'name' => 'Ful Name.',
'first_name' => 'First Name',
'last_name' => 'Last name',
'email' => 'user-1@test.com',
'picture' => [
'data' => [
'url' => 'data-url'
]
]
],
'credentials' => [
'token' => 'token',
'secret' => null,
'expires' => 1458423682
],
'validated' => true,
'link' => 'facebook-link-15579',
'provider' => 'Facebook'
],
'user' => '00000000-0000-0000-0000-000000000001',
'result' => [
'provider' => 'Facebook',
'username' => null,
'reference' => '9999911112255',
'avatar' => '',
'link' => 'facebook-link-15579',
'description' => null,
'token' => 'token',
'token_secret' => null,
'token_expires' => $tokenExpires,
'user_id' => '00000000-0000-0000-0000-000000000001',
'active' => true
]
]
];
}
/**
* Test linkSocialAccount with facebook and could not save social account
*
* @param array $data Test input data
* @param string $userId User id to add social account
* @param array $result Expected result
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return void
* @dataProvider providerFacebookLinkSocialAccountErrorSaving
*/
public function testlinkSocialAccountErrorSavingFacebookProvider($data, $userId, $result)
{
$user = $this->Table->get($userId);
$resultUser = $this->Behavior->linkSocialAccount($user, $data);
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\User', $resultUser);
$actual = $resultUser->social_accounts[0];
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\SocialAccount', $actual);
$actual = $user->getErrors();
$expected = [
'social_accounts' => [
[
'token' => [
'_empty' => 'This field cannot be left empty'
]
]
]
];
$this->assertEquals($expected, $actual);
$error = $user->getErrors('social_accounts');
$error = $error ? reset($error) : $message;
}
/**
* Provider for linkSocialAccount with facebook and could not save social account
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return array
*/
public function providerFacebookLinkSocialAccountErrorSaving()
{
$expiresTime = new Time();
$tokenExpires = $expiresTime->setTimestamp(1458423682)->format('Y-m-d H:i:s');
return [
'provider' => [
'data' => [
'id' => '9999911112255', //Reference existe mas provider google
'username' => null,
'full_name' => 'Full name',
'first_name' => 'First name',
'last_name' => 'Last name',
'email' => 'user-1@test.com',
'raw' => [
'id' => '9999911112255',
'name' => 'Ful Name.',
'first_name' => 'First Name',
'last_name' => 'Last name',
'email' => 'user-1@test.com',
'picture' => [
'data' => [
'url' => 'data-url'
]
]
],
'credentials' => [
'token' => '',
'secret' => null,
'expires' => 1458423682
],
'validated' => true,
'link' => 'facebook-link-15579',
'provider' => 'Facebook'
],
'user' => '00000000-0000-0000-0000-000000000001',
'result' => [
'provider' => 'Facebook',
'username' => null,
'reference' => '9999911112255',
'avatar' => '',
'link' => 'facebook-link-15579',
'description' => null,
'token' => 'token',
'token_secret' => null,
'token_expires' => $tokenExpires,
'user_id' => '00000000-0000-0000-0000-000000000001',
'active' => true
]
]
];
}
/**
* Test linkSocialAccount with facebook when account already exists
*
* @param array $data Test input data
* @param string $userId User id to add social account
* @param array $result Expected result
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return void
* @dataProvider providerFacebookLinkSocialAccountAccountExists
*/
public function testlinkSocialAccountFacebookProviderAccountExists($data, $userId, $result)
{
$user = $this->Table->get($userId);
$resultUser = $this->Behavior->linkSocialAccount($user, $data);
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\User', $resultUser);
$this->assertFalse($resultUser->has('social_accounts'));
$expected = [
'social_accounts' => [
'_existsIn' => __d('CakeDC/Users', 'Social account already associated to another user')
]
];
$actual = $user->getErrors();
$this->assertEquals($expected, $actual);
//Se for o usuário que já esta associado então okay
$socialAccount = $this->Table->SocialAccounts->find()->where([
'reference' => $data['id'],
'provider' => $data['provider']
])->firstOrFail();
$userBase = $this->Table->get('00000000-0000-0000-0000-000000000002', [
'contain' => ['SocialAccounts']
]);
$resultUser = $this->Behavior->linkSocialAccount($userBase, $data);
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\User', $resultUser);
$this->assertEquals([], $userBase->getErrors());
$actual = $resultUser->social_accounts[0];
$this->assertInstanceOf('\CakeDC\Users\Model\Entity\SocialAccount', $actual);
$actual->token_expires = $actual->token_expires->format('Y-m-d H:i:s');
foreach ($result as $property => $value) {
$this->assertEquals($value, $actual->$property);
}
}
/**
* Provider for linkSocialAccount with facebook when account already exists
*
* @author Marcelo Rocha <marcelo@promosapiens.com.br>
* @return array
*/
public function providerFacebookLinkSocialAccountAccountExists()
{
$expiresTime = new Time();
$tokenExpires = $expiresTime->setTimestamp(1458423682)->format('Y-m-d H:i:s');
return [
'provider' => [
'data' => [
'id' => 'reference-2-1',
'username' => null,
'full_name' => 'Full name',
'first_name' => 'First name',
'last_name' => 'Last name',
'email' => 'email@example.com',
'raw' => [
'id' => 'reference-2-1',
'name' => 'Ful Name.',
'first_name' => 'First Name',
'last_name' => 'Last name',
'email' => 'email@example.com',
'picture' => [
'data' => [
'url' => 'data-url'
]
]
],
'credentials' => [
'token' => 'token',
'secret' => null,
'expires' => 1458423682
],
'validated' => true,
'link' => 'facebook-link-15579',
'provider' => 'Facebook'
],
'user' => '00000000-0000-0000-0000-000000000001',
'result' => [
'id' => '00000000-0000-0000-0000-000000000003',
'provider' => 'Facebook',
'username' => null,
'reference' => 'reference-2-1',
'avatar' => '',
'link' => 'facebook-link-15579',
'description' => null,
'token' => 'token',
'token_secret' => null,
'token_expires' => $tokenExpires,
'user_id' => '00000000-0000-0000-0000-000000000002',
'active' => true
]
]
];
}
}