Example #1
0
 /**
  * A singleton method to create or return a single instance
  * of the {@link OA_DB_Table_Priority} object.
  *
  * @static
  * @return OA_DB_Table_Priority The created {@link OA_DB_Table_Priority} object.
  */
 function &singleton()
 {
     $static =& $GLOBALS['_OA']['TABLES'][__CLASS__];
     if (!isset($static)) {
         $static = new OA_DB_Table_Priority();
         // Don't use a reference here!
         $static->init(MAX_PATH . '/etc/tables_temp_priority.xml');
     }
     return $static;
 }
 /**
  * Tests creating/dropping all of the MPE temporary tables.
  *
  * Note that testing for the existence of temporary tables is done via
  * SELECT statements, as some databases (e.g. MySQL) do not allow
  * temporary tables to be seen via any administrative commands.
  *
  * Requirements:
  * Test 1: Test that all MPE temporary tables can be created and dropped.
  */
 function testAllMaintenancePriorityTables()
 {
     $tmpTables = array('tmp_ad_required_impression', 'tmp_ad_zone_impression');
     // Test 1
     $conf =& $GLOBALS['_MAX']['CONF'];
     $conf['table']['prefix'] = '';
     $oDbh =& OA_DB::singleton();
     foreach ($tmpTables as $tableName) {
         $query = "SELECT * FROM {$tableName}";
         RV::disableErrorHandling();
         $result = $oDbh->query($query);
         RV::enableErrorHandling();
         $this->assertEqual(strtolower(get_class($result)), 'mdb2_error');
     }
     $oTable =& OA_DB_Table_Priority::singleton();
     foreach ($tmpTables as $tableName) {
         $oTable->createTable($tableName);
     }
     $aExistingTables = OA_DB_Table::listOATablesCaseSensitive();
     foreach ($tmpTables as $tableName) {
         // Test that the table has been created
         $query = "SELECT * FROM {$tableName}";
         $result = $oDbh->query($query);
         $this->assertTrue($result);
         // Test that the table can be dropped
         // Use a different query to overcome MDB2 query buffering
         $query = "SELECT foo FROM {$tableName}";
         RV::disableErrorHandling();
         $result = $oDbh->query($query);
         RV::enableErrorHandling();
         $this->assertEqual(strtolower(get_class($result)), 'mdb2_error');
     }
     // Restore the testing environment
     TestEnv::restoreEnv();
 }
 /**
  * A method to test the saveAllocatedImpressions method.
  */
 function testSaveAllocatedImpressions()
 {
     $oDbh =& OA_DB::singleton();
     $oDal = new OA_Dal_Maintenance_Priority();
     // Create the required temporary table for the tests
     $oTable =& OA_DB_Table_Priority::singleton();
     $oTable->createTable('tmp_ad_zone_impression');
     // Prepare the test data
     $aData = array(array('ad_id' => 56, 'zone_id' => 11, 'required_impressions' => 9997, 'requested_impressions' => 9000), array('ad_id' => 56, 'zone_id' => 12, 'required_impressions' => 24, 'requested_impressions' => 24));
     $result = $oDal->saveAllocatedImpressions($aData);
     $query = "SELECT * FROM " . $oDbh->quoteIdentifier('tmp_ad_zone_impression', true) . " ORDER BY ad_id, zone_id";
     $rc = $oDbh->query($query);
     $result = $rc->fetchAll();
     $this->assertTrue(is_array($result));
     $this->assertTrue(count($result) == 2);
     $tmp = $result[0];
     $this->assertTrue(array_key_exists('ad_id', $tmp));
     $this->assertEqual($tmp['ad_id'], 56);
     $this->assertTrue(array_key_exists('zone_id', $tmp));
     $this->assertEqual($tmp['zone_id'], 11);
     $this->assertTrue(array_key_exists('required_impressions', $tmp));
     $this->assertEqual($tmp['required_impressions'], 9997);
     $this->assertTrue(array_key_exists('requested_impressions', $tmp));
     $this->assertEqual($tmp['requested_impressions'], 9000);
     $tmp = $result[1];
     $this->assertTrue(array_key_exists('ad_id', $tmp));
     $this->assertEqual($tmp['ad_id'], 56);
     $this->assertTrue(array_key_exists('zone_id', $tmp));
     $this->assertEqual($tmp['zone_id'], 12);
     $this->assertTrue(array_key_exists('required_impressions', $tmp));
     $this->assertEqual($tmp['required_impressions'], 24);
     $this->assertTrue(array_key_exists('requested_impressions', $tmp));
     $this->assertEqual($tmp['requested_impressions'], 24);
     TestEnv::dropTempTables();
 }
