SearchTest.php 22.8 KB
Newer Older
deb.monish's avatar
deb.monish committed
1 2 3 4 5
<?php
/*
 +--------------------------------------------------------------------+
 | CiviCRM version 4.7                                                |
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
6
 | Copyright CiviCRM LLC (c) 2004-2017                                |
deb.monish's avatar
deb.monish committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
 */

/**
 *  Include parent class definition
 */

/**
 *  Test Contribution Search form filters
 *
 * @package CiviCRM
 */
eileen's avatar
eileen committed
37
class CRM_Contribute_Form_SearchTest extends CiviUnitTestCase {
deb.monish's avatar
deb.monish committed
38 39

  protected $_individual;
deb.monish's avatar
deb.monish committed
40
  protected $_tablesToTruncate = array('civicrm_contribution', 'civicrm_line_item');
deb.monish's avatar
deb.monish committed
41 42 43

  public function setUp() {
    parent::setUp();
deb.monish's avatar
deb.monish committed
44
    $this->_individual = $this->individualCreate();
deb.monish's avatar
deb.monish committed
45 46 47 48 49 50
  }

  public function tearDown() {
  }

  /**
51
   *  CRM-19325: Test CRM_Contribute_Form_Search batch filters
deb.monish's avatar
deb.monish committed
52 53 54 55 56 57 58 59 60 61 62
   */
  public function testBatchFilter() {
    $this->quickCleanup($this->_tablesToTruncate);
    $contactID1 = $this->individualCreate(array(), 1);
    $contactID2 = $this->individualCreate(array(), 2);
    $batchTitle = CRM_Batch_BAO_Batch::generateBatchName();

    // create batch
    $batch = civicrm_api3('Batch', 'create', array(
      'created_id' => $this->_individual,
      'created_date' => CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s")),
deb.monish's avatar
deb.monish committed
63
      'status_id' => CRM_Core_Pseudoconstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry'),
deb.monish's avatar
deb.monish committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      'title' => $batchTitle,
      'item_count' => 2,
      'total' => 100,
      'type_id' => array_search('Contribution', CRM_Batch_BAO_Batch::buildOptions('type_id')),
    ));
    $batchID = $batch['id'];

    $batchEntry = array(
      'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
      'primary_contact_id' => array(
        1 => $contactID1,
        2 => $contactID2,
      ),
      'field' => array(
        1 => array(
          'financial_type' => 1,
          'total_amount' => 70,
deb.monish's avatar
deb.monish committed
81
          'receive_date' => '2013-07-24',
deb.monish's avatar
deb.monish committed
82 83 84 85 86 87 88 89
          'receive_date_time' => NULL,
          'payment_instrument' => 1,
          'check_number' => NULL,
          'contribution_status_id' => 1,
        ),
        2 => array(
          'financial_type' => 1,
          'total_amount' => 30,
deb.monish's avatar
deb.monish committed
90
          'receive_date' => '2014-07-24',
deb.monish's avatar
deb.monish committed
91 92 93 94 95 96 97 98 99 100
          'receive_date_time' => NULL,
          'payment_instrument' => 1,
          'check_number' => NULL,
          'contribution_status_id' => 1,
        ),
      ),
      'actualBatchTotal' => 100,
    );

    // create random contribution to check IS NULL filter more precisely
101
    $nonBatchContri = civicrm_api3('Contribution', 'create', array(
deb.monish's avatar
deb.monish committed
102 103
      'financial_type_id' => 1,
      'total_amount' => 123,
deb.monish's avatar
deb.monish committed
104
      'receive_date' => '2014-07-24',
deb.monish's avatar
deb.monish committed
105 106 107 108 109 110
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'check_number' => NULL,
      'contribution_status_id' => 1,
      'contact_id' => $this->_individual,
    ));
111
    $nonBatchContriID = $nonBatchContri['id'];
deb.monish's avatar
deb.monish committed
112 113 114 115 116 117

    // process batch entries
    $form = new CRM_Batch_Form_Entry();
    $form->setBatchID($batchID);
    $form->testProcessContribution($batchEntry);

118 119 120 121 122 123 124 125
    // fetch created contributions
    $entities = civicrm_api3('EntityBatch', 'get', array('batch_id' => $batchID));
    $ids = array();
    foreach ($entities['values'] as $value) {
      $ids[] = $value['entity_id'];
    }
    list($batchContriID1, $batchContriID2) = $ids;

deb.monish's avatar
deb.monish committed
126 127 128 129 130
    $useCases = array(
      // Case 1: Search for ONLY those contributions which are created from batch
      array(
        'form_value' => array('contribution_batch_id' => 'IS NOT NULL'),
        'expected_count' => 2,
131
        'expected_contribution' => array($batchContriID1, $batchContriID2),
deb.monish's avatar
deb.monish committed
132 133 134 135 136 137
        'expected_qill' => 'Batch Name Not Null',
      ),
      // Case 2: Search for ONLY those contributions which are NOT created from batch
      array(
        'form_value' => array('contribution_batch_id' => 'IS NULL'),
        'expected_count' => 1,
138
        'expected_contribution' => array($nonBatchContriID),
deb.monish's avatar
deb.monish committed
139 140 141 142 143 144
        'expected_qill' => 'Batch Name Is Null',
      ),
      // Case 3: Search for ONLY those contributions which are created from batch ID - $batchID
      array(
        'form_value' => array('contribution_batch_id' => $batchID),
        'expected_count' => 2,
145
        'expected_contribution' => array($batchContriID1, $batchContriID2),
deb.monish's avatar
deb.monish committed
146 147 148 149 150 151 152 153 154 155
        'expected_qill' => 'Batch Name = ' . $batchTitle,
      ),
    );
    foreach ($useCases as $case) {
      $fv = $case['form_value'];
      CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('contribution_batch_id'));
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
      list($select, $from, $where, $having) = $query->query();

      // get and assert contribution count
156 157 158 159 160 161 162 163
      $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll();
      foreach ($contributions as $key => $value) {
        $contributions[$key] = $value['id'];
      }
      // assert the contribution count
      $this->assertEquals($case['expected_count'], count($contributions));
      // assert the contribution IDs
      $this->checkArrayEquals($case['expected_contribution'], $contributions);
deb.monish's avatar
deb.monish committed
164 165 166 167 168 169
      // get and assert qill string
      $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
      $this->assertEquals($case['expected_qill'], $qill);
    }
  }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  /**
   *  CRM-20286: Test CRM_Contribute_Form_Search Card type filters
   */
  public function testCardTypeFilter() {
    $this->quickCleanup($this->_tablesToTruncate);
    $contactID1 = $this->individualCreate(array(), 1);
    $contactID2 = $this->individualCreate(array(), 2);
    $Contribution1 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 100,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID1,
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution1['id'],
      'payment_instrument_id' => 1,
deb.monish's avatar
deb.monish committed
191
      'card_type_id' => 1,
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      'total_amount' => 100,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);
    $Contribution2 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 150,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID1,
    ));
    $Contribution3 = civicrm_api3('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 200,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID2,
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution3['id'],
      'payment_instrument_id' => 1,
deb.monish's avatar
deb.monish committed
218
      'card_type_id' => 2,
219 220 221 222 223 224 225
      'total_amount' => 200,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);

    $useCases = array(
      // Case 1: Search for ONLY those contributions which have card type
      array(
deb.monish's avatar
deb.monish committed
226
        'form_value' => array('financial_trxn_card_type_id' => 'IS NOT NULL'),
227 228 229 230 231 232
        'expected_count' => 2,
        'expected_contribution' => array($Contribution1['id'], $Contribution3['id']),
        'expected_qill' => 'Card Type Not Null',
      ),
      // Case 2: Search for ONLY those contributions which have Card Type as Visa
      array(
deb.monish's avatar
deb.monish committed
233
        'form_value' => array('financial_trxn_card_type_id' => array(1)),
234 235 236 237 238 239
        'expected_count' => 1,
        'expected_contribution' => array($Contribution1['id']),
        'expected_qill' => 'Card Type In Visa',
      ),
      // Case 3: Search for ONLY those contributions which have Card Type as Amex
      array(
deb.monish's avatar
deb.monish committed
240
        'form_value' => array('financial_trxn_card_type_id' => array(3)),
241 242 243 244 245 246
        'expected_count' => 0,
        'expected_contribution' => array(),
        'expected_qill' => 'Card Type In Amex',
      ),
      // Case 4: Search for ONLY those contributions which have Card Type as Visa or MasterCard
      array(
deb.monish's avatar
deb.monish committed
247
        'form_value' => array('financial_trxn_card_type_id' => array(1, 2)),
248 249 250 251 252 253 254 255
        'expected_count' => 2,
        'expected_contribution' => array($Contribution1['id'], $Contribution3['id']),
        'expected_qill' => 'Card Type In Visa, MasterCard',
      ),
    );

    foreach ($useCases as $case) {
      $fv = $case['form_value'];
deb.monish's avatar
deb.monish committed
256
      CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('financial_trxn_card_type_id'));
