PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suite/joomla/utilities/JUtilityTest.php

https://github.com/dg482/joomla-platform
PHP | 502 lines | 322 code | 24 blank | 156 comment | 0 complexity | ef68027dd97f19efc0df142134c1b38a MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Utilities
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. include_once JPATH_PLATFORM . '/joomla/utilities/utility.php';
  10. /**
  11. * Test class for JUtility.
  12. * Generated by PHPUnit on 2009-10-26 at 22:28:32.
  13. *
  14. * @package Joomla.UnitTest
  15. * @subpackage Utilities
  16. *
  17. */
  18. class JUtilityTest extends JoomlaTestCase
  19. {
  20. /**
  21. * @var JUtility
  22. */
  23. protected $object;
  24. /**
  25. * Sets up the fixture, for example, opens a network connection.
  26. * This method is called before a test is executed.
  27. *
  28. * @return void
  29. */
  30. protected function setUp()
  31. {
  32. $this->saveFactoryState();
  33. }
  34. /**
  35. * Tears down the fixture, for example, closes a network connection.
  36. * This method is called after a test is executed.
  37. *
  38. * @return void
  39. */
  40. protected function tearDown()
  41. {
  42. $this->restoreFactoryState();
  43. }
  44. /**
  45. * Test cases for sendMail
  46. *
  47. * @return array
  48. */
  49. function casesSendMail()
  50. {
  51. return array(
  52. 'simple' => array(
  53. array(
  54. 'from' => "test",
  55. 'fromname' => 'Test Name',
  56. 'recipient' => 'Test Recip',
  57. 'subject' => 'Test Subject',
  58. 'body' => "This is the body of the message",
  59. 'mode' => 'Test Mode',
  60. 'cc' => 'Test CC',
  61. 'bcc' => 'Test BCC',
  62. 'attachment' => 'Test att',
  63. 'replyto' => 'test replyto',
  64. 'replytoname' => 'test replytoname',
  65. ),
  66. array(
  67. 'from' => "test",
  68. 'fromname' => 'Test Name',
  69. 'recipient' => 'Test Recip',
  70. 'subject' => 'Test Subject',
  71. 'body' => "This is the body of the message",
  72. 'mode' => 'Test Mode',
  73. 'cc' => 'Test CC',
  74. 'bcc' => 'Test BCC',
  75. 'attachment' => 'Test att',
  76. 'replyto' => 'test replyto',
  77. 'replytoname' => 'test replytoname',
  78. ),
  79. true,
  80. ),
  81. 'failure' => array(
  82. array(
  83. 'from' => "test",
  84. 'fromname' => 'Test Name',
  85. 'recipient' => 'Test Recip',
  86. 'subject' => 'Test Subject',
  87. 'body' => "This is the body of the message",
  88. 'mode' => 'Test Mode',
  89. 'cc' => 'Test CC',
  90. 'bcc' => 'Test BCC',
  91. 'attachment' => 'Test att',
  92. 'replyto' => 'test replyto',
  93. 'replytoname' => 'test replytoname',
  94. ),
  95. array(
  96. 'from' => "test",
  97. 'fromname' => 'Test Name',
  98. 'recipient' => 'Test Recip',
  99. 'subject' => 'Test Subject',
  100. 'body' => "This is the body of the message",
  101. 'mode' => 'Test Mode',
  102. 'cc' => 'Test CC',
  103. 'bcc' => 'Test BCC',
  104. 'attachment' => 'Test att',
  105. 'replyto' => 'test replyto',
  106. 'replytoname' => 'test replytoname',
  107. ),
  108. false,
  109. ),
  110. );
  111. }
  112. /**
  113. * Testing sendMail().
  114. *
  115. * @param array Input arguments for array
  116. * @param array Arguments received by array
  117. * @param bool Expected result of method call
  118. *
  119. * @return void
  120. * @dataProvider casesSendMail
  121. */
  122. public function testSendMail( $args, $expectedArgs, $expResult )
  123. {
  124. $mockMailer = $this->getMock('JMail', array('sendMail'));
  125. $mockMailer->expects($this->once())->method('sendMail')->with(
  126. $this->equalTo($expectedArgs['from']), $this->equalTo($expectedArgs['fromname']),
  127. $this->equalTo($expectedArgs['recipient']), $this->equalTo($expectedArgs['subject']),
  128. $this->equalTo($expectedArgs['body']), $this->equalTo($expectedArgs['mode']),
  129. $this->equalTo($expectedArgs['cc']), $this->equalTo($expectedArgs['bcc']),
  130. $this->equalTo($expectedArgs['attachment']), $this->equalTo($expectedArgs['replyto']),
  131. $this->equalTo($expectedArgs['replytoname'])
  132. )->will($this->returnValue($expResult));
  133. JFactory::$mailer = $mockMailer;
  134. $this->assertThat(
  135. JUtility::sendMail(
  136. $args['from'], $args['fromname'], $args['recipient'],
  137. $args['subject'], $args['body'], $args['mode'], $args['cc'], $args['bcc'],
  138. $args['attachment'], $args['replyto'], $args['replytoname']
  139. ),
  140. $this->equalTo($expResult)
  141. );
  142. }
  143. /**
  144. * Test cases for sendAdminMail
  145. *
  146. * @return array
  147. */
  148. function casesSendAdminMail()
  149. {
  150. return array(
  151. 'simple' => array(
  152. array(
  153. 'adminName' => "test",
  154. 'adminEmail' => 'Test Name',
  155. 'email' => 'Test Recip',
  156. 'type' => 'Test Subject',
  157. 'title' => "This is the body of the message",
  158. 'author' => 'Test Mode',
  159. 'url' => 'Test CC',
  160. ),
  161. array(
  162. 'adminName' => "test",
  163. 'adminEmail' => 'Test Name',
  164. 'email' => 'Test Recip',
  165. 'type' => 'Test Subject',
  166. 'title' => "This is the body of the message",
  167. 'author' => 'Test Mode',
  168. 'url' => 'Test CC',
  169. ),
  170. true,
  171. ),
  172. 'failure' => array(
  173. array(
  174. 'adminName' => "test",
  175. 'adminEmail' => 'Test Name',
  176. 'email' => 'Test Recip',
  177. 'type' => 'Test Subject',
  178. 'title' => "This is the body of the message",
  179. 'author' => 'Test Mode',
  180. 'url' => 'Test CC',
  181. ),
  182. array(
  183. 'adminName' => "test",
  184. 'adminEmail' => 'Test Name',
  185. 'email' => 'Test Recip',
  186. 'type' => 'Test Subject',
  187. 'title' => "This is the body of the message",
  188. 'author' => 'Test Mode',
  189. 'url' => 'Test CC',
  190. ),
  191. false,
  192. ),
  193. );
  194. }
  195. /**
  196. * Testing sendAdminMail().
  197. *
  198. * @param array Arguments for method call
  199. * @param array Arguments received by method
  200. * @param bool Expected return from call
  201. *
  202. * @return void
  203. *
  204. * @dataProvider casesSendAdminMail
  205. */
  206. public function testSendAdminMail( $args, $expectedArgs, $expResult )
  207. {
  208. $mockMailer = $this->getMock('JMail', array('sendAdminMail'));
  209. $mockMailer->expects($this->once())->method('sendAdminMail')->with(
  210. $this->equalTo($expectedArgs['adminName']), $this->equalTo($expectedArgs['adminEmail']),
  211. $this->equalTo($expectedArgs['email']), $this->equalTo($expectedArgs['type']),
  212. $this->equalTo($expectedArgs['title']), $this->equalTo($expectedArgs['author']),
  213. $this->equalTo($expectedArgs['url'])
  214. )->will($this->returnValue($expResult));
  215. JFactory::$mailer = $mockMailer;
  216. $this->assertThat(
  217. JUtility::sendAdminMail(
  218. $args['adminName'], $args['adminEmail'], $args['email'],
  219. $args['type'], $args['title'], $args['author'], $args['url']
  220. ),
  221. $this->equalTo($expResult)
  222. );
  223. }
  224. /**
  225. * Testing getHash().
  226. *
  227. * @return void
  228. */
  229. public function testGetHash()
  230. {
  231. $expResult = MD5("This is a test");
  232. /**$mockApplication = $this->getMock('JApplication', array('getHash'), array('site'));
  233. $mockApplication->expects($this->once())->method('getHash')->will(
  234. $this->returnValue($expResult)
  235. );**/
  236. require_once(JPATH_PLATFORM . '/joomla/registry/registry.php');
  237. $temp = JFactory::$application;
  238. JFactory::$application = new JRegistry();
  239. $this->assertThat(
  240. JUtility::getHash('This is a test'),
  241. $this->equalTo($expResult)
  242. );
  243. JFactory::$application = $temp;
  244. }
  245. /**
  246. * Test cases for getToken
  247. *
  248. * @return array
  249. */
  250. function casesToken()
  251. {
  252. return array(
  253. 'default' => array(
  254. null,
  255. false,
  256. 1
  257. ),
  258. 'false' => array(
  259. false,
  260. false,
  261. 2
  262. ),
  263. 'true' => array(
  264. true,
  265. true,
  266. 3
  267. ),
  268. );
  269. }
  270. /**
  271. * Testing getToken().
  272. *
  273. * @param bool Input to getToken call
  274. * @param bool Expected results from getToken
  275. *
  276. * @return void
  277. *
  278. * @dataProvider casesToken
  279. */
  280. public function testGetToken( $data, $expResult, $counter )
  281. {
  282. //include_once JPATH_BASE . '/libraries/joomla/application/application.php';
  283. /** $mockSession = $this->getMock('JSession', array('_start', 'getFormToken'), array(), 'JSession'.$counter, false);
  284. $mockSession
  285. ->expects($this->any())
  286. ->method('getFormToken')
  287. ->with($data)
  288. ->will($this->returnValue($expResult));**/
  289. JFactory::$session = new Mock_Session;
  290. $this->assertThat(
  291. is_null($data)?JUtility::getToken():JUtility::getToken($data),
  292. $this->equalTo($expResult)
  293. );
  294. JFactory::$session = null;
  295. }
  296. /**
  297. * Test cases for parseAttributes
  298. *
  299. * @return array
  300. */
  301. function casesParseAttributes()
  302. {
  303. return array(
  304. 'jdoc' => array(
  305. '<jdoc style="fred" />',
  306. array( 'style' => 'fred' )
  307. ),
  308. 'xml' => array(
  309. "<img hear=\"something\" there=\"somethingelse\" />",
  310. array( 'hear' => 'something', 'there' => 'somethingelse' )
  311. ),
  312. );
  313. }
  314. /**
  315. * Test parseAttributes
  316. *
  317. * @param string tag to be parsed
  318. * @param array resulting array of attribute values
  319. *
  320. * @return void
  321. *
  322. * @dataProvider casesParseAttributes
  323. */
  324. public function testParseAttributes( $tag, $expected )
  325. {
  326. $this->assertThat(
  327. JUtility::parseAttributes($tag),
  328. $this->equalTo($expected)
  329. );
  330. }
  331. /**
  332. * Test cases for isWinOS
  333. *
  334. * @return array
  335. */
  336. function casesWinOS()
  337. {
  338. return array(
  339. 'false' => array(
  340. false,
  341. ),
  342. 'true' => array(
  343. true,
  344. ),
  345. );
  346. }
  347. /**
  348. * Testing isWinOS().
  349. *
  350. * @param bool return value from mock
  351. *
  352. * @return void
  353. *
  354. * @dataProvider casesWinOS
  355. */
  356. public function testIsWinOS( $expResult )
  357. {
  358. $mockApplication = $this->getMock('JAplication', array('isWinOS'));
  359. $mockApplication->expects($this->once())->method('isWinOS')->will(
  360. $this->returnValue($expResult)
  361. );
  362. JFactory::$application = $mockApplication;
  363. $this->assertThat(
  364. JUtility::isWinOS(),
  365. $this->equalTo($expResult)
  366. );
  367. }
  368. /**
  369. * Test cases for isWinOS
  370. *
  371. * @return array
  372. */
  373. function casesDump()
  374. {
  375. return array(
  376. 'false' => array(
  377. array(
  378. 'fred' => 12,
  379. 'blue' => 2,
  380. 'banana' => 1,
  381. ),
  382. false,
  383. "<pre>array (\n 'fred' => 12,\n 'blue' => 2,\n 'banana' => 1,\n)</pre>"
  384. ),
  385. 'true' => array(
  386. array(
  387. 'fred' => 12,
  388. 'blue' => 2,
  389. 'banana' => 1,
  390. ),
  391. true,
  392. "<pre>array (\n 'fred' =&gt; 12,\n 'blue' =&gt; 2,\n 'banana' =&gt; 1,\n)</pre>"
  393. ),
  394. );
  395. }
  396. /**
  397. * Test dump
  398. *
  399. * @param mixed variable to be dumped
  400. * @param bool Should text be htmlsafe
  401. * @param string expected result
  402. *
  403. * @return void
  404. * @dataProvider casesDump
  405. */
  406. public function testDump( $value, $htmlsafe, $expected )
  407. {
  408. $this->assertThat(
  409. JUtility::dump($value, $htmlsafe),
  410. $this->equalTo($expected)
  411. );
  412. }
  413. /**
  414. * Test array_unshift_ref
  415. *
  416. * @return void
  417. * @todo Implement testArray_unshift_ref().
  418. */
  419. public function testArrayUnshiftRef()
  420. {
  421. $input = array( 3, 5, 7 );
  422. $expected = array( "fred", 3, 5, 7 );
  423. $value = "fred";
  424. $this->assertThat(
  425. JUtility::array_unshift_ref($input, $value),
  426. $this->equalTo(4)
  427. );
  428. $this->assertThat(
  429. $input,
  430. $this->equalTo($expected)
  431. );
  432. }
  433. /**
  434. * Test cases for return_bytes
  435. *
  436. * @return array
  437. */
  438. function casesReturnBytes()
  439. {
  440. return array(
  441. 'G' => array(
  442. '100G',
  443. 107374182400,
  444. ),
  445. 'M' => array(
  446. '10M',
  447. 10485760,
  448. ),
  449. 'K' => array(
  450. '100K',
  451. 102400,
  452. ),
  453. );
  454. }
  455. /**
  456. * Test return_bytes
  457. *
  458. * @param string Descirption of size in multiples of 1024
  459. * @param int Size in bytes
  460. *
  461. * @return void
  462. * @dataProvider casesReturnBytes
  463. */
  464. public function testReturnBytes( $bytes, $expected )
  465. {
  466. $this->assertThat(
  467. JUtility::return_bytes($bytes),
  468. $this->equalTo($expected)
  469. );
  470. }
  471. }
  472. class Mock_Session
  473. {
  474. function getFormToken($data)
  475. {
  476. return (bool) $data;
  477. }
  478. }