Merge pull request #9773 from monishdeb/CRM-19663
[civicrm-core.git] / tests / phpunit / WebTest / Contact / ContactReferenceFieldTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
29 /**
30 * Class WebTest_Contact_ContactReferenceFieldTest
31 */
32 class WebTest_Contact_ContactReferenceFieldTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testContactReferenceField() {
39 $this->webtestLogin();
40
41 /* add new group */
42
43 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload');
44
45 $groupName = 'group_' . substr(sha1(rand()), 0, 7);
46 $this->type("title", $groupName);
47
48 // fill description
49 $this->type("description", "Adding new group.");
50
51 // check Access Control
52 $this->click("group_type[1]");
53
54 // Clicking save.
55 $this->click("_qf_Edit_upload");
56 $this->waitForPageToLoad($this->getTimeoutMsec());
57
58 /* add contacts */
59
60 // Individual 1
61 $contact1 = substr(sha1(rand()), 0, 7);
62 $this->webtestAddContact($contact1, "Anderson", "{$contact1}@example.com");
63
64 // Add Individual 1 to group
65 $this->click('css=li#tab_group a');
66 $this->waitForElementPresent('_qf_GroupContact_next');
67 $this->select('group_id', "label={$groupName}");
68 $this->click('_qf_GroupContact_next');
69 $this->waitForText('crm-notification-container', "Added to Group");
70
71 // Individual 1
72 $contact2 = substr(sha1(rand()), 0, 7);
73 $this->webtestAddContact($contact2, "Anderson", "{$contact2}@example.com");
74
75 // Organization 1
76 $org1 = 'org_' . substr(sha1(rand()), 0, 5);
77 $this->webtestAddOrganization($org1, "{$org1}@example.com");
78
79 /* create custom group and fields */
80
81 // Add Custom group //
82
83 $this->openCiviPage('admin/custom/group', 'action=add&reset=1');
84
85 //fill custom group title
86 $customGroupTitle = 'custom_' . substr(sha1(rand()), 0, 7);
87 $this->click("title");
88 $this->type("title", $customGroupTitle);
89
90 //custom group extends
91 $this->click("extends[0]");
92 $this->select("extends[0]", "value=Contact");
93 $this->click("//option[@value='Contact']");
94 $this->click("_qf_Group_next-bottom");
95 $this->waitForElementPresent("newCustomField");
96
97 //Is custom group created?
98 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.");
99
100 $customGroupId = $this->urlArg('gid');
101
102 // Add contact reference fields
103 $contactRefFieldLabel1 = 'contact_ref_' . substr(sha1(rand()), 0, 4);
104 $this->waitForElementPresent("label");
105 $this->click("label");
106 $this->waitForElementPresent("label");
107 $this->type("label", $contactRefFieldLabel1);
108 $this->waitForElementPresent("data_type[0]");
109 $this->select("data_type[0]", "label=Contact Reference");
110
111 $this->waitForElementPresent("group_id");
112 $this->select("group_id", $groupName);
113
114 //clicking save
115 $this->click("xpath=//div[@class='ui-dialog-buttonset']/button[2]/span[2]/");
116
117 //Is custom field created?
118 $this->waitForText('crm-notification-container', "Custom field '$contactRefFieldLabel1' has been saved.");
119 $this->waitForElementPresent("xpath=//div[@class='ui-dialog-buttonset']/button[3]/span[2]/");
120 //add custom field - alphanumeric checkbox
121 $contactRefFieldLabel2 = 'contact_ref_' . substr(sha1(rand()), 0, 4);
122
123 $this->click("label");
124 $this->waitForElementPresent("label");
125 $this->type("label", $contactRefFieldLabel2);
126 $this->waitForElementPresent("data_type[0]");
127 $this->select("data_type[0]", "label=Contact Reference");
128
129 $this->waitForElementPresent("group_id");
130 $this->click("xpath=//form[@id='Field']//a[text()='Advanced Filter']");
131 $this->waitForElementPresent("filter");
132
133 $this->type("filter", "action=get&contact_type=Organization");
134
135 //clicking save
136 $this->click("xpath=//div[@class='ui-dialog-buttonset']/button[1]/span[2]/");
137
138 //Is custom field created?
139 $this->waitForText('crm-notification-container', "Custom field '$contactRefFieldLabel2' has been saved.");
140
141 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$customGroupId}");
142
143 $fieldid1 = explode("&id=", $this->getAttribute("xpath=//div[@id='field_page']//table/tbody/tr[1]/td[8]/span[1]/a[text()='Edit Field']@href"));
144 $fieldid1 = $fieldid1[1];
145
146 $contactRefFieldID1 = $fieldid1;
147
148 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$customGroupId}");
149
150 $fieldid2 = explode("&id=", $this->getAttribute("xpath=//div[@id='field_page']//table/tbody/tr[2]/td[8]/span[1]/a[text()='Edit Field']@href"));
151 $fieldid2 = $fieldid2[1];
152
153 $contactRefFieldID2 = $fieldid2;
154
155 // Visit custom group preview page
156 $this->openCiviPage('admin/custom/group', "action=preview&reset=1&id={$customGroupId}");
157
158 $this->clickAt("//*[@id='custom_{$contactRefFieldID1}_-1']/../div/a");
159 $this->keyDown("//*[@id='select2-drop']/div/input", " ");
160 $this->type("//*[@id='select2-drop']/div/input", "Anderson");
161 $this->typeKeys("//*[@id='select2-drop']/div/input", "Anderson");
162 $this->waitForElementPresent("css=div.select2-result-label span");
163 $this->assertElementContainsText("css=div.select2-result-label", "{$contact1}@example.com");
164 $this->assertElementNotContainsText("css=div.select2-result-label", "{$contact2}@example.com");
165
166 $this->openCiviPage('admin/custom/group', "action=preview&reset=1&id={$customGroupId}");
167
168 $this->clickAt("//*[@id='custom_{$contactRefFieldID2}_-1']/../div/a");
169 $this->keyDown("//*[@id='select2-drop']/div/input", " ");
170 $this->type("//*[@id='select2-drop']/div/input", $org1);
171 $this->typeKeys("//*[@id='select2-drop']/div/input", $org1);
172 $this->waitForElementPresent("css=div.select2-result-label");
173 $this->assertElementContainsText("css=div.select2-result-label", "{$org1}@example.com");
174 }
175
176 }