PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OA/Dal/Maintenance/tests/unit/Priority_getAgencyCampaignsDeliveriesToDate.dal.test.php

https://bitbucket.org/valmy/openx
PHP | 238 lines | 147 code | 32 blank | 59 comment | 0 complexity | bbdcb5e44a7267b61547022a6ba107c1 MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. require_once MAX_PATH . '/lib/OA/Dal/DataGenerator.php';
  27. require_once MAX_PATH . '/lib/OA/Dal/Maintenance/Priority.php';
  28. /**
  29. * A class for testing the getCampaignDeliveryToDate() method of the non-DB
  30. * specific OA_Dal_Maintenance_Priority class.
  31. *
  32. * @package OpenXDal
  33. * @subpackage TestSuite
  34. * @author Radek Maciaszek <radek@urbantrip.com>
  35. */
  36. class Test_OA_Dal_Maintenance_Priority_getAgencyCampaignsDeliveriesToDate extends UnitTestCase
  37. {
  38. public $idCampaign2;
  39. /**
  40. * The constructor method.
  41. */
  42. function Test_OA_Dal_Maintenance_Priority_getAgencyCampaignsDeliveriesToDate()
  43. {
  44. $this->UnitTestCase();
  45. }
  46. /**
  47. * Method to test the getAgencyCampaignsDeliveriesToDate method.
  48. *
  49. * Requirements:
  50. * Test 1: Test correct results are returned with no data.
  51. * Test 2: Test correct results are returned with single data entry.
  52. * Test 3: Test correct results are returned with multiple data entries.
  53. */
  54. function testGetAgencyEcpmRemnantCampaignsDeliveriesToDate()
  55. {
  56. TestEnv::restoreEnv();
  57. $priority = DataObjects_Campaigns::PRIORITY_ECPM;
  58. $oMaxDalMaintenance = new OA_Dal_Maintenance_Priority();
  59. list($agencyId1, $agencyId2) = $this->_commonTest($oMaxDalMaintenance, $priority);
  60. // Check that there are no results for agency 1 (when checking ecpm deliveries)
  61. $result = $oMaxDalMaintenance->getAgencyEcpmRemnantCampaignsDeliveriesToDate($agencyId1);
  62. $this->assertTrue(is_array($result));
  63. $this->assertEqual(count($result), 0);
  64. // Check that results for agency 2 are the same (when checking ecpm deliveries)
  65. $result = $oMaxDalMaintenance->getAgencyEcpmRemnantCampaignsDeliveriesToDate($agencyId2);
  66. $this->_testResult($result);
  67. DataGenerator::cleanUp();
  68. }
  69. /**
  70. * Method to test the getAgencyEcpmContractCampaignsDeliveriesToDate method.
  71. *
  72. * Requirements:
  73. * Test 1: Test correct results are returned with no data.
  74. * Test 2: Test correct results are returned with single data entry.
  75. * Test 3: Test correct results are returned with multiple data entries.
  76. */
  77. function testGetAgencyEcpmContractCampaignsDeliveriesToDate()
  78. {
  79. $priority = 7;
  80. $oMaxDalMaintenance = new OA_Dal_Maintenance_Priority();
  81. list($agencyId1, $agencyId2) = $this->_commonTest($oMaxDalMaintenance, $priority);
  82. // Check that there are no results for agency 1 (when checking ecpm deliveries)
  83. $result = $oMaxDalMaintenance->getAgencyEcpmContractCampaignsDeliveriesToDate($agencyId1, $priority);
  84. $this->assertTrue(is_array($result));
  85. $this->assertEqual(count($result), 0);
  86. // Check that results for agency 2 are the same (when checking ecpm deliveries)
  87. $result = $oMaxDalMaintenance->getAgencyEcpmContractCampaignsDeliveriesToDate($agencyId2, $priority);
  88. $this->_testResult($result);
  89. DataGenerator::cleanUp();
  90. }
  91. public function _testResult($result)
  92. {
  93. $this->assertTrue(is_array($result));
  94. $this->assertEqual(count($result), 1);
  95. foreach ($result as $id => $data) {
  96. $this->assertEqual($this->idCampaign2, $id);
  97. }
  98. $this->assertEqual($result[$this->idCampaign2]['sum_impressions'], 475);
  99. $this->assertEqual($result[$this->idCampaign2]['sum_clicks'], 25);
  100. $this->assertEqual($result[$this->idCampaign2]['sum_conversions'], 5);
  101. }
  102. public function _commonTest($oMaxDalMaintenance, $priority)
  103. {
  104. $oNow = new Date();
  105. $doClients = OA_Dal::factoryDO('clients');
  106. $idClient = DataGenerator::generateOne($doClients, true);
  107. $agencyId1 = DataGenerator::getReferenceId('agency');
  108. // Test 1
  109. $result = $oMaxDalMaintenance->getAgencyCampaignsDeliveriesToDate($agencyId1);
  110. $this->assertTrue(is_array($result));
  111. $this->assertEqual(count($result), 0);
  112. $doCampaigns = OA_Dal::factoryDO('campaigns');
  113. $doCampaigns->status = OA_ENTITY_STATUS_RUNNING;
  114. $doCampaigns->clientid = $idClient;
  115. $oYesterday = new Date();
  116. $oYesterday->subtractSeconds(86400);
  117. $oTomorrow = new Date();
  118. $oTomorrow->addSeconds(86400);
  119. $doCampaigns->activate_time = $oYesterday->format('%Y-%m-%d %H:%M:%S');
  120. $doCampaigns->expire_time = $oTomorrow->format('%Y-%m-%d %H:%M:%S');
  121. $doCampaigns->priority = '1';
  122. $doCampaigns->active = 1;
  123. $doCampaigns->views = 100;
  124. $doCampaigns->clicks = 200;
  125. $doCampaigns->conversions = 300;
  126. $doCampaigns->updated = $oNow->format('%Y-%m-%d %H:%M:%S');
  127. $idCampaign = DataGenerator::generateOne($doCampaigns);
  128. $doBanners = OA_Dal::factoryDO('banners');
  129. $doBanners->campaignid = $idCampaign;
  130. $doBanners->active = 1;
  131. $doBanners->status = OA_ENTITY_STATUS_RUNNING;
  132. $doBanners->acls_updated = $oNow->format('%Y-%m-%d %H:%M:%S');
  133. $doBanners->updated = $oNow->format('%Y-%m-%d %H:%M:%S');
  134. $idBanner = DataGenerator::generateOne($doBanners);
  135. $doInterAd = OA_Dal::factoryDO('data_intermediate_ad');
  136. $doInterAd->operation_interval = 60;
  137. $doInterAd->operation_interval_id = 0;
  138. $doInterAd->ad_id = $idBanner;
  139. $doInterAd->day = '2005-06-24';
  140. $doInterAd->creative_id = 0;
  141. $doInterAd->zone_id = 1;
  142. $doInterAd->requests = 500;
  143. $doInterAd->impressions = 475;
  144. $doInterAd->clicks = 25;
  145. $doInterAd->conversions = 5;
  146. $doInterAd->updated = $oNow->format('%Y-%m-%d %H:%M:%S');
  147. $doInterAd->interval_start = '2005-06-24 10:00:00';
  148. $doInterAd->date_time = '2005-06-24 10:00:00';
  149. $doInterAd->interval_end = '2005-06-24 10:59:59';
  150. $doInterAd->hour = 10;
  151. $idInterAd = DataGenerator::generateOne($doInterAd);
  152. $doInterAd->interval_start = '2005-06-24 11:00:00';
  153. $doInterAd->date_time = '2005-06-24 11:00:00';
  154. $doInterAd->interval_end = '2005-06-24 11:59:59';
  155. $doInterAd->hour = 11;
  156. $idInterAd = DataGenerator::generateOne($doInterAd);
  157. $result = $oMaxDalMaintenance->getAgencyCampaignsDeliveriesToDate($agencyId1);
  158. $this->assertTrue(is_array($result));
  159. $this->assertEqual(count($result), 1);
  160. foreach ($result as $id => $data) {
  161. $this->assertEqual($idCampaign, $id);
  162. }
  163. $this->assertEqual($result[$idCampaign]['sum_impressions'], 950);
  164. $this->assertEqual($result[$idCampaign]['sum_clicks'], 50);
  165. $this->assertEqual($result[$idCampaign]['sum_conversions'], 10);
  166. // Test 3
  167. $doClients = OA_Dal::factoryDO('clients');
  168. $idClient2 = DataGenerator::generateOne($doClients, true);
  169. $agencyId2 = DataGenerator::getReferenceId('agency');
  170. $doCampaigns = OA_Dal::factoryDO('campaigns');
  171. $doCampaigns->clientid = $idClient2;
  172. $doCampaigns->priority = $priority;
  173. $doCampaigns->ecpm_enabled = 1;
  174. $doCampaigns->status = OA_ENTITY_STATUS_RUNNING;
  175. $doCampaigns->revenue_type = MAX_FINANCE_CPC;
  176. $this->idCampaign2 = DataGenerator::generateOne($doCampaigns);
  177. $doBanners = OA_Dal::factoryDO('banners');
  178. $doBanners->campaignid = $this->idCampaign2;
  179. $doBanners->status = OA_ENTITY_STATUS_RUNNING;
  180. $idBanner2 = DataGenerator::generateOne($doBanners);
  181. $doInterAd->ad_id = $idBanner2;
  182. $idInterAd = DataGenerator::generateOne($doInterAd);
  183. // Check that results for agency 1 are still the same
  184. $result = $oMaxDalMaintenance->getAgencyCampaignsDeliveriesToDate($agencyId1);
  185. $this->assertTrue(is_array($result));
  186. $this->assertEqual(count($result), 1);
  187. foreach ($result as $id => $data) {
  188. $this->assertEqual($idCampaign, $id);
  189. }
  190. $this->assertEqual($result[$idCampaign]['sum_impressions'], 950);
  191. $this->assertEqual($result[$idCampaign]['sum_clicks'], 50);
  192. $this->assertEqual($result[$idCampaign]['sum_conversions'], 10);
  193. // Check results for agency 2
  194. $result = $oMaxDalMaintenance->getAgencyCampaignsDeliveriesToDate($agencyId2);
  195. $this->assertTrue(is_array($result));
  196. $this->assertEqual(count($result), 1);
  197. foreach ($result as $id => $data) {
  198. $this->assertEqual($this->idCampaign2, $id);
  199. }
  200. $this->assertEqual($result[$this->idCampaign2]['sum_impressions'], 475);
  201. $this->assertEqual($result[$this->idCampaign2]['sum_clicks'], 25);
  202. $this->assertEqual($result[$this->idCampaign2]['sum_conversions'], 5);
  203. return array($agencyId1, $agencyId2);
  204. }
  205. }
  206. ?>