function _testImageServe($timeZone) { OA_setTimeZone($timeZone); $fileName = 'tz_test.gif'; $doImages = OA_Dal::factoryDO('images'); $doImages->filename = $fileName; $doImages->contents = ''; $this->assertTrue(DataGenerator::generateOne($doImages)); $now = time(); $this->assertTrue($timeZone == 'UTC' || date('Z', $now), 'Time zone not correctly set'); // Simulate delivery OA_setTimeZoneUTC(); $aCreative = OA_Dal_Delivery_getCreative($fileName); $this->assertTrue($aCreative); // Serve with no If-Modified-Since header unset($GLOBALS['_HEADERS']); unset($_SERVER['HTTP_IF_MODIFIED_SINCE']); MAX_imageServe($aCreative, $fileName, 'gif'); if ($this->assertEqual(count($GLOBALS['_HEADERS']), 2, 'Mismatching headers with ' . $timeZone)) { $this->assertPattern('/^Last-Modified: /i', $GLOBALS['_HEADERS'][0]); $this->assertPattern('/^Content-Type: /i', $GLOBALS['_HEADERS'][1]); } // 1-day old If-Modified-Since header unset($GLOBALS['_HEADERS']); $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $now - 86400) . ' GMT'; MAX_imageServe($aCreative, $fileName, 'gif'); if ($this->assertEqual(count($GLOBALS['_HEADERS']), 2, 'Mismatching headers with ' . $timeZone)) { $this->assertPattern('/^Last-Modified: /i', $GLOBALS['_HEADERS'][0]); $this->assertPattern('/^Content-Type: /i', $GLOBALS['_HEADERS'][1]); } // 1-day future If-Modified-Since header unset($GLOBALS['_HEADERS']); $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $now + 86400) . ' GMT'; MAX_imageServe($aCreative, $fileName, 'gif'); if ($this->assertEqual(count($GLOBALS['_HEADERS']), 1, 'Mismatching headers with ' . $timeZone)) { $this->assertPattern('/^HTTP\\/1.0 304/i', $GLOBALS['_HEADERS'][0]); } // 1 minute ago If-Modified-Since header unset($GLOBALS['_HEADERS']); $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $now - 60) . ' GMT'; MAX_imageServe($aCreative, $fileName, 'gif'); if ($this->assertEqual(count($GLOBALS['_HEADERS']), 2, 'Mismatching headers with ' . $timeZone)) { $this->assertPattern('/^Last-Modified: /i', $GLOBALS['_HEADERS'][0]); $this->assertPattern('/^Content-Type: /i', $GLOBALS['_HEADERS'][1]); } // 1 minute in future If-Modified-Since header unset($GLOBALS['_HEADERS']); $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $now + 60) . ' GMT'; MAX_imageServe($aCreative, $fileName, 'gif'); if ($this->assertEqual(count($GLOBALS['_HEADERS']), 1, 'Mismatching headers with ' . $timeZone)) { $this->assertPattern('/^HTTP\\/1.0 304/i', $GLOBALS['_HEADERS'][0]); } }
/** * A method to be run before all tests. */ function setUp() { // Set up the configuration array to have an operation interval // of 60 minutes, and set the timezone to GMT $aConf =& $GLOBALS['_MAX']['CONF']; $aConf['maintenance']['operationInteval'] = 60; OA_setTimeZone('GMT'); //setTimeZoneLocation($aConf['timezone']['location']); // Set up the database handler object $this->oDbh =& OA_DB::singleton(); // Set up the service locator object $this->oServiceLocator =& OA_ServiceLocator::instance(); // Discover the number of operation intervals per week $this->intervalsPerWeek = OX_OperationInterval::operationIntervalsPerWeek(); // This test was written with a ZONE_FORECAST_DEFAULT_ZONE_IMPRESSIONS value of 10 in mind. OA_Dal_Maintenance_Priority::$ZONE_FORECAST_DEFAULT_ZONE_IMPRESSIONS = 10; }
/** * Check to see if this impression contains the valid date. * * @param string $limitation The date limitation * @param string $op The operator (either '==' or '!=') * @param array $aParams An array of additional parameters to be checked * @return boolean Whether this impression's date passes this limitation's test. */ function MAX_checkTime_Date($limitation, $op, $aParams = array()) { // Get timezone, if any $offset = strpos($limitation, '@'); if ($offset !== false) { $tz = substr($limitation, $offset + 1); $limitation = substr($limitation, 0, $offset); } else { $tz = false; } if ($limitation == '' && $limitation == '00000000') { return true; } $timestamp = !empty($aParams['timestamp']) ? $aParams['timestamp'] : time(); if ($tz && $tz != 'UTC') { OA_setTimeZone($tz); $date = date('Ymd', $timestamp); OA_setTimeZoneUTC(); } else { $date = gmdate('Ymd', $timestamp); } switch ($op) { case '==': return $date == $limitation; break; case '!=': return $date != $limitation; break; case '<=': return $date <= $limitation; break; case '>=': return $date >= $limitation; break; case '<': return $date < $limitation; break; case '>': return $date > $limitation; break; } return true; }
/** * Check to see if this impression contains the valid day. * * @param string $limitation The day limitation * @param string $op The operator (either '==' or '!=') * @param array $aParams An array of additional parameters to be checked * @return boolean Whether this impression's day passes this limitation's test. */ function MAX_checkTime_Day($limitation, $op, $aParams = array()) { // Get timezone, if any $offset = strpos($limitation, '@'); if ($offset !== false) { $tz = substr($limitation, $offset + 1); $limitation = substr($limitation, 0, $offset); } else { $tz = false; } if ($limitation == '') { return true; } $timestamp = !empty($aParams['timestamp']) ? $aParams['timestamp'] : time(); if ($tz && $tz != 'UTC') { OA_setTimeZone($tz); $day = date('w', $timestamp); OA_setTimeZoneUTC(); } else { $day = gmdate('w', $timestamp); } return MAX_limitationsMatchArrayValue($day, $limitation, $op, $aParams); }
function OA_setTimeZoneLocal() { $tz = !empty($GLOBALS['_MAX']['PREF']['timezone']) ? $GLOBALS['_MAX']['PREF']['timezone'] : 'GMT'; OA_setTimeZone($tz); }
/** * 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); } } }
/** * The constructor method. */ function __construct() { parent::__construct(); Mock::generatePartial('OA_Dll_Audit', 'PartialMockOA_Dll_Audit', array()); OA_setTimeZone('Europe/Rome'); }
/** * A method to log debugging messages to the location configured by the user. * * Note: If the global variable $currentTimezone is set, where the array * is the result of OX_Admin_Timezones::getTimezone(), called BEFORE any * timezone information has been set (i.e. before the init script has been * called), then this method will ensure that all debug messages are logged * in the SERVER TIME ZONE, rather than the time zone that the software * happens to be running in (i.e. the current manager timezone, or UTC for * maintenance). * * @static * @param mixed $message Either a string or a PEAR_Error object. * @param integer $priority The priority of the message. One of: * PEAR_LOG_EMERG, PEAR_LOG_ALERT, PEAR_LOG_CRIT * PEAR_LOG_ERR, PEAR_LOG_WARNING, PEAR_LOG_NOTICE * PEAR_LOG_INFO, PEAR_LOG_DEBUG * @return boolean True on success or false on failure. * * @TODO Logging to anything other than a file is probably broken - test! */ function debug($message = null, $priority = PEAR_LOG_INFO) { $aConf = $GLOBALS['_MAX']['CONF']; global $tempDebugPrefix; // Logging is not activated if ($aConf['log']['enabled'] == false) { unset($GLOBALS['tempDebugPrefix']); return true; } // Is this a "no message" log? if (is_null($message) && $aConf['log']['type'] == 'file') { // Set the priority to the highest level, so it is always logged $priority = PEAR_LOG_EMERG; } // Deal with the config file containing the log level by // name or by number $priorityLevel = is_numeric($aConf['log']['priority']) ? $aConf['log']['priority'] : @constant($aConf['log']['priority']); if (is_null($priorityLevel)) { $priorityLevel = $aConf['log']['priority']; } if ($priority > $priorityLevel) { unset($GLOBALS['tempDebugPrefix']); return true; } // Grab DSN if we are logging to a database $dsn = $aConf['log']['type'] == 'sql' ? Base::getDsn() : ''; // Instantiate a logger object based on logging options $aLoggerConf = array($aConf['log']['paramsUsername'], $aConf['log']['paramsPassword'], 'dsn' => $dsn, 'mode' => octdec($aConf['log']['fileMode']), 'timeFormat' => '%b %d %H:%M:%S %z'); if (is_null($message) && $aConf['log']['type'] == 'file') { $aLoggerConf['lineFormat'] = '%4$s'; } else { if ($aConf['log']['type'] == 'file') { $aLoggerConf['lineFormat'] = '%1$s %2$s [%3$9s] %4$s'; } } $ident = !empty($GLOBALS['_MAX']['LOG_IDENT']) ? $GLOBALS['_MAX']['LOG_IDENT'] : $aConf['log']['ident']; if ($ident == $aConf['log']['ident'] . '-delivery' && empty($aConf['deliveryLog']['enabled'])) { unset($GLOBALS['tempDebugPrefix']); return true; } $logFile = $ident == $aConf['log']['ident'] . '-delivery' ? $aConf['deliveryLog']['name'] : $aConf['log']['name']; $oLogger =& Log::singleton($aConf['log']['type'], MAX_PATH . '/var/' . $logFile, $ident, $aLoggerConf); // If log message is an error object, extract info if (PEAR::isError($message)) { $userinfo = $message->getUserInfo(); $message = $message->getMessage(); if (!empty($userinfo)) { if (is_array($userinfo)) { $userinfo = implode(', ', $userinfo); } $message .= ' : ' . $userinfo; } } // Obtain backtrace information $aBacktrace = debug_backtrace(); if ($aConf['log']['methodNames']) { // Show from four calls up the stack, to avoid the // showing the PEAR error call info itself $aErrorBacktrace = $aBacktrace[4]; if (isset($aErrorBacktrace['class']) && $aErrorBacktrace['type'] && isset($aErrorBacktrace['function'])) { $callInfo = $aErrorBacktrace['class'] . $aErrorBacktrace['type'] . $aErrorBacktrace['function'] . ': '; $message = $callInfo . $message; } } // Show entire stack, line-by-line if ($aConf['log']['lineNumbers']) { foreach ($aBacktrace as $aErrorBacktrace) { if (isset($aErrorBacktrace['file']) && isset($aErrorBacktrace['line'])) { $message .= "\n" . str_repeat(' ', 20 + strlen($aConf['log']['ident']) + strlen($oLogger->priorityToString($priority))); $message .= 'on line ' . $aErrorBacktrace['line'] . ' of "' . $aErrorBacktrace['file'] . '"'; } } } // Log messages in the local server timezone, if possible global $serverTimezone; if (!empty($serverTimezone)) { $currentTimezone = OX_Admin_Timezones::getTimezone(); OA_setTimeZone($serverTimezone); } // Log the message if (is_null($message) && $aConf['log']['type'] == 'file') { $message = ' '; } else { if (!is_null($tempDebugPrefix) && $aConf['log']['type'] == 'file') { $message = $tempDebugPrefix . $message; } } $result = $oLogger->log($message, $priority); // Restore the timezone if (!empty($currentTimezone)) { OA_setTimeZone($currentTimezone); } unset($GLOBALS['tempDebugPrefix']); return $result; }
/** * The constructor method. */ function OA_Dll_AuditTest() { $this->UnitTestCase(); Mock::generatePartial('OA_Dll_Audit', 'PartialMockOA_Dll_Audit', array()); OA_setTimeZone('Europe/Rome'); }