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