Global webtest cleanup
[civicrm-core.git] / tests / phpunit / WebTest / Contact / ProfileChecksumTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
28require_once 'CiviTest/CiviSeleniumTestCase.php';
29class WebTest_Contact_ProfileChecksumTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testProfileChecksum() {
42daf119 36 $this->webtestLogin('admin');
6a488035
TO
37
38 // Profile fields.
39 $fields = array(
40 'first_name' => array(
41 'type' => 'Individual',
42 'label' => 'First Name',
43 'default_value' => substr(sha1(rand()), 0, 7),
44 'update_value' => substr(sha1(rand()), 0, 7),
45 'element_name' => 'first_name',
46 ),
47 'last_name' => array(
48 'type' => 'Individual',
49 'label' => 'Last Name',
50 'default_value' => substr(sha1(rand()), 0, 7),
51 'update_value' => substr(sha1(rand()), 0, 7),
52 'element_name' => 'last_name',
53 ),
54 'email' => array(
55 'type' => 'Contact',
56 'label' => 'Email',
57 'location' => 0,
58 'default_value' => substr(sha1(rand()), 0, 5) . '@example.com',
59 'update_value' => substr(sha1(rand()), 0, 7) . '@example.com',
60 'element_name' => 'email-Primary',
61 ),
62 'city' => array(
63 'type' => 'Contact',
64 'label' => 'City',
65 'location' => 0,
66 'default_value' => substr(sha1(rand()), 0, 7),
67 'update_value' => substr(sha1(rand()), 0, 7),
68 'element_name' => 'city-Primary',
69 ),
70 'state_province' => array(
71 'type' => 'Contact',
72 'label' => 'State',
73 'location' => 0,
74 'default_value' => '1004',
75 'update_value' => '1031',
76 'update_value_label' => 'NY',
77 'element_name' => 'state_province-Primary',
78 'html_type' => 'select',
79 ),
80 'country' => array(
81 'type' => 'Contact',
82 'label' => 'Country',
83 'location' => 0,
84 'default_value' => '1228',
85 'update_value' => '1228',
86 'update_value_label' => 'United States',
87 'element_name' => 'country-Primary',
88 'html_type' => 'select',
89 ),
90 );
91
92 // Create a contact.
93 $this->webtestAddContact($fields['first_name']['default_value'], $fields['last_name']['default_value'], $fields['email']['default_value']);
94
95 // Get contact id from url.
96 $matches = array();
97 preg_match('/cid=([0-9]+)/', $this->getLocation(), $matches);
98 $contactId = $matches[1];
99
100 // Create profile for contact
101 $profileName = "Profile_" . substr(sha1(rand()), 0, 7);
102 $profileId = $this->_testCreateContactProfile($fields, $profileName);
103
104 // Check for profile create/edit permissions.
105 $permission = array('edit-1-profile-edit', 'edit-1-profile-create', 'edit-1-access-all-custom-data');
106 $this->changePermissions($permission);
107
108 // Get checksum of the newly created contact.
6a488035
TO
109 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId);
110
42daf119
CW
111 // logout.
112 $this->webtestLogout();
6a488035
TO
113
114 // Go to edit profile page of the created contact.
115 $this->open($this->sboxPath . "civicrm/profile/edit?id={$contactId}&gid={$profileId}&reset=1&cs={$cs}");
116 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
117 $this->assertStringsPresent(array($profileName));
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') {
123 $this->select($field['element_name'], "value={$field['update_value']}");
124 }
125 else {
126 $this->type($field['element_name'], $field['update_value']);
127 }
128 }
129 // Save profile.
130 $this->click("_qf_Edit_next");
131 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
132
133 // Check profile view page.
134 $this->assertStringsPresent(array($profileName));
135
136 // Check updated values of all fields.
137 $checkFieldValues = array();
138 foreach ($fields as $field) {
139 $checkFieldValues[] = isset($field['update_value_label']) ? $field['update_value_label'] : $field['update_value'];
140 }
141 $this->assertStringsPresent($checkFieldValues);
142 }
143
144 function _testCreateContactProfile($fields, $profileName) {
42daf119 145 // Add new profile.
6a488035
TO
146 $this->open($this->sboxPath . 'civicrm/admin/uf/group?reset=1');
147 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
148 $this->click('newCiviCRMProfile-top');
149 $this->waitForElementPresent('_qf_Group_next-top');
150
151 // Name of the profile.
152 $this->type('title', $profileName);
153 $this->click('_qf_Group_next-top');
154 $this->waitForPageToLoad($this->getTimeoutMsec());
155 $elements = $this->parseURL();
156 $profileId = $elements['queryString']['gid'];
157
158 // Add field to the profile.
159 foreach ($fields as $key => $values) {
160 $this->open($this->sboxPath . 'civicrm/admin/uf/group/field/add?reset=1&action=add&gid=' . $profileId);
161 $this->waitForPageToLoad($this->getTimeoutMsec());
162
163 $this->select("field_name[0]", "value={$values['type']}");
164 sleep(1);
165 $this->select("field_name[1]", "value={$key}");
166 if (isset($values['location'])) {
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