| 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 | |
| 28 | require_once 'CiviTest/CiviSeleniumTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Class WebTest_Contribute_UpdateContributionTest |
| 32 | */ |
| 33 | class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase { |
| 34 | |
| 35 | protected function setUp() { |
| 36 | parent::setUp(); |
| 37 | } |
| 38 | |
| 39 | function testChangeContributionAmount() { |
| 40 | $this->webtestLogin(); |
| 41 | $amount = 100; |
| 42 | //Offline Pay Later Contribution |
| 43 | $contact = $this->_testOfflineContribution($amount, "Pending"); |
| 44 | |
| 45 | $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low"); |
| 46 | |
| 47 | $this->type("sort_name", $contact['sort_name']); |
| 48 | $this->click("_qf_Search_refresh"); |
| 49 | |
| 50 | $this->waitForElementPresent("xpath=//*[@id='Search']//div[@id='contributionSearch']"); |
| 51 | $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']//table[@class='selector row-highlight']/tbody/tr[1]/td[11]/span/a[1]@href")); |
| 52 | if (!empty($contriIDOff)) { |
| 53 | $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1)); |
| 54 | } |
| 55 | |
| 56 | $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']/td[11]/span/a[2]", "total_amount", FALSE); |
| 57 | $this->type("total_amount", "90"); |
| 58 | $this->clickLink('_qf_Contribution_upload','',FALSE); |
| 59 | |
| 60 | // Is status message correct? |
| 61 | $this->waitForText('crm-notification-container', "The contribution record has been saved."); |
| 62 | |
| 63 | //For Contribution |
| 64 | $searchParams = array('id' => $contriIDOff); |
| 65 | $compareParams = array('total_amount' => '90.00'); |
| 66 | //For LineItem |
| 67 | $lineItemSearchParams = array('entity_id' => $contriIDOff); |
| 68 | $lineItemCompareParams = array('line_total' => '90.00'); |
| 69 | |
| 70 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams); |
| 71 | $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams); |
| 72 | |
| 73 | $total = $this->_getTotalContributedAmount($contriIDOff); |
| 74 | $compare = array('total_amount' => $total); |
| 75 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare); |
| 76 | |
| 77 | $amount = $this->_getFinancialItemAmount($contriIDOff); |
| 78 | $compare = array('total_amount' => $amount); |
| 79 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare); |
| 80 | |
| 81 | $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff); |
| 82 | $compare = array('total_amount' => $financialTrxnAmount); |
| 83 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare); |
| 84 | } |
| 85 | |
| 86 | function testPayLater() { |
| 87 | $this->webtestLogin(); |
| 88 | $amount = 100.00; |
| 89 | //Offline Pay Later Contribution |
| 90 | $this->_testOfflineContribution($amount, "Pending"); |
| 91 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 92 | $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 93 | $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom'); |
| 94 | $this->select("contribution_status_id", "label=Completed"); |
| 95 | $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 96 | $this->checkCRMAlert("The contribution record has been saved."); |
| 97 | |
| 98 | //Assertions |
| 99 | $search = array('id' => $contId); |
| 100 | $compare = array('contribution_status_id' => 1); |
| 101 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare); |
| 102 | |
| 103 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
| 104 | $search = array( 'entity_id' => $lineItem ); |
| 105 | $compare = array( 'status_id' => 1 ); |
| 106 | $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare); |
| 107 | |
| 108 | $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status"); |
| 109 | $this->assertEquals($status, '1', "Verify Completed Status"); |
| 110 | } |
| 111 | |
| 112 | function testChangePremium() { |
| 113 | $this->webtestLogin(); |
| 114 | $from = 'Premiums'; |
| 115 | $to = 'Premiums inventory'; |
| 116 | $financialType = array( |
| 117 | 'name' => 'Test Financial'.substr(sha1(rand()), 0, 7), |
| 118 | 'is_reserved' => 1, |
| 119 | 'is_deductible' => 1, |
| 120 | ); |
| 121 | $this->addeditFinancialType($financialType); |
| 122 | $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']"); |
| 123 | $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']"); |
| 124 | $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']"); |
| 125 | $this->clickAjaxLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom"); |
| 126 | $this->select("account_relationship", "label=Premiums Inventory Account is"); |
| 127 | $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']"); |
| 128 | $this->select("financial_account_id", "label=$to"); |
| 129 | $this->clickAjaxLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']"); |
| 130 | |
| 131 | $premiumName = 'Premium'.substr(sha1(rand()), 0, 7); |
| 132 | $amount = 500; |
| 133 | $sku = 'SKU'; |
| 134 | $price = 300; |
| 135 | $cost = 3.00; |
| 136 | $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1"); |
| 137 | // add premium |
| 138 | $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']); |
| 139 | |
| 140 | //add second premium |
| 141 | $premiumName2 = 'Premium'.substr(sha1(rand()), 0, 7); |
| 142 | $amount2 = 600; |
| 143 | $sku2 = 'SKU'; |
| 144 | $price2 = 200; |
| 145 | $cost2 = 2.00; |
| 146 | $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1"); |
| 147 | $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']); |
| 148 | |
| 149 | // add contribution with premium |
| 150 | $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone"); |
| 151 | |
| 152 | // create new contact using dialog |
| 153 | $this->createDialogContact(); |
| 154 | // select financial type |
| 155 | $this->select( "financial_type_id", "value=1" ); |
| 156 | // total amount |
| 157 | $this->type("total_amount", "100"); |
| 158 | // fill Premium information |
| 159 | $this->clickAjaxLink("xpath=//div[@id='Premium']", "product_name_0"); |
| 160 | $this->select('product_name_0', "label=$premiumName ( $sku )"); |
| 161 | // Clicking save. |
| 162 | $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']"); |
| 163 | // Is status message correct? |
| 164 | $this->checkCRMAlert("The contribution record has been saved."); |
| 165 | // verify if Contribution is created |
| 166 | //click through to the Contribution edit screen |
| 167 | $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 168 | $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "product_name_0"); |
| 169 | |
| 170 | $this->select('product_name_0', "label=$premiumName2 ( $sku2 )"); |
| 171 | // Clicking save. |
| 172 | $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[7][text()='$premiumName2']"); |
| 173 | $this->checkCRMAlert("The contribution record has been saved."); |
| 174 | |
| 175 | //Assertions |
| 176 | $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'"); |
| 177 | $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium"); |
| 178 | |
| 179 | $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'"); |
| 180 | $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium"); |
| 181 | } |
| 182 | |
| 183 | function testDeletePremium() { |
| 184 | $this->webtestLogin(); |
| 185 | $from = 'Premiums'; |
| 186 | $to = 'Premiums inventory'; |
| 187 | $financialType = array( |
| 188 | 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7), |
| 189 | 'is_reserved' => 1, |
| 190 | 'is_deductible' => 1, |
| 191 | ); |
| 192 | $this->addeditFinancialType($financialType); |
| 193 | $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']"); |
| 194 | $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td[text()='".$financialType['name']."']/../td[7]/span/a[text()='Accounts']"); |
| 195 | $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']"); |
| 196 | $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom", FALSE); |
| 197 | |
| 198 | $this->select("account_relationship", "label=Premiums Inventory Account is"); |
| 199 | $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']"); |
| 200 | $this->select("financial_account_id", "label=$to"); |
| 201 | $this->clickLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']", FALSE); |
| 202 | $premiumName = 'Premium' . substr(sha1(rand()), 0, 7); |
| 203 | $amount = 500; |
| 204 | $sku = 'SKU'; |
| 205 | $price = 300; |
| 206 | $cost = 3.00; |
| 207 | $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1"); |
| 208 | // add premium |
| 209 | $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']); |
| 210 | |
| 211 | // add contribution with premium |
| 212 | $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone"); |
| 213 | |
| 214 | // create new contact using dialog |
| 215 | $this->createDialogContact(); |
| 216 | // select financial type |
| 217 | $this->select("financial_type_id", "value=1"); |
| 218 | // total amount |
| 219 | $this->type("total_amount", "100"); |
| 220 | // fill Premium information |
| 221 | $this->click("xpath=//div[@id='Premium']"); |
| 222 | $this->waitForElementPresent("product_name_0"); |
| 223 | $this->select('product_name_0', "label=$premiumName ( $sku )"); |
| 224 | // Clicking save. |
| 225 | $this->clickLink("_qf_Contribution_upload"); |
| 226 | // Is status message correct? |
| 227 | $this->waitForText('crm-notification-container', "The contribution record has been saved."); |
| 228 | // verify if Contribution is created |
| 229 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']"); |
| 230 | //click through to the Contribution edit screen |
| 231 | $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 232 | $contId = explode('&', $contId[1]); |
| 233 | $contId = $contId[0]; |
| 234 | $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom", FALSE); |
| 235 | $this->waitForElementPresent("product_name_0"); |
| 236 | $this->select('product_name_0', "value=0"); |
| 237 | // Clicking save. |
| 238 | $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[7][not(text())]", FALSE); |
| 239 | |
| 240 | //Assertions |
| 241 | $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'"); |
| 242 | $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium"); |
| 243 | } |
| 244 | |
| 245 | function testChangePaymentInstrument() { |
| 246 | $this->webtestLogin(); |
| 247 | $label = 'TEST'.substr(sha1(rand()), 0, 7); |
| 248 | $amount = 100.00; |
| 249 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(); |
| 250 | $to = array_search('Accounts Receivable', $financialAccount); |
| 251 | $from = array_search('Deposit Bank Account', $financialAccount); |
| 252 | $this->addPaymentInstrument($label, $to); |
| 253 | $this->_testOfflineContribution($amount); |
| 254 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 255 | $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE); |
| 256 | $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 257 | $contId = explode('&', $contId[1]); |
| 258 | $contId = $contId[0]; |
| 259 | //change payment processor to newly created value |
| 260 | $this->select("payment_instrument_id", "label=$label"); |
| 261 | $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE); |
| 262 | $this->waitForAjaxContent(); |
| 263 | //Assertions |
| 264 | $subtractedTotal = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account'); |
| 265 | $this->assertEquals($subtractedTotal, -$amount, "Verify amount deleted from old account"); |
| 266 | $totalAmount = $this->_getPremiumActualCost($contId, NULL, 'Accounts Receivable'); |
| 267 | $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values"); |
| 268 | } |
| 269 | |
| 270 | function testRefundContribution() { |
| 271 | $this->webtestLogin(); |
| 272 | $amount = 100.00; |
| 273 | $this->_testOfflineContribution($amount); |
| 274 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 275 | $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom'); |
| 276 | //Contribution status |
| 277 | $this->select("contribution_status_id", "label=Refunded"); |
| 278 | $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 279 | $contId = explode('&', $contId[1]); |
| 280 | $contId = $contId[0]; |
| 281 | $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[6][text()='Refunded']"); |
| 282 | |
| 283 | //Assertions |
| 284 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
| 285 | $search = array('entity_id' => $lineItem); |
| 286 | $compare = array( |
| 287 | 'amount' => '100.00', |
| 288 | 'status_id' => 1, |
| 289 | ); |
| 290 | $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare); |
| 291 | $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'"); |
| 292 | $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.'); |
| 293 | } |
| 294 | |
| 295 | function testCancelPayLater() { |
| 296 | $this->webtestLogin(); |
| 297 | $amount = 100.00; |
| 298 | $this->_testOfflineContribution($amount, "Pending"); |
| 299 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 300 | $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE); |
| 301 | //Contribution status |
| 302 | $this->select("contribution_status_id", "label=Cancelled"); |
| 303 | $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 304 | $contId = explode('&', $contId[1]); |
| 305 | $contId = $contId[0]; |
| 306 | $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE); |
| 307 | $this->waitForText('crm-notification-container', "The contribution record has been saved."); |
| 308 | |
| 309 | //Assertions |
| 310 | $search = array('id' => $contId); |
| 311 | $compare = array('contribution_status_id' => 3); |
| 312 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare); |
| 313 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
| 314 | $itemParams = array( |
| 315 | 'amount' => '-100.00', |
| 316 | 'entity_id' => $lineItem, |
| 317 | ); |
| 318 | $defaults = array(); |
| 319 | $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults); |
| 320 | $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item'); |
| 321 | $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'"); |
| 322 | $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn'); |
| 323 | $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'"); |
| 324 | $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn'); |
| 325 | } |
| 326 | |
| 327 | function testChangeFinancialType() { |
| 328 | $this->webtestLogin(); |
| 329 | $amount = 100.00; |
| 330 | $this->_testOfflineContribution($amount); |
| 331 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']"); |
| 332 | $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE); |
| 333 | //Contribution status |
| 334 | $this->select("financial_type_id", "value=3"); |
| 335 | $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href")); |
| 336 | $contId = explode('&', $contId[1]); |
| 337 | $contId = $contId[0]; |
| 338 | $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE); |
| 339 | $this->waitForText('crm-notification-container', "The contribution record has been saved."); |
| 340 | |
| 341 | //Assertions |
| 342 | $search = array( 'id' => $contId ); |
| 343 | $compare = array( 'financial_type_id' => 3 ); |
| 344 | $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare); |
| 345 | |
| 346 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
| 347 | $itemParams = array( |
| 348 | 'amount' => '-100.00', |
| 349 | 'entity_id' => $lineItem, |
| 350 | ); |
| 351 | $item1 = $item2 = array(); |
| 352 | CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1); |
| 353 | $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item"); |
| 354 | $itemParams['amount'] = '100.00'; |
| 355 | CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2); |
| 356 | $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item"); |
| 357 | |
| 358 | $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'"); |
| 359 | $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'"); |
| 360 | $this->assertEquals($cValue1, "-100.00", "Verify Amount"); |
| 361 | $this->assertEquals($fValue1, "-100.00", "Verify Amount"); |
| 362 | $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'"); |
| 363 | $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'"); |
| 364 | $this->assertEquals($cValue2, "100.00", "Verify Amount"); |
| 365 | $this->assertEquals($fValue2, "100.00", "Verify Amount"); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * @param int $entityId |
| 370 | * @param null $from |
| 371 | * @param null $to |
| 372 | * @param null $cost |
| 373 | * @param null $entityTable |
| 374 | * @param string $select |
| 375 | * |
| 376 | * @return null|string |
| 377 | */function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") { |
| 378 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(); |
| 379 | $query = "SELECT |
| 380 | {$select} |
| 381 | FROM civicrm_financial_trxn ft |
| 382 | INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}"; |
| 383 | if ($entityTable) { |
| 384 | $query .= " AND eft.entity_table = {$entityTable}"; |
| 385 | } |
| 386 | if (!empty($to)) { |
| 387 | $to = array_search($to, $financialAccount); |
| 388 | $query .= " AND ft.to_financial_account_id = {$to}"; |
| 389 | } |
| 390 | if (!empty($from)) { |
| 391 | $from = array_search($from, $financialAccount); |
| 392 | $query .= " AND ft.from_financial_account_id = {$from}"; |
| 393 | } |
| 394 | if (!empty($cost)) { |
| 395 | $query .= " AND eft.amount = {$cost}"; |
| 396 | } |
| 397 | $query .= " ORDER BY ft.id DESC LIMIT 1"; |
| 398 | $result = CRM_Core_DAO::singleValueQuery($query); |
| 399 | return $result; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @param int $contId |
| 404 | * |
| 405 | * @return null|string |
| 406 | */function _getFinancialTrxnAmount($contId) { |
| 407 | $query = "SELECT |
| 408 | SUM( ft.total_amount ) AS total |
| 409 | FROM civicrm_financial_trxn AS ft |
| 410 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id |
| 411 | WHERE ceft.entity_table = 'civicrm_contribution' |
| 412 | AND ceft.entity_id = {$contId}"; |
| 413 | $result = CRM_Core_DAO::singleValueQuery($query); |
| 414 | return $result; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @param int $contId |
| 419 | * |
| 420 | * @return null|string |
| 421 | */function _getFinancialItemAmount($contId) { |
| 422 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
| 423 | $query = "SELECT |
| 424 | SUM(amount) |
| 425 | FROM civicrm_financial_item |
| 426 | WHERE entity_table = 'civicrm_line_item' |
| 427 | AND entity_id = {$lineItem}"; |
| 428 | $result = CRM_Core_DAO::singleValueQuery($query); |
| 429 | return $result; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @param int $contId |
| 434 | * |
| 435 | * @return null|string |
| 436 | */function _getTotalContributedAmount($contId) { |
| 437 | $query = "SELECT |
| 438 | SUM(amount) |
| 439 | FROM civicrm_entity_financial_trxn |
| 440 | WHERE entity_table = 'civicrm_contribution' |
| 441 | AND entity_id = {$contId}"; |
| 442 | $result = CRM_Core_DAO::singleValueQuery($query); |
| 443 | return $result; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @param $amount |
| 448 | * @param string $status |
| 449 | * @return array |
| 450 | */ |
| 451 | function _testOfflineContribution($amount, $status="Completed") { |
| 452 | |
| 453 | $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload"); |
| 454 | |
| 455 | // create new contact using dialog |
| 456 | $contact = $this->createDialogContact(); |
| 457 | |
| 458 | // select financial type |
| 459 | $this->select( "financial_type_id", "value=1" ); |
| 460 | |
| 461 | //Contribution status |
| 462 | $this->select("contribution_status_id", "label=$status"); |
| 463 | |
| 464 | // total amount |
| 465 | $this->type("total_amount", $amount); |
| 466 | |
| 467 | // select payment instrument type |
| 468 | $this->select("payment_instrument_id", "label=Credit Card"); |
| 469 | |
| 470 | |
| 471 | $this->type("trxn_id", "P20901X1" . rand(100, 10000)); |
| 472 | |
| 473 | |
| 474 | //Custom Data |
| 475 | //$this->click('CIVICRM_QFID_3_6'); |
| 476 | |
| 477 | // Clicking save. |
| 478 | $this->clickLink("_qf_Contribution_upload"); |
| 479 | |
| 480 | // Is status message correct? |
| 481 | $this->checkCRMAlert("The contribution record has been saved."); |
| 482 | |
| 483 | $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']"); |
| 484 | // verify if Membership is created |
| 485 | |
| 486 | //click through to the Membership view screen |
| 487 | $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']"); |
| 488 | |
| 489 | $expected = array( |
| 490 | 'Financial Type' => 'Donation', |
| 491 | 'Total Amount' => '100.00', |
| 492 | 'Contribution Status' => $status, |
| 493 | ); |
| 494 | $this->webtestVerifyTabularData($expected); |
| 495 | $this->click("_qf_ContributionView_cancel-bottom"); |
| 496 | |
| 497 | return $contact; |
| 498 | } |
| 499 | } |