function testMigrateData()
 {
     $prefix = $this->getPrefix();
     $this->initDatabase(126, array('zones', 'ad_zone_assoc', 'placement_zone_assoc', 'banners'));
     $aAValues = array(array('zoneid' => 1, 'zonetype' => 0, 'what' => ''), array('zoneid' => 2, 'zonetype' => 0, 'what' => 'bannerid:3'), array('zoneid' => 3, 'zonetype' => 3, 'what' => 'clientid:3'), array('zoneid' => 4, 'zonetype' => 3, 'what' => 'clientid:5', 'delivery' => phpAds_ZoneText), array('zoneid' => 5, 'zonetype' => 3, 'what' => 'clientid:5', 'delivery' => phpAds_ZoneBanner, 'width' => 468, 'height' => 60), array('zoneid' => 6, 'zonetype' => 0, 'what' => 'bannerid:2,bannerid:3'), array('zoneid' => 7, 'zonetype' => 3, 'what' => 'clientid:3,clientid:4'), array('zoneid' => 8, 'zonetype' => 0, 'what' => 'bannerid:2,bannerid:3,bannerid:4,bannerid:5'), array('zoneid' => 9, 'zonetype' => 3, 'what' => 'clientid:,clientid:3'));
     foreach ($aAValues as $aValues) {
         // Set empty defaults for NOT NULL fields
         $aValues['chain'] = $aValues['prepend'] = $aValues['append'] = '';
         $sql = OA_DB_Sql::sqlForInsert('zones', $aValues);
         $this->oDbh->exec($sql);
     }
     $aABannerValues = array(array('bannerid' => 1, 'campaignid' => 3), array('bannerid' => 2, 'campaignid' => 3), array('bannerid' => 3, 'campaignid' => 4), array('bannerid' => 4, 'campaignid' => 4), array('bannerid' => 5, 'campaignid' => 5, 'storagetype' => 'txt'), array('bannerid' => 6, 'campaignid' => 5, 'storagetype' => 'sql', 'width' => 468, 'height' => 60), array('bannerid' => 7, 'campaignid' => 5, 'storagetype' => 'sql', 'width' => 125, 'height' => 125));
     foreach ($aABannerValues as $aBannerValues) {
         // Set empty defaults for NOT NULL fields
         $aBannerValues['htmltemplate'] = $aBannerValues['htmlcache'] = $aBannerValues['bannertext'] = $aBannerValues['compiledlimitation'] = $aBannerValues['append'] = '';
         $sql = OA_DB_Sql::sqlForInsert('banners', $aBannerValues);
         $this->oDbh->exec($sql);
     }
     $this->upgradeToVersion(127);
     $aAssocTables = array("{$prefix}ad_zone_assoc WHERE link_type = 1" => 17, "{$prefix}ad_zone_assoc WHERE link_type = 0" => 7, "{$prefix}placement_zone_assoc" => 6);
     foreach ($aAssocTables as $assocTable => $cAssocs) {
         $rsCAssocs = DBC::NewRecordSet("SELECT count(*) AS cassocs FROM {$assocTable}");
         $this->assertTrue($rsCAssocs->find());
         $this->assertTrue($rsCAssocs->fetch());
         $this->assertEqual($cAssocs, $rsCAssocs->get('cassocs'), "%s: The table involved: {$assocTable}");
     }
 }
 function testMigrateData()
 {
     $prefix = $this->getPrefix();
     $this->initDatabase(121, array('clients', 'campaigns'));
     $aCampaigns = array(array('clientid' => 3, 'parent' => 1, 'views' => '100', target => '1000'), array('clientid' => 4, 'parent' => 1, 'views' => '200', target => '1'), array('clientid' => 5, 'parent' => 1, 'views' => '200', target => '0'));
     $cCampaigns = count($aCampaigns);
     $aAValues = array(array('clientid' => 1, 'parent' => 0, 'views' => '0', target => '0'), array('clientid' => 2, 'parent' => 0, 'views' => '0', target => '0'));
     $aAValues = array_merge($aAValues, $aCampaigns);
     foreach ($aAValues as $aValues) {
         $sql = OA_DB_Sql::sqlForInsert('clients', $aValues);
         $this->oDbh->exec($sql);
     }
     $this->upgradeToVersion(122);
     $tableCampaigns = $this->oDbh->quoteIdentifier($prefix . 'campaigns', true);
     $rsCampaigns = DBC::NewRecordSet("SELECT * from {$tableCampaigns}");
     $this->assertTrue($rsCampaigns->find());
     $this->assertEqual($cCampaigns, $rsCampaigns->getRowCount());
     for ($idxCampaign = 0; $idxCampaign < $cCampaigns; $idxCampaign++) {
         $this->assertTrue($rsCampaigns->fetch());
         $this->assertEqual($aCampaigns[$idxCampaign]['clientid'], $rsCampaigns->get('campaignid'));
         $this->assertEqual($aCampaigns[$idxCampaign]['parent'], $rsCampaigns->get('clientid'));
         $this->assertEqual($aCampaigns[$idxCampaign]['views'], $rsCampaigns->get('views'));
         $priority = $aCampaigns[$idxCampaign]['target'] > 0 ? 5 : 0;
         $this->assertEqual($priority, $rsCampaigns->get('priority'));
     }
     $tableClients = $this->oDbh->quoteIdentifier($prefix . 'clients', true);
     $rsClients = DBC::NewRecordSet("SELECT count(*) AS nclients FROM {$tableClients}");
     $this->assertTrue($rsClients->find());
     $this->assertTrue($rsClients->fetch());
     $this->assertEqual(count($aAValues) - $cCampaigns, $rsClients->get('nclients'));
     if ($this->oDbh->dbsyntax == 'pgsql') {
         $value = $this->oDbh->queryOne("SELECT NEXTVAL('{$prefix}campaigns_campaignid_seq')");
         $this->assertTrue($value > 5, "The current sequence value is {$value}.");
     }
 }
 function testSqlForInsert()
 {
     $sql = OA_DB_Sql::sqlForInsert('zones', array('zonetype' => 1, 'name' => "120x72"));
     $oDbh = OA_DB::singleton();
     $table = $oDbh->quoteIdentifier($this->getPrefix() . 'zones', true);
     $this->assertEqual("INSERT INTO {$table} (zonetype,name) VALUES (1,'120x72')", $sql);
 }
 function testMigrateCampaignIds()
 {
     $this->initDatabase(123, array('banners'));
     $sql = OA_DB_Sql::sqlForInsert('banners', array('bannerid' => '1', 'clientid' => '4', 'htmltemplate' => '', 'htmlcache' => '', 'bannertext' => '', 'compiledlimitation' => '', 'append' => ''));
     $this->oDbh->exec($sql);
     $this->upgradeToVersion(124);
     $table = $this->oDbh->quoteIdentifier($this->getPrefix() . 'banners', true);
     $rsBanners = DBC::NewRecordSet("SELECT campaignid FROM {$table}");
     $this->assertTrue($rsBanners->find());
     $this->assertTrue($rsBanners->fetch());
     $this->assertEqual(4, $rsBanners->get('campaignid'));
 }
 function testExecute()
 {
     if ($this->oDbh->dbsyntax == 'pgsql') {
         $prefix = $this->getPrefix();
         $this->initDatabase(581, array('affiliates'));
         $aAValues = array(array('name' => 'x'), array('name' => 'y'));
         foreach ($aAValues as $aValues) {
             $sql = OA_DB_Sql::sqlForInsert('affiliates', $aValues);
             $this->oDbh->exec($sql);
         }
         // Simulate upgrade from phpPgAds with a wrongly named sequence
         $sequenceName = "{$prefix}affiliates_affiliateid_seq";
         $this->oDbh->exec("ALTER TABLE {$sequenceName} RENAME TO {$prefix}foobar");
         $this->oDbh->exec("ALTER TABLE {$prefix}affiliates ALTER affiliateid SET DEFAULT nextval('{$prefix}foobar')");
         // Create a publisher using the nextID call to generate the ID beforehand (two will fail because IDs already exist)
         $aAValues = array(array('affiliateid' => $this->oDbh->nextID($prefix . 'affiliates_affiliateid'), 'name' => 'z1'), array('affiliateid' => $this->oDbh->nextID($prefix . 'affiliates_affiliateid'), 'name' => 'z2'), array('affiliateid' => $this->oDbh->nextID($prefix . 'affiliates_affiliateid'), 'name' => 'z3'));
         $aExpect = array('PEAR_Error', 'PEAR_Error', 'int');
         $this->oDbh->expectError(MDB2_ERROR_CONSTRAINT);
         foreach ($aAValues as $key => $aValues) {
             $sql = OA_DB_Sql::sqlForInsert('affiliates', $aValues);
             $result = $this->oDbh->exec($sql);
             $this->assertIsA($result, $aExpect[$key]);
         }
         $this->oDbh->popExpect();
         Mock::generatePartial('OA_UpgradeLogger', $mockLogger = 'OA_UpgradeLogger' . rand(), array('logOnly', 'logError', 'log'));
         $oLogger = new $mockLogger($this);
         $oLogger->setReturnValue('logOnly', true);
         $oLogger->setReturnValue('logError', true);
         $oLogger->setReturnValue('log', true);
         $mockUpgrade = new StdClass();
         $mockUpgrade->oLogger = $oLogger;
         $mockUpgrade->oDBUpgrader = new OA_DB_Upgrade($oLogger);
         $mockUpgrade->oDBUpgrader->oTable =& $this->oaTable;
         $postscript = new OA_UpgradePostscript_2_5_67_RC8();
         $postscript->execute(array(&$mockUpgrade));
         $value = $this->oDbh->queryOne("SELECT nextval('{$sequenceName}')");
         $this->assertEqual($value, 4, "The current sequence value is {$value} (expected: 4)");
     }
 }
 function testMigrateData()
 {
     $prefix = $this->getPrefix();
     $this->initDatabase(108, array('config', 'preference'));
     $this->setupPanConfig();
     $migration = new Migration_108();
     $migration->init($this->oDbh, MAX_PATH . '/var/DB_Upgrade.test.log');
     $aValues = array('gui_show_parents' => 't', 'updates_enabled' => 'f', 'client_welcome_msg' => '', 'updates_cache' => '');
     $sql = OA_DB_Sql::sqlForInsert('config', $aValues);
     $this->oDbh->exec($sql);
     $aValues += array('warn_admin' => 't', 'warn_limit' => '100');
     $migration->migrateData();
     $table = $this->oDbh->quoteIdentifier($prefix . 'preference', true);
     $rsPreference = DBC::NewRecordSet("SELECT * from {$table}");
     $rsPreference->find();
     $this->assertTrue($rsPreference->fetch());
     $aDataPreference = $rsPreference->toArray();
     foreach ($aValues as $column => $value) {
         $this->assertEqual($value, $aDataPreference[$column]);
     }
     $this->restorePanConfig();
 }
 function testMigrateAcls()
 {
     $prefix = $this->getPrefix();
     $this->initDatabase(127, array('banners', 'acls', 'channel', 'acls_channel'));
     $aTestData = array(array('weekday', '==', '0,1'), array('weekday', '==', '0,1,4'), array('weekday', '==', ''), array('time', '==', '5,6'), array('time', '==', '5'), array('date', '>=', '20070510'), array('clientip', '==', '150.254.170.189'), array('domain', '!=', 'www.openx.org'), array('language', '!=', '(hr)|(nl)'), array('language', '!=', '(en)'), array('continent', '==', 'AF'), array('country', '==', 'PL,GB'), array('browser', '==', '(^Mozilla/5.*Gecko)|(Opera)|(MSN)'), array('browser', '!=', '(^Mozilla/5.*Gecko)|(Opera)|(MSN)'), array('os', '==', '(Win)|(Windows CE)|(Mac)|(Linux)|(BSD)|(SunOS)|(IRIX)|(AIX)|(Unix)'), array('useragent', '==', 'ahahaha'), array('referer', '==', 'blabblah'), array('source', '==', 'www.openx.org'), array('url', '==', 'www.openx.org/contacts.php'), array('postal_code', '==', '44100'), array('city', '==', 'ferrara'), array('fips_code', '==', 'GB08,GBN5'), array('region', '==', 'USAK,USAZ'), array('dma_code', '==', '501,502'), array('area_code', '==', '66099'), array('org_isp', '==', 'openads'), array('netspeed', '==', '1,2'), array('fips_code', '==', 'IT01,IT02,IE01,IE02,DE01'), array('fips_code', '!=', 'GB08,IT01,IT02'));
     $aExpectedData = array(array('Time:Day', '=~', '0,1'), array('Time:Day', '=~', '0,1,4'), array('Time:Day', '=~', ''), array('Time:Hour', '=~', '5,6'), array('Time:Hour', '=~', '5'), array('Time:Date', '>=', '20070510'), array('Client:Ip', '==', '150.254.170.189'), array('Client:Domain', '!=', 'www.openx.org'), array('Client:Language', '!~', 'hr,nl'), array('Client:Language', '!~', 'en'), array('Geo:Continent', '=~', 'AF'), array('Geo:Country', '=~', 'PL,GB'), array('Client:Useragent', '=x', '(^Mozilla/5.*Gecko)|(Opera)|(MSN)'), array('Client:Useragent', '!x', '(^Mozilla/5.*Gecko)|(Opera)|(MSN)'), array('Client:Useragent', '=x', '(Win)|(Windows CE)|(Mac)|(Linux)|(BSD)|(SunOS)|(IRIX)|(AIX)|(Unix)'), array('Client:Useragent', '=x', 'ahahaha'), array('Site:Referingpage', '=~', 'blabblah'), array('Site:Source', '=x', 'www.openx.org'), array('Site:Pageurl', '=~', 'www.openx.org/contacts.php'), array('Geo:Postalcode', '=~', '44100'), array('Geo:City', '=~', '|ferrara'), array('Geo:Region', '=~', 'GB|08,N5'), array('Geo:Region', '=~', 'US|AK,AZ'), array('Geo:Dma', '=~', '501,502'), array('Geo:Areacode', '=~', '66099'), array('Geo:Organisation', '=~', 'openads'), array('Geo:Netspeed', '=~', 'dialup,cabledsl'), array('Geo:Region', '=~', 'DE|01', 'and'), array('Geo:Region', '!~', 'GB|08', 'and'), array('Geo:Region', '=~', 'IE|01,02', 'or'), array('Geo:Region', '=~', 'IT|01,02', 'or'), array('Geo:Region', '!~', 'IT|01,02', 'and'));
     $sql = OA_DB_Sql::sqlForInsert('banners', array('bannerid' => 1, 'htmltemplate' => '', 'htmlcache' => '', 'bannertext' => '', 'compiledlimitation' => '', 'append' => ''));
     $this->oDbh->exec($sql);
     $aValues = array();
     $idx = 0;
     foreach ($aTestData as $testData) {
         $aValues = array('bannerid' => 1, 'logical' => 'and', 'type' => $testData[0], 'comparison' => $testData[1], 'data' => $testData[2], 'executionorder' => $idx++);
         $sql = OA_DB_Sql::sqlForInsert('acls', $aValues);
         $this->oDbh->exec($sql);
     }
     $this->upgradeToVersion(128);
     $table = $this->oDbh->quoteIdentifier($prefix . 'acls', true);
     $rsAcls = DBC::NewRecordSet("\n        SELECT type, comparison, data, logical\n        FROM {$table}\n        ORDER BY executionorder");
     $this->assertTrue($rsAcls->find());
     for ($idx = 0; $idx < count($aExpectedData); $idx++) {
         $expected = $aExpectedData[$idx][0] . "|" . $aExpectedData[$idx][1] . "|" . $aExpectedData[$idx][2];
         $this->assertTrue($rsAcls->fetch());
         $this->assertEqual($aExpectedData[$idx][0], $rsAcls->get('type'), "%s IN TYPE FOR: {$expected}");
         $this->assertEqual($aExpectedData[$idx][1], $rsAcls->get('comparison'), "%s IN COMPARISON FOR: {$expected}");
         $this->assertEqual($aExpectedData[$idx][2], $rsAcls->get('data'));
         if (!empty($aExpectedData[$idx][3])) {
             $this->assertEqual($aExpectedData[$idx][3], $rsAcls->get('logical'));
         }
     }
     $this->assertFalse($rsAcls->fetch());
 }
 function _insertRawStatsRows($bannerId, $zoneId, $day, $hour, $table, $mapCRows)
 {
     for ($idxRow = 0; $idxRow < $mapCRows[$bannerId][$zoneId][$day][$hour]; $idxRow++) {
         $minutes = mt_rand(0, 59);
         $seconds = mt_rand(0, 59);
         $timestamp = "{$day} {$hour}:{$minutes}:{$seconds}";
         $aValues = array('bannerid' => $bannerId, 'zoneid' => $zoneId, 't_stamp' => $timestamp);
         $sql = OA_DB_Sql::sqlForInsert($table, $aValues);
         $this->oDbh->exec($sql);
     }
 }
 function migrateAcls()
 {
     $tableAcls = $this->oDBH->quoteIdentifier($this->getPrefix() . "acls", true);
     $sql = "SELECT * FROM {$tableAcls} ORDER BY bannerid, executionorder";
     $rsAcls = DBC::NewRecordSet($sql);
     if (!$rsAcls->find()) {
         return false;
     }
     $aInserts = array();
     $aOffsets = array();
     $aUpdates = array();
     while ($rsAcls->fetch()) {
         $bannerid = $rsAcls->get('bannerid');
         $executionorder = $rsAcls->get('executionorder');
         $oldType = $rsAcls->get('type');
         if (!isset($this->aAclsTypes[$oldType])) {
             $this->_logError("Unknown acls type: {$oldType}");
             return false;
         }
         $type = $this->aAclsTypes[$oldType];
         $oldComparison = $rsAcls->get('comparison');
         $oldData = $rsAcls->get('data');
         $oPlugin =& $this->_getDeliveryLimitationPlugin($type);
         if (!$oPlugin) {
             $this->_logError("Can't find code for delivery limitation plugin: {$type}.");
             return false;
         }
         $aNewAclsData = $oPlugin->getUpgradeFromEarly($oldComparison, $oldData);
         $comparison = $aNewAclsData['op'];
         $data = $aNewAclsData['data'];
         $aUpdates[] = "UPDATE {$tableAcls} SET type = '{$type}', comparison = '{$comparison}', data = '{$data}'\n\t        WHERE bannerid = {$bannerid}\n\t        AND executionorder = {$executionorder}";
         $aOffsets[$bannerid] = $executionorder;
         if (isset($aNewAclsData['add'])) {
             if (!isset($aInserts[$bannerid])) {
                 $aInserts[$bannerid] = array();
             }
             $aInserts[$bannerid] = array_merge($aInserts[$bannerid], $aNewAclsData['add']);
         }
     }
     foreach ($aUpdates as $update) {
         $result = $this->oDBH->exec($update);
         if (PEAR::isError($result)) {
             return $this->_logErrorAndReturnFalse("Couldn't execute update: {$update}");
         }
     }
     foreach ($aInserts as $bannerid => $aLimitations) {
         $this->_log("WARNING! Found region geotargeting limitations that are NOT COMPATIBLE with new targeting format!");
         $this->_log("WARNING! Upgrade will proceed, but delivery limitations may NOT be preserved...");
         foreach ($aLimitations as $aValues) {
             $aValues['bannerid'] = $bannerid;
             $aValues['executionorder'] = ++$aOffsets[$bannerid];
             $insert = OA_DB_Sql::sqlForInsert('acls', $aValues);
             $result = $this->oDBH->exec($insert);
             if (PEAR::isError($result)) {
                 return $this->_logErrorAndReturnFalse("Couldn't execute insert: {$insert}");
             }
             $this->_log("WARNING! Upgraded incompatible region geotargeting limitation. After upgrade, you should check limitations for Banner ID: {$bannerid}.");
         }
         $this->_log("WARNING! Upgrade of non-compatible region geotargeting limitations is complete.");
     }
     return true;
 }