PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/unit/core/oxutilsTest.php

https://github.com/GM-Alex/oxideshop_ce
PHP | 1390 lines | 801 code | 222 blank | 367 comment | 17 complexity | f1888866974ade9e3c2ee7e493124737 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * This file is part of OXID eShop Community Edition.
  4. *
  5. * OXID eShop Community Edition is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * OXID eShop Community Edition is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @link http://www.oxid-esales.com
  19. * @copyright (C) OXID eSales AG 2003-2014
  20. * @version OXID eShop CE
  21. */
  22. require_once realpath(".") . '/unit/OxidTestCase.php';
  23. require_once realpath(".") . '/unit/test_config.inc.php';
  24. class testOxUtils extends oxUtils
  25. {
  26. public function setNonPublicVar($name, $value)
  27. {
  28. $this->$name = $value;
  29. }
  30. public function getNonPublicVar($name, $value)
  31. {
  32. $this->$name = $value;
  33. }
  34. public function __call($sMethod, $aArgs)
  35. {
  36. if (substr($sMethod, 0, 4) == "UNIT") {
  37. $sMethod = str_replace("UNIT", "_", $sMethod);
  38. }
  39. if (method_exists($this, $sMethod)) {
  40. return call_user_func_array(array(& $this, $sMethod), $aArgs);
  41. }
  42. throw new oxSystemComponentException("Function '$sMethod' does not exist or is not accessible! (" . __CLASS__ . ")" . PHP_EOL);
  43. }
  44. }
  45. class Unit_Core_oxutilsTest extends OxidTestCase
  46. {
  47. protected $_sTestLogFileName = null;
  48. /**
  49. * Tear down the fixture.
  50. *
  51. * @return null
  52. */
  53. protected function tearDown()
  54. {
  55. oxRegistry::getUtils()->commitFileCache();
  56. clearstatcache();
  57. //removing test files from tmp dir
  58. $sFilePath = oxRegistry::getConfig()->getConfigParam('sCompileDir') . "*testFileCache*.txt";
  59. $aPathes = glob($sFilePath);
  60. if (is_array($aPathes)) {
  61. foreach ($aPathes as $sFilename) {
  62. @unlink($sFilename);
  63. }
  64. }
  65. if ($this->_sTestLogFileName !== null) {
  66. unlink($this->_sTestLogFileName);
  67. $this->_sTestLogFileName = null;
  68. }
  69. if (file_exists('tmp_testCacheName')) {
  70. unlink('tmp_testCacheName');
  71. }
  72. $oUtils = oxRegistry::getUtils();
  73. $sFileName = $oUtils->getCacheFilePath("testVal", false, 'php');
  74. if (file_exists($sFileName)) {
  75. unlink($sFileName);
  76. }
  77. $sFileName = $oUtils->getCacheFilePath('testCache1');
  78. if (file_exists($sFileName)) {
  79. unlink($sFileName);
  80. }
  81. parent::tearDown();
  82. }
  83. /**
  84. *
  85. * @return unknown_type
  86. */
  87. public function testExtractDomain()
  88. {
  89. $oUtils = new oxUtils();
  90. $this->assertEquals("oxid-esales.com", $oUtils->extractDomain("www.oxid-esales.com"));
  91. $this->assertEquals("oxid-esales.com", $oUtils->extractDomain("oxid-esales.com"));
  92. $this->assertEquals("127.0.0.1", $oUtils->extractDomain("127.0.0.1"));
  93. $this->assertEquals("oxid-esales.com", $oUtils->extractDomain("ssl.oxid-esales.com"));
  94. $this->assertEquals("oxid-esales", $oUtils->extractDomain("oxid-esales"));
  95. }
  96. public function testShowMessageAndExit()
  97. {
  98. $oSession = $this->getMock("oxSession", array("freeze"));
  99. $oSession->expects($this->once())->method('freeze');
  100. $oUtils = $this->getMock("oxUtils", array("getSession", "commitFileCache"));
  101. $oUtils->expects($this->once())->method('getSession')->will($this->returnValue($oSession));
  102. $oUtils->expects($this->once())->method('commitFileCache');
  103. $oUtils->showMessageAndExit("");
  104. }
  105. public function testWriteToLog()
  106. {
  107. $sLogMessage = $sLogFileName = md5(uniqid(rand(), true));
  108. $oUtils = new oxUtils();
  109. $oUtils->writeToLog($sLogMessage, $sLogFileName);
  110. $this->_sTestLogFileName = oxRegistry::getConfig()->getConfigParam('sShopDir') . 'log/' . $sLogFileName;
  111. clearstatcache();
  112. $this->assertTrue(file_exists($this->_sTestLogFileName));
  113. $this->assertEquals($sLogMessage, file_get_contents($this->_sTestLogFileName));
  114. }
  115. public function testSetLangCache()
  116. {
  117. $aLangCache = array("ggg" => "bbb");
  118. $sCacheName = 'tmp_testCacheName';
  119. $sCache = "<?php\n\$aLangCache = " . var_export($aLangCache, true) . ";";
  120. $oUtils = $this->getMock('oxutils', array('getCacheFilePath'));
  121. $oUtils->expects($this->once())->method('getCacheFilePath')->with($this->equalTo($sCacheName))->will($this->returnValue("tmp_testCacheName"));
  122. $oUtils->setLangCache($sCacheName, $aLangCache);
  123. }
  124. public function testgetLangCache()
  125. {
  126. $sCacheName = time();
  127. $aLangCache = array("ggg" => "bbb");
  128. $oUtils = new oxutils();
  129. $oUtils->setLangCache($sCacheName, $aLangCache);
  130. $this->assertEquals($aLangCache, $oUtils->getLangCache($sCacheName));
  131. }
  132. /**
  133. * Seo mode checker
  134. */
  135. public function testSeoIsActive()
  136. {
  137. // as now SEO is on by default
  138. $oUtils = new oxutils();
  139. $oConfig = $oUtils->getConfig();
  140. $oConfig->setConfigParam('aSeoModes', array('testshop' => array(2 => false, 3 => true)));
  141. $this->assertTrue($oUtils->seoIsActive());
  142. // cache test
  143. $this->assertTrue($oUtils->seoIsActive(false, 'testshop', 2));
  144. $this->assertFalse($oUtils->seoIsActive(true, 'testshop', 2));
  145. // config test
  146. $this->assertTrue($oUtils->seoIsActive(true, 'testshop', 3));
  147. }
  148. public function testGetArrFldName()
  149. {
  150. $sTestString = ".S.o.me.. . Na.me.";
  151. $sShouldBeResult = "__S__o__me____ __ Na__me__";
  152. $this->assertEquals($sShouldBeResult, oxRegistry::getUtils()->getArrFldName($sTestString));
  153. }
  154. public function optionsAndValuesProvider()
  155. {
  156. return array(
  157. array(true, true, 1),
  158. array(true, false, 1.2),
  159. array(false, true, 1.2),
  160. array(false, false, 1),
  161. );
  162. }
  163. /**
  164. * Tests how selection lists are outputted with show as net price option, and enter as net price option.
  165. * Tests all 4 combination of both options.
  166. *
  167. * @dataProvider optionsAndValuesProvider
  168. */
  169. public function testValueCalculationBasedOnOptions($blEnterNetPrice, $blShowNetPrice, $iVatModifier)
  170. {
  171. $myConfig = oxRegistry::getConfig();
  172. $oCurrency = $myConfig->getActShopCurrencyObject();
  173. modConfig::getInstance()->setConfigParam('bl_perfLoadSelectLists', true);
  174. modConfig::getInstance()->setConfigParam('bl_perfUseSelectlistPrice', true);
  175. modConfig::getInstance()->setConfigParam('blEnterNetPrice', $blEnterNetPrice);
  176. modConfig::getInstance()->setConfigParam('blShowNetPrice', $blShowNetPrice);
  177. $sTestString = "one!P!99.5%__oneValue@@two!P!12,41__twoValue@@three!P!-5,99__threeValue@@Lagerort__Lager 1@@";
  178. $aResult = oxRegistry::getUtils()->assignValuesFromText($sTestString, 20);
  179. $aShouldBe = array();
  180. $oObject = new stdClass();
  181. $oObject->price = '99.5';
  182. $oObject->priceUnit = '%';
  183. $oObject->fprice = '99.5%';
  184. $oObject->name = 'one +99.5%';
  185. $oObject->value = 'oneValue';
  186. $aShouldBe[] = $oObject;
  187. $dPrice = str_replace('.', ',', $this->_alterPrice(12.41, $iVatModifier, $blShowNetPrice, $blEnterNetPrice));
  188. $oObject = new stdClass();
  189. $oObject->price = '12.41';
  190. $oObject->fprice = '12,41';
  191. $oObject->priceUnit = 'abs';
  192. $oObject->name = "two +$dPrice " . $oCurrency->sign;
  193. $oObject->value = 'twoValue';
  194. $aShouldBe[] = $oObject;
  195. $dPrice = str_replace('.', ',', $this->_alterPrice(5.99, $iVatModifier, $blShowNetPrice, $blEnterNetPrice));
  196. $oObject = new stdClass();
  197. $oObject->price = '-5.99';
  198. $oObject->fprice = '-5,99';
  199. $oObject->priceUnit = 'abs';
  200. $oObject->name = "three -$dPrice " . $oCurrency->sign;
  201. $oObject->value = 'threeValue';
  202. $aShouldBe[] = $oObject;
  203. $oObject = new stdClass();
  204. $oObject->name = 'Lagerort';
  205. $oObject->value = 'Lager 1';
  206. $aShouldBe[] = $oObject;
  207. $this->assertEquals($aShouldBe, $aResult);
  208. }
  209. /**
  210. * Helper function to alter prices for checking correct vat prices
  211. *
  212. *
  213. */
  214. protected function _alterPrice($dPrice, $iVatModifier, $blShowNetPrice, $blEnterNetPrice)
  215. {
  216. if ($blEnterNetPrice && !$blShowNetPrice) {
  217. $dPrice *= $iVatModifier;
  218. } else {
  219. $dPrice /= $iVatModifier;
  220. }
  221. return round($dPrice, 2);
  222. }
  223. /**
  224. * Check of full version processor
  225. */
  226. public function testAssignValuesFromTextFull()
  227. {
  228. $myConfig = oxRegistry::getConfig();
  229. $oCurrency = $myConfig->getActShopCurrencyObject();
  230. modConfig::getInstance()->setConfigParam('bl_perfLoadSelectLists', true);
  231. modConfig::getInstance()->setConfigParam('bl_perfUseSelectlistPrice', true);
  232. $sTestString = "one!P!99.5%__oneValue@@two!P!12,41__twoValue@@three!P!-5,99__threeValue@@Lagerort__Lager 1@@";
  233. $aResult = oxRegistry::getUtils()->assignValuesFromText($sTestString);
  234. $aShouldBe = array();
  235. $oObject = new stdClass();
  236. $oObject->price = '99.5';
  237. $oObject->priceUnit = '%';
  238. $oObject->fprice = '99.5%';
  239. $oObject->name = 'one +99.5%';
  240. $oObject->value = 'oneValue';
  241. $aShouldBe[] = $oObject;
  242. $oObject = new stdClass();
  243. $oObject->price = '12.41';
  244. $oObject->fprice = '12,41';
  245. $oObject->priceUnit = 'abs';
  246. $oObject->name = 'two +12,41 ' . $oCurrency->sign;
  247. $oObject->value = 'twoValue';
  248. $aShouldBe[] = $oObject;
  249. $oObject = new stdClass();
  250. $oObject->price = '-5.99';
  251. $oObject->fprice = '-5,99';
  252. $oObject->priceUnit = 'abs';
  253. $oObject->name = 'three -5,99 ' . $oCurrency->sign;
  254. $oObject->value = 'threeValue';
  255. $aShouldBe[] = $oObject;
  256. $oObject = new stdClass();
  257. $oObject->name = 'Lagerort';
  258. $oObject->value = 'Lager 1';
  259. $aShouldBe[] = $oObject;
  260. $this->assertEquals($aShouldBe, $aResult);
  261. }
  262. /**
  263. * Check of full version processor
  264. */
  265. public function testAssignValuesFromTextFullIfPriceIsZero()
  266. {
  267. $myConfig = oxRegistry::getConfig();
  268. $oCurrency = $myConfig->getActShopCurrencyObject();
  269. modConfig::getInstance()->setConfigParam('bl_perfLoadSelectLists', true);
  270. modConfig::getInstance()->setConfigParam('bl_perfUseSelectlistPrice', true);
  271. $sTestString = "one__oneValue@@two!P!0.00__twoValue@@";
  272. $aResult = oxRegistry::getUtils()->assignValuesFromText($sTestString);
  273. $aShouldBe = array();
  274. $oObject = new stdClass();
  275. $oObject->name = 'one';
  276. $oObject->value = 'oneValue';
  277. $aShouldBe[] = $oObject;
  278. $oObject = new stdClass();
  279. $oObject->price = '0.00';
  280. $oObject->fprice = '0,00';
  281. $oObject->priceUnit = 'abs';
  282. $oObject->name = 'two';
  283. $oObject->value = 'twoValue';
  284. $aShouldBe[] = $oObject;
  285. $this->assertEquals($aShouldBe, $aResult);
  286. }
  287. /**
  288. * Check of full version processor (If NetPrice Is Entered)
  289. * FS#2616
  290. */
  291. public function testAssignValuesFromTextFullWithVat()
  292. {
  293. $myConfig = oxRegistry::getConfig();
  294. $oCurrency = $myConfig->getActShopCurrencyObject();
  295. modConfig::getInstance()->setConfigParam('bl_perfLoadSelectLists', true);
  296. modConfig::getInstance()->setConfigParam('bl_perfUseSelectlistPrice', true);
  297. modConfig::getInstance()->setConfigParam('blEnterNetPrice', true);
  298. $sTestString = "one!P!99.5%__oneValue@@two!P!12,41__twoValue@@";
  299. $aResult = oxRegistry::getUtils()->assignValuesFromText($sTestString, 19);
  300. $aShouldBe = array();
  301. $oObject = new stdClass();
  302. $oObject->price = '99.5';
  303. $oObject->priceUnit = '%';
  304. $oObject->fprice = '99.5%';
  305. $oObject->name = 'one +99.5%';
  306. $oObject->value = 'oneValue';
  307. $aShouldBe[] = $oObject;
  308. $oObject = new stdClass();
  309. $oObject->price = '12.41';
  310. $oObject->fprice = '12,41';
  311. $oObject->priceUnit = 'abs';
  312. $oObject->name = 'two +14,77 ' . $oCurrency->sign;
  313. $oObject->value = 'twoValue';
  314. $aShouldBe[] = $oObject;
  315. $this->assertEquals($aShouldBe, $aResult);
  316. }
  317. /**
  318. * Check of simplified version processor
  319. */
  320. public function testAssignValuesFromTextLite()
  321. {
  322. $myConfig = oxRegistry::getConfig();
  323. $oCurrency = $myConfig->getActShopCurrencyObject();
  324. modConfig::getInstance()->setConfigParam('bl_perfLoadSelectLists', false);
  325. modConfig::getInstance()->setConfigParam('bl_perfUseSelectlistPrice', false);
  326. $sTestString = "one!P!99.5%__oneValue@@two!P!12,41__twoValue@@three!P!-5,99__threeValue@@Lagerort__Lager 1@@";
  327. $aResult = oxRegistry::getUtils()->assignValuesFromText($sTestString);
  328. $aShouldBe = array();
  329. $oObject = new stdClass();
  330. $oObject->name = 'one';
  331. $oObject->value = 'oneValue';
  332. $aShouldBe[] = $oObject;
  333. $oObject = new stdClass();
  334. $oObject->name = 'two';
  335. $oObject->value = 'twoValue';
  336. $aShouldBe[] = $oObject;
  337. $oObject = new stdClass();
  338. $oObject->name = 'three';
  339. $oObject->value = 'threeValue';
  340. $aShouldBe[] = $oObject;
  341. $oObject = new stdClass();
  342. $oObject->name = 'Lagerort';
  343. $oObject->value = 'Lager 1';
  344. $aShouldBe[] = $oObject;
  345. $this->assertEquals($aShouldBe, $aResult);
  346. }
  347. public function testAssignValuesToText()
  348. {
  349. $aTestArray = array('one' => 11, 'two' => 22, 'three' => 33, 'fourfour' => 44.44);
  350. $sResult = oxRegistry::getUtils()->assignValuesToText($aTestArray);
  351. $sShouldBeResult = "one__11@@two__22@@three__33@@fourfour__44.44@@";
  352. $sShouldNotBeResult = "on__11@@two__22@@three__33@@fourfour__44.44@@";
  353. $this->assertEquals($sShouldBeResult, $sResult);
  354. $this->assertNotEquals($sShouldNotBeResult, $sResult);
  355. }
  356. public function testCurrency2Float()
  357. {
  358. $oActCur = oxRegistry::getConfig()->getActShopCurrencyObject();
  359. $fFloat = oxRegistry::getUtils()->currency2Float("10.322,32", $oActCur);
  360. $this->assertEquals($fFloat, 10322.32);
  361. $fFloat = oxRegistry::getUtils()->currency2Float("10,322.32", $oActCur);
  362. $this->assertEquals($fFloat, (float) "10.322.32");
  363. $fFloat = oxRegistry::getUtils()->currency2Float("10 322,32", $oActCur);
  364. $this->assertEquals($fFloat, (float) "10322.32");
  365. $fFloat = oxRegistry::getUtils()->currency2Float("10 322.32", $oActCur);
  366. $this->assertEquals($fFloat, (float) "10322.32");
  367. }
  368. /**
  369. * Testing if shop var saver writes num value with valid string to config correctly
  370. */
  371. public function testString2Float()
  372. {
  373. $oUtils = oxRegistry::getUtils();
  374. $fFloat = $oUtils->string2Float("10.322,32");
  375. $this->assertEquals($fFloat, 10322.32);
  376. $fFloat = $oUtils->string2Float("10,322.32");
  377. $this->assertEquals($fFloat, 10322.32);
  378. $fFloat = $oUtils->string2Float("10322,32");
  379. $this->assertEquals($fFloat, 10322.32);
  380. $fFloat = $oUtils->string2Float("10322.32");
  381. $this->assertEquals($fFloat, 10322.32);
  382. $fFloat = $oUtils->string2Float("10.32225");
  383. $this->assertEquals($fFloat, 10.32225);
  384. $fFloat = $oUtils->string2Float("10 000.32225");
  385. $this->assertEquals($fFloat, 10000.32225);
  386. $fFloat = $oUtils->string2Float("10 000.00");
  387. $this->assertEquals($fFloat, 10000);
  388. }
  389. /**
  390. * SE check, non admin mode, will cache result
  391. */
  392. public function testIsSearchEngineNonAdminNonSE()
  393. {
  394. // cleaning ..
  395. $myConfig = oxRegistry::getConfig();
  396. modConfig::getInstance()->setConfigParam('iDebug', 1);
  397. modConfig::getInstance()->setConfigParam('aRobots', array());
  398. $oUtils = $this->getMock('oxUtils', array('isAdmin'));
  399. $oUtils->expects($this->any())->method('isAdmin')->will($this->returnValue(false));
  400. $this->assertFalse($oUtils->isSearchEngine('xxx'));
  401. $this->assertFalse($oUtils->isSearchEngine('googlebot'));
  402. }
  403. public function testIsSearchEngineNonAdminSE()
  404. {
  405. // cleaning ..
  406. $myConfig = oxRegistry::getConfig();
  407. modConfig::getInstance()->setConfigParam('iDebug', 0);
  408. modConfig::getInstance()->setConfigParam('aRobots', array('googlebot', 'xxx'));
  409. $oUtils = $this->getMock('oxUtils', array('isAdmin'));
  410. $oUtils->expects($this->any())->method('isAdmin')->will($this->returnValue(false));
  411. $this->assertTrue($oUtils->isSearchEngine('googlebot'));
  412. $this->assertTrue($oUtils->isSearchEngine('xxx'));
  413. }
  414. public function testIsSearchEngineAdminAndDebugOn()
  415. {
  416. // cleaning ..
  417. $myConfig = oxRegistry::getConfig();
  418. modConfig::getInstance()->setConfigParam('iDebug', 1);
  419. modConfig::getInstance()->setConfigParam('aRobots', array('googlebot', 'xxx'));
  420. $oUtils = $this->getMock('oxUtils', array('isAdmin'));
  421. $oUtils->expects($this->any())->method('isAdmin')->will($this->returnValue(true));
  422. $this->assertFalse($oUtils->isSearchEngine('xxx'));
  423. $this->assertFalse($oUtils->isSearchEngine('googlebot'));
  424. }
  425. public function testIsSearchEngineAdminAndDebugOff()
  426. {
  427. // cleaning ..
  428. $myConfig = oxRegistry::getConfig();
  429. modConfig::getInstance()->setConfigParam('iDebug', 1);
  430. modConfig::getInstance()->setConfigParam('aRobots', array('googlebot', 'xxx'));
  431. $oUtils = $this->getMock('oxUtils', array('isAdmin'));
  432. $oUtils->expects($this->any())->method('isAdmin')->will($this->returnValue(true));
  433. $this->assertFalse($oUtils->isSearchEngine('googlebot'));
  434. $this->assertFalse($oUtils->isSearchEngine('xxx'));
  435. }
  436. public function testIsValidEmail()
  437. {
  438. $this->assertTrue(oxRegistry::getUtils()->isValidEmail('mathias.krieck@oxid-esales.com'));
  439. $this->assertTrue(oxRegistry::getUtils()->isValidEmail('mytest@com.org'));
  440. $this->assertFalse(oxRegistry::getUtils()->isValidEmail('�mathias.krieck@oxid-esales.com'));
  441. $this->assertFalse(oxRegistry::getUtils()->isValidEmail('my/test@com.org'));
  442. $this->assertFalse(oxRegistry::getUtils()->isValidEmail('@com.org'));
  443. $this->assertFalse(oxRegistry::getUtils()->isValidEmail('mytestcom.org'));
  444. $this->assertFalse(oxRegistry::getUtils()->isValidEmail('mytest@com'));
  445. }
  446. public function testLoadAdminProfile()
  447. {
  448. $aProfiles = oxRegistry::getUtils()->loadAdminProfile(array('640x480', '14'));
  449. $this->assertContains('640x480', $aProfiles[0]);
  450. $aProfiles = oxRegistry::getUtils()->loadAdminProfile(v);
  451. $this->assertNull($aProfiles);
  452. $aProfiles = oxRegistry::getUtils()->loadAdminProfile("teststring");
  453. $this->assertNull($aProfiles);
  454. }
  455. public function testFRound()
  456. {
  457. $myConfig = oxRegistry::getConfig();
  458. $this->assertEquals('9.84', oxRegistry::getUtils()->fRound('9.844'));
  459. $this->assertEquals('9.85', oxRegistry::getUtils()->fRound('9.845'));
  460. $this->assertEquals('9.85', oxRegistry::getUtils()->fRound('9.849'));
  461. $this->assertEquals('0', oxRegistry::getUtils()->fRound('blafoo'));
  462. $this->assertEquals('9', oxRegistry::getUtils()->fRound('9,849'));
  463. //negative
  464. $this->assertEquals('-9.84', oxRegistry::getUtils()->fRound('-9.844'));
  465. $this->assertEquals('-9.85', oxRegistry::getUtils()->fRound('-9.845'));
  466. $this->assertEquals('-9.85', oxRegistry::getUtils()->fRound('-9.849'));
  467. $this->assertEquals('-9', oxRegistry::getUtils()->fRound('-9,849'));
  468. $aCur = $myConfig->getCurrencyArray();
  469. $oCur = $aCur[1];
  470. $this->assertEquals('9.84', oxRegistry::getUtils()->fRound('9.844', $oCur));
  471. $this->assertEquals('9.85', oxRegistry::getUtils()->fRound('9.845', $oCur));
  472. $this->assertEquals('9.85', oxRegistry::getUtils()->fRound('9.849', $oCur));
  473. $this->assertEquals('0', oxRegistry::getUtils()->fRound('blafoo', $oCur));
  474. $this->assertEquals('9', oxRegistry::getUtils()->fRound('9,849', $oCur));
  475. $this->assertEquals('-9.84', oxRegistry::getUtils()->fRound('-9.844', $oCur));
  476. $this->assertEquals('-9.85', oxRegistry::getUtils()->fRound('-9.845', $oCur));
  477. $this->assertEquals('-9.85', oxRegistry::getUtils()->fRound('-9.849', $oCur));
  478. $this->assertEquals('-9', oxRegistry::getUtils()->fRound('-9,849', $oCur));
  479. $this->assertEquals('1522.61', oxRegistry::getUtils()->fRound('1522.605', $oCur));
  480. }
  481. public function testToFromStaticCache()
  482. {
  483. $oUtils = new oxutils();
  484. $sName = "SomeName";
  485. $mContent = "SomeContent";
  486. $sKey = "SomeKey";
  487. $oUtils->toStaticCache($sName, $mContent);
  488. $this->assertEquals($mContent, $oUtils->fromStaticCache($sName));
  489. $sName = "SomeOtherName";
  490. $mContent = "SomeOtherContent";
  491. $sKey = "SomeOtherKey";
  492. $oUtils->toStaticCache($sName, $mContent, $sKey);
  493. $aOut = $oUtils->fromStaticCache($sName);
  494. $this->assertEquals($mContent, $aOut[$sKey]);
  495. // testing non existing
  496. $this->assertNull($oUtils->fromStaticCache(time()));
  497. }
  498. public function testCleanStaticCacheSpecific()
  499. {
  500. $oUtils = new oxutils();
  501. $sName1 = "SomeName";
  502. $mContent1 = "SomeContent";
  503. $sKey1 = "SomeKey";
  504. $sName2 = "SomeName2";
  505. $mContent2 = "SomeContent2";
  506. $sKey2 = "SomeKey2";
  507. $oUtils->toStaticCache($sName1, $mContent1);
  508. $oUtils->toStaticCache($sName2, $mContent2);
  509. $oUtils->cleanStaticCache($sName2);
  510. $this->assertEquals($mContent1, $oUtils->fromStaticCache($sName1));
  511. $this->assertEquals(null, $oUtils->fromStaticCache($mContent1));
  512. }
  513. public function testCleanStaticCacheFullClean()
  514. {
  515. $oUtils = new oxutils();
  516. $sName1 = "SomeName";
  517. $mContent1 = "SomeContent";
  518. $sKey1 = "SomeKey";
  519. $sName2 = "SomeName2";
  520. $mContent2 = "SomeContent2";
  521. $sKey2 = "SomeKey2";
  522. $oUtils->toStaticCache($sName1, $mContent1);
  523. $oUtils->toStaticCache($sName2, $mContent2);
  524. $oUtils->cleanStaticCache();
  525. $this->assertEquals(null, $oUtils->fromStaticCache($sName1));
  526. $this->assertEquals(null, $oUtils->fromStaticCache($sName2));
  527. }
  528. public function testToFileCacheFileCache()
  529. {
  530. $sName = "testFileCache";
  531. $sInput = "test_test_test";
  532. $oUtils = new oxutils();
  533. $oUtils->toFileCache($sName, $sInput);
  534. $this->assertEquals($sInput, $oUtils->fromFileCache($sName));
  535. }
  536. public function testToFileCacheFileCacheDoubleWrite1()
  537. {
  538. $sName1 = "testFileCache";
  539. $sName2 = "testFileCache2";
  540. $sInput1 = "test_test_test";
  541. $sInput2 = "test_test";
  542. $oUtils = new oxutils();
  543. $oUtils->toFileCache($sName1, $sInput1);
  544. $oUtils->toFileCache($sName2, $sInput2);
  545. $this->assertEquals($sInput1, $oUtils->fromFileCache($sName1));
  546. $this->assertEquals($sInput2, $oUtils->fromFileCache($sName2));
  547. }
  548. public function testToFileCacheFileCacheDoubleWrite2()
  549. {
  550. $sName1 = "testFileCache";
  551. $sName2 = "testFileCache2";
  552. $sInput1 = "test_test_test";
  553. $sInput2 = "test_test";
  554. $oUtils = new oxutils();
  555. $oUtils->toFileCache($sName1, $sInput1);
  556. $this->assertEquals($sInput1, $oUtils->fromFileCache($sName1));
  557. $oUtils->toFileCache($sName2, $sInput2);
  558. $this->assertEquals($sInput2, $oUtils->fromFileCache($sName2));
  559. }
  560. public function testToFileCacheFileCacheDoubleWrite3()
  561. {
  562. $sName1 = "testFileCache1";
  563. $sName2 = "testFileCache2";
  564. $sInput1 = "test_test_test";
  565. $sInput2 = "test_test";
  566. $oUtils = $this->getProxyClass('oxutils');
  567. $oUtils->toFileCache($sName1, $sInput1);
  568. $oUtils->toFileCache($sName2, $sInput2);
  569. $oUtils->commitFileCache();
  570. $this->assertEquals($sInput1, $oUtils->fromFileCache($sName1));
  571. $this->assertEquals($sInput2, $oUtils->fromFileCache($sName2));
  572. }
  573. public function testOxResetFileCache()
  574. {
  575. $myConfig = oxRegistry::getConfig();
  576. $sName = "testFileCache";
  577. $sInput = "test_test_test";
  578. //getting cached files prefix
  579. $myUtilsTest = $this->getProxyClass("oxUtils");
  580. $sFilePath = $myUtilsTest->getCacheFilePath("test");
  581. $sCacheFilePrefix = preg_replace("/.*\/(ox[^_]*)_.*/", "$1", $sFilePath);
  582. $oUtils = oxRegistry::getUtils();
  583. for ($iMax = 0; $iMax < 10; $iMax++) {
  584. $oUtils->toFileCache($sName . "_" . $iMax, $sInput . "_" . $iMax);
  585. }
  586. $oUtils->commitFileCache();
  587. //checking if test files were written to temp dir
  588. $sFilePath = $myConfig->getConfigParam('sCompileDir') . "/{$sCacheFilePrefix}_testFileCache*.txt";
  589. $aPathes = glob($sFilePath);
  590. $this->assertEquals(10, count($aPathes), "Error writing test files to cache dir");
  591. //actual test
  592. $this->assertNull($oUtils->oxResetFileCache());
  593. $sFilePath = $myConfig->getConfigParam('sCompileDir') . "/{$sCacheFilePrefix}_testFileCache*.txt";
  594. $aPathes = glob($sFilePath);
  595. $this->assertTrue($aPathes == null);
  596. }
  597. public function testOxResetFileCacheSkipsTablesFieldNames()
  598. {
  599. $myConfig = oxRegistry::getConfig();
  600. $sName = "testFileCache";
  601. $sInput = "test_test_test";
  602. //getting cached files prefix
  603. $myUtilsTest = $this->getProxyClass("oxUtils");
  604. $sFilePath = $myUtilsTest->getCacheFilePath("test");
  605. $sCacheFilePrefix = preg_replace("/.*\/(ox[^_]*)_.*/", "$1", $sFilePath);
  606. //this file must be skipped
  607. $oUtils = oxRegistry::getUtils();
  608. $oUtils->toFileCache("fieldnames_testTest", "testCacheValue");
  609. $oUtils->commitFileCache();
  610. //checking if test file were written to temp dir
  611. $sFilePath = $myConfig->getConfigParam('sCompileDir') . "/{$sCacheFilePrefix}_fieldnames_testTest.txt";
  612. clearstatcache();
  613. $this->assertTrue(file_exists($sFilePath), "Error writing test files to cache dir");
  614. for ($iMax = 0; $iMax < 10; $iMax++) {
  615. $oUtils->toFileCache($sName . "_" . $iMax, $sInput . "_" . $iMax);
  616. }
  617. $oUtils->commitFileCache();
  618. //checking if test files were written to temp dir
  619. $sFilePath = $myConfig->getConfigParam('sCompileDir') . "/{$sCacheFilePrefix}_testFileCache*.txt";
  620. $aPathes = glob($sFilePath);
  621. $this->assertEquals(10, count($aPathes), "Error writing test files to cache dir: " . count($aPathes));
  622. //actual test
  623. $this->assertNull($oUtils->oxResetFileCache());
  624. $sFilePath = $myConfig->getConfigParam('sCompileDir') . "/{$sCacheFilePrefix}_fieldnames_testTest.txt";
  625. $aPathes = glob($sFilePath);
  626. @unlink($aPathes[0]); //deleting test cache file
  627. $this->assertEquals(1, count($aPathes));
  628. }
  629. public function testResetTemplateCache()
  630. {
  631. $myConfig = oxRegistry::getConfig();
  632. $oUtils = oxRegistry::getUtils();
  633. $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty(true);
  634. $sTmpDir = $myConfig->getConfigParam('sCompileDir') . "/smarty/";
  635. $aTemplates = array('message/success.tpl', 'message/notice.tpl', 'message/errors.tpl',);
  636. foreach ($aTemplates as $sTpl) {
  637. $oSmarty->fetch($sTpl);
  638. }
  639. $sRemoveTemplate = basename(reset($aTemplates));
  640. $sLeaveTemplate = basename(array_pop($aTemplates));
  641. //checking if test files were written to temp dir
  642. $this->assertEquals(1, count(glob("{$sTmpDir}/*{$sRemoveTemplate}.php")), "File written " . $sRemoveTemplate);
  643. $this->assertEquals(1, count(glob("{$sTmpDir}/*{$sLeaveTemplate}.php")), "File written " . $sLeaveTemplate);
  644. //Remove templates
  645. $this->assertNull($oUtils->resetTemplateCache($aTemplates));
  646. $this->assertEquals(0, count(glob("{$sTmpDir}/*{$sRemoveTemplate}.php")), "File removed " . $sRemoveTemplate);
  647. $this->assertEquals(1, count(glob("{$sTmpDir}/*{$sLeaveTemplate}.php")), "File left " . $sLeaveTemplate);
  648. }
  649. public function testResetLanguageCache()
  650. {
  651. $myConfig = oxRegistry::getConfig();
  652. $oUtils = oxRegistry::getUtils();
  653. $oSmarty = oxRegistry::get("oxUtilsView")->getSmarty(true);
  654. $sTmpDir = $myConfig->getConfigParam('sCompileDir');
  655. $aFiles = array('langcache_1_a', 'langcache_1_b', 'langcache_1_c');
  656. foreach ($aFiles as $sFile) {
  657. $oUtils->setLangCache($sFile, array($sFile));
  658. }
  659. foreach ($aFiles as $sFile) {
  660. $this->assertEquals(array($sFile), $oUtils->getLangCache($sFile));
  661. }
  662. $this->assertNull($oUtils->resetLanguageCache());
  663. foreach ($aFiles as $sFile) {
  664. $this->assertNull($oUtils->getLangCache($sFile));
  665. }
  666. }
  667. public function testGetRemoteCachePath()
  668. {
  669. touch('misc/actions_main.inc.php', time(), time());
  670. $this->assertEquals('misc/actions_main.inc.php', oxRegistry::getUtils()->GetRemoteCachePath('http://www.blafoo.null', 'misc/actions_main.inc.php'));
  671. //ensure that file is older than 24h
  672. touch('misc/actions_main.inc.php', time() - 90000, time() - 90000);
  673. $this->assertEquals('misc/actions_main.inc.php', oxRegistry::getUtils()->GetRemoteCachePath(oxRegistry::getConfig()->getShopURL(), 'misc/actions_main.inc.php'));
  674. touch('misc/actions_main.inc.php', time() - 90000, time() - 90000);
  675. $this->assertEquals('misc/actions_main.inc.php', oxRegistry::getUtils()->GetRemoteCachePath('http://www.blafoo.null', 'misc/actions_main.inc.php'));
  676. $this->assertEquals(false, oxRegistry::getUtils()->GetRemoteCachePath('http://www.blafoo.null', 'misc/blafoo.test'));
  677. }
  678. public function testCheckAccessRights()
  679. {
  680. $mySession = oxRegistry::getSession();
  681. $backUpAuth = $mySession->getVariable("auth");
  682. $mySession->setVariable("auth", "oxdefaultadmin");
  683. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  684. // self::$test_sql_used = null;
  685. modDB::getInstance()->addClassFunction('getOne', create_function('$sql', 'return 1;'));
  686. $mySession->setVariable("auth", "oxdefaultadmin");
  687. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  688. $mySession->setVariable("auth", "blafooUser");
  689. //self::$test_sql_used = null;
  690. modDB::getInstance()->addClassFunction('getOne', create_function('$sql', 'return 0;'));
  691. $this->assertEquals(false, oxRegistry::getUtils()->checkAccessRights());
  692. $mySession->setVariable("auth", $backUpAuth);
  693. modDB::getInstance()->cleanup();
  694. }
  695. public function testCheckAccessRightsChecksSubshopAdminShop()
  696. {
  697. $mySession = oxRegistry::getSession();
  698. $backUpAuth = $mySession->getVariable("auth");
  699. $e = null;
  700. try {
  701. modDB::getInstance()->addClassFunction('getOne', create_function('$sql', 'return 1;'));
  702. $mySession->setVariable("auth", "blafooUser");
  703. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  704. modConfig::setRequestParameter('fnc', 'chshp');
  705. $this->assertEquals(false, oxRegistry::getUtils()->checkAccessRights());
  706. modConfig::setRequestParameter('fnc', null);
  707. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  708. modConfig::setRequestParameter('actshop', 1);
  709. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  710. modConfig::setRequestParameter('actshop', 2);
  711. $this->assertEquals(false, oxRegistry::getUtils()->checkAccessRights());
  712. modConfig::setRequestParameter('actshop', null);
  713. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  714. modConfig::setRequestParameter('shp', 1);
  715. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  716. modConfig::setRequestParameter('shp', 2);
  717. $this->assertEquals(false, oxRegistry::getUtils()->checkAccessRights());
  718. modConfig::setRequestParameter('shp', null);
  719. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  720. modConfig::setRequestParameter('currentadminshop', 1);
  721. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  722. modConfig::setRequestParameter('currentadminshop', 2);
  723. $this->assertEquals(false, oxRegistry::getUtils()->checkAccessRights());
  724. modConfig::setRequestParameter('currentadminshop', null);
  725. $this->assertEquals(true, oxRegistry::getUtils()->checkAccessRights());
  726. } catch (Exception $e) {
  727. }
  728. $mySession->setVariable("auth", $backUpAuth);
  729. modDB::getInstance()->cleanup();
  730. if ($e) {
  731. throw $e;
  732. }
  733. }
  734. public function testIsValidAlpha()
  735. {
  736. $this->assertEquals(true, oxRegistry::getUtils()->isValidAlpha('oxid'));
  737. $this->assertEquals(true, oxRegistry::getUtils()->isValidAlpha('oxid1'));
  738. $this->assertEquals(false, oxRegistry::getUtils()->isValidAlpha('oxid.'));
  739. $this->assertEquals(false, oxRegistry::getUtils()->isValidAlpha('oxid{'));
  740. $this->assertEquals(true, oxRegistry::getUtils()->isValidAlpha('oxi_d'));
  741. $this->assertEquals(false, oxRegistry::getUtils()->isValidAlpha('ox\\id'));
  742. }
  743. public function testAddUrlParameters()
  744. {
  745. $oUtils = new oxUtils();
  746. $sURL = 'http://www.url.com';
  747. $aParams = array('string' => 'someString', 'bool1' => false, 'bool2' => true, 'int' => 1234, 'float' => 123.45, 'negfloat' => -123.45);
  748. $sReturnURL = "http://www.url.com?string=someString&bool1=&bool2=1&int=1234&float=123.45&negfloat=-123.45";
  749. $this->assertEquals($sReturnURL, $oUtils->UNITaddUrlParameters($sURL, $aParams));
  750. $sURL = 'http://www.url.com/index.php?cl=aaa';
  751. $sReturnURL = "http://www.url.com/index.php?cl=aaa&string=someString&bool1=&bool2=1&int=1234&float=123.45&negfloat=-123.45";
  752. $this->assertEquals($sReturnURL, $oUtils->UNITaddUrlParameters($sURL, $aParams));
  753. }
  754. public function testOxMimeContentType()
  755. {
  756. $oUtils = new oxUtils();
  757. $sFile = 'asdnasd/asdasd.asd.ad.ad.asd.gif';
  758. $this->assertEquals('image/gif', $oUtils->oxMimeContentType($sFile));
  759. $sFile = 'asdnasd/asdasd.asd.ad.ad.asd.jpeg';
  760. $this->assertEquals('image/jpeg', $oUtils->oxMimeContentType($sFile));
  761. $sFile = 'asdnasd/asdasd.asd.ad.ad.asd.jpg';
  762. $this->assertEquals('image/jpeg', $oUtils->oxMimeContentType($sFile));
  763. $sFile = 'asdnasd/asdasd.asd.ad.ad.asd.png';
  764. $this->assertEquals('image/png', $oUtils->oxMimeContentType($sFile));
  765. $sFile = 'asdnasd/asdasd.asd.ad.ad.asdjpeg';
  766. $this->assertEquals(false, $oUtils->oxMimeContentType($sFile));
  767. $this->assertEquals(false, $oUtils->oxMimeContentType(''));
  768. }
  769. public function testStrManStrRem()
  770. {
  771. $sTests = "myblaaFooString!";
  772. $sKey = "oxid987654321";
  773. $oUtils = new oxUtils();
  774. $sCode = $oUtils->strMan($sTests, $sKey);
  775. $this->assertNotEquals($sTests, $sCode);
  776. $sCode = $oUtils->strRem($sCode, $sKey);
  777. $this->assertEquals($sCode, $sTests);
  778. $sCode = $oUtils->strMan($sTests);
  779. $this->assertNotEquals($sTests, $sCode);
  780. $sCode = $oUtils->strRem($sCode);
  781. $this->assertEquals($sTests, $sCode);
  782. }
  783. public function testStrRot13()
  784. {
  785. $sTests = "myblaaFooString!";
  786. $sCode = oxRegistry::getUtils()->strRot13($sTests);
  787. $this->assertEquals($sCode, "zloynnSbbFgevat!");
  788. }
  789. public function testRedirectOffline_WithDefaultHeader()
  790. {
  791. $oConfig = $this->getMock('oxConfig', array('getShopUrl'));
  792. $oConfig->expects($this->once())->method('getShopUrl')->will($this->returnValue('http://shopUrl/'));
  793. $oUtils = $this->getMock('oxutils', array('redirect'));
  794. $oUtils->expects($this->once())->method('redirect')->with($this->equalTo('http://shopUrl/offline.html'), $this->equalTo(false), $this->equalTo(302));
  795. $oUtils->setConfig($oConfig);
  796. $oUtils->redirectOffline();
  797. }
  798. public function testRedirectOffline_WithDifferentHeader()
  799. {
  800. $oConfig = $this->getMock('oxConfig', array('getShopUrl'));
  801. $oConfig->expects($this->once())->method('getShopUrl')->will($this->returnValue('http://shopUrl/'));
  802. $oUtils = $this->getMock('oxutils', array('redirect'));
  803. $oUtils->expects($this->once())->method('redirect')->with($this->equalTo('http://shopUrl/offline.html'), $this->equalTo(false), $this->equalTo(500));
  804. $oUtils->setConfig($oConfig);
  805. $oUtils->redirectOffline(500);
  806. }
  807. public function testRedirect()
  808. {
  809. $oSession = $this->getMock('oxsession', array('freeze'));
  810. $oSession->expects($this->once())->method('freeze');
  811. $oUtils = $this->getMock('oxutils', array('_simpleRedirect', 'getSession'));
  812. $oUtils->expects($this->once())->method('_simpleRedirect')->with($this->equalTo('url?redirected=1'));
  813. $oUtils->expects($this->once())->method('getSession')->will($this->returnValue($oSession));
  814. $oUtils->redirect('url');
  815. }
  816. public function providerRedirectCodes()
  817. {
  818. return array(
  819. array(301, 'HTTP/1.1 301 Moved Permanently'),
  820. array(302, 'HTTP/1.1 302 Found'),
  821. array(500, 'HTTP/1.1 500 Internal Server Error'),
  822. array(423958, 'HTTP/1.1 302 Found'),
  823. );
  824. }
  825. /**
  826. * @param int $iCode header code
  827. * @param string $sHeader formed expected header string
  828. *
  829. * @dataProvider providerRedirectCodes
  830. */
  831. public function testRedirectCodes($iCode, $sHeader)
  832. {
  833. $oSession = $this->getMock('oxsession', array('freeze'));
  834. $oSession->expects($this->any())->method('freeze');
  835. // test also any other to redirect only temporary
  836. $oUtils = $this->getMock('oxutils', array('_simpleRedirect', 'getSession'));
  837. $oUtils->expects($this->once())->method('_simpleRedirect')->with($this->equalTo('url'), $this->equalTo($sHeader));
  838. $oUtils->expects($this->once())->method('getSession')->will($this->returnValue($oSession));
  839. $oUtils->redirect('url', false, $iCode);
  840. }
  841. public function testReRedirect()
  842. {
  843. modConfig::setRequestParameter('redirected', '1');
  844. $oUtils = $this->getMock('oxutils', array('_simpleRedirect', '_addUrlParameters', 'getSession'));
  845. $oUtils->expects($this->never())->method('_simpleRedirect');
  846. $oUtils->expects($this->never())->method('_addUrlParameters');
  847. $oUtils->expects($this->never())->method('getSession');
  848. $oUtils->redirect('url');
  849. }
  850. public function testRedirectWithEncodedEntities()
  851. {
  852. $oUtils = $this->getMock('oxutils', array('_simpleRedirect'));
  853. $oUtils->expects($this->once())->method('_simpleRedirect')->with($this->equalTo('url?param1=1&param2=2&param3=3&redirected=1'));
  854. $oUtils->redirect('url?param1=1&param2=2&amp;param3=3');
  855. }
  856. public function testFromFileCacheEmpty()
  857. {
  858. $oUtils = new oxutils();
  859. $sCacheHit = $oUtils->fromFileCache("notexistantkey");
  860. $this->assertFalse($sCacheHit === false);
  861. $this->assertNull($sCacheHit);
  862. }
  863. public function testCheckUrlEndingSlash()
  864. {
  865. $oUtils = new oxutils();
  866. $this->assertEquals("http://www.site.de/", $oUtils->checkUrlEndingSlash("http://www.site.de/"));
  867. $this->assertEquals("http://www.site.de/", $oUtils->checkUrlEndingSlash("http://www.site.de"));
  868. }
  869. public function testCacheRaceConditions0Size()
  870. {
  871. $oUtils = new oxutils();
  872. $sFileName = $oUtils->getCacheFilePath('testCache1');
  873. @unlink($sFileName);
  874. $oUtils->toFileCache('testCache1', 'teststs');
  875. $oUtils->commitFileCache();
  876. $this->assertEquals(serialize(array('content' => 'teststs')), file_get_contents($sFileName));
  877. unlink($sFileName);
  878. }
  879. public function testCacheRaceConditionsNon0Size()
  880. {
  881. $oUtils = new oxutils();
  882. $sFileName = $oUtils->getCacheFilePath('testCache2');
  883. @unlink($sFileName);
  884. $oUtils->toFileCache('testCache2', 'teststs');
  885. $oUtils->commitFileCache();
  886. $sFileContents = file_get_contents($sFileName);
  887. $this->assertEquals(serialize(array('content' => 'teststs')), $sFileContents);
  888. unlink($sFileName);
  889. }
  890. public function testCacheRaceConditionsIgnoredBySisterProcess()
  891. {
  892. $oUtils1 = new oxutils();
  893. $oUtils2 = new oxutils();
  894. $sFileName = $oUtils1->getCacheFilePath('testCache3');
  895. @unlink($sFileName);
  896. $oUtils1->toFileCache('testCache3', 'instance1111');
  897. $oUtils2->toFileCache('testCache3', 'instance2222');
  898. $oUtils1->commitFileCache();
  899. $oUtils2->commitFileCache();
  900. $sFileContents = file_get_contents($sFileName);
  901. $this->assertEquals(serialize(array('content' => 'instance1111')), $sFileContents);
  902. unlink($sFileName);
  903. }
  904. public function testCachingLockRelease()
  905. {
  906. clearstatcache();
  907. $oUtils1 = new oxutils();
  908. $sFileName = $oUtils1->getCacheFilePath('testCache3');
  909. @unlink($sFileName);
  910. $this->assertFalse(file_exists($sFileName));
  911. $oUtils1->toFileCache('testCache3', 'instance1111');
  912. clearstatcache();
  913. $this->assertTrue(file_exists($sFileName));
  914. $this->assertEquals(0, filesize($sFileName));
  915. $oUtils1->commitFileCache();
  916. clearstatcache();
  917. $this->assertEquals(serialize(array('content' => 'instance1111')), file_get_contents($sFileName));
  918. $this->assertNotEquals(0, filesize($sFileName));
  919. $oUtils2 = new oxutils();
  920. $oUtils2->toFileCache('testCache3', 'instance2222');
  921. clearstatcache();
  922. $this->assertTrue(file_exists($sFileName));
  923. $this->assertEquals(0, filesize($sFileName));
  924. $oUtils2->commitFileCache();
  925. clearstatcache();
  926. $this->assertEquals(serialize(array('content' => 'instance2222')), file_get_contents($sFileName));
  927. $this->assertNotEquals(0, filesize($sFileName));
  928. unlink($sFileName);
  929. }
  930. /**
  931. *
  932. */
  933. public function testCanPreview()
  934. {
  935. modConfig::setRequestParameter("preview", null);
  936. $oUtils = new oxUtils();
  937. $this->assertNull($oUtils->canPreview());
  938. modConfig::setRequestParameter("preview", "132");
  939. oxTestModules::addFunction('oxUtilsServer', 'getOxCookie', '{ return "123"; }');
  940. $this->assertFalse($oUtils->canPreview());
  941. $oUser = new oxUser();
  942. $oUser->load("oxdefaultadmin");
  943. $oUtils = $this->getMock("oxUtils", array("getUser"));
  944. $oUtils->expects($this->any())->method("getUser")->will($this->returnValue($oUser));
  945. modConfig::setRequestParameter("preview", $oUtils->getPreviewId());
  946. oxTestModules::addFunction('oxUtilsServer', 'getOxCookie', '{ return "123"; }');
  947. $this->assertTrue($oUtils->canPreview());
  948. }
  949. /**
  950. * oxUtils::getPreviewId() test case
  951. *
  952. * @return null
  953. */
  954. public function testGetPreviewId()
  955. {
  956. $sAdminSid = oxRegistry::get("oxUtilsServer")->getOxCookie('admin_sid');
  957. $sCompare = md5($sAdminSid . "testID" . "testPass" . "tesrRights");
  958. $oUser = $this->getMock("oxUser", array("getId"));
  959. $oUser->expects($this->once())->method("getId")->will($this->returnValue("testID"));
  960. $oUser->oxuser__oxpassword = new oxField("testPass");
  961. $oUser->oxuser__oxrights = new oxField("tesrRights");
  962. $oUtils = $this->getMock("oxUtils", array("getUser"));
  963. $oUtils->expects($this->once())->method("getUser")->will($this->returnValue($oUser));
  964. $this->assertEquals($sCompare, $oUtils->getPreviewId());
  965. }
  966. public function testHandlePageNotFoundError()
  967. {
  968. $this->markTestIncomplete("Incorrect test for page not found headers. Normal headers mixed up with page not found");
  969. oxTestModules::addFunction('oxutils', 'showMessageAndExit', '{$this->showMessageAndExitCall[] = $aA; }');
  970. oxTestModules::addFunction('oxutils', 'setHeader', '{$this->setHeaderCall[] = $aA;}');
  971. oxTestModules::addFunction('oxUtilsView', 'getTemplateOutput', '{$this->getTemplateOutputCall[] = $aA; return "msg_".count($this->getTemplateOutputCall);}');
  972. oxRegistry::getUtils()->handlePageNotFoundError();
  973. $this->assertGreaterThanOrEqual(1, count(oxRegistry::getUtils()->setHeaderCall));
  974. $this->assertEquals(1, count(oxRegistry::get("oxUtilsView")->getTemplateOutputCall));
  975. $this->assertEquals(1, count(oxRegistry::getUtils()->showMessageAndExitCall));
  976. $this->assertEquals("msg_1", oxRegistry::getUtils()->showMessageAndExitCall[0][0]);
  977. $this->assertEquals("HTTP/1.0 404 Not Found", oxRegistry::getUtils()->setHeaderCall[0][0]);
  978. oxRegistry::getUtils()->handlePageNotFoundError("url aa");
  979. $this->assertGreaterThanOrEqual(2, count(oxRegistry::getUtils()->setHeaderCall));
  980. $this->assertEquals(2, count(oxRegistry::get("oxUtilsView")->getTemplateOutputCall));
  981. $this->assertEquals(2, count(oxRegistry::getUtils()->showMessageAndExitCall));
  982. $this->assertEquals("msg_2", oxRegistry::getUtils()->showMessageAndExitCall[1][0]);
  983. $this->assertEquals("HTTP/1.0 404 Not Found", oxRegistry::getUtils()->setHeaderCall[1][0]);
  984. oxTestModules::addFunction('oxUBase', 'render', '{throw new Exception();}');
  985. oxRegistry::getUtils()->handlePageNotFoundError("url aa");
  986. $this->assertEquals(3, count(oxRegistry::getUtils()->setHeaderCall));
  987. $this->assertEquals(2, count(oxRegistry::get("oxUtilsView")->getTemplateOutputCall));
  988. $this->assertEquals(3, count(oxRegistry::getUtils()->showMessageAndExitCall));
  989. $this->assertEquals("Page not found.", oxRegistry::getUtils()->showMessageAndExitCall[2][0]);
  990. }
  991. public function testToPhpFileCache()
  992. {
  993. $sTestArray = array("testVal1", "key1" => "testVal2");
  994. $oUtils = oxRegistry::getUtils();
  995. $oUtils->toPhpFileCache("testVal", $sTestArray);
  996. $oUtils->commitFileCache();
  997. $sFileName = oxRegistry::getUtils()->getCacheFilePath("testVal", false, 'php');
  998. include($sFileName);
  999. $this->assertEquals($_aCacheContents['content'], $sTestArray);
  1000. unlink($sFileName);
  1001. }
  1002. /**
  1003. * Test for bug #1737
  1004. *
  1005. */
  1006. public function testToPhpFileCacheException()
  1007. {
  1008. $oSubj = $this->getMock("oxUtils", array("getCacheFilePath"));
  1009. $oSubj->expects($this->any())->method("getCacheFilePath")->will($this->returnValue(false));
  1010. oxTestModules::addModuleObject("oxUtils", $oSubj);
  1011. $sTestArray = array("testVal1", "key1" => "testVal2");
  1012. oxRegistry::getUtils()->toPhpFileCache("testVal2", $sTestArray);
  1013. $aCacheContents = oxRegistry::getUtils()->fromPhpFileCache("testVal2");
  1014. $this->assertNull($aCacheContents);
  1015. }
  1016. public function testFromPhpFileCache()
  1017. {
  1018. $sTestArray = array("testVal1", "key1" => "testVal2");
  1019. $oUtils = oxRegistry::getUtils();
  1020. $oUtils->toPhpFileCache("testVal", $sTestArray);
  1021. $oUtils->commitFileCache();
  1022. $this->assertEquals($oUtils->fromPhpFileCache("testVal"), $sTestArray);
  1023. }
  1024. /**
  1025. * oxUtils::getCacheMeta() & oxUtils::setCacheMeta() test case
  1026. *
  1027. * @return null
  1028. */
  1029. public function testGetCacheMetaSetCacheMeta()
  1030. {
  1031. $oUtils = new oxUtils();
  1032. $oUtils->setCacheMeta("xxx", "yyy");
  1033. $this->assertFalse($oUtils->getCacheMeta("yyy"));
  1034. $this->assertEquals("yyy", $oUtils->getCacheMeta("xxx"));
  1035. }
  1036. /**
  1037. * oxUtils::_readFile() test case
  1038. *
  1039. * @return null
  1040. */
  1041. public function testReadFile()
  1042. {
  1043. $sFilePath = oxRegistry::getUtils()->getCacheFilePath("testVal", false, 'php');
  1044. if (($hFile = @fopen($sFilePath, "w")) !== false) {
  1045. fwrite($hFile, serialize("test"));
  1046. fclose($hFile);
  1047. $oUtils = new oxUtils();
  1048. $this->assertEquals("test", $oUtils->UNITreadFile($sFilePath));
  1049. return;
  1050. }
  1051. $this->markTestSkipped("Unable to create file {$sFilePath}");
  1052. }
  1053. /**
  1054. * oxUtils::_includeFile() test case
  1055. *
  1056. * @return null
  1057. */
  1058. public function testIncludeFile()
  1059. {
  1060. $sFilePath = oxRegistry::getUtils()->getCacheFilePath("testVal", false, 'php');
  1061. if ((

Large files files are truncated, but you can click here to view the full file