Merge pull request #14919 from eileenmcnaughton/mem_review
[civicrm-core.git] / tests / phpunit / CRMTraits / Profile / ProfileTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Trait PriceSetTrait
30 *
31 * Trait for working with Price Sets in tests
32 */
33 trait CRMTraits_Profile_ProfileTrait {
34
35 /**
36 * Add a profile to a contribution page.
37 *
38 * @param array $joinParams
39 * Must contain entity_id at minimum.
40 * @param array $ufGroupParams
41 */
42 protected function createJoinedProfile($joinParams, $ufGroupParams = []) {
43 $profileID = $this->createProfile($ufGroupParams);
44 $joinParams = array_merge([
45 'uf_group_id' => $profileID,
46 'entity_table' => 'civicrm_contribution_page',
47 'weight' => 1,
48 ], $joinParams);
49 if (empty($joinParams['module'])) {
50 $joinParams['module'] = $joinParams['entity_table'] === 'civicrm_event' ? 'CiviEvent' : 'CiviContribute';
51 }
52 if ($joinParams['module'] !== 'CiviContribute' && empty($joinParams['module_data'])) {
53 $params['module_data'] = [$joinParams['module'] => []];
54 }
55 $this->callAPISuccess('UFJoin', 'create', $joinParams);
56 }
57
58 /**
59 * Create a profile.
60 *
61 * @param $ufGroupParams
62 *
63 * @return int
64 */
65 protected function createProfile($ufGroupParams) {
66 $profile = $this->callAPISuccess('UFGroup', 'create', array_merge([
67 'group_type' => 'Contact',
68 'title' => 'Back end title',
69 'frontend_title' => 'Public title',
70 'name' => 'our profile',
71
72 ], $ufGroupParams));
73 $this->ids['UFGroup'][$profile['values'][$profile['id']]['name']] = $profile['id'];
74
75 $this->callAPISuccess('UFField', 'create', [
76 'uf_group_id' => $profile['id'],
77 'field_name' => 'first_name',
78 ]);
79 return $profile['id'];
80 }
81
82 /**
83 * Ensure we don't have a profile with the id or one to ensure that we are not casting an array to it.
84 */
85 protected function eliminateUFGroupOne() {
86 $profileID = $this->createProfile(['name' => 'dummy_for_removing']);
87 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_join SET uf_group_id = $profileID WHERE uf_group_id = 1");
88 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_field SET uf_group_id = $profileID WHERE uf_group_id = 1");
89 CRM_Core_DAO::executeQuery('UPDATE civicrm_uf_group SET id = 900 WHERE id = 1');
90 $this->ids['UFGroup']['dummy'] = $profileID;
91 }
92
93 /**
94 * Bring back UF group one.
95 */
96 protected function restoreUFGroupOne() {
97 if (!isset($this->ids['UFGroup']['dummy'])) {
98 return;
99 }
100 $profileID = $this->ids['UFGroup']['dummy'];
101 CRM_Core_DAO::executeQuery('UPDATE civicrm_uf_group SET id = 1 WHERE id = 900');
102 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_join SET uf_group_id = 1 WHERE uf_group_id = $profileID");
103 CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_field SET uf_group_id = 1 WHERE uf_group_id = $profileID");
104 }
105
106 }