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
<?php
namespace Codeception\Module;
use Codeception\Exception\ModuleException as ModuleException;
use Codeception\Exception\ModuleConfigException as ModuleConfigException;
use Codeception\Lib\Driver\Facebook as FacebookDriver;
use Codeception\Lib\Interfaces\DependsOnModule;
use Codeception\Lib\Interfaces\RequiresPackage;
use Codeception\Lib\Notification;
use Codeception\Module as BaseModule;
/**
* Provides testing for projects integrated with Facebook API.
* Relies on Facebook's tool Test User API.
*
* <div class="alert alert-info">
* To use this module with Composer you need <em>"facebook/php-sdk4": "5.*"</em> package.
* </div>
*
* ## Status
*
* [ ](https://codeship.com/projects/160201)
*
* * Stability: **beta**
* * Maintainer: **tiger-seo**
* * Contact: tiger.seo@codeception.com
*
* ## Config
*
* * app_id *required* - Facebook application ID
* * secret *required* - Facebook application secret
* * test_user - Facebook test user parameters:
* * name - You can specify a name for the test user you create. The specified name will also be used in the email address assigned to the test user.
* * locale - You can specify a locale for the test user you create, the default is en_US. The list of supported locales is available at https://www.facebook.com/translations/FacebookLocales.xml
* * permissions - An array of permissions. Your app is granted these permissions for the new test user. The full list of permissions is available at https://developers.facebook.com/docs/authentication/permissions
*
* ### Config example
*
* modules:
* enabled:
* - Facebook:
* depends: PhpBrowser
* app_id: 412345678901234
* secret: ccb79c1b0fdff54e4f7c928bf233aea5
* test_user:
* name: FacebookGuy
* locale: uk_UA
* permissions: [email, publish_stream]
*
* ### Test example:
*
* ``` php
* <?php
* $I = new ApiGuy($scenario);
* $I->am('Guest');
* $I->wantToTest('check-in to a place be published on the Facebook using API');
* $I->haveFacebookTestUserAccount();
* $accessToken = $I->grabFacebookTestUserAccessToken();
* $I->haveHttpHeader('Auth', 'FacebookToken ' . $accessToken);
* $I->amGoingTo('send request to the backend, so that it will publish on user\'s wall on Facebook');
* $I->sendPOST('/api/v1/some-api-endpoint');
* $I->seePostOnFacebookWithAttachedPlace('167724369950862');
*
* ```
*
* ``` php
* <?php
* $I = new WebGuy($scenario);
* $I->am('Guest');
* $I->wantToTest('log in to site using Facebook');
* $I->haveFacebookTestUserAccount(); // create facebook test user
* $I->haveTestUserLoggedInOnFacebook(); // so that facebook will not ask us for login and password
* $fbUserFirstName = $I->grabFacebookTestUserFirstName();
* $I->amOnPage('/welcome');
* $I->see('Welcome, Guest');
* $I->click('Login with Facebook');
* $I->see('Welcome, ' . $fbUserFirstName);
*
* ```
*
* @since 1.6.3
* @author tiger.seo@gmail.com
*/
class Facebook extends BaseModule implements DependsOnModule, RequiresPackage
{
protected $requiredFields = ['app_id', 'secret'];
/**
* @var FacebookDriver
*/
protected $facebook;
/**
* @var array
*/
protected $testUser = [];
/**
* @var PhpBrowser
*/
protected $browserModule;
protected $dependencyMessage = <<<EOF
Example configuring PhpBrowser
--
modules
enabled:
- Facebook:
depends: PhpBrowser
app_id: 412345678901234
secret: ccb79c1b0fdff54e4f7c928bf233aea5
test_user:
name: FacebookGuy
locale: uk_UA
permissions: [email, publish_stream]
EOF;
public function _requires()
{
return ['Facebook\Facebook' => '"facebook/graph-sdk": "~5.3"'];
}
public function _depends()
{
return ['Codeception\Module\PhpBrowser' => $this->dependencyMessage];
}
public function _inject(PhpBrowser $browserModule)
{
$this->browserModule = $browserModule;
}
protected function deleteTestUser()
{
if (array_key_exists('id', $this->testUser)) {
// make api-call for test user deletion
$this->facebook->deleteTestUser($this->testUser['id']);
$this->testUser = [];
}
}
public function _initialize()
{
Notification::deprecate('Facebook module is not maintained and will be deprecated. Contact Codeception team if you are interested in maintaining it');
if (!array_key_exists('test_user', $this->config)) {
$this->config['test_user'] = [
'permissions' => [],
'name' => 'Codeception Testuser'
];
} elseif (!array_key_exists('permissions', $this->config['test_user'])) {
$this->config['test_user']['permissions'] = [];
} elseif (!array_key_exists('name', $this->config['test_user'])) {
$this->config['test_user']['name'] = "codeception testuser";
}
$this->facebook = new FacebookDriver(
[
'app_id' => $this->config['app_id'],
'secret' => $this->config['secret'],
],
function ($title, $message) {
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$this->debugSection($title, $message);
}
}
);
}
public function _afterSuite()
{
$this->deleteTestUser();
}
/**
* Get facebook test user be created.
*
* *Please, note that the test user is created only at first invoke, unless $renew arguments is true.*
*
* @param bool $renew true if the test user should be recreated
*/
public function haveFacebookTestUserAccount($renew = false)
{
if ($renew) {
$this->deleteTestUser();
}
// make api-call for test user creation only if it's not yet created
if (!array_key_exists('id', $this->testUser)) {
$this->testUser = $this->facebook->createTestUser(
$this->config['test_user']['name'],
$this->config['test_user']['permissions']
);
}
}
/**
* Get facebook test user be logged in on facebook.
* This is done by going to facebook.com
*
* @throws ModuleConfigException
*/
public function haveTestUserLoggedInOnFacebook()
{
if (!array_key_exists('id', $this->testUser)) {
throw new ModuleException(
__CLASS__,
'Facebook test user was not found. Did you forget to create one?'
);
}
$callbackUrl = $this->browserModule->_getUrl();
$this->browserModule->amOnUrl('https://facebook.com/login');
$this->browserModule->submitForm('#login_form', [
'email' => $this->grabFacebookTestUserEmail(),
'pass' => $this->grabFacebookTestUserPassword()
]);
// if login in successful we are back on login screen:
$this->browserModule->dontSeeInCurrentUrl('/login');
$this->browserModule->amOnUrl($callbackUrl);
}
/**
* Returns the test user access token.
*
* @return string
*/
public function grabFacebookTestUserAccessToken()
{
return $this->testUser['access_token'];
}
/**
* Returns the test user id.
*
* @return string
*/
public function grabFacebookTestUserId()
{
return $this->testUser['id'];
}
/**
* Returns the test user email.
*
* @return string
*/
public function grabFacebookTestUserEmail()
{
return $this->testUser['email'];
}
/**
* Returns URL for test user auto-login.
*
* @return string
*/
public function grabFacebookTestUserLoginUrl()
{
return $this->testUser['login_url'];
}
public function grabFacebookTestUserPassword()
{
return $this->testUser['password'];
}
/**
* Returns the test user name.
*
* @return string
*/
public function grabFacebookTestUserName()
{
if (!array_key_exists('profile', $this->testUser)) {
$this->testUser['profile'] = $this->facebook->getTestUserInfo($this->grabFacebookTestUserAccessToken());
}
return $this->testUser['profile']['name'];
}
/**
* Please, note that you must have publish_actions permission to be able to publish to user's feed.
*
* @param array $params
*/
public function postToFacebookAsTestUser($params)
{
$this->facebook->sendPostToFacebook($this->grabFacebookTestUserAccessToken(), $params);
}
/**
*
* Please, note that you must have publish_actions permission to be able to publish to user's feed.
*
* @param string $placeId Place identifier to be verified against user published posts
*/
public function seePostOnFacebookWithAttachedPlace($placeId)
{
$token = $this->grabFacebookTestUserAccessToken();
$this->debugSection('Access Token', $token);
$place = $this->facebook->getVisitedPlaceTagForTestUser($placeId, $token);
$this->assertEquals($placeId, $place['id'], "The place was not found on facebook page");
}
/**
*
* Please, note that you must have publish_actions permission to be able to publish to user's feed.
*
* @param string $message published post to be verified against the actual post on facebook
*/
public function seePostOnFacebookWithMessage($message)
{
$posts = $this->facebook->getLastPostsForTestUser($this->grabFacebookTestUserAccessToken());
$facebook_post_message = '';
$this->assertNotEquals($message, $facebook_post_message, "You can not test for an empty message post");
if ($posts['data']) {
foreach ($posts['data'] as $post) {
if (array_key_exists('message', $post) && ($post['message'] == $message)) {
$facebook_post_message = $post['message'];
}
}
}
$this->assertEquals($message, $facebook_post_message, "The post message was not found on facebook page");
}
}