99namespace OCA \Files_Sharing \Tests \Controller ;
1010
1111use OC \Files \Storage \Wrapper \Wrapper ;
12+ use OC \Session \Internal ;
1213use OCA \Federation \TrustedServers ;
1314use OCA \Files_Sharing \Controller \ShareAPIController ;
1415use OCA \Files_Sharing \External \Storage ;
2627use OCP \Files \IRootFolder ;
2728use OCP \Files \Mount \IMountPoint ;
2829use OCP \Files \Mount \IShareOwnerlessMount ;
30+ use OCP \Files \Node ;
2931use OCP \Files \NotFoundException ;
3032use OCP \Files \Storage \IStorage ;
3133use OCP \IAppConfig ;
5961use Test \TestCase ;
6062use Test \Traits \EmailValidatorTrait ;
6163
64+ /**
65+ * Internal mock to allow mocking the Talk controller used for room shares,
66+ * needed when Talk is not installed during tests (PHPUnit does not allow mocking of non-existing classes).
67+ */
68+ interface InternalTalkShareAPIController {
69+ public function formatShare (IShare $ share ): array ;
70+ public function canAccessShare (IShare $ share , string $ user ): bool ;
71+ public function createShare (IShare $ share , string $ shareWith , int $ permissions , string $ expireDate ): void ;
72+ }
73+
6274/**
6375 * Class ShareAPIControllerTest
6476 *
@@ -1831,25 +1843,15 @@ public function testCanAccessRoomShare(
18311843 ->with ('spreed ' )
18321844 ->willReturn (true );
18331845
1834- // This is not possible anymore with PHPUnit 10+
1835- // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
1836- // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
1837- $ helper = $ this ->getMockBuilder (\stdClass::class)
1838- ->addMethods (['canAccessShare ' ])
1839- ->getMock ();
1840- $ helper ->method ('canAccessShare ' )
1846+ $ this ->mockTalkController ()
1847+ ->method ('canAccessShare ' )
18411848 ->with ($ share , $ this ->currentUser )
18421849 ->willReturn ($ canAccessShareByHelper );
1843-
1844- $ this ->serverContainer ->method ('get ' )
1845- ->with ('\OCA\Talk\Share\Helper\ShareAPIController ' )
1846- ->willReturn ($ helper );
18471850 }
18481851
18491852 $ this ->assertEquals ($ expected , $ this ->invokePrivate ($ this ->ocs , 'canAccessShare ' , [$ share ]));
18501853 }
18511854
1852-
18531855 public function testCreateShareNoPath (): void {
18541856 $ this ->expectException (OCSNotFoundException::class);
18551857 $ this ->expectExceptionMessage ('Please specify a file or folder path ' );
@@ -2663,13 +2665,8 @@ public function testCreateShareRoom(): void {
26632665 ->with ('spreed ' )
26642666 ->willReturn (true );
26652667
2666- // This is not possible anymore with PHPUnit 10+
2667- // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2668- // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2669- $ helper = $ this ->getMockBuilder (\stdClass::class)
2670- ->addMethods (['createShare ' ])
2671- ->getMock ();
2672- $ helper ->method ('createShare ' )
2668+ $ this ->mockTalkController ()
2669+ ->method ('createShare ' )
26732670 ->with (
26742671 $ share ,
26752672 'recipientRoom ' ,
@@ -2684,10 +2681,6 @@ function ($share): void {
26842681 }
26852682 );
26862683
2687- $ this ->serverContainer ->method ('get ' )
2688- ->with ('\OCA\Talk\Share\Helper\ShareAPIController ' )
2689- ->willReturn ($ helper );
2690-
26912684 $ this ->shareManager ->method ('createShare ' )
26922685 ->with ($ this ->callback (function (IShare $ share ) use ($ path ) {
26932686 return $ share ->getNode () === $ path
@@ -2772,13 +2765,8 @@ public function testCreateShareRoomHelperThrowException(): void {
27722765 ->with ('spreed ' )
27732766 ->willReturn (true );
27742767
2775- // This is not possible anymore with PHPUnit 10+
2776- // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
2777- // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
2778- $ helper = $ this ->getMockBuilder (\stdClass::class)
2779- ->addMethods (['createShare ' ])
2780- ->getMock ();
2781- $ helper ->method ('createShare ' )
2768+ $ this ->mockTalkController ()
2769+ ->method ('createShare ' )
27822770 ->with (
27832771 $ share ,
27842772 'recipientRoom ' ,
@@ -2790,10 +2778,6 @@ function ($share): void {
27902778 }
27912779 );
27922780
2793- $ this ->serverContainer ->method ('get ' )
2794- ->with ('\OCA\Talk\Share\Helper\ShareAPIController ' )
2795- ->willReturn ($ helper );
2796-
27972781 $ this ->shareManager ->expects ($ this ->never ())->method ('createShare ' );
27982782
27992783 $ ocs ->createShare ('valid-path ' , Constants::PERMISSION_ALL , IShare::TYPE_ROOM , 'recipientRoom ' );
@@ -4941,6 +4925,7 @@ function ($user) {
49414925 $ expects ['attributes ' ] = \json_encode ($ shareParams ['attributes ' ]);
49424926 }
49434927 if (isset ($ shareParams ['node ' ])) {
4928+ /** @var Node&MockObject */
49444929 $ node = $ this ->createMock ($ shareParams ['node ' ]['class ' ]);
49454930
49464931 $ node ->method ('getMimeType ' )->willReturn ($ shareParams ['node ' ]['mimeType ' ]);
@@ -5214,22 +5199,13 @@ public function testFormatRoomShare(array $expects, bool $helperAvailable, array
52145199 ->with ('spreed ' )
52155200 ->willReturn (true );
52165201
5217- // This is not possible anymore with PHPUnit 10+
5218- // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
5219- // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
5220- $ helper = $ this ->getMockBuilder (\stdClass::class)
5221- ->addMethods (['formatShare ' , 'canAccessShare ' ])
5222- ->getMock ();
5223- $ helper ->method ('formatShare ' )
5202+ $ helper = $ this ->mockTalkController ();
5203+ $ helper ->method ('formatShare ' )
52245204 ->with ($ share )
52255205 ->willReturn ($ formatShareByHelper );
52265206 $ helper ->method ('canAccessShare ' )
52275207 ->with ($ share )
52285208 ->willReturn (true );
5229-
5230- $ this ->serverContainer ->method ('get ' )
5231- ->with ('\OCA\Talk\Share\Helper\ShareAPIController ' )
5232- ->willReturn ($ helper );
52335209 }
52345210
52355211 $ result = $ this ->invokePrivate ($ this ->ocs , 'formatShare ' , [$ share ]);
@@ -5647,4 +5623,21 @@ public function testWrapperStorageUnwrapped(): void {
56475623
56485624 $ this ->invokePrivate ($ ocs , 'checkInheritedAttributes ' , [$ share ]);
56495625 }
5626+
5627+ /**
5628+ * Helper to allow testing Talk integration even if Talk
5629+ * is not available during tests.
5630+ */
5631+ private function mockTalkController (): MockObject {
5632+ if (class_exists (\OCA \Talk \Share \Helper \ShareAPIController::class)) {
5633+ $ helper = $ this ->createMock (\OCA \Talk \Share \Helper \ShareAPIController::class);
5634+ } else {
5635+ $ helper = $ this ->createMock (InternalTalkShareAPIController::class);
5636+ }
5637+
5638+ $ this ->serverContainer ->method ('get ' )
5639+ ->with (\OCA \Talk \Share \Helper \ShareAPIController::class)
5640+ ->willReturn ($ helper );
5641+ return $ helper ;
5642+ }
56505643}
0 commit comments