/tests/suite/joomla/utilities/JUtilityTest.php
PHP | 502 lines | 322 code | 24 blank | 156 comment | 0 complexity | ef68027dd97f19efc0df142134c1b38a MD5 | raw file
- <?php
- /**
- * @package Joomla.UnitTest
- * @subpackage Utilities
- *
- * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
- include_once JPATH_PLATFORM . '/joomla/utilities/utility.php';
- /**
- * Test class for JUtility.
- * Generated by PHPUnit on 2009-10-26 at 22:28:32.
- *
- * @package Joomla.UnitTest
- * @subpackage Utilities
- *
- */
- class JUtilityTest extends JoomlaTestCase
- {
- /**
- * @var JUtility
- */
- protected $object;
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- protected function setUp()
- {
- $this->saveFactoryState();
- }
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- *
- * @return void
- */
- protected function tearDown()
- {
- $this->restoreFactoryState();
- }
- /**
- * Test cases for sendMail
- *
- * @return array
- */
- function casesSendMail()
- {
- return array(
- 'simple' => array(
- array(
- 'from' => "test",
- 'fromname' => 'Test Name',
- 'recipient' => 'Test Recip',
- 'subject' => 'Test Subject',
- 'body' => "This is the body of the message",
- 'mode' => 'Test Mode',
- 'cc' => 'Test CC',
- 'bcc' => 'Test BCC',
- 'attachment' => 'Test att',
- 'replyto' => 'test replyto',
- 'replytoname' => 'test replytoname',
- ),
- array(
- 'from' => "test",
- 'fromname' => 'Test Name',
- 'recipient' => 'Test Recip',
- 'subject' => 'Test Subject',
- 'body' => "This is the body of the message",
- 'mode' => 'Test Mode',
- 'cc' => 'Test CC',
- 'bcc' => 'Test BCC',
- 'attachment' => 'Test att',
- 'replyto' => 'test replyto',
- 'replytoname' => 'test replytoname',
- ),
- true,
- ),
- 'failure' => array(
- array(
- 'from' => "test",
- 'fromname' => 'Test Name',
- 'recipient' => 'Test Recip',
- 'subject' => 'Test Subject',
- 'body' => "This is the body of the message",
- 'mode' => 'Test Mode',
- 'cc' => 'Test CC',
- 'bcc' => 'Test BCC',
- 'attachment' => 'Test att',
- 'replyto' => 'test replyto',
- 'replytoname' => 'test replytoname',
- ),
- array(
- 'from' => "test",
- 'fromname' => 'Test Name',
- 'recipient' => 'Test Recip',
- 'subject' => 'Test Subject',
- 'body' => "This is the body of the message",
- 'mode' => 'Test Mode',
- 'cc' => 'Test CC',
- 'bcc' => 'Test BCC',
- 'attachment' => 'Test att',
- 'replyto' => 'test replyto',
- 'replytoname' => 'test replytoname',
- ),
- false,
- ),
- );
- }
- /**
- * Testing sendMail().
- *
- * @param array Input arguments for array
- * @param array Arguments received by array
- * @param bool Expected result of method call
- *
- * @return void
- * @dataProvider casesSendMail
- */
- public function testSendMail( $args, $expectedArgs, $expResult )
- {
- $mockMailer = $this->getMock('JMail', array('sendMail'));
- $mockMailer->expects($this->once())->method('sendMail')->with(
- $this->equalTo($expectedArgs['from']), $this->equalTo($expectedArgs['fromname']),
- $this->equalTo($expectedArgs['recipient']), $this->equalTo($expectedArgs['subject']),
- $this->equalTo($expectedArgs['body']), $this->equalTo($expectedArgs['mode']),
- $this->equalTo($expectedArgs['cc']), $this->equalTo($expectedArgs['bcc']),
- $this->equalTo($expectedArgs['attachment']), $this->equalTo($expectedArgs['replyto']),
- $this->equalTo($expectedArgs['replytoname'])
- )->will($this->returnValue($expResult));
- JFactory::$mailer = $mockMailer;
- $this->assertThat(
- JUtility::sendMail(
- $args['from'], $args['fromname'], $args['recipient'],
- $args['subject'], $args['body'], $args['mode'], $args['cc'], $args['bcc'],
- $args['attachment'], $args['replyto'], $args['replytoname']
- ),
- $this->equalTo($expResult)
- );
- }
- /**
- * Test cases for sendAdminMail
- *
- * @return array
- */
- function casesSendAdminMail()
- {
- return array(
- 'simple' => array(
- array(
- 'adminName' => "test",
- 'adminEmail' => 'Test Name',
- 'email' => 'Test Recip',
- 'type' => 'Test Subject',
- 'title' => "This is the body of the message",
- 'author' => 'Test Mode',
- 'url' => 'Test CC',
- ),
- array(
- 'adminName' => "test",
- 'adminEmail' => 'Test Name',
- 'email' => 'Test Recip',
- 'type' => 'Test Subject',
- 'title' => "This is the body of the message",
- 'author' => 'Test Mode',
- 'url' => 'Test CC',
- ),
- true,
- ),
- 'failure' => array(
- array(
- 'adminName' => "test",
- 'adminEmail' => 'Test Name',
- 'email' => 'Test Recip',
- 'type' => 'Test Subject',
- 'title' => "This is the body of the message",
- 'author' => 'Test Mode',
- 'url' => 'Test CC',
- ),
- array(
- 'adminName' => "test",
- 'adminEmail' => 'Test Name',
- 'email' => 'Test Recip',
- 'type' => 'Test Subject',
- 'title' => "This is the body of the message",
- 'author' => 'Test Mode',
- 'url' => 'Test CC',
- ),
- false,
- ),
- );
- }
- /**
- * Testing sendAdminMail().
- *
- * @param array Arguments for method call
- * @param array Arguments received by method
- * @param bool Expected return from call
- *
- * @return void
- *
- * @dataProvider casesSendAdminMail
- */
- public function testSendAdminMail( $args, $expectedArgs, $expResult )
- {
- $mockMailer = $this->getMock('JMail', array('sendAdminMail'));
- $mockMailer->expects($this->once())->method('sendAdminMail')->with(
- $this->equalTo($expectedArgs['adminName']), $this->equalTo($expectedArgs['adminEmail']),
- $this->equalTo($expectedArgs['email']), $this->equalTo($expectedArgs['type']),
- $this->equalTo($expectedArgs['title']), $this->equalTo($expectedArgs['author']),
- $this->equalTo($expectedArgs['url'])
- )->will($this->returnValue($expResult));
- JFactory::$mailer = $mockMailer;
- $this->assertThat(
- JUtility::sendAdminMail(
- $args['adminName'], $args['adminEmail'], $args['email'],
- $args['type'], $args['title'], $args['author'], $args['url']
- ),
- $this->equalTo($expResult)
- );
- }
- /**
- * Testing getHash().
- *
- * @return void
- */
- public function testGetHash()
- {
- $expResult = MD5("This is a test");
- /**$mockApplication = $this->getMock('JApplication', array('getHash'), array('site'));
- $mockApplication->expects($this->once())->method('getHash')->will(
- $this->returnValue($expResult)
- );**/
- require_once(JPATH_PLATFORM . '/joomla/registry/registry.php');
- $temp = JFactory::$application;
- JFactory::$application = new JRegistry();
- $this->assertThat(
- JUtility::getHash('This is a test'),
- $this->equalTo($expResult)
- );
- JFactory::$application = $temp;
- }
- /**
- * Test cases for getToken
- *
- * @return array
- */
- function casesToken()
- {
- return array(
- 'default' => array(
- null,
- false,
- 1
- ),
- 'false' => array(
- false,
- false,
- 2
- ),
- 'true' => array(
- true,
- true,
- 3
- ),
- );
- }
- /**
- * Testing getToken().
- *
- * @param bool Input to getToken call
- * @param bool Expected results from getToken
- *
- * @return void
- *
- * @dataProvider casesToken
- */
- public function testGetToken( $data, $expResult, $counter )
- {
- //include_once JPATH_BASE . '/libraries/joomla/application/application.php';
- /** $mockSession = $this->getMock('JSession', array('_start', 'getFormToken'), array(), 'JSession'.$counter, false);
- $mockSession
- ->expects($this->any())
- ->method('getFormToken')
- ->with($data)
- ->will($this->returnValue($expResult));**/
- JFactory::$session = new Mock_Session;
- $this->assertThat(
- is_null($data)?JUtility::getToken():JUtility::getToken($data),
- $this->equalTo($expResult)
- );
- JFactory::$session = null;
- }
- /**
- * Test cases for parseAttributes
- *
- * @return array
- */
- function casesParseAttributes()
- {
- return array(
- 'jdoc' => array(
- '<jdoc style="fred" />',
- array( 'style' => 'fred' )
- ),
- 'xml' => array(
- "<img hear=\"something\" there=\"somethingelse\" />",
- array( 'hear' => 'something', 'there' => 'somethingelse' )
- ),
- );
- }
- /**
- * Test parseAttributes
- *
- * @param string tag to be parsed
- * @param array resulting array of attribute values
- *
- * @return void
- *
- * @dataProvider casesParseAttributes
- */
- public function testParseAttributes( $tag, $expected )
- {
- $this->assertThat(
- JUtility::parseAttributes($tag),
- $this->equalTo($expected)
- );
- }
- /**
- * Test cases for isWinOS
- *
- * @return array
- */
- function casesWinOS()
- {
- return array(
- 'false' => array(
- false,
- ),
- 'true' => array(
- true,
- ),
- );
- }
- /**
- * Testing isWinOS().
- *
- * @param bool return value from mock
- *
- * @return void
- *
- * @dataProvider casesWinOS
- */
- public function testIsWinOS( $expResult )
- {
- $mockApplication = $this->getMock('JAplication', array('isWinOS'));
- $mockApplication->expects($this->once())->method('isWinOS')->will(
- $this->returnValue($expResult)
- );
- JFactory::$application = $mockApplication;
- $this->assertThat(
- JUtility::isWinOS(),
- $this->equalTo($expResult)
- );
- }
- /**
- * Test cases for isWinOS
- *
- * @return array
- */
- function casesDump()
- {
- return array(
- 'false' => array(
- array(
- 'fred' => 12,
- 'blue' => 2,
- 'banana' => 1,
- ),
- false,
- "<pre>array (\n 'fred' => 12,\n 'blue' => 2,\n 'banana' => 1,\n)</pre>"
- ),
- 'true' => array(
- array(
- 'fred' => 12,
- 'blue' => 2,
- 'banana' => 1,
- ),
- true,
- "<pre>array (\n 'fred' => 12,\n 'blue' => 2,\n 'banana' => 1,\n)</pre>"
- ),
- );
- }
- /**
- * Test dump
- *
- * @param mixed variable to be dumped
- * @param bool Should text be htmlsafe
- * @param string expected result
- *
- * @return void
- * @dataProvider casesDump
- */
- public function testDump( $value, $htmlsafe, $expected )
- {
- $this->assertThat(
- JUtility::dump($value, $htmlsafe),
- $this->equalTo($expected)
- );
- }
- /**
- * Test array_unshift_ref
- *
- * @return void
- * @todo Implement testArray_unshift_ref().
- */
- public function testArrayUnshiftRef()
- {
- $input = array( 3, 5, 7 );
- $expected = array( "fred", 3, 5, 7 );
- $value = "fred";
- $this->assertThat(
- JUtility::array_unshift_ref($input, $value),
- $this->equalTo(4)
- );
- $this->assertThat(
- $input,
- $this->equalTo($expected)
- );
- }
- /**
- * Test cases for return_bytes
- *
- * @return array
- */
- function casesReturnBytes()
- {
- return array(
- 'G' => array(
- '100G',
- 107374182400,
- ),
- 'M' => array(
- '10M',
- 10485760,
- ),
- 'K' => array(
- '100K',
- 102400,
- ),
- );
- }
- /**
- * Test return_bytes
- *
- * @param string Descirption of size in multiples of 1024
- * @param int Size in bytes
- *
- * @return void
- * @dataProvider casesReturnBytes
- */
- public function testReturnBytes( $bytes, $expected )
- {
- $this->assertThat(
- JUtility::return_bytes($bytes),
- $this->equalTo($expected)
- );
- }
- }
- class Mock_Session
- {
- function getFormToken($data)
- {
- return (bool) $data;
- }
- }