Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / OpenIDTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
9f8f650e 3use Civi\Api4\OpenID;
4
aba1cd8b
EM
5/**
6 * Class CRM_Core_BAO_OpenIDTest
acb109b7 7 * @group headless
aba1cd8b 8 */
6a488035 9class CRM_Core_BAO_OpenIDTest extends CiviUnitTestCase {
39b959db 10
594a9328 11 public function tearDown(): void {
9f8f650e 12 $this->quickCleanup(['civicrm_contact', 'civicrm_openid', 'civicrm_email']);
13 parent::tearDown();
6a488035
TO
14 }
15
16 /**
100fef9d 17 * Add() method (create and update modes)
6a488035 18 */
00be9182 19 public function testAdd() {
6ae19242 20 $contactId = $this->individualCreate();
6a488035
TO
21 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
22
23 $openIdURL = "http://test-username.civicrm.org/";
9099cab3 24 $params = [
6a488035
TO
25 'contact_id' => $contactId,
26 'location_type_id' => 1,
27 'openid' => $openIdURL,
28 'is_primary' => 1,
9099cab3 29 ];
6a488035 30
9f8f650e 31 $openId = OpenID::create(FALSE)->setValues($params)->execute()->first()['id'];
6a488035
TO
32
33 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURL, 'id', 'openid',
34 'Database check for created OpenID.'
35 );
36
9f8f650e 37 // Now modify an existing open-id record
6a488035 38
9099cab3 39 $params = [
6a488035
TO
40 'id' => $openId,
41 'contact_id' => $contactId,
42 'openid' => $openIdURL,
43 'is_bulkmail' => 1,
44 'allowed_to_login' => 1,
9099cab3 45 ];
6a488035 46
9f8f650e 47 OpenID::update(FALSE)->addWhere('id', '=', $openId)->setValues($params)->execute();
6a488035
TO
48
49 $allowedToLogin = $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openId, 'allowed_to_login', 'id',
50 'Database check on updated OpenID record.'
51 );
52 $this->assertEquals($allowedToLogin, 1, 'Verify allowed_to_login value is 1.');
6a488035
TO
53 }
54
6a488035 55 /**
100fef9d 56 * AllOpenIDs() method - get all OpenIDs for the given contact
6a488035 57 */
00be9182 58 public function testAllOpenIDs() {
6ae19242 59 $contactId = $this->individualCreate();
6a488035
TO
60 $this->assertDBRowExist('CRM_Contact_DAO_Contact', $contactId);
61
62 // create first openid
63 $openIdURLOne = "http://test-one-username.civicrm.org/";
9099cab3 64 $params = [
6a488035
TO
65 'contact_id' => $contactId,
66 'location_type_id' => 1,
67 'openid' => $openIdURLOne,
68 'is_primary' => 1,
69 'allowed_to_login' => 1,
9099cab3 70 ];
6a488035 71
9f8f650e 72 $openIdOne = OpenID::create(FALSE)->setValues($params)->execute()->first()['id'];
6a488035
TO
73 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLOne, 'id', 'openid',
74 'Database check for created OpenID.'
75 );
76
77 // create second openid
78 $openIdURLTwo = "http://test-two-username.civicrm.org/";
9099cab3 79 $params = [
6a488035
TO
80 'contact_id' => $contactId,
81 'location_type_id' => 1,
82 'openid' => $openIdURLTwo,
9099cab3 83 ];
6a488035 84
9f8f650e 85 $openIdTwo = OpenID::create(FALSE)->setValues($params)->execute()->first()['id'];
6a488035
TO
86
87 $this->assertDBNotNull('CRM_Core_DAO_OpenID', $openIdURLTwo, 'id', 'openid',
88 'Database check for created OpenID.'
89 );
90
91 // obtain all openids for the contact
92 $openIds = CRM_Core_BAO_OpenID::allOpenIDs($contactId);
93
94 // check number of openids for the contact
9f8f650e 95 $this->assertCount(2, $openIds, 'Checking number of returned open-ids.');
6a488035
TO
96
97 // check first openid values
98 $this->assertEquals($openIdURLOne, $openIds[$openIdOne]['openid'],
99 'Confirm first openid value.'
100 );
101 $this->assertEquals(1, $openIds[$openIdOne]['is_primary'], 'Confirm is_primary field value.');
102 $this->assertEquals(1, $openIds[$openIdOne]['allowed_to_login'], 'Confirm allowed_to_login field value.');
103
104 // check second openid values
105 $this->assertEquals($openIdURLTwo, $openIds[$openIdTwo]['openid'],
106 'Confirm second openid value.'
107 );
108 $this->assertEquals(0, $openIds[$openIdTwo]['is_primary'], 'Confirm is_primary field value for second openid.');
109 $this->assertEquals(0, $openIds[$openIdTwo]['allowed_to_login'], 'Confirm allowed_to_login field value for second openid.');
6a488035 110 }
96025800 111
6a488035 112}