Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/private/Accounts/AccountManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016, Björn Schießle
Expand Down Expand Up @@ -33,9 +34,12 @@
use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use function json_decode;
use function json_last_error;

/**
* Class AccountManager
Expand All @@ -59,6 +63,9 @@ class AccountManager implements IAccountManager {
/** @var IJobList */
private $jobList;

/** @var ILogger */
private $logger;

/**
* AccountManager constructor.
*
Expand All @@ -68,10 +75,12 @@ class AccountManager implements IAccountManager {
*/
public function __construct(IDBConnection $connection,
EventDispatcherInterface $eventDispatcher,
IJobList $jobList) {
IJobList $jobList,
ILogger $logger) {
$this->connection = $connection;
$this->eventDispatcher = $eventDispatcher;
$this->jobList = $jobList;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -137,6 +146,11 @@ public function getUser(IUser $user) {
}

$userDataArray = json_decode($result[0]['data'], true);
$jsonError = json_last_error();
if ($userDataArray === null || $jsonError !== JSON_ERROR_NONE) {
$this->logger->critical("User data of $uid contained invalid JSON (error $jsonError), hence falling back to a default user record");
Copy link
Contributor Author

@kesselb kesselb Dec 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_last_error_msg() would log a readable error message. But the numeric code is also fine for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about that!

For the sake of finally getting this in for 18 I'll leave it as is :)

return $this->buildDefaultUserRecord($user);
}

$userDataArray = $this->addMissingDefaultValues($userDataArray);

Expand Down
8 changes: 2 additions & 6 deletions lib/private/Accounts/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ public function changeUserHook($params) {
* @return AccountManager
*/
protected function getAccountManager() {
if (is_null($this->accountManager)) {
$this->accountManager = new AccountManager(
\OC::$server->getDatabaseConnection(),
\OC::$server->getEventDispatcher(),
\OC::$server->getJobList()
);
if ($this->accountManager === null) {
$this->accountManager = \OC::$server->query(AccountManager::class);
}
return $this->accountManager;
}
Expand Down
19 changes: 12 additions & 7 deletions tests/lib/Accounts/AccountsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OC\Accounts\AccountManager;
use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
Expand All @@ -42,21 +44,24 @@ class AccountsManagerTest extends TestCase {
/** @var \OCP\IDBConnection */
private $connection;

/** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */
/** @var EventDispatcherInterface|MockObject */
private $eventDispatcher;

/** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */
/** @var IJobList|MockObject */
private $jobList;

/** @var string accounts table name */
private $table = 'accounts';

/** @var ILogger|MockObject */
private $logger;

protected function setUp(): void {
parent::setUp();
$this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
->disableOriginalConstructor()->getMock();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->connection = \OC::$server->getDatabaseConnection();
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->jobList = $this->createMock(IJobList::class);
$this->logger = $this->createMock(ILogger::class);
}

protected function tearDown(): void {
Expand All @@ -69,11 +74,11 @@ protected function tearDown(): void {
* get a instance of the accountManager
*
* @param array $mockedMethods list of methods which should be mocked
* @return \PHPUnit_Framework_MockObject_MockObject | AccountManager
* @return MockObject | AccountManager
*/
public function getInstance($mockedMethods = null) {
return $this->getMockBuilder(AccountManager::class)
->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList])
->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList, $this->logger])
->setMethods($mockedMethods)
->getMock();

Expand Down