3 require_once 'CiviTest/Contact.php';
6 * Class CRM_Core_BAO_OpenIDTest
9 class CRM_Core_BAO_OpenIDTest
extends CiviUnitTestCase
{
10 public function tearDown() {
11 // If we truncate only contact, then stale domain and openid records will be left.
12 // If we truncate none of these tables, then contactDelete() will incrementally
14 //$tablesToTruncate = array('civicrm_domain', 'civicrm_contact', 'civicrm_openid');
15 //$this->quickCleanup($tablesToTruncate);
18 public function setUp() {
23 * Add() method (create and update modes)
25 public function testAdd() {
26 $contactId = Contact
::createIndividual();
27 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
29 $openIdURL = "http://test-username.civicrm.org/";
31 'contact_id' => $contactId,
32 'location_type_id' => 1,
33 'openid' => $openIdURL,
37 $openObject = CRM_Core_BAO_OpenID
::add($params);
39 $openId = $openObject->id
;
41 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURL, 'id', 'openid',
42 'Database check for created OpenID.'
45 // Now call add() to modify an existing open-id record
49 'contact_id' => $contactId,
50 'openid' => $openIdURL,
52 'allowed_to_login' => 1,
55 CRM_Core_BAO_OpenID
::add($params);
57 $allowedToLogin = $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openId, 'allowed_to_login', 'id',
58 'Database check on updated OpenID record.'
60 $this->assertEquals($allowedToLogin, 1, 'Verify allowed_to_login value is 1.');
62 $this->contactDelete($contactId);
63 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);
67 * IfAllowedToLogin() method (set and reset allowed_to_login)
69 public function testIfAllowedToLogin() {
70 $contactId = Contact
::createIndividual();
71 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
72 $openIdURL = "http://test-username.civicrm.org/";
75 'contact_id' => $contactId,
76 'location_type_id' => 1,
77 'openid' => $openIdURL,
81 $openObject = CRM_Core_BAO_OpenID
::add($params);
83 $openId = $openObject->id
;
84 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURL, 'id', 'openid',
85 'Database check for created OpenID.'
88 $allowedToLogin = CRM_Core_BAO_OpenID
::isAllowedToLogin($openIdURL);
89 $this->assertEquals($allowedToLogin, FALSE, 'Verify allowed_to_login value is 0.');
91 // Now call add() to modify an existing open-id record
95 'contact_id' => $contactId,
96 'openid' => $openIdURL,
98 'allowed_to_login' => 1,
101 CRM_Core_BAO_OpenID
::add($params);
103 $allowedToLogin = CRM_Core_BAO_OpenID
::isAllowedToLogin($openIdURL);
105 $this->assertEquals($allowedToLogin, TRUE, 'Verify allowed_to_login value is 1.');
106 $this->contactDelete($contactId);
107 //domain contact doesn't really get deleted //
108 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);
112 * AllOpenIDs() method - get all OpenIDs for the given contact
114 public function testAllOpenIDs() {
115 $contactId = Contact
::createIndividual();
116 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
118 // create first openid
119 $openIdURLOne = "http://test-one-username.civicrm.org/";
121 'contact_id' => $contactId,
122 'location_type_id' => 1,
123 'openid' => $openIdURLOne,
125 'allowed_to_login' => 1,
128 $openObjectOne = CRM_Core_BAO_OpenID
::add($params);
130 $openIdOne = $openObjectOne->id
;
131 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLOne, 'id', 'openid',
132 'Database check for created OpenID.'
135 // create second openid
136 $openIdURLTwo = "http://test-two-username.civicrm.org/";
138 'contact_id' => $contactId,
139 'location_type_id' => 1,
140 'openid' => $openIdURLTwo,
143 $openObjectTwo = CRM_Core_BAO_OpenID
::add($params);
144 $openIdTwo = $openObjectTwo->id
;
146 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLTwo, 'id', 'openid',
147 'Database check for created OpenID.'
150 // obtain all openids for the contact
151 $openIds = CRM_Core_BAO_OpenID
::allOpenIDs($contactId);
153 // check number of openids for the contact
154 $this->assertEquals(2, count($openIds), 'Checking number of returned open-ids.');
156 // check first openid values
157 $this->assertEquals($openIdURLOne, $openIds[$openIdOne]['openid'],
158 'Confirm first openid value.'
160 $this->assertEquals(1, $openIds[$openIdOne]['is_primary'], 'Confirm is_primary field value.');
161 $this->assertEquals(1, $openIds[$openIdOne]['allowed_to_login'], 'Confirm allowed_to_login field value.');
163 // check second openid values
164 $this->assertEquals($openIdURLTwo, $openIds[$openIdTwo]['openid'],
165 'Confirm second openid value.'
167 $this->assertEquals(0, $openIds[$openIdTwo]['is_primary'], 'Confirm is_primary field value for second openid.');
168 $this->assertEquals(0, $openIds[$openIdTwo]['allowed_to_login'], 'Confirm allowed_to_login field value for second openid.');
170 $this->contactDelete($contactId);
171 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);