? ? Success

User tests: Successful: Unsuccessful:

avatar gauravjain028
gauravjain028
15 Oct 2014

public static function getUser($id = null)
{
$instance = self::getSession()->get('user');
if (is_null($id))
{
if (!($instance instanceof JUser))
{
$instance = JUser::getInstance();
}
}
elseif ($instance->id != $id)
{
$instance = JUser::getInstance($id);
}
return $instance;
}

When I write a unit test case for a function, which has the code JFactory::getUser(); inside, then I need to set user in session. Otherwise session does not contain anything, and return NULL on first line of function. If $instance is null then it gets an error for "Accessing undefined property on on non-object" ($instance->id ).

There should be a check before using "elseif ($instance->id != $id)"

avatar rbsl-gaurav rbsl-gaurav - open - 15 Oct 2014
avatar jissues-bot jissues-bot - change - 15 Oct 2014
Labels Added: ?
avatar Bakual
Bakual - comment - 15 Oct 2014

Please add description and testing instructions to your PR.

avatar rbsl-gaurav
rbsl-gaurav - comment - 15 Oct 2014

@Bakual : Done

avatar wilsonge
wilsonge - comment - 15 Oct 2014

I understand why you're doing this having constantly gone through the unit tests in the CMS. But I think you're just making workarounds for not adding the user object into your mock.

avatar rbsl-gaurav
rbsl-gaurav - comment - 16 Oct 2014

@wilsonge : Yes, I got this issue while writing the testcase. Yes, I can mock the object, but I am in the situation where mocking the object will not work, I need to set the data in session.

In the above function $instance is checked against JUser, if $id is null, then a new instance is created. Same thing should be done, if you need instance of any particular user.
If there is possibility that $instance can be null, then it should be checked before access its property.

That's why I thought it should be fixed in Joomla as well.

avatar nicksavov nicksavov - change - 16 Oct 2014
The description was changed
Labels Added: ?
avatar nicksavov nicksavov - change - 16 Oct 2014
Labels Added: ?
avatar mbabker
mbabker - assign - 16 Oct 2014
Assigned to mbabker
avatar brianteeman brianteeman - change - 1 Jan 2015
Labels Removed: ?
avatar mbabker
mbabker - comment - 24 Feb 2015

I'm hitting PHP notices working with several CLI scripts that are fetching a JUser object via JFactory. Command line doesn't have the same concept of sessions as web requests, so $instance as set in JFactory::getUser() always results in a null object. Ironically, part of this is addressed with 3.4 as a is_string($id) check was added. However, it will continue to throw a notice if you are working on the command line and pass an integer into the function. This command line script throws a notice with the current staging branch.

avatar brianteeman brianteeman - change - 22 May 2015
Category Unit Tests
avatar mbabker
mbabker - comment - 27 Jun 2015

Coming back around to this. My previous test script still fails with a Notice: Trying to get property of non-object in libraries/joomla/factory.php on line 246 message. The proposed fix does remove the message. Though there is a change in behavior, pre-patch an empty JUser object is dumped and post-patch the dump is null (presumably to be expected).

This PR needs to be updated with current staging to be merged.

avatar mbabker
mbabker - comment - 19 Jul 2015

See #7473, that PR is rebased to current staging.

avatar mbabker mbabker - change - 19 Jul 2015
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2015-07-19 21:01:45
Closed_By mbabker
avatar mbabker mbabker - close - 19 Jul 2015

Add a Comment

Login with GitHub to post a comment