Fix tests

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2019-12-19 20:54:15 +01:00
parent 6fa7295b42
commit e5edd96b74
5 changed files with 247 additions and 230 deletions

View File

@@ -84,7 +84,7 @@ test-unit:
ifeq (, $(shell which phpunit 2> /dev/null))
@echo "No phpunit command available, downloading a copy from the web"
mkdir -p $(build_tools_directory)
curl -sSL https://phar.phpunit.de/phpunit-5.7.phar -o $(build_tools_directory)/phpunit.phar
curl -sSL https://phar.phpunit.de/phpunit-8.2.phar -o $(build_tools_directory)/phpunit.phar
php $(build_tools_directory)/phpunit.phar -c tests/phpunit.xml --coverage-clover build/php-unit.coverage.xml
php $(build_tools_directory)/phpunit.phar -c tests/phpunit.integration.xml --coverage-clover build/php-integration.coverage.xml
else

View File

@@ -594,7 +594,7 @@ class CardService {
$card = $this->cardMapper->find($cardId);
$boardId = $this->cardMapper->findBoardId($cardId);
$boardUsers = array_keys($this->permissionService->findUsers($boardId));
$boardUsers = array_keys($this->permissionService->findUsers($boardId, true));
if (!in_array($userId, $boardUsers)) {
throw new BadRequestException('The user is not part of the board');
}

View File

@@ -221,9 +221,9 @@ class PermissionService {
* @param $boardId
* @return array
*/
public function findUsers($boardId) {
public function findUsers($boardId, $refresh = false) {
// cache users of a board so we don't query them for every cards
if (array_key_exists((string) $boardId, $this->users)) {
if (array_key_exists((string) $boardId, $this->users) && !$refresh) {
return $this->users[(string) $boardId];
}
try {

View File

@@ -102,34 +102,25 @@ class AssignedUsersMapperTest extends \Test\TestCase {
$this->stacks = $stacks;
}
/**
* @covers ::__construct
*/
public function testConstructor() {
//$this->assertAttributeInstanceOf(IDBConnection::class, 'db', $this->assignedUsersMapper);
//$this->assertAttributeEquals(AssignedUsers::class, 'entityClass', $this->assignedUsersMapper);
//$this->assertAttributeEquals('*PREFIX*deck_assigned_users', 'tableName', $this->assignedUsersMapper);
}
/**
* @covers ::find
*/
public function testFind() {
$uids = [];
$this->cardService->assignUser($this->cards[0]->getId(), self::TEST_USER1);
$this->cardService->assignUser($this->cards[0]->getId(), self::TEST_USER4);
$this->cardService->assignUser($this->cards[0]->getId(), self::TEST_USER2);
$assignedUsers = $this->assignedUsersMapper->find($this->cards[0]->getId());
foreach ($assignedUsers as $user) {
$uids[$user->getParticipant()] = $user;
}
$this->assertArrayHasKey(self::TEST_USER1, $uids);
$this->assertArrayNotHasKey(self::TEST_USER2, $uids);
$this->assertArrayHasKey(self::TEST_USER2, $uids);
$this->assertArrayNotHasKey(self::TEST_USER3, $uids);
$this->assertArrayHasKey(self::TEST_USER4, $uids);
$this->assertArrayNotHasKey(self::TEST_USER4, $uids);
$this->cardService->unassignUser($this->cards[0]->getId(), self::TEST_USER1);
$this->cardService->unassignUser($this->cards[0]->getId(), self::TEST_USER4);
$this->cardService->unassignUser($this->cards[0]->getId(), self::TEST_USER2);
}
/**

View File

@@ -25,6 +25,7 @@ namespace OCA\Deck\Service;
use OCA\Deck\Activity\ActivityManager;
use OCA\Deck\BadRequestException;
use OCA\Deck\Db\AssignedUsers;
use OCA\Deck\Db\AssignedUsersMapper;
use OCA\Deck\Db\Card;
@@ -46,22 +47,22 @@ use Test\TestCase;
class CardServiceTest extends TestCase {
/** @var CardService|MockObject */
private $cardService;
/** @var CardMapper|MockObject */
private $cardMapper;
/** @var StackMapper|MockObject */
private $stackMapper;
/** @var PermissionService|MockObject */
private $permissionService;
/** @var NotificationHelper */
private $notificationHelper;
/** @var AssignedUsersMapper|MockObject */
private $assignedUsersMapper;
/** @var BoardService|MockObject */
private $boardService;
/** @var LabelMapper|MockObject */
private $labelMapper;
/** @var CardService|MockObject */
private $cardService;
/** @var CardMapper|MockObject */
private $cardMapper;
/** @var StackMapper|MockObject */
private $stackMapper;
/** @var PermissionService|MockObject */
private $permissionService;
/** @var NotificationHelper */
private $notificationHelper;
/** @var AssignedUsersMapper|MockObject */
private $assignedUsersMapper;
/** @var BoardService|MockObject */
private $boardService;
/** @var LabelMapper|MockObject */
private $labelMapper;
private $boardMapper;
/** @var AttachmentService|MockObject */
private $attachmentService;
@@ -77,15 +78,15 @@ class CardServiceTest extends TestCase {
private $changeHelper;
public function setUp(): void {
parent::setUp();
$this->cardMapper = $this->createMock(CardMapper::class);
$this->stackMapper = $this->createMock(StackMapper::class);
$this->boardMapper = $this->createMock(BoardMapper::class);
$this->labelMapper = $this->createMock(LabelMapper::class);
$this->permissionService = $this->createMock(PermissionService::class);
$this->boardService = $this->createMock(BoardService::class);
$this->notificationHelper = $this->createMock(NotificationHelper::class);
$this->assignedUsersMapper = $this->createMock(AssignedUsersMapper::class);
parent::setUp();
$this->cardMapper = $this->createMock(CardMapper::class);
$this->stackMapper = $this->createMock(StackMapper::class);
$this->boardMapper = $this->createMock(BoardMapper::class);
$this->labelMapper = $this->createMock(LabelMapper::class);
$this->permissionService = $this->createMock(PermissionService::class);
$this->boardService = $this->createMock(BoardService::class);
$this->notificationHelper = $this->createMock(NotificationHelper::class);
$this->assignedUsersMapper = $this->createMock(AssignedUsersMapper::class);
$this->attachmentService = $this->createMock(AttachmentService::class);
$this->activityManager = $this->createMock(ActivityManager::class);
$this->commentsManager = $this->createMock(ICommentsManager::class);
@@ -108,10 +109,10 @@ class CardServiceTest extends TestCase {
$this->changeHelper,
$this->eventDispatcher,
'user1'
);
}
);
}
public function mockActivity($type, $object, $subject) {
public function mockActivity($type, $object, $subject) {
// ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE
$event = $this->createMock(IEvent::class);
$this->activityManager->expects($this->once())
@@ -123,214 +124,244 @@ class CardServiceTest extends TestCase {
->with($event);
}
public function testFind() {
public function testFind() {
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->willReturn($user);
$card = new Card();
$card->setId(1337);
$this->cardMapper->expects($this->any())
->method('find')
->with(123)
->willReturn($card);
$this->assignedUsersMapper->expects($this->any())
$card = new Card();
$card->setId(1337);
$this->cardMapper->expects($this->any())
->method('find')
->with(123)
->willReturn($card);
$this->assignedUsersMapper->expects($this->any())
->method('find')
->with(1337)
->willReturn(['user1', 'user2']);
$cardExpected = new Card();
$cardExpected->setId(1337);
$cardExpected->setAssignedUsers(['user1', 'user2']);
$this->assertEquals($cardExpected, $this->cardService->find(123));
}
$this->assertEquals($cardExpected, $this->cardService->find(123));
}
public function testCreate() {
$card = new Card();
$card->setTitle('Card title');
$card->setOwner('admin');
$card->setStackId(123);
$card->setOrder(999);
$card->setType('text');
$this->cardMapper->expects($this->once())
->method('insert')
->willReturn($card);
$b = $this->cardService->create('Card title', 123, 'text', 999, 'admin');
public function testCreate() {
$card = new Card();
$card->setTitle('Card title');
$card->setOwner('admin');
$card->setStackId(123);
$card->setOrder(999);
$card->setType('text');
$this->cardMapper->expects($this->once())
->method('insert')
->willReturn($card);
$b = $this->cardService->create('Card title', 123, 'text', 999, 'admin');
$this->assertEquals($b->getTitle(), 'Card title');
$this->assertEquals($b->getOwner(), 'admin');
$this->assertEquals($b->getType(), 'text');
$this->assertEquals($b->getOrder(), 999);
$this->assertEquals($b->getStackId(), 123);
}
$this->assertEquals($b->getTitle(), 'Card title');
$this->assertEquals($b->getOwner(), 'admin');
$this->assertEquals($b->getType(), 'text');
$this->assertEquals($b->getOrder(), 999);
$this->assertEquals($b->getStackId(), 123);
}
public function testDelete() {
$cardToBeDeleted = new Card();
$this->cardMapper->expects($this->once())
->method('find')
->willReturn($cardToBeDeleted);
$this->cardMapper->expects($this->once())
->method('update')
->willReturn($cardToBeDeleted);
$this->cardService->delete(123);
$this->assertTrue($cardToBeDeleted->getDeletedAt() <= time(), 'deletedAt is in the past');
}
public function testDelete() {
$cardToBeDeleted = new Card();
$this->cardMapper->expects($this->once())
->method('find')
->willReturn($cardToBeDeleted);
$this->cardMapper->expects($this->once())
->method('update')
->willReturn($cardToBeDeleted);
$this->cardService->delete(123);
$this->assertTrue($cardToBeDeleted->getDeletedAt() <= time(), 'deletedAt is in the past');
}
public function testUpdate() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) { return $c; });
$actual = $this->cardService->update(123, 'newtitle', 234, 'text', 999, 'foo', 'admin', '2017-01-01 00:00:00', null);
$this->assertEquals('newtitle', $actual->getTitle());
$this->assertEquals(234, $actual->getStackId());
$this->assertEquals('text', $actual->getType());
$this->assertEquals(999, $actual->getOrder());
$this->assertEquals('foo', $actual->getDescription());
$this->assertEquals('2017-01-01T00:00:00+00:00', $actual->getDuedate());
}
public function testUpdate() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) { return $c; });
$actual = $this->cardService->update(123, 'newtitle', 234, 'text', 999, 'foo', 'admin', '2017-01-01 00:00:00', null);
$this->assertEquals('newtitle', $actual->getTitle());
$this->assertEquals(234, $actual->getStackId());
$this->assertEquals('text', $actual->getType());
$this->assertEquals(999, $actual->getOrder());
$this->assertEquals('foo', $actual->getDescription());
$this->assertEquals('2017-01-01T00:00:00+00:00', $actual->getDuedate());
}
public function testUpdateArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('update');
public function testUpdateArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('update');
$this->expectException(StatusException::class);
$this->cardService->update(123, 'newtitle', 234, 'text', 999, 'foo', 'admin', '2017-01-01 00:00:00', null, true);
}
$this->cardService->update(123, 'newtitle', 234, 'text', 999, 'foo', 'admin', '2017-01-01 00:00:00', null, true);
}
public function testRename() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) { return $c; });
$actual = $this->cardService->rename(123, 'newtitle');
$this->assertEquals('newtitle', $actual->getTitle());
}
public function testRename() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) { return $c; });
$actual = $this->cardService->rename(123, 'newtitle');
$this->assertEquals('newtitle', $actual->getTitle());
}
public function testRenameArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('update');
$this->expectException(StatusException::class);
$this->cardService->rename(123, 'newtitle');
}
public function dataReorder() {
return [
[0, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
[0, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]],
[1, 3, [0, 2, 3, 1, 4, 5, 6, 7, 8, 9]]
];
}
/** @dataProvider dataReorder */
public function testReorder($cardId, $newPosition, $order) {
$cards = $this->getCards();
$cardsTmp = [];
$this->cardMapper->expects($this->at(0))->method('findAll')->willReturn($cards);
$result = $this->cardService->reorder($cardId, 123, $newPosition);
foreach ($result as $card) {
$actual[$card->getOrder()] = $card->getId();
}
$this->assertEquals($order, $actual);
}
private function getCards() {
$cards = [];
for($i=0; $i<10; $i++) {
$cards[$i] = new Card();
$cards[$i]->setTitle($i);
$cards[$i]->setOrder($i);
$cards[$i]->setId($i);
}
return $cards;
}
public function testReorderArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('findAll')->willReturn([$card]);
$this->cardMapper->expects($this->never())->method('update')->willReturnCallback(function($c) { return $c; });
$this->expectException(StatusException::class);
$actual = $this->cardService->reorder(123, 234, 1);
}
public function testArchive() {
$card = new Card();
$this->assertFalse($card->getArchived());
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) {
return $c;
});
$this->assertTrue($this->cardService->archive(123)->getArchived());
}
public function testUnarchive() {
$card = new Card();
$card->setArchived(true);
$this->assertTrue($card->getArchived());
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) {
return $c;
});
$this->assertFalse($this->cardService->unarchive(123)->getArchived());
}
public function testAssignLabel() {
$card = new Card();
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('assignLabel');
$this->cardService->assignLabel(123, 999);
}
public function testAssignLabelArchived() {
$card = new Card();
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('assignLabel');
$this->expectException(StatusException::class);
$this->cardService->assignLabel(123, 999);
}
public function testRemoveLabel() {
$card = new Card();
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('removeLabel');
$this->cardService->removeLabel(123, 999);
}
public function testRemoveLabelArchived() {
$card = new Card();
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('removeLabel');
public function testRenameArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('update');
$this->expectException(StatusException::class);
$this->cardService->removeLabel(123, 999);
}
$this->cardService->rename(123, 'newtitle');
}
public function testAssignUser() {
$assignments = [];
$this->assignedUsersMapper->expects($this->once())
public function dataReorder() {
return [
[0, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
[0, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]],
[1, 3, [0, 2, 3, 1, 4, 5, 6, 7, 8, 9]]
];
}
/** @dataProvider dataReorder */
public function testReorder($cardId, $newPosition, $order) {
$cards = $this->getCards();
$cardsTmp = [];
$this->cardMapper->expects($this->at(0))->method('findAll')->willReturn($cards);
$result = $this->cardService->reorder($cardId, 123, $newPosition);
foreach ($result as $card) {
$actual[$card->getOrder()] = $card->getId();
}
$this->assertEquals($order, $actual);
}
private function getCards() {
$cards = [];
for($i=0; $i<10; $i++) {
$cards[$i] = new Card();
$cards[$i]->setTitle($i);
$cards[$i]->setOrder($i);
$cards[$i]->setId($i);
}
return $cards;
}
public function testReorderArchived() {
$card = new Card();
$card->setTitle('title');
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('findAll')->willReturn([$card]);
$this->cardMapper->expects($this->never())->method('update')->willReturnCallback(function($c) { return $c; });
$this->expectException(StatusException::class);
$actual = $this->cardService->reorder(123, 234, 1);
}
public function testArchive() {
$card = new Card();
$this->assertFalse($card->getArchived());
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) {
return $c;
});
$this->assertTrue($this->cardService->archive(123)->getArchived());
}
public function testUnarchive() {
$card = new Card();
$card->setArchived(true);
$this->assertTrue($card->getArchived());
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function($c) {
return $c;
});
$this->assertFalse($this->cardService->unarchive(123)->getArchived());
}
public function testAssignLabel() {
$card = new Card();
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('assignLabel');
$this->cardService->assignLabel(123, 999);
}
public function testAssignLabelArchived() {
$card = new Card();
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('assignLabel');
$this->expectException(StatusException::class);
$this->cardService->assignLabel(123, 999);
}
public function testRemoveLabel() {
$card = new Card();
$card->setArchived(false);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->once())->method('removeLabel');
$this->cardService->removeLabel(123, 999);
}
public function testRemoveLabelArchived() {
$card = new Card();
$card->setArchived(true);
$this->cardMapper->expects($this->once())->method('find')->willReturn($card);
$this->cardMapper->expects($this->never())->method('removeLabel');
$this->expectException(StatusException::class);
$this->cardService->removeLabel(123, 999);
}
public function testAssignUser() {
$assignments = [];
$this->assignedUsersMapper->expects($this->once())
->method('find')
->with(123)
->willReturn($assignments);
$assignment = new AssignedUsers();
$assignment->setCardId(123);
$assignment->setParticipant('admin');
$this->assignedUsersMapper->expects($this->once())
$assignment = new AssignedUsers();
$assignment->setCardId(123);
$assignment->setParticipant('admin');
$this->cardMapper->expects($this->once())
->method('findBoardId')
->willReturn(1);
$this->permissionService->expects($this->once())
->method('findUsers')
->with(1)
->willReturn(['admin' => 'admin', 'user1' => 'user1']);
$this->assignedUsersMapper->expects($this->once())
->method('insert')
->with($assignment)
->willReturn($assignment);
$actual = $this->cardService->assignUser(123, 'admin');
$this->assertEquals($assignment, $actual);
$actual = $this->cardService->assignUser(123, 'admin');
$this->assertEquals($assignment, $actual);
}
public function testAssignUserNoParticipant() {
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('The user is not part of the board');
$assignments = [];
$this->assignedUsersMapper->expects($this->once())
->method('find')
->with(123)
->willReturn($assignments);
$assignment = new AssignedUsers();
$assignment->setCardId(123);
$assignment->setParticipant('admin');
$this->cardMapper->expects($this->once())
->method('findBoardId')
->willReturn(1);
$this->permissionService->expects($this->once())
->method('findUsers')
->with(1)
->willReturn(['user2' => 'user2', 'user1' => 'user1']);
$actual = $this->cardService->assignUser(123, 'admin');
}
public function testAssignUserExisting() {
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('The user is already assigned to the card');
$assignment = new AssignedUsers();
$assignment->setCardId(123);
$assignment->setParticipant('admin');
@@ -364,13 +395,8 @@ class CardServiceTest extends TestCase {
$this->assertEquals($assignment, $actual);
}
/**
* @expectException \OCA\Deck\NotFoundException
*
*
*
*/
public function testUnassignUserNotExisting() {
$this->expectException(NotFoundException::class);
$assignment = new AssignedUsers();
$assignment->setCardId(123);
$assignment->setParticipant('admin');