Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add some new tests, clean up some others. #1736

Merged
merged 1 commit into from

2 participants

@elinw

This adds tests to a number of classes and also reorganizes some to be more useful (for example separating into separate tests so all assertions are tested regardless of failures of other assertions) including JFactory, JApplicationBase, JSession, and JUserHelper.

@elinw

There is one failing test on this pr which is the getExpire() test. The fact is the method works as expected but the expected value should be 900. If you dump out the object that is what is there, and that is what getExpire() returns. The problem is that the test as written has expected value of 20 which is what you would expect if $this->object had actually been successfully defined at the top of the test class. However, when the full suite of tests is run this does not happen and instead the expiration is set to 900 which is the default of 15 * 60 as calculated in the factory. Because of how sessions are managed changing this is complicated and it seems unnecessary since all we want to know is if the method is returning the value that the property has. So therefore I am thinking that I will change the expected to 900 and have all passing tests. (When just some tests are run the test currently passes with a value of 20. We may or may not consider it a problem that the value is not changing to 20 but that is not the responsibility of this specific test.)

Yes we do have a default session length of 15 hours (see #1715) but that's not related to whether getExpire() works.

@elinw

Just to be clear I don't actually think this is ready to merge because the fact is that there is something wrong with how this whole test is working, which may be a problem in the test or may be a problem in the class. It should not be the case that the expecteds change depending on whether you run the test by itself or if you run it as part of a full suite or if you add a new test to the suite.
The getInstance test actually illustrates exactly that you can't change the expired value using getInstance when there is an existing session just by using getInstance. Dumping out the oldSession and newSession objects shows that they are both the same object and both have expire=900 despite what is set in the test.
If instead we started the test with
$this->object = new JSession('none', array('expire' => 20, 'force_ssl' => true, 'name' => 'name', 'id' => 'id', 'security' => 'security'));
we would see that the getInstance tests fail and we would also see the getFormTokenTest fail but the old getExpire test passes with expected of 20.

I'm reasonably certain that the getInstance test should be
$this->assertSame(
$oldSession,$newSession
);

but either way it is the same thing in terms of the test passing because it's just getting a default session i.e. expire=900 for both in the current test.

@elinw

Ok now if jenkins says it's ok I think these are ready.

@pasamio pasamio merged commit e6a697e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 8, 2012
  1. @elinw
This page is out of date. Refresh to see the latest.
View
60 tests/suites/unit/joomla/JFactoryTest.php
@@ -232,10 +232,10 @@ public function testGetXml()
*
* @return void
*
- * @since 11.3
+ * @since 12.3
* @covers JFactory::getDate
*/
- public function testGetDate()
+ public function testGetDateUnchanged()
{
JFactory::$language = $this->getMockLanguage();
@@ -246,6 +246,19 @@ public function testGetDate()
$this->equalTo('2001-01-01 01:01:01'),
'Tests that a date passed in comes back unchanged.'
);
+ }
+
+ /**
+ * Tests the JFactory::getDate method.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JFactory::getDate
+ */
+ public function testGetDateNow()
+ {
+ JFactory::$language = $this->getMockLanguage();
$date = JFactory::getDate('now');
sleep(2);
@@ -256,6 +269,19 @@ public function testGetDate()
$this->equalTo($date),
'Tests that the cache for the same time is working.'
);
+ }
+
+ /**
+ * Tests the JFactory::getDate method.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JFactory::getDate
+ */
+ public function testGetDateUTC1()
+ {
+ JFactory::$language = $this->getMockLanguage();
$tz = 'Etc/GMT+0';
$date = JFactory::getDate('2001-01-01 01:01:01', $tz);
@@ -265,6 +291,19 @@ public function testGetDate()
$this->equalTo('2001-01-01 01:01:01'),
'Tests that a date passed in with UTC timezone string comes back unchanged.'
);
+ }
+
+ /**
+ * Tests the JFactory::getDate method.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JFactory::getDate
+ */
+ public function testGetDateUTC2()
+ {
+ JFactory::$language = $this->getMockLanguage();
$tz = new DateTimeZone('Etc/GMT+0');
$date = JFactory::getDate('2001-01-01 01:01:01', $tz);
@@ -275,4 +314,21 @@ public function testGetDate()
'Tests that a date passed in with UTC timezone comes back unchanged.'
);
}
+
+ /**
+ * Tests the JFactory::getUser method.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JFactory::getUser
+ */
+ public function testGetUserInstance()
+ {
+ $this->assertInstanceOf(
+ 'JUser',
+ JFactory::getUser(),
+ 'Line: ' . __LINE__
+ );
+ }
}
View
28 tests/suites/unit/joomla/application/JApplicationBaseTest.php
@@ -92,7 +92,7 @@ public function testLoadDispatcher()
* @covers JApplicationBase::loadIdentity
* @covers JApplicationBase::getIdentity
*/
- public function testLoadGetIdentity()
+ public function testLoadGetIdentityCorrectClass()
{
$mock = $this->getMock('JUser', array(), array(), '', false);
$this->class->loadIdentity($mock);
@@ -103,12 +103,38 @@ public function testLoadGetIdentity()
$this->class,
'Tests that the identity object is the correct class.'
);
+ }
+ /**
+ * Tests the JApplicationBase::loadIdentity and JApplicationBase::getIdentity methods.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JApplicationBase::loadIdentity
+ * @covers JApplicationBase::getIdentity
+ */
+ public function testLoadGetIdentityGetJUser()
+ {
+ $mock = $this->getMock('JUser', array(), array(), '', false);
+ $this->class->loadIdentity($mock);
$this->assertInstanceOf(
'JUser',
$this->class->getIdentity()
);
+ }
+ /**
+ * Tests the JApplicationBase::loadIdentity and JApplicationBase::getIdentity methods.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @covers JApplicationBase::loadIdentity
+ * @covers JApplicationBase::getIdentity
+ */
+ public function testLoadGetIdentity99()
+ {
// Mock the session.
JFactory::$session = $this->getMockSession(array('get.user.id' => 99));
View
66 tests/suites/unit/joomla/session/JSessionTest.php
@@ -78,11 +78,11 @@ protected function tearDown()
'first_instance' => array(
'none',
array('expire' => 99),
- 'Line: ' . __LINE__ . ': ' . 'Should not be a different instance '
+ 'Line: ' . __LINE__ . ': ' . 'Should not be a different instance and options should not change'
),
'second_instance' => array(
'database',
- array('expire' => 15),
+ array(),
'Line: ' . __LINE__ . ': ' . 'Should not be a different instance '
)
);
@@ -103,14 +103,18 @@ public function testGetInstance($store, $options)
{
$oldSession = $this->object;
$newSession = JSession::getInstance($store, $options);
+ // The properties and values should be identical to each other.
$this->assertThat(
$oldSession,
$this->identicalTo($newSession)
);
+
+ // They should be the same object.
+ $this->assertSame($oldSession,$newSession);
}
/**
- * Test...
+ * Test getState
*
* @covers JSession::getState
*
@@ -118,11 +122,15 @@ public function testGetInstance($store, $options)
*/
public function testGetState()
{
- $this->assertEquals('active', $this->object->getState(), 'Session should be active');
+ $this->assertEquals(
+ TestReflection::getValue($this->object, '_state'),
+ $this->object->getState(),
+ 'Session state should be the same'
+ );
}
/**
- * Test...
+ * Test getExpire()
*
* @covers JSession::getExpire
*
@@ -130,11 +138,15 @@ public function testGetState()
*/
public function testGetExpire()
{
- $this->assertEquals(20, $this->object->getExpire(), 'Session expire should be 20');
+ $this->assertEquals(
+ TestReflection::getValue($this->object, '_expire'),
+ $this->object->getExpire(),
+ 'Session expire time should be the same'
+ );
}
/**
- * Test...
+ * Test getToken
*
* @covers JSession::getToken
*
@@ -154,7 +166,7 @@ public function testGetToken()
}
/**
- * Test...
+ * Test hasToken
*
* @covers JSession::hasToken
*
@@ -173,7 +185,7 @@ public function testHasToken()
}
/**
- * Test...
+ * Test getFormToken
*
* @covers JSession::getFormToken
*
@@ -189,44 +201,37 @@ public function testGetFormToken()
->with($this->equalTo('secret'))
->will($this->returnValue('abc'));
+ $this->object->set('secret','abc');
$expected = md5('abc' . $user->get('id', 0) . $this->object->getToken(false));
- $this->assertEquals($expected, $this->object->getFormToken(), 'Form token should be calculated as above');
+ $this->assertEquals($expected, $this->object->getFormToken(), 'Form token should be calculated as above.');
}
/**
- * Test...
+ * Test getName
*
- * @todo Implement testGetName().
- * @covers JSession::hasToken
+ * @covers JSession::getName
*
* @return void
*/
public function testGetName()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $this->assertEquals(session_name(), $this->object->getName(), 'Session names should match.');
}
/**
- * Test...
+ * Test getId
*
- * @todo Implement testGetId().
- * @covers JSession::hasToken
+ * @covers JSession::getId
*
* @return void
*/
public function testGetId()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $this->assertEquals(session_id(), $this->object->getId(), 'Session ids should match.');
}
/**
- * Test...
+ * Test getStores
*
* @covers JSession::getStores
*
@@ -253,18 +258,15 @@ public function testGetStores()
}
/**
- * Test...
- *
- * @todo Implement testIsNew().
+ * Test isNew
*
* @return void
*/
public function testIsNew()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $this->object->set('session.counter', 1);
+
+ $this->assertEquals(true, $this->object->isNew(), '$isNew should be true.');
}
/**
View
155 tests/suites/unit/joomla/user/JUserHelperTest.php
@@ -72,8 +72,17 @@ public function casesGetUserGroups()
array(
'code' => 'SOME_ERROR_CODE',
'msg' => 'JLIB_USER_ERROR_UNABLE_TO_LOAD_USER',
- 'info' => ''
- ),
+ 'info' => ''),
+ ),
+ 'publisher' => array(
+ 43,
+ array(5 => 5,),
+ array(),
+ ),
+ 'manager' => array(
+ 44,
+ array(2 => 2, 6 => 6),
+ array(),
),
);
}
@@ -119,15 +128,18 @@ public function casesGetUserId()
}
/**
- * TestingGetUserGroups().
+ * TestingGetUserId().
*
- * @param string $username User name
- * @param int $expected Expected user id
- * @param array $error Expected error info
+ * @param string $username User name
+ * @param integer $expected Expected user id
+ * @param array $error Expected error info
*
* @dataProvider casesGetUserId
* @covers JUserHelper::getUserId
+ *
* @return void
+ *
+ * @since 12.2
*/
public function testGetUserId($username, $expected, $error)
{
@@ -138,4 +150,135 @@ public function testGetUserId($username, $expected, $error)
);
}
+
+ /**
+ * Test cases for testAddUserToGroup
+ *
+ * @return array
+ */
+ public function casesAddUserToGroup()
+ {
+ return array(
+ 'publisher' => array(
+ 43,
+ 6,
+ true
+ ),
+ 'manager' => array(
+ 44,
+ 6,
+ true
+ ),
+ );
+ }
+ /**
+ * Testing addUserToGroup().
+ *
+ * @param string $userId User id
+ * @param integer $groupId Group to add user to
+ * @param boolean $expected Expected params
+ *
+ * @dataProvider casesAddUsertoGroup
+ * @covers JUserHelper::addUsertoGroup
+ * @return void
+ *
+ * @since 12.3
+ */
+ public function testAddUserToGroup($userId, $groupId, $expected)
+ {
+ $this->assertThat(
+ JUserHelper::addUserToGroup($userId, $groupId),
+ $this->equalTo($expected)
+ );
+ }
+
+ /**
+ * Testing addUserToGroup() with expected exception.
+ *
+ * @return void
+ *
+ * @since 12.3
+ * @expectedException RuntimeException
+ * @covers JUserHelper::addUsertoGroup
+ */
+ public function testAddUserToGroupException()
+ {
+ JUserHelper::addUserToGroup(44, 99);
+ }
+
+ /**
+ * Test cases for testRemoveUserFromGroup
+ *
+ * @return array
+ */
+ public function casesRemoveUserFromGroup()
+ {
+ return array(
+ 'publisher' => array(
+ 43,
+ 8,
+ true
+ ),
+ 'manager' => array(
+ 44,
+ 6,
+ true
+ ),
+ );
+ }
+
+ /**
+ * Testing removeUserFromGroup().
+ *
+ * @param string $userId User id
+ * @param integer $groupId Group to remove user from
+ * @param boolean $expected Expected params
+ *
+ * @dataProvider casesRemoveUserFromGroup
+ * @covers JUserHelper::removeUserFromGroup
+ * @return void
+ */
+ public function testRemoveUserFromGroup($userId, $groupId, $expected)
+ {
+ $this->assertThat(
+ JUserHelper::removeUserFromGroup($userId, $groupId),
+ $this->equalTo($expected)
+ );
+ }
+
+ /**
+ * Test cases for testActivateUser
+ *
+ * @return array
+ */
+ public function casesActivateUser()
+ {
+ return array(
+ 'Valid User' => array(
+ '30cc6de70fb18231196a28dd83363d57',
+ true),
+ 'Invalid User' => array(
+ '30cc6de70fb18231196a28dd83363d72',
+ false),
+ );
+ }
+ /**
+ * Testing activateUser().
+ *
+ * @param string $activation Activation string
+ * @param boolean $expected Expected params
+ *
+ * @dataProvider casesActivateUser
+ * @covers JUserHelper::activateUser
+ * @return void
+ *
+ * @since 12.3
+ */
+ public function testActivateUser($activation, $expected)
+ {
+ $this->assertThat(
+ JUserHelper::activateUser($activation),
+ $this->equalTo($expected)
+ );
+ }
}
View
2  tests/suites/unit/joomla/user/JUserTest.php
@@ -244,7 +244,7 @@ public function testAuthorise($userId, $action, $asset, $expected)
}
/**
- * Test...
+ * Test getAuthorisedCategories
*
* @covers JUser::getAuthorisedCategories
* @todo Implement testGetAuthorisedCategories().
View
40 tests/suites/unit/joomla/user/JUserTest.xml
@@ -3404,6 +3404,24 @@ separator=
<column>profile_value</column>
<column>ordering</column>
</table>
+ <row>
+ <value>42</value>
+ <value>profile.favoritebook</value>
+ <value>"War and Peace"</value>
+ <value>1</value>
+ </row>
+ <row>
+ <value>43</value>
+ <value>profile.favoritebook</value>
+ <value>"Alice in Wonderland"</value>
+ <value>1</value>
+ </row>
+ <row>
+ <value>43</value>
+ <value>profile.favoritemovie</value>
+ <value>"The Wizard of Oz"</value>
+ <value>2</value>
+ </row>
<table name="jos_user_usergroup_map">
<column>user_id</column>
<column>group_id</column>
@@ -3419,6 +3437,14 @@ separator=
<value>44</value>
<value>6</value>
</row>
+ <row>
+ <value>44</value>
+ <value>2</value>
+ </row>
+ <row>
+ <value>100</value>
+ <value>2</value>
+ </row>
</table>
<table name="jos_usergroups">
<column>id</column>
@@ -3566,6 +3592,20 @@ separator=
<value></value>
<value>{&quot;admin_language&quot;:&quot;&quot;,&quot;language&quot;:&quot;&quot;,&quot;editor&quot;:&quot;&quot;,&quot;helpsite&quot;:&quot;&quot;,&quot;timezone&quot;:&quot;&quot;}</value>
</row>
+ <row>
+ <value>100</value>
+ <value>Activate</value>
+ <value>activate</value>
+ <value>activate@example.com</value>
+ <value>b69eafa62e549e5fa875e127fadc3c83:VapLaQZx00iYDRwgMjgsfyIHgoe01DK8</value>
+ <value>deprecated</value>
+ <value>1</value>
+ <value>1</value>
+ <value>2010-02-13 00:34:42</value>
+ <value>0000-00-00 00:00:00</value>
+ <value>30cc6de70fb18231196a28dd83363d57</value>
+ <value>{&quot;admin_language&quot;:&quot;&quot;,&quot;language&quot;:&quot;&quot;,&quot;editor&quot;:&quot;&quot;,&quot;helpsite&quot;:&quot;&quot;,&quot;timezone&quot;:&quot;&quot;}</value>
+ </row>
</table>
<table name="jos_viewlevels">
<column>id</column>
Something went wrong with that request. Please try again.