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