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