2014-07-03 11:48:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2019-01-24 15:52:38 +00:00
|
|
|
* @copyright Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
|
|
|
|
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-07-03 11:48:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-02 09:57:24 +00:00
|
|
|
namespace Test\Activity;
|
2014-07-03 11:48:15 +00:00
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IRequest;
|
2017-10-24 13:26:53 +00:00
|
|
|
use OCP\IUser;
|
2016-11-10 14:33:00 +00:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2016-05-02 09:57:24 +00:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var \OC\Activity\Manager */
|
2014-07-03 11:48:15 +00:00
|
|
|
private $activityManager;
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
2015-03-25 16:46:19 +00:00
|
|
|
protected $request;
|
2016-11-10 14:33:00 +00:00
|
|
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
2015-03-25 16:46:19 +00:00
|
|
|
protected $session;
|
2016-11-10 14:33:00 +00:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2015-03-25 16:46:19 +00:00
|
|
|
protected $config;
|
2016-11-10 14:33:00 +00:00
|
|
|
/** @var IValidator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $validator;
|
2015-03-25 16:46:19 +00:00
|
|
|
|
2019-11-21 15:40:38 +00:00
|
|
|
protected function setUp(): void {
|
2014-11-10 21:59:50 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->session = $this->createMock(IUserSession::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->validator = $this->createMock(IValidator::class);
|
2015-03-25 16:46:19 +00:00
|
|
|
|
2016-05-02 09:57:24 +00:00
|
|
|
$this->activityManager = new \OC\Activity\Manager(
|
2015-03-25 16:46:19 +00:00
|
|
|
$this->request,
|
|
|
|
$this->session,
|
2016-11-10 14:33:00 +00:00
|
|
|
$this->config,
|
|
|
|
$this->validator
|
2015-03-25 16:46:19 +00:00
|
|
|
);
|
|
|
|
|
2019-01-24 15:52:38 +00:00
|
|
|
$this->assertSame([], self::invokePrivate($this->activityManager, 'getConsumers'));
|
2015-08-20 08:25:49 +00:00
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
$this->activityManager->registerConsumer(function() {
|
|
|
|
return new NoOpConsumer();
|
|
|
|
});
|
2015-08-20 08:25:49 +00:00
|
|
|
|
2019-01-24 15:52:38 +00:00
|
|
|
$this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
|
|
|
|
$this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
|
2014-07-03 11:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
public function testGetConsumers() {
|
2019-01-24 15:52:38 +00:00
|
|
|
$consumers = self::invokePrivate($this->activityManager, 'getConsumers');
|
2015-07-23 09:00:29 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($consumers);
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:27:18 +00:00
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
public function testGetConsumersInvalidConsumer() {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
$this->activityManager->registerConsumer(function() {
|
2016-05-02 09:57:24 +00:00
|
|
|
return new \stdClass();
|
2015-07-23 09:00:29 +00:00
|
|
|
});
|
|
|
|
|
2019-01-24 15:52:38 +00:00
|
|
|
self::invokePrivate($this->activityManager, 'getConsumers');
|
2014-07-03 11:48:15 +00:00
|
|
|
}
|
2015-03-25 16:46:19 +00:00
|
|
|
|
|
|
|
public function getUserFromTokenThrowInvalidTokenData() {
|
|
|
|
return [
|
|
|
|
[null, []],
|
|
|
|
['', []],
|
|
|
|
['12345678901234567890123456789', []],
|
|
|
|
['1234567890123456789012345678901', []],
|
|
|
|
['123456789012345678901234567890', []],
|
|
|
|
['123456789012345678901234567890', ['user1', 'user2']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUserFromTokenThrowInvalidTokenData
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @param array $users
|
|
|
|
*/
|
|
|
|
public function testGetUserFromTokenThrowInvalidToken($token, $users) {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
|
|
|
|
2015-04-01 10:13:49 +00:00
|
|
|
$this->mockRSSToken($token, $token, $users);
|
2015-06-03 10:03:02 +00:00
|
|
|
self::invokePrivate($this->activityManager, 'getUserFromToken');
|
2015-03-25 16:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getUserFromTokenData() {
|
|
|
|
return [
|
|
|
|
[null, '123456789012345678901234567890', 'user1'],
|
|
|
|
['user2', null, 'user2'],
|
|
|
|
['user2', '123456789012345678901234567890', 'user2'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUserFromTokenData
|
|
|
|
*
|
|
|
|
* @param string $userLoggedIn
|
|
|
|
* @param string $token
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGetUserFromToken($userLoggedIn, $token, $expected) {
|
|
|
|
if ($userLoggedIn !== null) {
|
|
|
|
$this->mockUserSession($userLoggedIn);
|
|
|
|
}
|
2015-04-01 10:13:49 +00:00
|
|
|
$this->mockRSSToken($token, '123456789012345678901234567890', ['user1']);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->activityManager->getCurrentUserId());
|
|
|
|
}
|
2015-03-25 16:46:19 +00:00
|
|
|
|
2015-04-01 10:13:49 +00:00
|
|
|
protected function mockRSSToken($requestToken, $userToken, $users) {
|
|
|
|
if ($requestToken !== null) {
|
2015-03-25 16:46:19 +00:00
|
|
|
$this->request->expects($this->any())
|
|
|
|
->method('getParam')
|
|
|
|
->with('token', '')
|
2015-04-01 10:13:49 +00:00
|
|
|
->willReturn($requestToken);
|
2015-03-25 16:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getUsersForUserValue')
|
2015-04-01 10:13:49 +00:00
|
|
|
->with('activity', 'rsstoken', $userToken)
|
|
|
|
->willReturn($users);
|
2015-03-25 16:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockUserSession($user) {
|
2017-10-24 13:26:53 +00:00
|
|
|
$mockUser = $this->getMockBuilder(IUser::class)
|
2015-03-25 16:46:19 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$mockUser->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$this->session->expects($this->any())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->session->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($mockUser);
|
|
|
|
}
|
2015-08-20 08:25:49 +00:00
|
|
|
|
2019-11-27 14:27:18 +00:00
|
|
|
|
2015-08-20 08:25:49 +00:00
|
|
|
public function testPublishExceptionNoApp() {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\BadMethodCallException::class);
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:27:18 +00:00
|
|
|
|
2015-08-20 08:25:49 +00:00
|
|
|
public function testPublishExceptionNoType() {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\BadMethodCallException::class);
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$event->setApp('test');
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:27:18 +00:00
|
|
|
|
2015-08-20 08:25:49 +00:00
|
|
|
public function testPublishExceptionNoAffectedUser() {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\BadMethodCallException::class);
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$event->setApp('test')
|
|
|
|
->setType('test_type');
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:27:18 +00:00
|
|
|
|
2015-08-20 08:25:49 +00:00
|
|
|
public function testPublishExceptionNoSubject() {
|
2019-11-27 14:27:18 +00:00
|
|
|
$this->expectException(\BadMethodCallException::class);
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$event->setApp('test')
|
|
|
|
->setType('test_type')
|
|
|
|
->setAffectedUser('test_affected');
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataPublish() {
|
|
|
|
return [
|
2016-11-10 14:33:00 +00:00
|
|
|
[null, ''],
|
|
|
|
['test_author', 'test_author'],
|
2015-08-20 08:25:49 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPublish
|
2016-11-10 14:33:00 +00:00
|
|
|
* @param string|null $author
|
|
|
|
* @param string $expected
|
2015-08-20 08:25:49 +00:00
|
|
|
*/
|
2016-11-10 14:33:00 +00:00
|
|
|
public function testPublish($author, $expected) {
|
2015-08-20 08:25:49 +00:00
|
|
|
if ($author !== null) {
|
2017-10-24 13:26:53 +00:00
|
|
|
$authorObject = $this->getMockBuilder(IUser::class)
|
2015-08-20 08:25:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$authorObject->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($author);
|
|
|
|
$this->session->expects($this->atLeastOnce())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($authorObject);
|
|
|
|
}
|
|
|
|
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$event->setApp('test')
|
|
|
|
->setType('test_type')
|
|
|
|
->setSubject('test_subject', [])
|
2016-11-10 14:33:00 +00:00
|
|
|
->setAffectedUser('test_affected')
|
|
|
|
->setObject('file', 123);
|
2015-08-20 08:25:49 +00:00
|
|
|
|
|
|
|
$consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$consumer->expects($this->once())
|
|
|
|
->method('receive')
|
|
|
|
->with($event)
|
2016-11-10 14:33:00 +00:00
|
|
|
->willReturnCallback(function(\OCP\Activity\IEvent $event) use ($expected) {
|
2015-08-20 08:25:49 +00:00
|
|
|
$this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly');
|
|
|
|
$this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly');
|
2016-11-10 14:33:00 +00:00
|
|
|
$this->assertSame($expected, $event->getAuthor(), 'Author name not set correctly');
|
2015-08-20 08:25:49 +00:00
|
|
|
});
|
|
|
|
$this->activityManager->registerConsumer(function () use ($consumer) {
|
|
|
|
return $consumer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPublishAllManually() {
|
2016-11-10 14:33:00 +00:00
|
|
|
$event = $this->activityManager->generateEvent();
|
2015-08-20 08:25:49 +00:00
|
|
|
$event->setApp('test_app')
|
|
|
|
->setType('test_type')
|
|
|
|
->setAffectedUser('test_affected')
|
|
|
|
->setAuthor('test_author')
|
|
|
|
->setTimestamp(1337)
|
|
|
|
->setSubject('test_subject', ['test_subject_param'])
|
|
|
|
->setMessage('test_message', ['test_message_param'])
|
|
|
|
->setObject('test_object_type', 42, 'test_object_name')
|
|
|
|
->setLink('test_link')
|
|
|
|
;
|
|
|
|
|
|
|
|
$consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$consumer->expects($this->once())
|
|
|
|
->method('receive')
|
|
|
|
->willReturnCallback(function(\OCP\Activity\IEvent $event) {
|
|
|
|
$this->assertSame('test_app', $event->getApp(), 'App not set correctly');
|
|
|
|
$this->assertSame('test_type', $event->getType(), 'Type not set correctly');
|
|
|
|
$this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly');
|
|
|
|
$this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly');
|
|
|
|
$this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly');
|
|
|
|
$this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly');
|
|
|
|
$this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly');
|
|
|
|
$this->assertSame('test_message', $event->getMessage(), 'Message not set correctly');
|
|
|
|
$this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly');
|
|
|
|
$this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly');
|
|
|
|
$this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly');
|
|
|
|
$this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly');
|
|
|
|
$this->assertSame('test_link', $event->getLink(), 'Link not set correctly');
|
|
|
|
});
|
|
|
|
$this->activityManager->registerConsumer(function () use ($consumer) {
|
|
|
|
return $consumer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
2014-07-03 11:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
class NoOpConsumer implements \OCP\Activity\IConsumer {
|
|
|
|
|
2015-08-20 08:25:49 +00:00
|
|
|
public function receive(\OCP\Activity\IEvent $event) {
|
|
|
|
|
2015-07-23 09:00:29 +00:00
|
|
|
}
|
|
|
|
}
|