Merge pull request #2927 from pratik-joshi/CRM-14269
[civicrm-core.git] / tests / phpunit / WebTest / Contact / GroupAddTest.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_GroupAddTest extends CiviSeleniumTestCase {
29
30 protected function setUp() {
31 parent::setUp();
32 }
33
34 function testGroupAdd() {
35 $this->webtestLogin();
36
37 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload-bottom');
38
39 // Group name
40 $params = array('name' => 'group_' . substr(sha1(rand()), 0, 7));
41
42 // fill group name
43 $this->type("title", $params['name']);
44
45 // fill description
46 $this->type("description", "Adding new group.");
47
48 // check Access Control
49 if (isset($params['type1']) && $params['type1'] !== FALSE) {
50 $this->click("group_type[1]");
51 }
52
53 // check Mailing List
54 if (isset($params['type2']) && $params['type2'] !== FALSE) {
55 $this->click("group_type[2]");
56 }
57
58 // select Visibility as Public Pages
59 $params['visibility'] = 'Public Pages';
60
61 $this->select("visibility", "value={$params['visibility']}");
62
63 // Clicking save.
64 $this->click("_qf_Edit_upload");
65 $this->waitForPageToLoad($this->getTimeoutMsec());
66
67 // Is status message correct?
68 $this->waitForText('crm-notification-container', "The Group '{$params['name']}' has been saved.");
69
70 $this->openCiviPage('group', 'reset=1');
71 $this->type('title', $params['name']);
72 $this->click('_qf_Search_refresh');
73 $this->waitForElementPresent("xpath=//table[@id='crm-group-selector']/tbody/tr/td[contains(., '{$params['name']}')]");
74 $createdBy = $this->getText("xpath=//table[@id='crm-group-selector']/tbody/tr/td[3]/a");
75 $this->click("xpath=//table[@id='crm-group-selector']/tbody/tr/td[7]/span/a[2]");
76 $this->waitForElementPresent("xpath=//form[@id='Edit']/div[2]/div/table[2]/tbody/tr/td[2]/select");
77
78 //assert created by in the edit page
79 $this->assertTrue($this->isElementPresent(
80 "xpath=//form[@id='Edit']/div[2]/div/table/tbody/tr[2]/td[contains(text(), '{$createdBy}')]"));
81 $this->openCiviPage('group', 'reset=1');
82
83 //search groups using created by
84 $this->type('created_by', $createdBy);
85 $this->click('_qf_Search_refresh');
86
87 //show maximum no. of groups on first result set page
88 //as many groups can be created by same creator
89 //and checking is done on first result set page
90 $this->waitForVisible('crm-group-selector_processing');
91 $this->select("xpath=//select[@name='crm-group-selector_length']", '100');
92 $this->waitForVisible('crm-group-selector_processing');
93
94 $this->waitForElementPresent("xpath=//table[@id='crm-group-selector']/tbody/tr/td[contains(., '{$params['name']}')]");
95 $this->assertTrue($this->isElementPresent("xpath=//table[@id='crm-group-selector']/tbody/tr/td[contains(., '{$params['name']}')]/following-sibling::td[2]/a[text()='{$createdBy}']"));
96
97 //check link of the contact who created the group
98 $this->click("xpath=//table[@id='crm-group-selector']/tbody//tr/td[1][contains(.,'{$params['name']}')]/following-sibling::td[2]/a");
99 $this->waitForPageToLoad($this->getTimeoutMsec());
100 $name = explode(',', $createdBy);
101 $name1 = isset($name[1]) ? trim($name[1]) : NULL;
102 $name0 = trim($name[0]);
103 $displayName = isset($name1) ? "{$name1} {$name0}" : "{$name0}";
104 $this->assertElementContainsText("css=div.crm-summary-display_name", $displayName);
105 }
106
107 function testGroupReserved() {
108 $this->webtestLogin('admin');
109
110 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload-bottom');
111
112 // take group name
113 $params = array('name' => 'group_' . substr(sha1(rand()), 0, 7));
114
115 // fill group name
116 $this->type("title", $params['name']);
117
118 // fill description
119 $this->type("description", "Adding new reserved group.");
120
121 // check Access Control
122 if (isset($params['type1']) && $params['type1'] !== FALSE) {
123 $this->click("group_type[1]");
124 }
125
126 // check Mailing List
127 if (isset($params['type2']) && $params['type2'] !== FALSE) {
128 $this->click("group_type[2]");
129 }
130
131 // select Visibility as Public Pages
132 if (empty($params['visibility'])) {
133 $params['visibility'] = 'Public Pages';
134 }
135
136 $this->select("visibility", "value={$params['visibility']}");
137
138 // Check Reserved box
139 $this->click("is_reserved");
140
141 // Clicking save.
142 $this->click("_qf_Edit_upload");
143 $this->waitForPageToLoad($this->getTimeoutMsec());
144
145 // Is status message correct?
146 $this->assertTrue($this->isTextPresent("The Group '{$params['name']}' has been saved."));
147
148 // Create a new role w/o reserved group permissions
149 $role = 'role' . substr(sha1(rand()), 0, 7);
150 $this->open($this->sboxPath . "admin/people/permissions/roles");
151
152 $this->waitForElementPresent("edit-add");
153 $this->type("edit-name", $role);
154 $this->click("edit-add");
155 $this->waitForPageToLoad($this->getTimeoutMsec());
156
157 $this->open($this->sboxPath . "admin/people/permissions/roles");
158 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
159 $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
160 $roleId = end($roleId);
161 $user = $this->_testCreateUser($roleId);
162 $permissions = array(
163 "edit-{$roleId}-view-all-contacts",
164 "edit-{$roleId}-access-civicrm",
165 );
166 $this->changePermissions($permissions);
167
168 // Now logout as admin, login as regular user and verify that Group settings,
169 // delete and disable links are not available
170 $this->webtestLogin($user, 'Test12345');
171
172 $this->openCiviPage('group', 'reset=1');
173 $this->type('title', $params['name']);
174 $this->click('_qf_Search_refresh');
175 $this->waitForTextPresent("Adding new reserved group.");
176 // Settings link should NOT be included in selector
177 // after search returns with only the reserved group.
178 $this->assertElementNotContainsText("css=td.crm-group-group_links", "Settings");
179
180 //login as admin and delete the role
181 $this->webtestLogin('admin');
182 $this->open($this->sboxPath . "admin/people/permissions/roles");
183 $this->_roleDelete($role);
184 }
185
186 function _testCreateUser($roleid) {
187 $this->open($this->sboxPath . "admin/people/create");
188
189 $this->waitForElementPresent("edit-submit");
190
191 $name = "TestUser" . substr(sha1(rand()), 0, 4);
192 $this->type("edit-name", $name);
193
194 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
195 $this->type("edit-mail", $emailId);
196 $this->type("edit-pass-pass1", "Test12345");
197 $this->type("edit-pass-pass2", "Test12345");
198 $role = "edit-roles-" . $roleid;
199 $this->check("name=roles[$roleid] value={$roleid}");
200
201 //Add profile Details
202 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
203 $lastName = 'An' . substr(sha1(rand()), 0, 7);
204
205 $this->type("first_name", $firstName);
206 $this->type("last_name", $lastName);
207
208 //Address Details
209 $this->type("street_address-1", "902C El Camino Way SW");
210 $this->type("city-1", "Dumfries");
211 $this->type("postal_code-1", "1234");
212 $this->select("state_province-1", "value=1019");
213
214 $this->click("edit-submit");
215 $this->waitForPageToLoad($this->getTimeoutMsec());
216 return $name;
217 }
218
219 function _roleDelete($role) {
220 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']");
221 $this->click("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']");
222 $this->waitForElementPresent('edit-delete');
223 $this->click('edit-delete');
224 $this->waitForPageToLoad($this->getTimeoutMsec());
225 $this->click("edit-submit");
226 $this->waitForTextPresent("The role has been deleted.");
227 }
228 }