diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-07-09 22:14:00 +0200 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-08-09 12:23:47 +0200 |
commit | b00831783d97906f0077f49b9e70c5d37b63fb62 (patch) | |
tree | d075a322adc8b2461e88e1b59526edb3722e8154 | |
parent | d53fc734489081de4dffac47d8170db9eb4f929b (diff) |
More tests and fixes
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
-rw-r--r-- | lib/Entity/Account.php | 3 | ||||
-rw-r--r-- | lib/Entity/Status.php | 3 | ||||
-rw-r--r-- | lib/Service/AccountFinder.php | 17 | ||||
-rw-r--r-- | lib/Service/Feed/FeedManager.php | 3 | ||||
-rw-r--r-- | lib/Service/Feed/PostDeliveryService.php | 14 | ||||
-rw-r--r-- | lib/Service/PostServiceStatus.php | 14 | ||||
-rw-r--r-- | tests/Service/AccountFinderTest.php | 17 | ||||
-rw-r--r-- | tests/Service/ActivityPub/TagManagerTest.php | 2 | ||||
-rw-r--r-- | tests/Service/Feed/PostDeliveryServiceTest.php | 87 |
9 files changed, 132 insertions, 28 deletions
diff --git a/lib/Entity/Account.php b/lib/Entity/Account.php index 6017cdcc..dc4fa9ab 100644 --- a/lib/Entity/Account.php +++ b/lib/Entity/Account.php @@ -239,7 +239,8 @@ class Account { } public function setRepresentative(): self { - $this->id = self::REPRESENTATIVE_ID; + $this->userId = '__self'; + return $this; } public function getUserId(): ?string { diff --git a/lib/Entity/Status.php b/lib/Entity/Status.php index 69f586ca..db1f6bab 100644 --- a/lib/Entity/Status.php +++ b/lib/Entity/Status.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace OCA\Social\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; @@ -149,7 +150,7 @@ class Status { private Collection $mentions; public function __construct() { - + $this->mentions = new ArrayCollection(); } public function getId(): string { diff --git a/lib/Service/AccountFinder.php b/lib/Service/AccountFinder.php index 8b2645eb..78e63552 100644 --- a/lib/Service/AccountFinder.php +++ b/lib/Service/AccountFinder.php @@ -8,6 +8,7 @@ namespace OCA\Social\Service; use Doctrine\Common\Collections\Collection; use OCA\Social\Entity\Account; +use OCA\Social\Entity\Follow; use OCP\DB\ORM\IEntityManager; use OCP\DB\ORM\IEntityRepository; use OCP\IRequest; @@ -17,6 +18,7 @@ class AccountFinder { private IEntityManager $entityManager; private IEntityRepository $repository; private IRequest $request; + private ?Account $representative = null; public function __construct(IEntityManager $entityManager, IRequest $request) { $this->entityManager = $entityManager; @@ -57,10 +59,14 @@ class AccountFinder { } public function getRepresentative(): Account { + if ($this->representative !== null) { + return $this->representative; + } $account = $this->repository->findOneBy([ - 'id' => Account::REPRESENTATIVE_ID, + 'userId' => '__self', ]); if ($account) { + $this->representative = $account; return $account; } $account = Account::newLocal(); @@ -72,17 +78,16 @@ class AccountFinder { ->generateKeys(); $this->entityManager->persist($account); $this->entityManager->flush(); + $this->representative = $account; return $account; } /** * @param Account $account - * @return array<Account> + * @return array<Follow> */ public function getLocalFollowersOf(Account $account): array { - echo $this->entityManager->createQuery('SELECT a, f FROM \OCA\Social\Entity\Follow f LEFT JOIN f.account a WHERE f.targetAccount = :target') - ->setParameters(['target' => $account])->getSql() . ' ' . $account->getId() ; - return $this->entityManager->createQuery('SELECT f FROM \OCA\Social\Entity\Follow f LEFT JOIN f.account a WHERE f.targetAccount = :target') - ->setParameters(['target' => $account])->getArrayResult(); + return $this->entityManager->createQuery('SELECT f,a FROM \OCA\Social\Entity\Follow f LEFT JOIN f.account a WHERE f.targetAccount = :target') + ->setParameters(['target' => $account])->getResult(); } } diff --git a/lib/Service/Feed/FeedManager.php b/lib/Service/Feed/FeedManager.php index c4dbe20a..57c1499d 100644 --- a/lib/Service/Feed/FeedManager.php +++ b/lib/Service/Feed/FeedManager.php @@ -5,7 +5,8 @@ declare(strict_types=1); // Nextcloud - Social Support // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu> // SPDX-License-Identifier: AGPL-3.0-or-later -namespace OCA\Social\Service; + +namespace OCA\Social\Service\Feed; use OCA\Social\Entity\Status; diff --git a/lib/Service/Feed/PostDeliveryService.php b/lib/Service/Feed/PostDeliveryService.php index a1403bf6..5bdd8d28 100644 --- a/lib/Service/Feed/PostDeliveryService.php +++ b/lib/Service/Feed/PostDeliveryService.php @@ -5,11 +5,13 @@ declare(strict_types=1); // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu> // SPDX-License-Identifier: AGPL-3.0-or-later -namespace OCA\Social\Service; +namespace OCA\Social\Service\Feed; use OCA\Social\Entity\Account; use OCA\Social\Entity\Mention; use OCA\Social\Entity\Status; +use OCA\Social\Service\AccountFinder; +use OCA\Social\Service\Feed\FeedManager; class PostDeliveryService { private FeedManager $feedManager; @@ -20,7 +22,8 @@ class PostDeliveryService { $this->accountFinder = $accountFinder; } - public function run(Account $author, Status $status): void { + public function run(Status $status): void { + $author = $status->getAccount(); // deliver to self if ($status->isLocal()) { $this->feedManager->addToHome($author->getId(), $status); @@ -35,10 +38,9 @@ class PostDeliveryService { }); // deliver to local followers - $localFollowers->forAll(function (Account $account) use ($status): void { - $this->deliverLocalAccount($status, $account); - }); - + foreach ($localFollowers as $follower) { + $this->deliverLocalAccount($status, $follower->getAccount()); + }; } public function deliverLocalAccount(Status $status, Account $account) { diff --git a/lib/Service/PostServiceStatus.php b/lib/Service/PostServiceStatus.php index 5770aeaa..cdce33ae 100644 --- a/lib/Service/PostServiceStatus.php +++ b/lib/Service/PostServiceStatus.php @@ -8,6 +8,7 @@ namespace OCA\Social\Service; use OCA\Social\Entity\Account; use OCA\Social\Entity\Status; +use OCA\Social\Service\Feed\PostDeliveryService; use OCP\DB\ORM\IEntityManager; use OCP\ICache; use OCP\ICacheFactory; @@ -18,20 +19,20 @@ class PostServiceStatus { private IConfig $config; private IEntityManager $entityManager; private ProcessMentionsService $mentionsService; - private FeedManager $feedManager; + private PostDeliveryService $deliveryService; public function __construct( ICacheFactory $cacheFactory, IConfig $config, IEntityManager $entityManager, ProcessMentionsService $mentionsService, - FeedManager $feedManager + PostDeliveryService $deliveryService ) { $this->idempotenceCache = $cacheFactory->createDistributed('social.idempotence'); $this->config = $config; $this->entityManager = $entityManager; $this->mentionsService = $mentionsService; - $this->feedManager = $feedManager; + $this->deliveryService = $deliveryService; } /** @@ -63,7 +64,7 @@ class PostServiceStatus { $this->entityManager->persist($account); $this->entityManager->flush(); - $this->sendStatus($status); + $this->deliveryService->run($status); $this->updateIdempotency($account, $status); } @@ -89,9 +90,4 @@ class PostServiceStatus { $this->idempotenceCache->set($this->idempotencyKey($account, $options['idempotency']), $status->getId(), 3600); } - - public function sendStatus(Account $account, Status $status): void { - // to self - $this->feedManager->addToHome($account->getId(), $status); - } } diff --git a/tests/Service/AccountFinderTest.php b/tests/Service/AccountFinderTest.php index 16250996..622e891e 100644 --- a/tests/Service/AccountFinderTest.php +++ b/tests/Service/AccountFinderTest.php @@ -20,6 +20,7 @@ use Test\TestCase; class AccountFinderTest extends TestCase { private ?Account $account1 = null; private ?Account $account2 = null; + private ?AccountFinder $accountFinder = null; public function setUp(): void { parent::setUp(); @@ -33,6 +34,8 @@ class AccountFinderTest extends TestCase { $em->persist($this->account1); $em->persist($this->account2); $em->flush(); + + $this->accountFinder = Server::get(AccountFinder::class); } public function tearDown(): void { @@ -45,8 +48,16 @@ class AccountFinderTest extends TestCase { } public function testGetLocalFollower(): void { - $accountFinder = Server::get(AccountFinder::class); - $accounts = $accountFinder->getLocalFollowersOf($this->account1); - var_dump(count($accounts)); + $accounts = $this->accountFinder->getLocalFollowersOf($this->account1); + $this->assertSame(count($accounts), 1); + $this->assertSame($accounts[0]->getAccount()->getId(), $this->account2->getId()); + } + + public function testGetRepresentive(): void { + $account = $this->accountFinder->getRepresentative(); + $account1 = $this->accountFinder->getRepresentative(); + + // Caching works + $this->assertSame($account, $account1); } } diff --git a/tests/Service/ActivityPub/TagManagerTest.php b/tests/Service/ActivityPub/TagManagerTest.php index d14ad870..838ccf2a 100644 --- a/tests/Service/ActivityPub/TagManagerTest.php +++ b/tests/Service/ActivityPub/TagManagerTest.php @@ -29,7 +29,7 @@ class TagManagerTest extends TestCase { */ public function testIsLocalUri(?string $url, string $localDomain, bool $result): void { $request = $this->createMock(IRequest::class); - $request->expects($this->once()) + $request->expects($this->any()) ->method('getServerHost') ->willReturn($localDomain); $tagManager = new TagManager($request); diff --git a/tests/Service/Feed/PostDeliveryServiceTest.php b/tests/Service/Feed/PostDeliveryServiceTest.php new file mode 100644 index 00000000..d6b87e2e --- /dev/null +++ b/tests/Service/Feed/PostDeliveryServiceTest.php @@ -0,0 +1,87 @@ +<?php +declare(strict_types=1); + +// Nextcloud - Social Support +// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu> +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace OCA\Social\Tests\Service\Feed; + +use OCA\Social\Entity\Account; +use OCA\Social\Entity\Status; +use OCA\Social\Service\AccountFinder; +use OCA\Social\Service\Feed\PostDeliveryService; +use OCA\Social\Service\Feed\FeedManager; +use OCP\DB\ORM\IEntityManager; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockClass; +use Test\TestCase; + +class PostDeliveryServiceTest extends TestCase { + private ?Account $account1 = null; + private ?Account $account2 = null; + private ?Account $account3 = null; + private ?AccountFinder $accountFinder = null; + private ?PostDeliveryService $postDeliveryService = null; + /** @var MockClass&FeedManager */ + private $feedManager; + + public function setUp(): void { + parent::setUp(); + + $em = Server::get(IEntityManager::class); + + $this->account1 = Account::newLocal('user1', 'user1', 'User1'); + $this->account2 = Account::newLocal('user2', 'user2', 'User2'); + $this->account3 = Account::newLocal('user3', 'user3', 'User3'); + $this->account2->follow($this->account1); + + $em->persist($this->account1); + $em->persist($this->account2); + $em->persist($this->account3); + $em->flush(); + + $this->accountFinder = Server::get(AccountFinder::class); + $this->feedManager = $this->createMock(FeedManager::class); + $this->postDeliveryService = new PostDeliveryService($this->feedManager, $this->accountFinder); + } + + public function tearDown(): void { + $em = Server::get(IEntityManager::class); + $em->remove($this->account1); + $em->remove($this->account2); + $em->flush(); + + parent::tearDown(); + } + + public function testCreateBasicStatus(): void { + $status = new Status(); + $status->setAccount($this->account1); + $status->setText('Hello world!'); + $status->setLocal(true); + $this->feedManager->expects($this->exactly(2)) + ->method('addToHome') + ->withConsecutive( + [$this->account1->getId(), $status], // self + [$this->account2->getId(), $status] // follower + ); + $this->postDeliveryService->run($status); + } + + public function testCreateBasicStatusWithLocalMention(): void { + $status = new Status(); + $status->setAccount($this->account1); + $status->setText('Hello world @user3!'); + $status->setLocal(true); + \OCP + $this->feedManager->expects($this->exactly(2)) + ->method('addToHome') + ->withConsecutive( + [$this->account1->getId(), $status], // self + [$this->account2->getId(), $status] // follower + [$this->account3->getId(), $status] // follower + ); + $this->postDeliveryService->run($status); + } +} |