257 258 259 260 261 262 263 264 265
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
      list($select, $from, $where, $having) = $query->query();

      // get and assert contribution count
      $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll();
      foreach ($contributions as $key => $value) {
        $contributions[$key] = $value['id'];
      }
      // assert the contribution count
deb.monish's avatar
deb.monish committed
266
      //$this->assertEquals($case['expected_count'], count($contributions));
267 268 269 270 271 272 273 274
      // assert the contribution IDs
      $this->checkArrayEquals($case['expected_contribution'], $contributions);
      // get and assert qill string
      $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
      $this->assertEquals($case['expected_qill'], $qill);
    }
  }

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  /**
   *  CRM-20391: Test CRM_Contribute_Form_Search Card Number filters
   */
  public function testCardNumberFilter() {
    $this->quickCleanup($this->_tablesToTruncate);
    $contactID1 = $this->individualCreate(array(), 1);
    $contactID2 = $this->individualCreate(array(), 2);
    $Contribution1 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 100,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID1,
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution1['id'],
      'payment_instrument_id' => 1,
deb.monish's avatar
deb.monish committed
296
      'card_type_id' => 1,
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      'total_amount' => 100,
      'pan_truncation' => 1234,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);
    $Contribution2 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 150,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID1,
    ));
    $Contribution3 = civicrm_api3('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 200,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID2,
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution3['id'],
      'payment_instrument_id' => 1,
deb.monish's avatar
deb.monish committed
324
      'card_type_id' => 2,
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
      'total_amount' => 200,
      'pan_truncation' => 5678,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);

    $useCases = array(
      // Case 1: Search for ONLY those contributions which have card number
      array(
        'form_value' => array('financial_trxn_pan_truncation' => 'IS NOT NULL'),
        'expected_count' => 2,
        'expected_contribution' => array($Contribution1['id'], $Contribution3['id']),
        'expected_qill' => 'Card Number Not Null',
      ),
      // Case 2: Search for ONLY those contributions which have Card Number as 1234
      array(
        'form_value' => array('financial_trxn_pan_truncation' => 1234),
        'expected_count' => 1,
        'expected_contribution' => array($Contribution1['id']),
        'expected_qill' => 'Card Number Like %1234%',
      ),
      // Case 3: Search for ONLY those contributions which have Card Number as 8888
      array(
        'form_value' => array('financial_trxn_pan_truncation' => 8888),
        'expected_count' => 0,
        'expected_contribution' => array(),
        'expected_qill' => 'Card Number Like %8888%',
      ),
    );

    foreach ($useCases as $case) {
      $fv = $case['form_value'];
      CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('financial_trxn_pan_truncation'));
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
      list($select, $from, $where, $having) = $query->query();

      // get and assert contribution count
      $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll();
      foreach ($contributions as $key => $value) {
        $contributions[$key] = $value['id'];
      }
      // assert the contribution count
      $this->assertEquals($case['expected_count'], count($contributions));
      // assert the contribution IDs
      $this->checkArrayEquals($case['expected_contribution'], $contributions);
      // get and assert qill string
      $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
      $this->assertEquals($case['expected_qill'], $qill);
    }
  }

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  /**
   *  Test CRM_Contribute_Form_Search Recurring Contribution Status Id filters
   */
  public function testContributionRecurStatusFilter() {
    $this->quickCleanup($this->_tablesToTruncate);
    $contactID1 = $this->individualCreate(array(), 1);
    $contactID2 = $this->individualCreate(array(), 2);
    // "In Progress" recurring contribution for contactID1
    $ContributionRecur1 = $this->callAPISuccess('ContributionRecur', 'create', array(
      'sequential' => 1,
      'contact_id' => $contactID1,
      'frequency_interval' => 1,
      'frequency_unit' => "month",
      'amount' => 11,
      'currency' => "CAD",
      'payment_instrument_id' => 1,
      'contribution_status_id' => 5,
      'financial_type_id' => "Donation",
    ));
    $Contribution1 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 11,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument_id' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID1,
      'contribution_recur_id' => $ContributionRecur1['id'],
      'financial_type_id' => "Donation",
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution1['id'],
      'payment_instrument_id' => 1,
      'card_type_id' => 1,
      'total_amount' => 11,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);
    // "Completed" recurring contribution for contactID2
    $ContributionRecur2 = $this->callAPISuccess('ContributionRecur', 'create', array(
      'sequential' => 1,
      'contact_id' => $contactID2,
      'frequency_interval' => 1,
      'frequency_unit' => "month",
      'amount' => 22,
      'currency' => "CAD",
      'payment_instrument_id' => 1,
      'contribution_status_id' => 1,
      'financial_type_id' => "Donation",
    ));
    $Contribution2 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 22,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 1,
      'contact_id' => $contactID2,
      'contribution_recur_id' => $ContributionRecur2['id'],
      'financial_type_id' => "Donation",
    ));
    $params = array(
      'to_financial_account_id' => 1,
      'status_id' => 1,
      'contribution_id' => $Contribution2['id'],
      'payment_instrument_id' => 1,
      'card_type_id' => 1,
      'total_amount' => 22,
    );
    CRM_Core_BAO_FinancialTrxn::create($params);

    $useCases = array(
      // Case 1: Search for ONLY those recurring contributions with status "In Progress"
      array(
        'form_value' => array('contribution_recur_contribution_status_id' => 5),
        'expected_count' => 1,
        'expected_contact' => array($contactID1),
        'expected_qill' => "Recurring Contribution Status = 'In Progress'",
      ),
      // Case 2: Search for ONLY those recurring contributions with status "Completed"
      array(
        'form_value' => array('contribution_recur_contribution_status_id' => 1),
        'expected_count' => 1,
        'expected_contact' => array($contactID2),
        'expected_qill' => "Recurring Contribution Status = 'Completed'",
      ),
      // Case 3: Search for ONLY those recurring contributions with status "Cancelled"
      array(
        'form_value' => array('contribution_recur_contribution_status_id' => 3),
        'expected_count' => 0,
        'expected_contact' => array(),
        'expected_qill' => "Recurring Contribution Status = 'Cancelled'",
      ),
    );

    foreach ($useCases as $case) {
      $fv = $case['form_value'];
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
      list($select, $from, $where, $having) = $query->query();

      // get and assert contribution count
      $contacts = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT contact_a.id %s %s AND contact_a.id IS NOT NULL', $from, $where))->fetchAll();
      foreach ($contacts as $key => $value) {
        $contacts[$key] = $value['id'];
      }
      // assert the contribution count
      $this->assertEquals($case['expected_count'], count($contacts));
      // assert the contribution IDs
      $this->checkArrayEquals($case['expected_contact'], $contacts);
      // get and assert qill string
      $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
      $this->assertEquals($case['expected_qill'], $qill);
    }
  }
