Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / OpenIDTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_openid_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_OpenIDTest extends CiviUnitTestCase {
20
21 protected $_apiversion = 3;
22 protected $_params;
23 protected $id;
24 protected $_entity;
25
26 public $DBResetRequired = FALSE;
27
28 public function setUp() {
29 parent::setUp();
30 $this->useTransaction(TRUE);
31
32 $this->_entity = 'OpenID';
33 $this->_contactID = $this->organizationCreate();
34 $this->_params = [
35 'contact_id' => $this->_contactID,
36 'openid' => 'My OpenID handle',
37 'location_type_id' => 1,
38 ];
39 }
40
41 /**
42 * @param int $version
43 * @dataProvider versionThreeAndFour
44 */
45 public function testCreateOpenID($version) {
46 $this->_apiversion = $version;
47 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
48 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
49 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
50 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
51 }
52
53 /**
54 * If no location is specified when creating a new openid, it should default to
55 * the LocationType default
56 *
57 * @param int $version
58 * @dataProvider versionThreeAndFour
59 */
60 public function testCreateOpenIDDefaultLocation($version) {
61 $this->_apiversion = $version;
62 $params = $this->_params;
63 unset($params['location_type_id']);
64 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
65 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
66 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
67 }
68
69 /**
70 * @param int $version
71 * @dataProvider versionThreeAndFour
72 */
73 public function testGetOpenID($version) {
74 $this->_apiversion = $version;
75 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
76 $result = $this->callAPIAndDocument($this->_entity, 'get', $this->_params, __FUNCTION__, __FILE__);
77 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
78 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
79 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
80 }
81
82 /**
83 * @param int $version
84 * @dataProvider versionThreeAndFour
85 */
86 public function testDeleteOpenID($version) {
87 $this->_apiversion = $version;
88 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
89 $deleteParams = ['id' => $result['id']];
90 $result = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
91 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
92 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
93 }
94
95 /**
96 * @param int $version
97 * @dataProvider versionThreeAndFour
98 */
99 public function testDeleteOpenIDInvalid($version) {
100 $this->_apiversion = $version;
101 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
102 $deleteParams = ['id' => 600];
103 $result = $this->callAPIFailure($this->_entity, 'delete', $deleteParams);
104 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
105 $this->assertEquals(1, $checkDeleted['count'], 'In line ' . __LINE__);
106 }
107
108 }