Get the user folder of the correct user

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-03-03 13:12:24 +01:00 committed by Roeland Jago Douma
parent 15fa306ae8
commit d4200a08cc
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 68 additions and 51 deletions

View File

@ -23,7 +23,7 @@ namespace OCA\Comments\Notification;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
@ -35,8 +35,8 @@ class Notifier implements INotifier {
/** @var IFactory */
protected $l10nFactory;
/** @var Folder */
protected $userFolder;
/** @var IRootFolder */
protected $rootFolder;
/** @var ICommentsManager */
protected $commentsManager;
@ -49,13 +49,13 @@ class Notifier implements INotifier {
public function __construct(
IFactory $l10nFactory,
Folder $userFolder,
IRootFolder $rootFolder,
ICommentsManager $commentsManager,
IURLGenerator $url,
IUserManager $userManager
) {
$this->l10nFactory = $l10nFactory;
$this->userFolder = $userFolder;
$this->rootFolder = $rootFolder;
$this->commentsManager = $commentsManager;
$this->url = $url;
$this->userManager = $userManager;
@ -93,7 +93,8 @@ class Notifier implements INotifier {
if($parameters[0] !== 'files') {
throw new \InvalidArgumentException('Unsupported comment object');
}
$nodes = $this->userFolder->getById($parameters[1]);
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
$nodes = $userFolder->getById($parameters[1]);
if(empty($nodes)) {
throw new \InvalidArgumentException('Cannot resolve file id to Node instance');
}

View File

@ -22,6 +22,7 @@
namespace OCA\Comments\Tests\Unit\AppInfo;
use OCA\Comments\AppInfo\Application;
use OCA\Comments\Notification\Notifier;
use Test\TestCase;
/**
@ -56,7 +57,8 @@ class ApplicationTest extends TestCase {
'OCA\Comments\Activity\Listener',
'OCA\Comments\Activity\Provider',
'OCA\Comments\Activity\Setting',
'OCA\Comments\Notification\Listener'
'OCA\Comments\Notification\Listener',
Notifier::class,
];
foreach($services as $service) {

View File

@ -28,6 +28,7 @@ use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IL10N;
use OCP\IURLGenerator;
@ -41,41 +42,33 @@ class NotifierTest extends TestCase {
/** @var Notifier */
protected $notifier;
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $l10nFactory;
/** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
protected $l;
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
protected $folder;
/** @var ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */
protected $commentsManager;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $url;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
protected $userManager;
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject */
protected $notification;
/** @var IComment|\PHPUnit_Framework_MockObject_MockObject */
protected $comment;
/** @var string */
protected $lc = 'tlh_KX';
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject */
protected $notification;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
protected $l;
/** @var IComment|\PHPUnit_Framework_MockObject_MockObject */
protected $comment;
protected function setUp() {
parent::setUp();
$this->l10nFactory = $this->getMockBuilder('OCP\L10N\IFactory')->getMock();
$this->folder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->commentsManager = $this->getMockBuilder('OCP\Comments\ICommentsManager')->getMock();
$this->l10nFactory = $this->createMock(IFactory::class);
$this->folder = $this->createMock(IRootFolder::class);
$this->commentsManager = $this->createMock(ICommentsManager::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->userManager = $this->getMockBuilder('OCP\IUserManager')->getMock();
$this->userManager = $this->createMock(IUserManager::class);
$this->notifier = new Notifier(
$this->l10nFactory,
@ -92,8 +85,8 @@ class NotifierTest extends TestCase {
return vsprintf($text, $parameters);
}));
$this->notification = $this->getMockBuilder('OCP\Notification\INotification')->getMock();
$this->comment = $this->getMockBuilder('OCP\Comments\IComment')->getMock();
$this->notification = $this->createMock(INotification::class);
$this->comment = $this->createMock(IComment::class);
}
public function testPrepareSuccess() {
@ -102,24 +95,31 @@ class NotifierTest extends TestCase {
$message = 'Huraga mentioned you in a comment on “Gre\'thor.odp”';
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($displayName);
/** @var Node|\PHPUnit_Framework_MockObject_MockObject */
$node = $this->getMockBuilder('OCP\Files\Node')->getMock();
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
$node = $this->createMock(Node::class);
$node
->expects($this->atLeastOnce())
->method('getName')
->willReturn($fileName);
$this->folder
->expects($this->once())
$userFolder = $this->createMock(Folder::class);
$this->folder->expects($this->once())
->method('getUserFolder')
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->with('678')
->willReturn([$node]);
$this->notification->expects($this->once())
->method('getUser')
->willReturn('you');
$this->notification
->expects($this->once())
->method('getApp')
@ -172,7 +172,7 @@ class NotifierTest extends TestCase {
->willReturn('users');
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willReturn($this->comment);
@ -189,19 +189,26 @@ class NotifierTest extends TestCase {
$fileName = 'Gre\'thor.odp';
$message = 'A (now) deleted user mentioned you in a comment on “Gre\'thor.odp”';
/** @var Node|\PHPUnit_Framework_MockObject_MockObject */
$node = $this->getMockBuilder('OCP\Files\Node')->getMock();
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
$node = $this->createMock(Node::class);
$node
->expects($this->atLeastOnce())
->method('getName')
->willReturn($fileName);
$this->folder
->expects($this->once())
$userFolder = $this->createMock(Folder::class);
$this->folder->expects($this->once())
->method('getUserFolder')
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->with('678')
->willReturn([$node]);
$this->notification->expects($this->once())
->method('getUser')
->willReturn('you');
$this->notification
->expects($this->once())
->method('getApp')
@ -254,7 +261,7 @@ class NotifierTest extends TestCase {
->willReturn(ICommentsManager::DELETED_USER);
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willReturn($this->comment);
@ -292,7 +299,7 @@ class NotifierTest extends TestCase {
->method('get');
$this->commentsManager
->expects(($this->never()))
->expects($this->never())
->method('get');
$this->userManager
@ -329,7 +336,7 @@ class NotifierTest extends TestCase {
->method('get');
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willThrowException(new NotFoundException());
@ -347,7 +354,7 @@ class NotifierTest extends TestCase {
$displayName = 'Huraga';
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($displayName);
@ -390,7 +397,7 @@ class NotifierTest extends TestCase {
->willReturn('users');
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willReturn($this->comment);
@ -410,7 +417,7 @@ class NotifierTest extends TestCase {
$displayName = 'Huraga';
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($displayName);
@ -454,7 +461,7 @@ class NotifierTest extends TestCase {
->willReturn('users');
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willReturn($this->comment);
@ -474,17 +481,24 @@ class NotifierTest extends TestCase {
$displayName = 'Huraga';
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($displayName);
$this->folder
->expects($this->once())
$userFolder = $this->createMock(Folder::class);
$this->folder->expects($this->once())
->method('getUserFolder')
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->with('678')
->willReturn([]);
$this->notification->expects($this->once())
->method('getUser')
->willReturn('you');
$this->notification
->expects($this->once())
->method('getApp')
@ -520,7 +534,7 @@ class NotifierTest extends TestCase {
->willReturn('users');
$this->commentsManager
->expects(($this->once()))
->expects($this->once())
->method('get')
->willReturn($this->comment);