Add tests for favorite action

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2016-12-04 12:54:34 +01:00
parent 7c1b288b57
commit 1a802d3383
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
1 changed files with 55 additions and 8 deletions

View File

@ -24,7 +24,9 @@
*/ */
namespace OCA\Files\Tests\Service; namespace OCA\Files\Tests\Service;
use OC\Tags;
use OCA\Files\Service\TagService; use OCA\Files\Service\TagService;
use OCP\Activity\IManager;
use OCP\IUserSession; use OCP\IUserSession;
/** /**
@ -41,13 +43,19 @@ class TagServiceTest extends \Test\TestCase {
*/ */
private $user; private $user;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
private $activityManager;
/** /**
* @var \OCP\Files\Folder * @var \OCP\Files\Folder
*/ */
private $root; private $root;
/** /**
* @var \OCA\Files\Service\TagService * @var \OCA\Files\Service\TagService|\PHPUnit_Framework_MockObject_MockObject
*/ */
private $tagService; private $tagService;
@ -59,6 +67,7 @@ class TagServiceTest extends \Test\TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->user = $this->getUniqueID('user'); $this->user = $this->getUniqueID('user');
$this->activityManager = $this->createMock(IManager::class);
\OC::$server->getUserManager()->createUser($this->user, 'test'); \OC::$server->getUserManager()->createUser($this->user, 'test');
\OC_User::setUserId($this->user); \OC_User::setUserId($this->user);
\OC_Util::setupFS($this->user); \OC_Util::setupFS($this->user);
@ -67,8 +76,8 @@ class TagServiceTest extends \Test\TestCase {
/** /**
* @var \OCP\IUserSession * @var \OCP\IUserSession
*/ */
$userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$userSession->expects($this->any()) $this->userSession->expects($this->any())
->method('getUser') ->method('getUser')
->withAnyParameters() ->withAnyParameters()
->will($this->returnValue($user)); ->will($this->returnValue($user));
@ -76,11 +85,24 @@ class TagServiceTest extends \Test\TestCase {
$this->root = \OC::$server->getUserFolder(); $this->root = \OC::$server->getUserFolder();
$this->tagger = \OC::$server->getTagManager()->load('files'); $this->tagger = \OC::$server->getTagManager()->load('files');
$this->tagService = new TagService( $this->tagService = $this->getTagService(['addActivity']);
$userSession, }
/**
* @param array $methods
* @return TagService|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getTagService(array $methods = []) {
return $this->getMockBuilder(TagService::class)
->setConstructorArgs([
$this->userSession,
$this->activityManager,
$this->tagger, $this->tagger,
$this->root $this->root,
); ])
->setMethods($methods)
->getMock();
} }
protected function tearDown() { protected function tearDown() {
@ -93,6 +115,9 @@ class TagServiceTest extends \Test\TestCase {
$tag1 = 'tag1'; $tag1 = 'tag1';
$tag2 = 'tag2'; $tag2 = 'tag2';
$this->tagService->expects($this->never())
->method('addActivity');
$subdir = $this->root->newFolder('subdir'); $subdir = $this->root->newFolder('subdir');
$testFile = $subdir->newFile('test.txt'); $testFile = $subdir->newFile('test.txt');
$testFile->putContent('test contents'); $testFile->putContent('test contents');
@ -124,6 +149,28 @@ class TagServiceTest extends \Test\TestCase {
} }
$this->assertTrue($caught); $this->assertTrue($caught);
$subdir->delete();
}
public function testFavoriteActivity() {
$subdir = $this->root->newFolder('subdir');
$file = $subdir->newFile('test.txt');
$this->tagService->expects($this->exactly(2))
->method('addActivity')
->withConsecutive(
[true, $file->getId(), 'subdir/test.txt'],
[false, $file->getId(), 'subdir/test.txt']
);
// set tags
$this->tagService->updateFileTags('subdir/test.txt', [Tags::TAG_FAVORITE]);
// remove tag
$this->tagService->updateFileTags('subdir/test.txt', []);
$subdir->delete(); $subdir->delete();
} }
} }