/**
  * Saves a record of a new contribution to the contribution_tracking_table
  * @param array $params A staged array of parameters that can be processed
  * by the ContributionTrackingProcessor.
  * @return integer The id of the saved contribution in the
  * contribution_tracking table
  */
 static function saveNewContribution($params = array())
 {
     $db = ContributionTrackingProcessor::contributionTrackingConnection();
     $params['ts'] = $db->timestamp();
     $owa_ref = null;
     if (array_key_exists('owa_ref', $params) && $params['owa_ref'] != null) {
         if ($params['owa_ref'] == null || is_numeric($params['owa_ref'])) {
             $owa_ref = $params['owa_ref'];
         } else {
             $owa_ref = ContributionTrackingProcessor::get_owa_ref_id($params['owa_ref']);
         }
     }
     $params['owa_ref'] = $owa_ref;
     $tracked_contribution = ContributionTrackingProcessor::stage_contribution($params);
     $db->insert('contribution_tracking', $tracked_contribution);
     $contribution_tracking_id = $db->insertId();
     return $contribution_tracking_id;
 }
 /**
  * tests the stage_contribution function, and as a by-product,
  * the getContributionDefaults function as well.
  * Asserts that:
  * 		A staged contribution with no relevant fields will come back equal
  * to exactly the defaults
  * 		A staged contribution with some relevant fields will come back as
  * the defaults, with keys overwritten by the supplied fields where they
  * exist
  * 		A staged contribution with some relevant fields and some irrelevant
  * fields will come back as the defaults, with relevant keys overwritten by
  * the supplied fields where they exist. The irrelevant fields should not
  * come back at all.
  * 		A staged contribution with boolean (checkbox) fields will come back
  * with those values either set to "1" or "0", depending solely on whether
  * they exist in the supplied parameters or not.
  */
 function testStageContribution()
 {
     $start = array('bears' => 'green', 'emus' => 'purple');
     $expected = ContributionTrackingProcessor::getContributionDefaults();
     $result = ContributionTrackingProcessor::stage_contribution($start);
     $this->assertEquals($expected, $result, "Staged Contribution with no defined fields should be exactly all the default values.");
     $additional = array('note' => 'B Flat', 'referrer' => 'phpunit_processor', 'anonymous' => 'Raspberries');
     $expected = array('note' => 'B Flat', 'referrer' => 'phpunit_processor', 'anonymous' => 1, 'utm_source' => null, 'utm_medium' => null, 'utm_campaign' => null, 'optout' => 0, 'language' => null, 'owa_session' => null, 'owa_ref' => null, 'ts' => null);
     $result = ContributionTrackingProcessor::stage_contribution($additional);
     $this->assertEquals($expected, $result, "Contribution not staging properly.");
     $start = array_merge($start, $additional);
     $result = ContributionTrackingProcessor::stage_contribution($start);
     $this->assertEquals($expected, $result, "Contribution not staging properly.");
     $complete = array('note' => 'Batman', 'referrer' => 'phpunit_processor', 'anonymous' => 'of course', 'utm_source' => 'batcave', 'utm_medium' => 'Alfred', 'utm_campaign' => 'Joker', 'language' => 'squeak!', 'owa_session' => 'arghargh', 'owa_ref' => 'test', 'ts' => '11235813');
     $expected = $complete;
     $expected['anonymous'] = 1;
     $expected['optout'] = 0;
     $result = ContributionTrackingProcessor::stage_contribution($complete);
     $this->assertEquals($expected, $result, "Contribution not staging properly.");
 }