1515use OCA \Polls \Exceptions \InvalidShareTypeException ;
1616use OCA \Polls \Service \ShareService ;
1717use OCA \Polls \Tests \Unit \UnitTestCase ;
18+ use OCA \Polls \UserSession ;
1819use OCP \ISession ;
1920use OCP \Server ;
2021
@@ -23,15 +24,15 @@ class ShareServiceTest extends UnitTestCase {
2324 private ShareMapper $ shareMapper ;
2425 private PollMapper $ pollMapper ;
2526 private ISession $ session ;
27+ private UserSession $ userSession ;
2628
2729 private Poll $ poll ;
2830 private Share $ userShare ;
2931
3032 protected function setUp (): void {
3133 parent ::setUp ();
3234 $ this ->session = Server::get (ISession::class);
33- $ this ->session ->set ('ncPollsUserId ' , 'admin ' );
34-
35+ $ this ->userSession = Server::get (UserSession::class);
3536 $ this ->shareService = Server::get (ShareService::class);
3637 $ this ->shareMapper = Server::get (ShareMapper::class);
3738 $ this ->pollMapper = Server::get (PollMapper::class);
@@ -40,12 +41,16 @@ protected function setUp(): void {
4041 $ poll ->setOwner ('admin ' );
4142 $ this ->poll = $ this ->pollMapper ->insert ($ poll );
4243
43- // Pre-create a TYPE_USER share for admin (used in setType tests)
44+ // Pre-create a TYPE_USER share for admin.
45+ // Also used to establish a proper logged-in session so that
46+ // UserSession::getIsLoggedIn() returns true (required for PERMISSION_SHARE_ADD).
4447 $ share = $ this ->fm ->instance ('OCA\Polls\Db\Share ' );
4548 $ share ->setPollId ($ this ->poll ->getId ());
4649 $ share ->setType (Share::TYPE_USER );
4750 $ share ->setUserId ('admin ' );
4851 $ this ->userShare = $ this ->shareMapper ->insert ($ share );
52+
53+ $ this ->login ();
4954 }
5055
5156 protected function tearDown (): void {
@@ -57,6 +62,12 @@ protected function tearDown(): void {
5762 }
5863 }
5964
65+ private function login (): void {
66+ $ this ->userSession ->cleanSession ();
67+ $ this ->session ->set (UserSession::SESSION_KEY_SHARE_TOKEN , $ this ->userShare ->getToken ());
68+ $ this ->session ->set (UserSession::SESSION_KEY_USER_ID , 'admin ' );
69+ }
70+
6071 // --- add ---
6172
6273 public function testAddPublicShareCreatesShare (): void {
0 commit comments