/** * A method to test the MAX_Delivery_log_logVariableValues() function. */ function test_MAX_Delivery_log_logVariableValues() { $aConf =& $GLOBALS['_MAX']['CONF']; $aConf['maintenance']['operationInterval'] = 60; $GLOBALS['_MAX']['NOW'] = time(); $oNowDate = new Date($GLOBALS['_MAX']['NOW']); // Test to ensure that the openXDeliveryLog plugin's data bucket // table does not exist $oTable = new OA_DB_Table(); $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . 'data_bkt_a_var'); $this->assertFalse($tableExists); // Test calling the main logging function without any plugins installed, // to ensure that this does not result in any kind of error $aVariables = array(55 => array('variable_id' => 55, 'tracker_id' => 1, 'name' => 'fooVar', 'type' => 'string', 'variablecode' => ''), 66 => array('variable_id' => 66, 'tracker_id' => 1, 'name' => 'barVar', 'type' => 'string', 'variablecode' => '')); $_GET['fooVar'] = 'foo'; $_GET['barVar'] = 'bar'; MAX_Delivery_log_logVariableValues($aVariables, 1, 1, 'singleDB'); // Install the openXDeliveryLog plugin TestEnv::installPluginPackage('openXDeliveryLog', false); // Test to ensure that the openXDeliveryLog plugin's data bucket // table now does exist $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . 'data_bkt_a_var'); $this->assertTrue($tableExists); // Ensure that there are is nothing logged in the data bucket table $doData_bkt_a_var = OA_Dal::factoryDO('data_bkt_a_var'); $doData_bkt_a_var->find(); $rows = $doData_bkt_a_var->getRowCount(); $this->assertEqual($rows, 0); // Call the variable value logging function MAX_Delivery_log_logVariableValues($aVariables, 1, 1, 'singleDB'); // Ensure that the data was logged correctly $doData_bkt_a_var = OA_Dal::factoryDO('data_bkt_a_var'); $doData_bkt_a_var->find(); $rows = $doData_bkt_a_var->getRowCount(); $this->assertEqual($rows, 2); $doData_bkt_a_var = OA_Dal::factoryDO('data_bkt_a_var'); $doData_bkt_a_var->server_conv_id = 1; $doData_bkt_a_var->server_raw_ip = 'singleDB'; $doData_bkt_a_var->tracker_variable_id = 55; $doData_bkt_a_var->find(); $rows = $doData_bkt_a_var->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt_a_var->fetch(); $this->assertEqual($doData_bkt_a_var->value, 'foo'); $this->assertEqual($doData_bkt_a_var->date_time, $oNowDate->format('%Y-%m-%d %H:%M:%S')); // Uninstall the openXDeliveryLog plugin TestEnv::uninstallPluginPackage('openXDeliveryLog', false); // Restore the test configuration file TestEnv::restoreConfig(); }
/** * A method to perform the migration of logged bucket-based supplementary * raw statistics data from the bucket table(s) into a final statistics table. * * @param string $statisticsTableName The name of the statistics table the * data is to be migrated to. * @param array $aMigrationDetails An array containing the details of the * bucket data to migrate into the statistics * table. See the * Plugins_DeliveryLog::getStatisticsMigration() * method for details. * @param array $aDates An array containing the PEAR Date objects representing the * start and end dates for the operation interval being migrated, * indexed by "start" and "end", respectively. * @return mixed A PEAR_Error or MDB2_Error object on failure, otherwise, the number * of rows of raw data that were migrated from the bucket table to the * statistics table. */ function summariseBucketsRawSupplementary($statisticsTableName, $aMigrationDetails, $aDates) { // Perform basic checking of the parameters; assumes that $aMigrationDetails // has already been checked by the Plugins_DeliveryLog::testStatisticsMigration() // method if ($aMigrationDetails['method'] != 'rawSupplementary') { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with migration map method '{$aMigrationDetails['method']}' != 'rawSupplementary'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['masterTablePrimaryKeys']) != count($aMigrationDetails['bucketTablePrimaryKeys'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'masterTablePrimaryKeys' and 'bucketTablePrimaryKeys' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['masterTableKeys']) != count($aMigrationDetails['bucketTableKeys'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'masterTableKeys' and 'bucketTableKeys' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['source']) != count($aMigrationDetails['destination'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'source' and 'destination' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (!is_a($aDates['start'], 'Date') || !is_a($aDates['end'], 'Date')) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid start/end date parameters -- not Date objects."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (!OX_OperationInterval::checkIntervalDates($aDates['start'], $aDates['end'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid start/end date parameters -- not operation interval bounds."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } // Ensure that tables exist before trying to run commands based on // plugin components $oTable = new OA_DB_Table(); if (!$oTable->extistsTable($statisticsTableName)) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid statistics table '{$statisticsTableName}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } if (!$oTable->extistsTable($aMigrationDetails['masterTable'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid master table '{$aMigrationDetails['masterTable']}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } if (!$oTable->extistsTable($aMigrationDetails['bucketTable'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid bucket table '{$aMigrationDetails['bucketTable']}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } // Prepare the previously migrated raw data statistics table columns array $aMasterColumns = array(); foreach ($aMigrationDetails['masterTablePrimaryKeys'] as $value) { $aMasterColumns[] = $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['masterTableKeys'] as $value) { $aMasterColumns[] = $this->oDbh->quoteIdentifier($value, true); } // Prepare the query to locate the data in columns in the statistics // table which contains the previously migrated raw bucket data, // which will then be used to locate the supplementary raw data and // also to ensure that when this supplementary raw data is migrated // to its statistics table, the supplementary raw data can be // connected with the previously migrated raw data $query = "\n SELECT\n " . implode(', ', $aMasterColumns) . "\n FROM\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterTable'], true) . "\n WHERE\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterDateTimeColumn'], true) . " >= " . $this->oDbh->quote($aDates['start']->format('%Y-%m-%d %H:%M:%S'), 'timestamp') . "\n AND\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterDateTimeColumn'], true) . " <= " . $this->oDbh->quote($aDates['end']->format('%Y-%m-%d %H:%M:%S'), 'timestamp'); // Prevent any strange database error from causing execution to halt // by overriding the error handler, run the query, and return the // MDB2_Error object, if required PEAR::pushErrorHandling(null); $rsResult = $this->oDbh->query($query); PEAR::popErrorHandling(); if (PEAR::isError($rsResult)) { return $rsResult; } // Were any rows found for previously migrated summarised raw data? if ($rsResult->numRows() == 0) { return 0; } // Ensure that the required arrays are sorted by key ksort($aMigrationDetails['masterTableKeys']); ksort($aMigrationDetails['bucketTableKeys']); ksort($aMigrationDetails['source']); ksort($aMigrationDetails['destination']); // Prepare the destination columns array $aDestinationColumns = array(); foreach ($aMigrationDetails['bucketTablePrimaryKeys'] as $value) { $aDestinationColumns[] = $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['destination'] as $value) { $aDestinationColumns[] = $this->oDbh->quoteIdentifier($value, true); } $counter = 0; while ($aRow = $rsResult->fetchRow()) { // Prepare the select column statements array $aSelectColumnStatements = array(); foreach ($aMigrationDetails['bucketTablePrimaryKeys'] as $value) { $aSelectColumnStatements[] = $this->oDbh->quote($aRow[$value], 'text') . ' AS ' . $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['destination'] as $key => $value) { $aSelectColumnStatements[] = $this->oDbh->quoteIdentifier($aMigrationDetails['source'][$key], true) . ' AS ' . $this->oDbh->quoteIdentifier($value, true); } // Prepare the where statementes array $aWhereStatements = array(); foreach ($aMigrationDetails['masterTableKeys'] as $key => $value) { $aWhereStatements[] = $this->oDbh->quoteIdentifier($aMigrationDetails['bucketTableKeys'][$key], true) . ' = ' . $this->oDbh->quote($aRow[$value], 'text'); } // Prepare the query to migrate the supplementary raw data from bucket // table to the statistics table $query = "\n INSERT INTO\n " . $this->oDbh->quoteIdentifier($statisticsTableName, true) . "\n (" . implode(', ', $aDestinationColumns) . ")\n SELECT\n " . implode(', ', $aSelectColumnStatements) . "\n FROM\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['bucketTable'], true) . "\n WHERE\n " . implode(' AND ', $aWhereStatements); // Prevent any strange database error from causing execution to halt // by overriding the error handler, run the query, and return the // result (either the number or rows affected, or an MDB2_Error // object on query/database error) PEAR::pushErrorHandling(null); $result = $this->oDbh->exec($query); PEAR::popErrorHandling(); if (PEAR::isError($result)) { return $result; } $counter += $result; } return $counter; }
/** * A method to test the MAX_Delivery_log_logConversion() function. */ function test_MAX_Delivery_log_logConversion() { $aConf =& $GLOBALS['_MAX']['CONF']; $aConf['maintenance']['operationInterval'] = 60; $GLOBALS['_MAX']['NOW'] = time(); $oNowDate = new Date($GLOBALS['_MAX']['NOW']); $aDates = OX_OperationInterval::convertDateToOperationIntervalStartAndEndDates($oNowDate); $intervalStart = $aDates['start']->format('%Y-%m-%d %H:%M:%S'); $oConversionDate = new Date(); $oConversionDate->copy($oNowDate); $oConversionDate->subtractSeconds(60); $_SERVER['REMOTE_ADDR'] = '127.0.0.99'; // Test to ensure that the openXDeliveryLog plugin's data bucket // table does not exist $oTable = new OA_DB_Table(); $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . 'data_bkt_a'); $this->assertFalse($tableExists); // Test calling the main logging function without any plugins installed, // to ensure that this does not result in any kind of error $aConversion = array('action_type' => MAX_CONNECTION_AD_CLICK, 'tracker_type' => MAX_CONNECTION_TYPE_SALE, 'status' => MAX_CONNECTION_STATUS_APPROVED, 'cid' => 2, 'zid' => 3, 'dt' => $GLOBALS['_MAX']['NOW'] - 60, 'window' => 60); MAX_Delivery_log_logConversion(1, $aConversion); // Install the openXDeliveryLog plugin TestEnv::installPluginPackage('openXDeliveryLog', false); // Test to ensure that the openXDeliveryLog plugin's data bucket // table now does exist $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . 'data_bkt_a'); $this->assertTrue($tableExists); // Ensure that there are is nothing logged in the data bucket table $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 0); // Call the conversion logging function $aConversionInfo = MAX_Delivery_log_logConversion(1, $aConversion); // Ensure that the data was logged correctly $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->server_conv_id = 1; $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt_a->fetch(); $this->assertEqual($doData_bkt_a->server_ip, 'singleDB'); $this->assertEqual($doData_bkt_a->tracker_id, 1); $this->assertEqual($doData_bkt_a->date_time, $oNowDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->action_date_time, $oConversionDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->creative_id, 2); $this->assertEqual($doData_bkt_a->zone_id, 3); $this->assertEqual($doData_bkt_a->ip_address, '127.0.0.99'); $this->assertEqual($doData_bkt_a->action, MAX_CONNECTION_AD_CLICK); $this->assertEqual($doData_bkt_a->window, 60); $this->assertEqual($doData_bkt_a->status, MAX_CONNECTION_STATUS_APPROVED); $this->assertTrue(is_array($aConversionInfo)); $this->assertTrue(is_array($aConversionInfo['deliveryLog:oxLogConversion:logConversion'])); $this->assertEqual($aConversionInfo['deliveryLog:oxLogConversion:logConversion']['server_conv_id'], 1); $this->assertEqual($aConversionInfo['deliveryLog:oxLogConversion:logConversion']['server_raw_ip'], 'singleDB'); $aConversion['cid'] = 5; // Call the conversion logging function $aConversionInfo = MAX_Delivery_log_logConversion(1, $aConversion); // Ensure that the data was logged correctly $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 2); $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->server_conv_id = 1; $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt_a->fetch(); $this->assertEqual($doData_bkt_a->server_ip, 'singleDB'); $this->assertEqual($doData_bkt_a->tracker_id, 1); $this->assertEqual($doData_bkt_a->date_time, $oNowDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->action_date_time, $oConversionDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->creative_id, 2); $this->assertEqual($doData_bkt_a->zone_id, 3); $this->assertEqual($doData_bkt_a->ip_address, '127.0.0.99'); $this->assertEqual($doData_bkt_a->action, MAX_CONNECTION_AD_CLICK); $this->assertEqual($doData_bkt_a->window, 60); $this->assertEqual($doData_bkt_a->status, MAX_CONNECTION_STATUS_APPROVED); $doData_bkt_a = OA_Dal::factoryDO('data_bkt_a'); $doData_bkt_a->server_conv_id = 2; $doData_bkt_a->find(); $rows = $doData_bkt_a->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt_a->fetch(); $this->assertEqual($doData_bkt_a->server_ip, 'singleDB'); $this->assertEqual($doData_bkt_a->tracker_id, 1); $this->assertEqual($doData_bkt_a->date_time, $oNowDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->action_date_time, $oConversionDate->format('%Y-%m-%d %H:%M:%S')); $this->assertEqual($doData_bkt_a->creative_id, 5); $this->assertEqual($doData_bkt_a->zone_id, 3); $this->assertEqual($doData_bkt_a->ip_address, '127.0.0.99'); $this->assertEqual($doData_bkt_a->action, MAX_CONNECTION_AD_CLICK); $this->assertEqual($doData_bkt_a->window, 60); $this->assertEqual($doData_bkt_a->status, MAX_CONNECTION_STATUS_APPROVED); $this->assertTrue(is_array($aConversionInfo)); $this->assertTrue(is_array($aConversionInfo['deliveryLog:oxLogConversion:logConversion'])); $this->assertEqual($aConversionInfo['deliveryLog:oxLogConversion:logConversion']['server_conv_id'], 2); $this->assertEqual($aConversionInfo['deliveryLog:oxLogConversion:logConversion']['server_raw_ip'], 'singleDB'); // Uninstall the openXDeliveryLog plugin TestEnv::uninstallPluginPackage('openXDeliveryLog', false); // Restore the test configuration file TestEnv::restoreConfig(); }
/** * A method to test the MAX_Delivery_log_logAdRequest(), * MAX_Delivery_log_logAdImpression() and MAX_Delivery_log_logAdClick() * functions. */ function testRequestImpressionClickFunction() { $aConf =& $GLOBALS['_MAX']['CONF']; $aConf['maintenance']['operationInterval'] = 60; $GLOBALS['_MAX']['NOW'] = time(); $oNowDate = new Date($GLOBALS['_MAX']['NOW']); $aDates = OX_OperationInterval::convertDateToOperationIntervalStartAndEndDates($oNowDate); $intervalStart = $aDates['start']->format('%Y-%m-%d %H:%M:%S'); $aTables = array('MAX_Delivery_log_logAdRequest' => 'data_bkt_r', 'MAX_Delivery_log_logAdImpression' => 'data_bkt_m', 'MAX_Delivery_log_logAdClick' => 'data_bkt_c'); foreach ($aTables as $function => $table) { // Test to ensure that the openXDeliveryLog plugin's data bucket // table does not exist $oTable = new OA_DB_Table(); $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . $table); $this->assertFalse($tableExists); // Test calling the main logging function without any plugins installed, // to ensure that this does not result in any kind of error unset($GLOBALS['_MAX']['deliveryData']['Plugin_deliveryDataPrepare_oxDeliveryDataPrepare_dataCommon']); call_user_func_array($function, array(1, 1)); } // Install the openXDeliveryLog plugin TestEnv::installPluginPackage('openXDeliveryLog', false); foreach ($aTables as $function => $table) { // Test to ensure that the openXDeliveryLog plugin's data bucket // table now does exist $tableExists = $oTable->extistsTable($aConf['table']['prefix'] . $table); $this->assertTrue($tableExists); // Ensure that there are is nothing logged in the data bucket table $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 0); // Call the main logging function unset($GLOBALS['_MAX']['deliveryData']['Plugin_deliveryDataPrepare_oxDeliveryDataPrepare_dataCommon']); call_user_func_array($function, array(1, 1)); // Ensure that the data was logged correctly $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); if ($table == 'data_bkt_r') { // Request logging is disabled by default. Nothing should have been logged by now $this->assertEqual($rows, 0); // Enable it $GLOBALS['_MAX']['CONF']['logging']['adRequests'] = true; unset($GLOBALS['_MAX']['deliveryData']['Plugin_deliveryDataPrepare_oxDeliveryDataPrepare_dataCommon']); call_user_func_array($function, array(1, 1)); // Now ensure that the data was logged correctly $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); } $this->assertEqual($rows, 1); $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->creative_id = 1; $doData_bkt->zone_id = 1; $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt->fetch(); $this->assertEqual($doData_bkt->count, 1); $this->assertEqual($doData_bkt->interval_start, $intervalStart); // Call the main logging function again unset($GLOBALS['_MAX']['deliveryData']['Plugin_deliveryDataPrepare_oxDeliveryDataPrepare_dataCommon']); call_user_func_array($function, array(1, 1)); // Ensure that the data was logged correctly $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->creative_id = 1; $doData_bkt->zone_id = 1; $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt->fetch(); $this->assertEqual($doData_bkt->count, 2); $this->assertEqual($doData_bkt->interval_start, $intervalStart); // Call the main logging function again, but with a differen // creative/zone pair unset($GLOBALS['_MAX']['deliveryData']['Plugin_deliveryDataPrepare_oxDeliveryDataPrepare_dataCommon']); call_user_func_array($function, array(2, 1)); // Ensure that the data was logged correctly $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 2); $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->creative_id = 1; $doData_bkt->zone_id = 1; $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt->fetch(); $this->assertEqual($doData_bkt->count, 2); $this->assertEqual($doData_bkt->interval_start, $intervalStart); $doData_bkt = OA_Dal::factoryDO($table); $doData_bkt->creative_id = 2; $doData_bkt->zone_id = 1; $doData_bkt->find(); $rows = $doData_bkt->getRowCount(); $this->assertEqual($rows, 1); $doData_bkt->fetch(); $this->assertEqual($doData_bkt->count, 1); $this->assertEqual($doData_bkt->interval_start, $intervalStart); } // Uninstall the openXDeliveryLog plugin TestEnv::uninstallPluginPackage('openXDeliveryLog', false); // Restore the test configuration file TestEnv::restoreConfig(); }