Merge pull request #4851 from totten/master-createtest-bao
[civicrm-core.git] / tests / phpunit / WebTest / Profile / ProfileCountryState.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
29 /**
30 * Class WebTest_Profile_ProfileCountryState
31 */
32 class WebTest_Profile_ProfileCountryState extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testStateCountry() {
39 $this->webtestLogin();
40 $config = CRM_Core_Config::singleton();
41 // Add new profile.
42 $this->openCiviPage('admin/uf/group', 'reset=1');
43 $this->click('newCiviCRMProfile-top');
44 $this->waitForElementPresent('_qf_Group_next-bottom');
45
46 //Name of profile
47 $profileTitle = 'Country state province web test temp';
48 $this->type('title', $profileTitle);
49
50 // Standalone form or directory
51 $this->click('uf_group_type_Profile');
52
53 //click on save
54 $this->click('_qf_Group_next');
55 $this->waitForPageToLoad($this->getTimeoutMsec());
56
57 //check for profile create
58 $this->waitForText('crm-notification-container', "Profile '{$profileTitle}' has been added. You can add fields to this profile now.");
59 $gid = $this->urlArg('gid');
60
61 //Add Country field to profile
62 $this->openCiviPage('admin/uf/group/field/add', array('action' => 'add', 'reset' => 1, 'gid' => $gid), 'field_name[0]');
63 $this->click('field_name[0]');
64 $this->select('field_name[0]', 'value=Contact');
65 $this->click("//option[@value='Contact']");
66 $this->click('field_name[1]');
67 $this->select('field_name[1]', 'value=country');
68 $this->click("//option[@value='country']");
69 $this->click('is_required');
70
71 $this->click('_qf_Field_next_new');
72 $this->waitForPageToLoad($this->getTimeoutMsec());
73
74 //Add State field to profile
75 $this->click('field_name[0]');
76 $this->select('field_name[0]', 'value=Contact');
77 $this->click("//option[@value='Contact']");
78 $this->click('field_name[1]');
79 $this->select('field_name[1]', 'value=state_province');
80 $this->click("xpath=//select[@id='field_name_1']/option[@value='state_province']");
81 $this->click('is_required');
82 //click on save
83 $this->click('_qf_Field_next');
84 $this->waitForPageToLoad($this->getTimeoutMsec());
85
86 if ($gid) {
87 $this->openCiviPage('admin/setting/localization', 'reset=1', '_qf_Localization_next-bottom');
88 $country = array(1001 => 'Afghanistan', 1013 => 'Australia', 1039 => 'Canada', 1101 => 'India');
89 $enabledCountries = $this->getSelectOptions("countryLimit-t");
90 $enabledStates = $this->getSelectOptions("provinceLimit-t");
91 $newCountry = array();
92 foreach ($country as $countryID => $countryName) {
93 if (!in_array($countryName, $enabledCountries)) {
94 $newCountry[$countryID] = $countryName;
95 $this->addSelection("countryLimit-f", "label=$countryName");
96 $this->click("xpath=//select[@id='countryLimit-f']/option[@value='$countryID']");
97 $this->click("xpath=//tr[@class='crm-localization-form-block-countryLimit']/td[2]/table//tbody/tr/td[2]/input[@name='add']");
98 }
99 if (!in_array($countryName, $enabledStates)) {
100 $this->addSelection("provinceLimit-f", "label=$countryName");
101 $this->click("//option[@value='$countryID']");
102 $this->click("xpath=//tr[@class='crm-localization-form-block-provinceLimit']/td[2]/table//tbody/tr/td[2]/input[@name='add']");
103 }
104 $added = TRUE;
105 }
106 if ($added) {
107 $this->click("_qf_Localization_next-bottom");
108 $this->waitForPageToLoad($this->getTimeoutMsec());
109 $this->waitForText('crm-notification-container', "Saved");
110 }
111 $this->openCiviPage("profile/create", "gid=$gid&reset=1", NULL);
112
113 $this->waitForElementPresent("xpath=//form[@id='Edit']/div[2]/div/div/div[2]/select");
114 $this->click("xpath=//form[@id='Edit']/div[2]/div/div/div[2]/select");
115 $countryID = array_rand($country);
116 $states = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID, 'id');
117 $stateID = array_rand($states);
118 $this->select("xpath=//form[@id='Edit']/div[2]/div/div/div[2]/select", "value=$countryID");
119 $this->waitForElementPresent("xpath=//form[@id='Edit']/div[2]/div/div[2]/div[2]/select/option[@value=$stateID]");
120 $this->click("xpath=//form[@id='Edit']/div[2]/div/div[2]/div[2]/select");
121 $this->select("xpath=//form[@id='Edit']/div[2]/div/div[2]/div[2]/select", "value=$stateID");
122 $this->clickLink('_qf_Edit_next', NULL);
123
124 // Delete profile
125 $this->openCiviPage('admin/uf/group', 'action=delete&id=' . $gid, '_qf_Group_next-bottom');
126 $this->clickLink('_qf_Group_next-bottom', 'newCiviCRMProfile-bottom');
127 $this->waitForText('crm-notification-container', "Profile '{$profileTitle}' has been deleted.");
128
129 $this->openCiviPage("admin/setting/localization", "reset=1", "_qf_Localization_next-bottom");
130 $enabledCountries = $this->getSelectOptions("countryLimit-t");
131 $enabledStates = $this->getSelectOptions("provinceLimit-t");
132 $removed = FALSE;
133 foreach ($newCountry as $countryID => $countryName) {
134 $this->addSelection("countryLimit-t", "label=$countryName");
135 $this->click("xpath=//select[@id='countryLimit-t']/option[@value='$countryID']");
136 $this->click("xpath=//tr[@class='crm-localization-form-block-countryLimit']/td[2]/table//tbody/tr/td[2]/input[@name='remove']");
137
138 $this->addSelection("provinceLimit-t", "label=$countryName");
139 $this->click("//option[@value='$countryID']");
140 $this->click("xpath=//tr[@class='crm-localization-form-block-provinceLimit']/td[2]/table//tbody/tr/td[2]/input[@name='remove']");
141 $removed = TRUE;
142 }
143 if ($removed) {
144 $this->click("_qf_Localization_next-bottom");
145 $this->waitForPageToLoad($this->getTimeoutMsec());
146 $this->waitForText('crm-notification-container', "Saved");
147 }
148 }
149 }
150 }