| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 'WebTest/Import/ImportCiviSeleniumTestCase.php'; |
| 28 | |
| 29 | /** |
| 30 | * Class WebTest_Import_ContactCustomDataTest |
| 31 | */ |
| 32 | class WebTest_Import_ContactCustomDataTest extends ImportCiviSeleniumTestCase { |
| 33 | |
| 34 | protected function setUp() { |
| 35 | parent::setUp(); |
| 36 | } |
| 37 | |
| 38 | public function testCustomDataImport() { |
| 39 | $this->webtestLogin(); |
| 40 | |
| 41 | $firstName1 = 'Ma_' . substr(sha1(rand()), 0, 7); |
| 42 | // Add a custom group and custom field |
| 43 | $customDataParams = $this->_addCustomData(); |
| 44 | |
| 45 | // Add New Strict Rule |
| 46 | $newRuleTitle = 'IndividualStrict_' . substr(sha1(rand()), 0, 7); |
| 47 | $this->openCiviPage("contact/deduperules", "reset=1"); |
| 48 | |
| 49 | $this->click("xpath=//div[@id='browseValues_Individual']/div[2]/a/span"); |
| 50 | $this->waitForElementPresent('_qf_DedupeRules_next-bottom'); |
| 51 | $this->type('title', $newRuleTitle); |
| 52 | $this->click("CIVICRM_QFID_Supervised_used"); |
| 53 | $this->select("where_0", "label=$customDataParams[1]"); |
| 54 | $this->type('weight_0', '10'); |
| 55 | $this->type('threshold', '10'); |
| 56 | $this->click('_qf_DedupeRules_next-bottom'); |
| 57 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 58 | $this->waitForText("crm-notification-container", "The rule '{$newRuleTitle}' has been saved."); |
| 59 | |
| 60 | $rgId = explode('&rgid=', $this->getAttribute("xpath=//div[@id='browseValues_Individual']/div/div/table/tbody//tr/td[text()='{$newRuleTitle}']/../td[3]/span/a[1][text()='Use Rule']@href")); |
| 61 | $rgId = explode('&', $rgId[1]); |
| 62 | |
| 63 | // Add Contact |
| 64 | $firstName2 = 'An_' . substr(sha1(rand()), 0, 7); |
| 65 | $this->webtestAddContact($firstName2, "Summerson"); |
| 66 | |
| 67 | // Edit and expand all tabs |
| 68 | $this->click('link=Edit'); |
| 69 | $this->waitForElementPresent('_qf_Contact_cancel'); |
| 70 | $this->click('link=Expand all tabs'); |
| 71 | |
| 72 | // Fill custom data |
| 73 | $this->waitForElementPresent("{$customDataParams[0]}_1"); |
| 74 | $this->type("{$customDataParams[0]}_1", 'This is a test field'); |
| 75 | $this->click('_qf_Contact_upload_view'); |
| 76 | |
| 77 | // Get sample import data. |
| 78 | list($headers, $rows) = $this->_individualCustomCSVData($customDataParams, $firstName1); |
| 79 | |
| 80 | // Import and check Individual contacts in Skip mode. |
| 81 | $other = array( |
| 82 | 'saveMapping' => TRUE, |
| 83 | 'callbackImportSummary' => 'checkDuplicateContacts', |
| 84 | 'dedupe' => $rgId[0], |
| 85 | ); |
| 86 | |
| 87 | // Check duplicates |
| 88 | $this->importContacts($headers, $rows, 'Individual', 'Skip', array(), $other); |
| 89 | |
| 90 | // Import without duplicate checking |
| 91 | $other = array('saveMapping' => TRUE); |
| 92 | $this->importContacts($headers, $rows, 'Individual', 'No Duplicate Checking', array(), $other); |
| 93 | |
| 94 | // Type search name in autocomplete. |
| 95 | $this->click('sort_name_navigation'); |
| 96 | $this->type('css=input#sort_name_navigation', $firstName1); |
| 97 | $this->typeKeys('css=input#sort_name_navigation', $firstName1); |
| 98 | |
| 99 | // Wait for result list. |
| 100 | $this->waitForElementPresent("css=ul.ui-autocomplete li"); |
| 101 | |
| 102 | // Visit contact summary page. |
| 103 | $this->click("css=ul.ui-autocomplete li"); |
| 104 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 105 | $this->assertTrue($this->isTextPresent('This is a test field')); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Helper function to provide data for custom data import. |
| 110 | * |
| 111 | * @param array $customDataParams |
| 112 | * @param string $firstName1 |
| 113 | * |
| 114 | * @return array |
| 115 | */ |
| 116 | public function _individualCustomCSVData($customDataParams, $firstName1) { |
| 117 | $headers = array( |
| 118 | 'first_name' => 'First Name', |
| 119 | 'last_name' => 'Last Name', |
| 120 | "custom_{$customDataParams[0]}" => "{$customDataParams[1]} :: {$customDataParams[2]}", |
| 121 | ); |
| 122 | |
| 123 | $rows = array( |
| 124 | array( |
| 125 | 'first_name' => $firstName1, |
| 126 | 'last_name' => 'Anderson', |
| 127 | "custom_{$customDataParams[0]}" => 'This is a test field', |
| 128 | ), |
| 129 | ); |
| 130 | |
| 131 | return array($headers, $rows); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param $originalHeaders |
| 136 | * @param $originalRows |
| 137 | * @param $checkSummary |
| 138 | */ |
| 139 | public function checkDuplicateContacts($originalHeaders, $originalRows, $checkSummary) { |
| 140 | $this->assertTrue($this->isTextPresent('CiviCRM has detected one record which is a duplicate of existing CiviCRM contact record. These records have not been imported.')); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return array |
| 145 | */ |
| 146 | public function _addCustomData() { |
| 147 | |
| 148 | $this->openCiviPage("admin/custom/group", "reset=1"); |
| 149 | |
| 150 | //add new custom data |
| 151 | $this->click("//a[@id='newCustomDataGroup']/span"); |
| 152 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 153 | |
| 154 | //fill custom group title |
| 155 | $customGroupTitle = 'Custom ' . substr(sha1(rand()), 0, 7); |
| 156 | $this->click('title'); |
| 157 | $this->type('title', $customGroupTitle); |
| 158 | |
| 159 | //custom group extends |
| 160 | $this->click('extends[0]'); |
| 161 | $this->select('extends[0]', "value=Contact"); |
| 162 | $this->click("//option[@value='Contact']"); |
| 163 | $this->clickLink('_qf_Group_next-bottom'); |
| 164 | |
| 165 | //Is custom group created? |
| 166 | $this->waitForText('crm-notification-container', $customGroupTitle); |
| 167 | $this->waitForElementPresent('_qf_Field_cancel-bottom'); |
| 168 | $gid = $this->urlArg('gid'); |
| 169 | |
| 170 | // create another custom field - Date |
| 171 | $customField = 'Custom field ' . substr(sha1(rand()), 0, 4); |
| 172 | $this->type('label', $customField); |
| 173 | |
| 174 | //enter pre help message |
| 175 | $this->type("help_pre", "this is field pre help"); |
| 176 | |
| 177 | //enter post help message |
| 178 | $this->type("help_post", "this field post help"); |
| 179 | |
| 180 | //Is searchable? |
| 181 | $this->click("is_searchable"); |
| 182 | |
| 183 | // clicking save |
| 184 | $this->click('_qf_Field_done-bottom'); |
| 185 | $this->waitForElementPresent("xpath=//div[@id='field_page']//table/tbody//tr/td"); |
| 186 | |
| 187 | $this->waitForText('crm-notification-container', "Custom field '{$customField}' has been saved."); |
| 188 | $customFieldId = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr/td/div[text()='$customField']/../../td[8]/span/a@href")); |
| 189 | $customFieldId = $customFieldId[1]; |
| 190 | |
| 191 | return array("custom_{$customFieldId}", $customField, $customGroupTitle); |
| 192 | } |
| 193 | |
| 194 | } |