/**
  * 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.");
 }
 /**
  * There are a few values that come in, which are both generated by
  * checkboxes, and are the exact inverse of the way we save them in the
  * table.
  * For these values, if the key exists (and is not explicit false), it is
  * received as "true". Therefore, the rekey'd value should be false.
  * However, the old key not existing isn't exactly conclusive.
  * @param array $array The array to rekey (by reference)
  * @param string $oldkey The key to change
  * @param string $invertedkey The key meant to contain the inverted boolean
  * of the old key.
  */
 static function rekey_invert_boolean(&$array, $oldkey, $invertedkey)
 {
     if (array_key_exists($oldkey, $array)) {
         if ($array[$oldkey] !== false) {
             unset($array[$oldkey]);
             $array[$invertedkey] = false;
         } else {
             $array[$invertedkey] = 1;
         }
         return;
     }
     if (array_key_exists($invertedkey, $array)) {
         ContributionTrackingProcessor::stage_checkbox($array, $invertedkey);
         return;
     }
     //at this point, neither key exists. We go with the default.
     $default = ContributionTrackingProcessor::getContributionDefaults();
     if (array_key_exists($invertedkey, $default)) {
         $array[$invertedkey] = $default[$invertedkey];
     }
 }