Example #4
0
 /**
  * Method to create/register/return the Maintenance Priority table class.
  *
  * @access private
  * @return OA_DB_Table_Priority
  */
 function &_getMaxTablePriorityObj()
 {
     $dbType = strtolower($GLOBALS['_MAX']['CONF']['database']['type']);
     $oServiceLocator =& OA_ServiceLocator::instance();
     $oTable = $oServiceLocator->get('OA_DB_Table_Priority');
     if (!$oTable) {
         $oTable =& OA_DB_Table_Priority::singleton();
         $oServiceLocator->register('OA_DB_Table_Priority', $oTable);
     }
     return $oTable;
 }
 /**
  * execute the Priority engine tasks
  */
 function runPriority()
 {
     // Run maintenance before recalculating priorities
     $this->runMaintenance();
     $this->printHeading('Starting updatePriorities; date: ' . $this->_getDateTimeString(), 3);
     $oMaintenancePriority = new OA_Maintenance_Priority_AdServer();
     $oMaintenancePriority->updatePriorities();
     $this->printPriorities();
     $this->printHeading('End updatePriorities; date: ' . $this->_getDateTimeString(), 3);
     // Hack! The TestEnv class doesn't always drop temp tables for some
     // reason, so drop them "by hand", just in case.
     $dbType = strtolower($GLOBALS['_MAX']['CONF']['database']['type']);
     $oTable =& OA_DB_Table_Priority::singleton();
     $oTable->dropTable("tmp_ad_required_impression");
     $oTable->dropTable("tmp_ad_zone_impression");
 }
 /**
  * A method to distribute the calculated required campaign impressions between the campaign's
  * children advertisements. Impression allocation takes in to account ad weight, and the number
  * of operations intervals the ad will be active in given date/time delivery limitations, and
  * the pattern of available impressions for the zone(s) the advertisements are linked to.
  *
  * The calculated ad impressions are written to the temporary table tmp_ad_required_impression
  * for later analysis by the {@link OA_Maintenance_Priority_AdServer_Task_AllocateZoneImpressions}
  * class.
  *
  * @param array $aCampaigns An array of {@link OX_Maintenance_Priority_Campaign} objects which require
  *                          that their total required impressions be distributed between the
  *                          component advertisements.
  */
 function distributeCampaignImpressions($aCampaigns)
 {
     // Create an array for storing required ad impressions
     $aRequiredAdImpressions = array();
     // Get the current operation interval start/end dates
     $aCurrentOperationIntervalDates = OX_OperationInterval::convertDateToOperationIntervalStartAndEndDates($this->_getDate());
     // For each campaign
     foreach ($aCampaigns as $oCampaign) {
         OA::debug('  - Distributing impression inventory requirements for campaign ID: ' . $oCampaign->id, PEAR_LOG_DEBUG);
         $adsCount = count($oCampaign->aAds);
         OA::debug("    - Campaign has {$adsCount} ads.", PEAR_LOG_DEBUG);
         // Get date object to represent campaign expiration date
         if ($oCampaign->impressionTargetDaily > 0 || $oCampaign->clickTargetDaily > 0 || $oCampaign->conversionTargetDaily > 0) {
             // The campaign has a daily target to meet, so treat the
             // campaign as if it expires at the end of "today", regardless
             // of the existance of any activation or expiration dates that
             // may (or may not) be set for the campaign
             $oCampaignExpiryDate = new Date($this->_getDate());
             $oCampaignExpiryDate->setTZ($this->currentTz);
             $oCampaignExpiryDate->setHour(23);
             $oCampaignExpiryDate->setMinute(59);
             $oCampaignExpiryDate->setSecond(59);
             $oCampaignExpiryDate->toUTC();
             // Unless the campaign has an expiry date and it happens before the end of today
             if (!empty($oCampaign->expireTime)) {
                 if ($oCampaignExpiryDate->after($this->_getDate($oCampaign->expireTime))) {
                     $oCampaignExpiryDate = $this->_getDate($oCampaign->expireTime);
                 }
             }
         } else {
             if (!empty($oCampaign->expireTime) && ($oCampaign->impressionTargetTotal > 0 || $oCampaign->clickTargetTotal > 0 || $oCampaign->conversionTargetTotal > 0)) {
                 // The campaign has an expiration date, and has some kind of
                 // (total) inventory requirement, so treat the campaign as if
                 // it expires at the expiration date/time
                 $oCampaignExpiryDate = $this->_getDate($oCampaign->expireTime);
             } else {
                 // Error! There should not be any other kind of high-priority
                 // campaign in terms of activation/expiration dates and
                 // either (total) inventory requirements or daily targets
                 $message = "- Error calculating the end date for Campaign ID {$oCampaign->id}";
                 OA::debug($message, PEAR_LOG_ERR);
                 continue;
             }
         }
         // Determine number of remaining operation intervals for campaign
         $message = "    - Calculating campaign remaining operation intervals.";
         OA::debug($message, PEAR_LOG_DEBUG);
         $campaignRemainingOperationIntervals = OX_OperationInterval::getIntervalsRemaining($aCurrentOperationIntervalDates['start'], $oCampaignExpiryDate);
         // For all ads in the campaign, determine:
         // - If the ad is capable of delivery in the current operation
         //   interval, or not, based on if it is linked to any zones, and,
         //   if so:
         // - If the ad is capable of delivery in the current operation
         //   interval, or not, based on delivery limitation(s), and if so;
         // - The result of the weight of the ad multiplied by the
         //   number of operation intervals remaining in which the ad
         //   is capable of delivering
         $aAdZones = array();
         $aAdDeliveryLimitations = array();
         $aAdBlockedForCurrentOI = array();
         $aAdWeightRemainingOperationIntervals = array();
         $aInvalidAdIds = array();
         reset($oCampaign->aAds);
         while (list($key, $oAd) = each($oCampaign->aAds)) {
             // Only calculate values for active ads
             if ($oAd->active && $oAd->weight > 0) {
                 $message = "    - Calculating remaining operation intervals for ad ID: {$oAd->id}";
                 OA::debug($message, PEAR_LOG_DEBUG);
                 // Get all zones associated with the ad
                 $aAdsZones = $this->oDal->getAdZoneAssociationsByAds(array($oAd->id));
                 $aAdZones[$oAd->id] = @$aAdsZones[$oAd->id];
                 if (is_null($aAdZones[$oAd->id])) {
                     $aInvalidAdIds[] = $oAd->id;
                     $message = "      - Ad ID {$oAd->id} has no linked zones, will skip...";
                     OA::debug($message, PEAR_LOG_ERR);
                     continue;
                 }
                 // Prepare a delivery limitation object for the ad
                 $aAdDeliveryLimitations[$oAd->id] = new OA_Maintenance_Priority_DeliveryLimitation($oAd->getDeliveryLimitations());
                 // Is the ad blocked from delivering in the current operation interval?
                 $aAdBlockedForCurrentOI[$oAd->id] = $aAdDeliveryLimitations[$oAd->id]->deliveryBlocked($aCurrentOperationIntervalDates['start']);
                 // Determine how many operation intervals remain that the ad can deliver in
                 $adRemainingOperationIntervals = $aAdDeliveryLimitations[$oAd->id]->getActiveAdOperationIntervals($campaignRemainingOperationIntervals, $aCurrentOperationIntervalDates['start'], $oCampaignExpiryDate);
                 // Determine the value of the ad weight multiplied by the number
                 // of operation intervals remaining that the ad can deliver in
                 if ($oAd->weight > 0) {
                     $aAdWeightRemainingOperationIntervals[$oAd->id] = $oAd->weight * $adRemainingOperationIntervals;
                 } else {
                     $aAdWeightRemainingOperationIntervals[$oAd->id] = 0;
                 }
             }
         }
         // Get the total sum of the ad weight * remaining OI values
         $sumAdWeightRemainingOperationIntervals = array_sum($aAdWeightRemainingOperationIntervals);
         // For each (active) ad that is capable of delivering in the current
         // operation interval, determine how many of the campaign's required
         // impressions should be alloced as the ad's required impressions
         // For each advertisement
         reset($oCampaign->aAds);
         while (list($key, $oAd) = each($oCampaign->aAds)) {
             if (in_array($oAd->id, $aInvalidAdIds)) {
                 OA::debug('       - Skipping ad ID: ' . $oAd->id, PEAR_LOG_DEBUG);
                 continue;
             }
             OA::debug('     - Calculating required impressions for ad ID: ' . $oAd->id, PEAR_LOG_DEBUG);
             // Get impressions required
             $totalRequiredAdImpressions = 0;
             if ($oAd->active && $oAd->weight > 0 && $aAdBlockedForCurrentOI[$oAd->id] !== true) {
                 $totalRequiredAdImpressions = $oCampaign->requiredImpressions * ($aAdWeightRemainingOperationIntervals[$oAd->id] / $sumAdWeightRemainingOperationIntervals);
             }
             if ($totalRequiredAdImpressions <= 0) {
                 OA::debug('       - No required impressions for ad ID: ' . $oAd->id, PEAR_LOG_DEBUG);
                 continue;
             }
             // Based on the average zone pattern of the zones the ad is
             // linked to, calculate how many of these impressions should
             // be delivered in the next operation interval
             OA::debug('       - Calculating next OI required impressions for ad ID: ' . $oAd->id, PEAR_LOG_DEBUG);
             $oAd->requiredImpressions = $this->_getAdImpressions($oAd, $totalRequiredAdImpressions, $aCurrentOperationIntervalDates['start'], $oCampaignExpiryDate, $aAdDeliveryLimitations[$oAd->id], $aAdZones[$oAd->id]);
             $aRequiredAdImpressions[] = array('ad_id' => $oAd->id, 'required_impressions' => $oAd->requiredImpressions);
         }
     }
     // Save the required impressions into the temporary database table
     OA::setTempDebugPrefix('- ');
     // Check if table exists
     if (!isset($GLOBALS['_OA']['DB_TABLES']['tmp_ad_required_impression'])) {
         if ($this->oTable->createTable('tmp_ad_required_impression', null, true) !== false) {
             // Remember that table was created
             $GLOBALS['_OA']['DB_TABLES']['tmp_ad_required_impression'] = true;
         }
     }
     $this->oDal->saveRequiredAdImpressions($aRequiredAdImpressions);
 }
 /**
  * Method to test the getAllZonesWithAllocInv method.
  *
  * Requirements:
  * Test 1: Test with no data, and ensure no data returned.
  * Test 2: Test with sample data, and ensure the correct data is returned.
  */
 function DEPRECATED_testGetAllZonesWithAllocInv()
 {
     $conf = $GLOBALS['_MAX']['CONF'];
     $oDbh =& OA_DB::singleton();
     $oMaxDalMaintenance = new OA_Dal_Maintenance_Priority();
     // Create the required temporary table for the tests
     $oTable =& OA_DB_Table_Priority::singleton();
     $oTable->createTable('tmp_ad_zone_impression');
     $tableTmp = $oDbh->quoteIdentifier('tmp_ad_zone_impression', true);
     // Test 1
     $result =& $oMaxDalMaintenance->getAllZonesWithAllocInv();
     $this->assertEqual(count($result), 0);
     // Test 2
     $query = "\n            INSERT INTO\n                {$tableTmp}\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions\n                )\n            VALUES\n                (\n                    1,\n                    1,\n                    2,\n                    3\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                {$tableTmp}\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions\n                )\n            VALUES\n                (\n                    1,\n                    2,\n                    4,\n                    5\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                {$tableTmp}\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions\n                )\n            VALUES\n                (\n                    2,\n                    2,\n                    6,\n                    7\n                )";
     $rows = $oDbh->exec($query);
     $result =& $oMaxDalMaintenance->getAllZonesWithAllocInv();
     $this->assertEqual(count($result), 3);
     $this->assertEqual($result[0]['zone_id'], 1);
     $this->assertEqual($result[0]['ad_id'], 1);
     $this->assertEqual($result[0]['required_impressions'], 2);
     $this->assertEqual($result[0]['requested_impressions'], 3);
     $this->assertEqual($result[1]['zone_id'], 2);
     $this->assertEqual($result[1]['ad_id'], 1);
     $this->assertEqual($result[1]['required_impressions'], 4);
     $this->assertEqual($result[1]['requested_impressions'], 5);
     $this->assertEqual($result[2]['zone_id'], 2);
     $this->assertEqual($result[2]['ad_id'], 2);
     $this->assertEqual($result[2]['required_impressions'], 6);
     $this->assertEqual($result[2]['requested_impressions'], 7);
     TestEnv::dropTempTables();
 }
 /**
  * The test to ensure that a campaign with banners that have time-based
  * delivery limitations are correctly prioritised by the MPE.
  *
  * Test Basis:
  *
  * - One campaign, running from 2008-02-26 to 2008-02-27 (2 days).
  * - Booked impressions of 48,000 impressions (i.e. 1,000 per hour
  *     required on average).
  * - Two banners in the zone, both with weight one.
  *   - Banner ID 1 has a Time:Date delivery limitation, to only allow
  *       the banner to deliver on 2008-02-26.
  *   - Banner ID 2 has a Time:Date delivery limitation, to only allow
  *       the banner to deliver on 2008-02-27.
  * - The campaign is linked to one constantly delivering zone (at
  *     1,000 impressions per hour).
  *
  * - Run the MPE with an OI of 60 minutes for the two days of the
  *     campaign lifetime, and assume that all required impressions
  *     allocated to the banner(s) are delivered.
  *
  * The expected result of this is that the MPE should allocate 1,000
  * impressions per hour for Banner ID 1 all day on 2008-02-26, and
  * 1,000 impressions per hour for Banner ID 2 all day on 2008-02-37.
  */
 function testCampaign()
 {
     $aConf =& $GLOBALS['_MAX']['CONF'];
     $aConf['maintenance']['operationInteval'] = 60;
     $aConf['priority']['useZonePatterning'] = false;
     OA_setTimeZone('GMT');
     $oServiceLocator =& OA_ServiceLocator::instance();
     $oServiceLocator->register('now', new Date('2008-02-27'));
     // Prepare the test campaign
     $doCampaigns = OA_Dal::factoryDO('campaigns');
     $doCampaigns->views = 48000;
     $doCampaigns->clicks = -1;
     $doCampaigns->conversions = -1;
     $doCampaigns->activate_time = '2008-02-26 00:00:00';
     $doCampaigns->expire_time = '2008-02-27 23:59:59';
     $doCampaigns->priority = 10;
     $doCampaigns->target_impression = 0;
     $doCampaigns->target_click = 0;
     $doCampaigns->target_conversion = 0;
     $campaignId = DataGenerator::generateOne($doCampaigns);
     // Prepare the first banner
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $campaignId;
     $doBanners->active = 't';
     $doBanners->weight = 1;
     $bannerId1 = DataGenerator::generateOne($doBanners);
     $doAcls = OA_Dal::factoryDO('acls');
     $doAcls->bannerid = $bannerId1;
     $doAcls->logical = 'and';
     $doAcls->type = 'deliveryLimitations:Time:Date';
     $doAcls->comparison = '==';
     $doAcls->data = '20080226';
     $doAcls->executionorder = 0;
     DataGenerator::generateOne($doAcls);
     // Prepare the second banner
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $campaignId;
     $doBanners->active = 't';
     $doBanners->weight = 1;
     $bannerId2 = DataGenerator::generateOne($doBanners);
     $doAcls = OA_Dal::factoryDO('acls');
     $doAcls->bannerid = $bannerId2;
     $doAcls->logical = 'and';
     $doAcls->type = 'deliveryLimitations:Time:Date';
     $doAcls->comparison = '==';
     $doAcls->data = '20080227';
     $doAcls->executionorder = 0;
     DataGenerator::generateOne($doAcls);
     // Prepare the zone
     $doZones = OA_Dal::factoryDO('zones');
     $zoneId = DataGenerator::generateOne($doZones);
     // Link the banners to the zone
     $doAd_zone_assoc = OA_Dal::factoryDO('ad_zone_assoc');
     $doAd_zone_assoc->zone_id = $zoneId;
     $doAd_zone_assoc->ad_id = $bannerId1;
     DataGenerator::generateOne($doAd_zone_assoc);
     $doAd_zone_assoc = OA_Dal::factoryDO('ad_zone_assoc');
     $doAd_zone_assoc->zone_id = $zoneId;
     $doAd_zone_assoc->ad_id = $bannerId2;
     DataGenerator::generateOne($doAd_zone_assoc);
     // Run the code to get the required ad impressions over
     // the 48 hour period of the test
     for ($counter = 1; $counter <= 48; $counter++) {
         // Set the "current" date/time that the MPE would be
         // running at for the appropriate hour of the test
         $oNowDate = new Date('2008-02-26 00:00:01');
         $oNowDate->addSeconds(($counter - 1) * SECONDS_PER_HOUR);
         $oServiceLocator->register('now', $oNowDate);
         // Run the code to get the required ad impressions
         $oGetRequiredAdImpressionsLifetime = new OA_Maintenance_Priority_AdServer_Task_GetRequiredAdImpressionsLifetime();
         $oGetRequiredAdImpressionsLifetime->run();
         // Test that 1,000 impressions have been "required" for
         // the appropriate banner
         $query = "SELECT * FROM tmp_ad_required_impression";
         $rsRequiredImpression = DBC::NewRecordSet($query);
         $rsRequiredImpression->find();
         $aRequiredImpressions = $rsRequiredImpression->getAll();
         $this->assertTrue(is_array($aRequiredImpressions), "No array for required impressions SQL result in test hour {$counter}");
         $this->assertEqual(count($aRequiredImpressions), 1, "More than one row found for required impressions SQL result in test hour {$counter}");
         $this->assertTrue(is_array($aRequiredImpressions[0]), "Badly formatted result row for required impressions SQL result in test hour {$counter}");
         $this->assertEqual(count($aRequiredImpressions[0]), 2, "Badly formatted result row for required impressions SQL result in test hour {$counter}");
         $bannerId = $aRequiredImpressions[0]['ad_id'];
         if ($counter <= 24) {
             $this->assertEqual($bannerId, $bannerId1, "Expected required impressions for banner ID {$bannerId1} in test hour {$counter}");
         } else {
             $this->assertEqual($bannerId, $bannerId2, "Expected required impressions for banner ID {$bannerId2} in test hour {$counter}");
         }
         $impressions = $aRequiredImpressions[0]['required_impressions'];
         $this->assertEqual($impressions, 1000, "Incorrectly requested {$impressions} impressions instead of 1000 in test hour {$counter}");
         // Insert the required impressions for the banner into the
         // data_intermediate_ad table, as if the delivery has occurred,
         // so that the next hour's test is based on delivery having happened
         $aDates = OX_OperationInterval::convertDateToOperationIntervalStartAndEndDates($oNowDate);
         $operationIntervalId = OX_OperationInterval::convertDateToOperationIntervalID($oNowDate);
         $doData_intermediate_ad = OA_Dal::factoryDO('data_intermediate_ad');
         $doData_intermediate_ad->day = $aDates['start']->format('%Y-%m-%d');
         $doData_intermediate_ad->hour = $aDates['start']->format('%H');
         $doData_intermediate_ad->operation_interval = $aConf['maintenance']['operationInteval'];
         $doData_intermediate_ad->operation_interval_id = $operationIntervalId;
         $doData_intermediate_ad->interval_start = $aDates['start']->format('%Y-%m-%d %H:%M:%S');
         $doData_intermediate_ad->interval_end = $aDates['end']->format('%Y-%m-%d %H:%M:%S');
         $doData_intermediate_ad->ad_id = $bannerId;
         $doData_intermediate_ad->zone_id = $zoneId;
         $doData_intermediate_ad->requests = $impressions;
         $doData_intermediate_ad->impressions = $impressions;
         $doData_intermediate_ad->clicks = 0;
         $doData_intermediate_ad->conversions = 0;
         DataGenerator::generateOne($doData_intermediate_ad);
         // Drop the temporary table that is used to store the
         // required impressions, so that it does not interfer
         // with the next test run in the loop
         unset($GLOBALS['_OA']['DB_TABLES']['tmp_ad_required_impression']);
         $oTable =& OA_DB_Table_Priority::singleton();
         foreach ($oTable->aDefinition['tables'] as $tableName => $aTable) {
             $oTable->truncateTable($tableName);
             $oTable->dropTable($tableName);
         }
     }
 }
 public function _generateData($priority)
 {
     $oDbh =& OA_DB::singleton();
     // Create the required temporary table for the tests
     $oTable =& OA_DB_Table_Priority::singleton();
     $oTable->createTable('tmp_ad_zone_impression');
     // set up agencies, affiliates and zones
     $idClient1 = DataGenerator::generateOne('clients', true);
     $agencyId1 = DataGenerator::getReferenceId('agency');
     $idClient2 = DataGenerator::generateOne('clients', true);
     $agencyId2 = DataGenerator::getReferenceId('agency');
     $idClient3 = DataGenerator::generateOne('clients', true);
     $agencyId3 = DataGenerator::getReferenceId('agency');
     // Add affiliates (websites)
     $doAffiliates = OA_Dal::factoryDO('affiliates');
     $doAffiliates->agencyid = $agencyId1;
     $affiliateId1 = DataGenerator::generateOne($doAffiliates);
     $doAffiliates = OA_Dal::factoryDO('affiliates');
     $doAffiliates->agencyid = $agencyId3;
     $affiliateId2 = DataGenerator::generateOne($doAffiliates);
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->description = 'Test zone';
     $doZones->affiliateid = $affiliateId1;
     $this->idZone1 = DataGenerator::generateOne($doZones);
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->description = 'Test zone';
     $doZones->affiliateid = $affiliateId2;
     $idZone2 = DataGenerator::generateOne($doZones);
     $doCampaigns = OA_Dal::factoryDO('campaigns');
     $doCampaigns->clientid = $idClient1;
     $doCampaigns->priority = --$priority;
     $doCampaigns->ecpm_enabled = 1;
     $idCampaign1 = DataGenerator::generateOne($doCampaigns);
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $idCampaign1;
     $idAd1 = DataGenerator::generateOne($doBanners);
     $doCampaigns = OA_Dal::factoryDO('campaigns');
     $doCampaigns->clientid = $idClient1;
     $doCampaigns->priority = ++$priority;
     $doCampaigns->ecpm_enabled = 1;
     $idCampaign2 = DataGenerator::generateOne($doCampaigns);
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $idCampaign2;
     $idAd2 = DataGenerator::generateOne($doBanners);
     $doCampaigns = OA_Dal::factoryDO('campaigns');
     $doCampaigns->clientid = $idClient1;
     $doCampaigns->priority = ++$priority;
     $doCampaigns->ecpm_enabled = 1;
     $idCampaign3 = DataGenerator::generateOne($doCampaigns);
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $idCampaign3;
     $idAd3 = DataGenerator::generateOne($doBanners);
     $query = "\n            INSERT INTO\n                tmp_ad_zone_impression\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions,\n                    to_be_delivered\n                )\n            VALUES\n                (\n                    1,\n                    {$idZone2},\n                    2,\n                    3,\n                    1\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                tmp_ad_zone_impression\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions,\n                    to_be_delivered\n                )\n            VALUES\n                (\n                    {$idAd1},\n                    {$this->idZone1},\n                    1,\n                    5,\n                    1\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                tmp_ad_zone_impression\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions,\n                    to_be_delivered\n                )\n            VALUES\n                (\n                    {$idAd2},\n                    {$this->idZone1},\n                    2,\n                    5,\n                    1\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                tmp_ad_zone_impression\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions,\n                    to_be_delivered\n                )\n            VALUES\n                (\n                    {$idAd3},\n                    {$this->idZone1},\n                    5,\n                    5,\n                    1\n                )";
     $rows = $oDbh->exec($query);
     $query = "\n            INSERT INTO\n                tmp_ad_zone_impression\n                (\n                    ad_id,\n                    zone_id,\n                    required_impressions,\n                    requested_impressions,\n                    to_be_delivered\n                )\n            VALUES\n                (\n                    2,\n                    {$this->idZone1},\n                    6,\n                    7,\n                    0\n                )";
     $rows = $oDbh->exec($query);
     return array($agencyId1, $agencyId2);
 }
 function _testBatchInsert($method)
 {
     $oTable =& OA_DB_Table_Priority::singleton();
     $oTable->createTable('tmp_ad_required_impression');
     $this->assertEqual(array(), $this->_getbatchInsertRecords());
     $aData = array(array('ad_id' => '23', 'required_impressions' => '140'), array('ad_id' => '29', 'required_impressions' => '120'));
     $result = OA_Dal::$method('tmp_ad_required_impression', array('ad_id', 'required_impressions'), $aData);
     $this->assertEqual($result, 2);
     $result = $this->_getbatchInsertRecords();
     $this->assertTrue(count($result) == 2);
     $this->assertEqual($result, $aData);
     $oneMoreRow = array(array(100, 2));
     $result = OA_Dal::$method('tmp_ad_required_impression', array('ad_id', 'required_impressions'), $oneMoreRow);
     $this->assertEqual($result, 1);
     $result = $this->_getbatchInsertRecords();
     $this->assertTrue(count($result) == 3);
     $this->assertEqual($result, array_merge($aData, array(array('ad_id' => 100, 'required_impressions' => 2))));
     TestEnv::dropTempTables();
 }
Example #11
0
 static function dropTempTables()
 {
     $oDbh =& OA_DB::singleton();
     // Truncate & drop all existing temporary tables
     $oTable =& OA_DB_Table_Priority::singleton();
     foreach ($oTable->aDefinition['tables'] as $tableName => $aTable) {
         if ($oTable->existsTemporaryTable($tableName)) {
             $oTable->truncateTable($tableName);
             $oTable->dropTable($tableName);
         }
     }
     $oTable =& OA_DB_Table_Statistics::singleton();
     foreach ($oTable->aDefinition['tables'] as $tableName => $aTable) {
         if ($oTable->existsTemporaryTable($tableName)) {
             $oTable->truncateTable($tableName);
             $oTable->dropTable($tableName);
         }
     }
 }