Merge pull request #8607 from twomice/CRM-18508
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / OpenIDTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_OpenIDTest
5 * @group headless
6 */
7 class CRM_Core_BAO_OpenIDTest extends CiviUnitTestCase {
8 public function tearDown() {
9 // If we truncate only contact, then stale domain and openid records will be left.
10 // If we truncate none of these tables, then contactDelete() will incrementally
11 // clean correctly.
12 //$tablesToTruncate = array('civicrm_domain', 'civicrm_contact', 'civicrm_openid');
13 //$this->quickCleanup($tablesToTruncate);
14 }
15
16 public function setUp() {
17 parent::setUp();
18 }
19
20 /**
21 * Add() method (create and update modes)
22 */
23 public function testAdd() {
24 $contactId = $this->individualCreate();
25 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
26
27 $openIdURL = "http://test-username.civicrm.org/";
28 $params = array(
29 'contact_id' => $contactId,
30 'location_type_id' => 1,
31 'openid' => $openIdURL,
32 'is_primary' => 1,
33 );
34
35 $openObject = CRM_Core_BAO_OpenID::add($params);
36
37 $openId = $openObject->id;
38
39 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURL, 'id', 'openid',
40 'Database check for created OpenID.'
41 );
42
43 // Now call add() to modify an existing open-id record
44
45 $params = array(
46 'id' => $openId,
47 'contact_id' => $contactId,
48 'openid' => $openIdURL,
49 'is_bulkmail' => 1,
50 'allowed_to_login' => 1,
51 );
52
53 CRM_Core_BAO_OpenID::add($params);
54
55 $allowedToLogin = $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openId, 'allowed_to_login', 'id',
56 'Database check on updated OpenID record.'
57 );
58 $this->assertEquals($allowedToLogin, 1, 'Verify allowed_to_login value is 1.');
59
60 $this->contactDelete($contactId);
61 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);
62 }
63
64 /**
65 * IfAllowedToLogin() method (set and reset allowed_to_login)
66 */
67 public function testIfAllowedToLogin() {
68 $contactId = $this->individualCreate();
69 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
70 $openIdURL = "http://test-username.civicrm.org/";
71
72 $params = array(
73 'contact_id' => $contactId,
74 'location_type_id' => 1,
75 'openid' => $openIdURL,
76 'is_primary' => 1,
77 );
78
79 $openObject = CRM_Core_BAO_OpenID::add($params);
80
81 $openId = $openObject->id;
82 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURL, 'id', 'openid',
83 'Database check for created OpenID.'
84 );
85
86 $allowedToLogin = CRM_Core_BAO_OpenID::isAllowedToLogin($openIdURL);
87 $this->assertEquals($allowedToLogin, FALSE, 'Verify allowed_to_login value is 0.');
88
89 // Now call add() to modify an existing open-id record
90
91 $params = array(
92 'id' => $openId,
93 'contact_id' => $contactId,
94 'openid' => $openIdURL,
95 'is_bulkmail' => 1,
96 'allowed_to_login' => 1,
97 );
98
99 CRM_Core_BAO_OpenID::add($params);
100
101 $allowedToLogin = CRM_Core_BAO_OpenID::isAllowedToLogin($openIdURL);
102
103 $this->assertEquals($allowedToLogin, TRUE, 'Verify allowed_to_login value is 1.');
104 $this->contactDelete($contactId);
105 //domain contact doesn't really get deleted //
106 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);
107 }
108
109 /**
110 * AllOpenIDs() method - get all OpenIDs for the given contact
111 */
112 public function testAllOpenIDs() {
113 $contactId = $this->individualCreate();
114 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
115
116 // create first openid
117 $openIdURLOne = "http://test-one-username.civicrm.org/";
118 $params = array(
119 'contact_id' => $contactId,
120 'location_type_id' => 1,
121 'openid' => $openIdURLOne,
122 'is_primary' => 1,
123 'allowed_to_login' => 1,
124 );
125
126 $openObjectOne = CRM_Core_BAO_OpenID::add($params);
127
128 $openIdOne = $openObjectOne->id;
129 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLOne, 'id', 'openid',
130 'Database check for created OpenID.'
131 );
132
133 // create second openid
134 $openIdURLTwo = "http://test-two-username.civicrm.org/";
135 $params = array(
136 'contact_id' => $contactId,
137 'location_type_id' => 1,
138 'openid' => $openIdURLTwo,
139 );
140
141 $openObjectTwo = CRM_Core_BAO_OpenID::add($params);
142 $openIdTwo = $openObjectTwo->id;
143
144 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLTwo, 'id', 'openid',
145 'Database check for created OpenID.'
146 );
147
148 // obtain all openids for the contact
149 $openIds = CRM_Core_BAO_OpenID::allOpenIDs($contactId);
150
151 // check number of openids for the contact
152 $this->assertEquals(2, count($openIds), 'Checking number of returned open-ids.');
153
154 // check first openid values
155 $this->assertEquals($openIdURLOne, $openIds[$openIdOne]['openid'],
156 'Confirm first openid value.'
157 );
158 $this->assertEquals(1, $openIds[$openIdOne]['is_primary'], 'Confirm is_primary field value.');
159 $this->assertEquals(1, $openIds[$openIdOne]['allowed_to_login'], 'Confirm allowed_to_login field value.');
160
161 // check second openid values
162 $this->assertEquals($openIdURLTwo, $openIds[$openIdTwo]['openid'],
163 'Confirm second openid value.'
164 );
165 $this->assertEquals(0, $openIds[$openIdTwo]['is_primary'], 'Confirm is_primary field value for second openid.');
166 $this->assertEquals(0, $openIds[$openIdTwo]['allowed_to_login'], 'Confirm allowed_to_login field value for second openid.');
167
168 $this->contactDelete($contactId);
169 $this->assertDBRowNotExist('CRM_Contact_DAO_Contact', $contactId);
170 }
171
172 }