User tests: Successful: Unsuccessful:
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)"
Labels |
Added:
?
|
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.
@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.
Labels |
Added:
?
|
Labels |
Added:
?
|
Labels |
Removed:
?
|
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.
Category | ⇒ | Unit Tests |
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.
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2015-07-19 21:01:45 |
Closed_By | ⇒ | mbabker |
Please add description and testing instructions to your PR.