Merge pull request #17209 from civicrm/5.25
[civicrm-core.git] / tests / phpunit / CRMTraits / Profile / ProfileTrait.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 * Trait PriceSetTrait
14 *
15 * Trait for working with Price Sets in tests
16 */
17 trait CRMTraits_Profile_ProfileTrait {
18
19 /**
20 * Add a profile to a contribution page.
21 *
22 * @param array $joinParams
23 * Must contain entity_id at minimum.
24 * @param array $ufGroupParams
25 */
26 protected function createJoinedProfile($joinParams, $ufGroupParams = []) {
27 $profileID = $this->createProfile($ufGroupParams);
28 $joinParams = array_merge([
29 'uf_group_id' => $profileID,
30 'entity_table' => 'civicrm_contribution_page',
31 'weight' => 1,
32 ], $joinParams);
33 if (empty($joinParams['module'])) {
34 $joinParams['module'] = $joinParams['entity_table'] === 'civicrm_event' ? 'CiviEvent' : 'CiviContribute';
35 }
36 if ($joinParams['module'] !== 'CiviContribute' && empty($joinParams['module_data'])) {
37 $params['module_data'] = [$joinParams['module'] => []];
38 }
39 $this->callAPISuccess('UFJoin', 'create', $joinParams);
40 }
41
42 /**
43 * Create a profile.
44 *
45 * @param $ufGroupParams
46 *
47 * @return int
48 */
49 protected function createProfile($ufGroupParams) {
50 $profile = $this->callAPISuccess('UFGroup', 'create', array_merge([
51 'group_type' => 'Contact',
52 'title' => 'Back end title',
53 'frontend_title' => 'Public title',
54 'name' => 'our profile',
55
56 ], $ufGroupParams));
57 $this->ids['UFGroup'][$profile['values'][$profile['id']]['name']] = $profile['id'];
58
59 $this->callAPISuccess('UFField', 'create', [
60 'uf_group_id' => $profile['id'],
61 'field_name' => 'first_name',
62 ]);
63 return $profile['id'];
64 }
65
66 /**
67 * Ensure we don't have a profile with the id or one to ensure that we are not casting an array to it.
68 */
69 protected function eliminateUFGroupOne() {
70 $profileID = $this->createProfile(['name' => 'dummy_for_removing']);
71 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_join SET uf_group_id = $profileID WHERE uf_group_id = 1");
72 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_field SET uf_group_id = $profileID WHERE uf_group_id = 1");
73 CRM_Core_DAO::executeQuery('UPDATE civicrm_uf_group SET id = 900 WHERE id = 1');
74 $this->ids['UFGroup']['dummy'] = $profileID;
75 }
76
77 /**
78 * Bring back UF group one.
79 */
80 protected function restoreUFGroupOne() {
81 if (!isset($this->ids['UFGroup']['dummy'])) {
82 return;
83 }
84 $profileID = $this->ids['UFGroup']['dummy'];
85 CRM_Core_DAO::executeQuery('UPDATE civicrm_uf_group SET id = 1 WHERE id = 900');
86 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_join SET uf_group_id = 1 WHERE uf_group_id = $profileID");
87 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_field SET uf_group_id = 1 WHERE uf_group_id = $profileID");
88 }
89
90 }