fbconnect.test in Facebook Connect 7.2
Same filename and directory in other branches
Defines FBConnect Test Case.
See also
http://developers.facebook.com/docs/test_users
http://developers.facebook.com/docs/authentication/#authenticating-as-an...
File
tests/fbconnect.testView source
<?php
/**
* @file
* Defines FBConnect Test Case.
*
* @see http://drupal.org/node/278126
* @see http://developers.facebook.com/docs/test_users
* @see http://developers.facebook.com/docs/authentication/#authenticating-as-an-application
*
*/
class FbconnectTestCase extends DrupalWebTestCase {
/**
* Implements getInfo().
*/
public static function getInfo() {
return array(
'name' => t('FBConnect functionality'),
'description' => t('Test FBConnect.'),
'group' => t('FBConnect'),
);
}
protected $_fb_users = array();
function _createFbTestUser($installed = 'true', $permissions = '') {
$facebook_config = fbconnect_get_config();
unset($_SESSION['fbconnect_token']);
unset($_COOKIE['fbconnect_token_' . $facebook_config['app_id']]);
setcookie('fbconnect_token_' . $facebook_config['app_id'], '');
$url = "/{$facebook_config['app_id']}/accounts/test-users?installed={$installed}&permissions={$permissions}";
$fb_user = fbconnect_graph_query($url, array(), 'POST', TRUE);
if (!empty($fb_user)) {
$fb_user = $fb_user
->asArray();
$this->_fb_users[] = $fb_user;
}
$this
->verbose('_createFbTestUser: fb_user=' . var_export($fb_user, TRUE));
$this
->assertTrue(isset($fb_user['id']), 'created test FB user', 'FBConnect');
return $fb_user;
}
/**
* Implements setUp().
*/
function setUp() {
// Change cookie file for user.
$this->cookieFile = file_stream_wrapper_get_instance_by_scheme('temporary')
->getDirectoryPath() . '/cookie.txt';
$this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
$this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
$this->settings_keys = array(
'fbconnect_appid',
'fbconnect_skey',
);
foreach ($this->settings_keys as $key) {
$this->settings[$key] = variable_get($key, NULL);
}
if (!facebook_client_session()) {
$this
->error('fbconnect is not properly installed. See README.txt');
return;
}
parent::setUp('fbconnect', 'fbconnect_login', 'fbconnect_test');
foreach ($this->settings as $key => $value) {
variable_set($key, $value);
}
}
function tearDown() {
parent::tearDown();
$facebook_config = fbconnect_get_config();
unset($_SESSION['fbconnect_token']);
unset($_COOKIE['fbconnect_token_' . $facebook_config['app_id']]);
foreach ($this->_fb_users as $fb_user) {
$result = fbconnect_graph_query('/' . $fb_user['id'], array(), 'DELETE', TRUE)
->asArray();
$this
->assertEqual($result['success'], TRUE, 'deleted test FB user', 'FBConnect');
}
$this->_fb_users = array();
}
function testLogin() {
$account = $this
->drupalCreateUser();
$fb_user = $this
->_createFbTestUser('true', 'email');
$edit = array(
'fbuid' => $fb_user['id'],
'access_token' => $fb_user['access_token'],
);
$this
->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
$this
->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
$this
->assertTrue(fbconnect_login_register($account->uid, $fb_user['id']), 'fbconnect_register returned true', 'FBConnect');
$this
->assertNotNull(_get_user_fbuid($account->uid), 'FB Account is connected', 'FBConnect');
$this
->drupalHead('fbconnect/authorize');
$this
->assertLink('My account', 0, 'User is logged in', 'FBConnect');
}
function testLinking() {
$account = $this
->drupalCreateUser();
$fb_user = $this
->_createFbTestUser('true');
$edit = array(
'fbuid' => $fb_user['id'],
'access_token' => $fb_user['access_token'],
);
$this
->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
$this
->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
$edit = array(
'name' => $account->name,
'pass' => $account->pass_raw,
);
$this
->drupalPost('user', $edit, 'Log in', array(
'query' => array(
'destination' => 'fbconnect/authorize',
),
));
$this
->assertLink('My account', 0, 'User is logged in', 'FBConnect');
}
function testRegistration() {
variable_set('fbconnect_reg_options', 1);
variable_set('user_register', USER_REGISTER_VISITORS);
$fb_user = $this
->_createFbTestUser('true');
$edit = array(
'fbuid' => $fb_user['id'],
'access_token' => $fb_user['access_token'],
);
$this
->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
$this
->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
$edit = array(
'mail' => $this
->randomName() . '@example.com',
);
$this
->drupalPost('fbconnect/authorize', $edit, 'Create new account');
$this
->assertLink('My account', 0, 'User is logged in', 'FBConnect');
}
function testFastRegistration() {
variable_set('fbconnect_fast_reg', 1);
variable_set('fbconnect_reg_options', 1);
$fb_user = $this
->_createFbTestUser('true', 'email');
$edit = array(
'fbuid' => $fb_user['id'],
'access_token' => $fb_user['access_token'],
);
$this
->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
$this
->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
$this
->drupalHead('fbconnect/authorize', array(
'query' => array(
'fb-destination' => '',
),
));
$this
->assertLink('My account', 0, 'User is logged in', 'FBConnect');
}
}
Classes
Name | Description |
---|---|
FbconnectTestCase | @file Defines FBConnect Test Case. |