Merge pull request #4785 from jitendrapurohit/CRM-15735
[civicrm-core.git] / tests / phpunit / WebTest / Contact / ProfileChecksumTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*/
26
6a488035 27require_once 'CiviTest/CiviSeleniumTestCase.php';
e9479dcf
EM
28
29/**
30 * Class WebTest_Contact_ProfileChecksumTest
31 */
6a488035
TO
32class WebTest_Contact_ProfileChecksumTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testProfileChecksum() {
42daf119 39 $this->webtestLogin('admin');
6a488035
TO
40
41 // Profile fields.
42 $fields = array(
43 'first_name' => array(
44 'type' => 'Individual',
45 'label' => 'First Name',
46 'default_value' => substr(sha1(rand()), 0, 7),
47 'update_value' => substr(sha1(rand()), 0, 7),
48 'element_name' => 'first_name',
49 ),
50 'last_name' => array(
51 'type' => 'Individual',
52 'label' => 'Last Name',
53 'default_value' => substr(sha1(rand()), 0, 7),
54 'update_value' => substr(sha1(rand()), 0, 7),
55 'element_name' => 'last_name',
56 ),
57 'email' => array(
58 'type' => 'Contact',
59 'label' => 'Email',
60 'location' => 0,
61 'default_value' => substr(sha1(rand()), 0, 5) . '@example.com',
62 'update_value' => substr(sha1(rand()), 0, 7) . '@example.com',
63 'element_name' => 'email-Primary',
64 ),
65 'city' => array(
66 'type' => 'Contact',
67 'label' => 'City',
68 'location' => 0,
69 'default_value' => substr(sha1(rand()), 0, 7),
70 'update_value' => substr(sha1(rand()), 0, 7),
71 'element_name' => 'city-Primary',
72 ),
6a488035
TO
73 'country' => array(
74 'type' => 'Contact',
75 'label' => 'Country',
76 'location' => 0,
77 'default_value' => '1228',
78 'update_value' => '1228',
79 'update_value_label' => 'United States',
80 'element_name' => 'country-Primary',
81 'html_type' => 'select',
82 ),
bf333c47
JP
83 'state_province' => array(
84 'type' => 'Contact',
85 'label' => 'State',
86 'location' => 0,
87 'default_value' => '1004',
88 'update_value' => '1031',
89 'update_value_label' => 'NY',
90 'element_name' => 'state_province-Primary',
91 'html_type' => 'select',
92 ),
6a488035
TO
93 );
94
95 // Create a contact.
96 $this->webtestAddContact($fields['first_name']['default_value'], $fields['last_name']['default_value'], $fields['email']['default_value']);
97
98 // Get contact id from url.
a471a3b6 99 $contactId = $this->urlArg('cid');
6a488035
TO
100
101 // Create profile for contact
102 $profileName = "Profile_" . substr(sha1(rand()), 0, 7);
103 $profileId = $this->_testCreateContactProfile($fields, $profileName);
104
105 // Check for profile create/edit permissions.
b5e4fcd9 106 $permission = array('edit-1-profile-edit', 'edit-1-profile-create', 'edit-1-access-all-custom-data', 'edit-1-edit-all-contacts');
6a488035
TO
107 $this->changePermissions($permission);
108
109 // Get checksum of the newly created contact.
6a488035
TO
110 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId);
111
42daf119
CW
112 // logout.
113 $this->webtestLogout();
6a488035
TO
114
115 // Go to edit profile page of the created contact.
b45c587e
CW
116 $this->openCiviPage("profile/edit", "id={$contactId}&gid={$profileId}&reset=1&cs={$cs}", NULL);
117 $this->waitForTextPresent($profileName);
6a488035
TO
118
119 // Check all profile fields, update their values.
120 foreach ($fields as $field) {
121 $this->assertTrue($this->isElementPresent($field['element_name']), "Missing Field: {$field['label']}.");
122 if (isset($field['html_type']) && $field['html_type'] == 'select') {
bf333c47 123 $this->waitForVisible($field['element_name']);
6a488035
TO
124 $this->select($field['element_name'], "value={$field['update_value']}");
125 }
126 else {
127 $this->type($field['element_name'], $field['update_value']);
128 }
129 }
130 // Save profile.
131 $this->click("_qf_Edit_next");
132 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
133
134 // Check profile view page.
b45c587e 135 $this->waitForTextPresent($profileName);
6a488035
TO
136
137 // Check updated values of all fields.
138 $checkFieldValues = array();
139 foreach ($fields as $field) {
140 $checkFieldValues[] = isset($field['update_value_label']) ? $field['update_value_label'] : $field['update_value'];
141 }
142 $this->assertStringsPresent($checkFieldValues);
143 }
144
4cbe18b8
EM
145 /**
146 * @param $fields
100fef9d 147 * @param string $profileName
4cbe18b8
EM
148 *
149 * @return null
150 */
6a488035 151 function _testCreateContactProfile($fields, $profileName) {
42daf119 152 // Add new profile.
071a6d2e 153 $this->openCiviPage("admin/uf/group", "reset=1");
6a488035
TO
154 $this->click('newCiviCRMProfile-top');
155 $this->waitForElementPresent('_qf_Group_next-top');
156
157 // Name of the profile.
158 $this->type('title', $profileName);
b5e4fcd9 159 $this->click('uf_group_type_Profile');
6a488035
TO
160 $this->click('_qf_Group_next-top');
161 $this->waitForPageToLoad($this->getTimeoutMsec());
a471a3b6 162 $profileId = $this->urlArg('gid');
6a488035
TO
163
164 // Add field to the profile.
165 foreach ($fields as $key => $values) {
071a6d2e 166 $this->openCiviPage("admin/uf/group/field/add", "reset=1&action=add&gid=$profileId");
6a488035
TO
167
168 $this->select("field_name[0]", "value={$values['type']}");
efb29358
CW
169 // Because it tends to cause problems, all uses of sleep() must be justified in comments
170 // Sleep should never be used for wait for anything to load from the server
171 // Justification for this instance: FIXME
6a488035
TO
172 sleep(1);
173 $this->select("field_name[1]", "value={$key}");
174 if (isset($values['location'])) {
efb29358
CW
175 // Because it tends to cause problems, all uses of sleep() must be justified in comments
176 // Sleep should never be used for wait for anything to load from the server
177 // Justification for this instance: FIXME
6a488035
TO
178 sleep(1);
179 $this->select("field_name[2]", "value={$values['location']}");
180 }
181 $this->type("label", $values['label']);
182 $this->click('_qf_Field_next-top');
183 $this->waitForPageToLoad($this->getTimeoutMsec());
184 }
185 return $profileId;
186 }
187}
188