sluc23's avatar
sluc23 committed
490 491

  /**
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
   *  CRM-21343: Test CRM_Contribute_Form_Search Cancelled filters
   */
  public function testCancelledFilter() {
    $this->quickCleanup($this->_tablesToTruncate);
    $contactID1 = $this->individualCreate(array(), 1);
    $contactID2 = $this->individualCreate(array(), 2);
    $Contribution1 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 100,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 3,
      'cancel_date' => date('Ymd'),
      'cancel_reason' => 'Insufficient funds',
      'contact_id' => $contactID1,
    ));
    $Contribution2 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 150,
      'receive_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 3,
      'cancel_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')),
      'cancel_reason' => 'Insufficient funds',
      'contact_id' => $contactID2,
    ));
    $Contribution3 = $this->callAPISuccess('Contribution', 'create', array(
      'financial_type_id' => 1,
      'total_amount' => 200,
      'receive_date' => date('Ymd'),
      'receive_date_time' => NULL,
      'payment_instrument' => 1,
      'contribution_status_id' => 3,
      'cancel_date' => date('Ymd'),
      'cancel_reason' => 'Invalid Credit Card Number',
      'contact_id' => $contactID1,
    ));

    $useCases = array(
      // Case 1: Search for Cancelled Date
      array(
        'form_value' => array('cancel_date' => date('Y-m-d')),
        'expected_count' => 2,
        'expected_contribution' => array($Contribution1['id'], $Contribution3['id']),
        'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'",
      ),
      // Case 2: Search for Cancelled Reason
      array(
        'form_value' => array('cancel_reason' => 'Invalid Credit Card Number'),
        'expected_count' => 1,
        'expected_contribution' => array($Contribution3['id']),
        'expected_qill' => "Cancel Reason Like '%Invalid Credit Card Number%'",
      ),
      // Case 3: Search for Cancelled Date and Cancelled Reason
      array(
        'form_value' => array('cancel_date' => date('Y-m-d'), 'cancel_reason' => 'Insufficient funds'),
        'expected_count' => 1,
        'expected_contribution' => array($Contribution1['id']),
        'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'ANDCancel Reason Like '%Insufficient funds%'",
      ),
    );

    foreach ($useCases as $case) {
      $fv = $case['form_value'];
      CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('cancel_date', 'cancel_reason'));
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv));
      list($select, $from, $where, $having) = $query->query();

      // get and assert contribution count
      $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL AND civicrm_contribution.contribution_status_id = 3', $from, $where))->fetchAll();
      foreach ($contributions as $key => $value) {
        $contributions[$key] = $value['id'];
      }
      // assert the contribution count
      $this->assertEquals($case['expected_count'], count($contributions));
      // assert the contribution IDs
      $this->checkArrayEquals($case['expected_contribution'], $contributions);
      // get and assert qill string
      $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
      $this->assertEquals($case['expected_qill'], $qill);
    }
  }
sluc23's avatar
sluc23 committed
576

deb.monish's avatar
deb.monish committed
577
}