99
1010use OC \DB \Exceptions \DbalException ;
1111use OC \DB \QueryBuilder \Literal ;
12+ use OC \Files \Config \UserMountCache ;
1213use OC \Files \Mount \MountPoint ;
1314use OC \Files \Storage \Storage ;
1415use OC \User \Manager ;
1516use OCP \Cache \CappedMemoryCache ;
1617use OCP \DB \QueryBuilder \IQueryBuilder ;
1718use OCP \Diagnostics \IEventLogger ;
1819use OCP \EventDispatcher \IEventDispatcher ;
20+ use OCP \Files \Config \Event \UserMountAddedEvent ;
21+ use OCP \Files \Config \Event \UserMountRemovedEvent ;
22+ use OCP \Files \Config \Event \UserMountUpdatedEvent ;
1923use OCP \Files \Config \ICachedMountInfo ;
2024use OCP \ICacheFactory ;
2125use OCP \IConfig ;
2933 * @group DB
3034 */
3135class UserMountCacheTest extends TestCase {
32- /**
33- * @var IDBConnection
34- */
35- private $ connection ;
36-
37- /**
38- * @var IUserManager
39- */
40- private $ userManager ;
41-
42- /**
43- * @var \OC\Files\Config\UserMountCache
44- */
45- private $ cache ;
46-
47- private $ fileIds = [];
36+ private IDBConnection $ connection ;
37+ private IUserManager $ userManager ;
38+ private IEventDispatcher $ eventDispatcher ;
39+ private UserMountCache $ cache ;
40+ private array $ fileIds = [];
4841
4942 protected function setUp (): void {
5043 parent ::setUp ();
5144
5245 $ this ->fileIds = [];
46+
5347 $ this ->connection = \OC ::$ server ->getDatabaseConnection ();
48+
5449 $ config = $ this ->getMockBuilder (IConfig::class)
5550 ->disableOriginalConstructor ()
5651 ->getMock ();
@@ -62,13 +57,22 @@ protected function setUp(): void {
6257 ->expects ($ this ->any ())
6358 ->method ('getAppValue ' )
6459 ->willReturnArgument (2 );
60+
6561 $ this ->userManager = new Manager ($ config , $ this ->createMock (ICacheFactory::class), $ this ->createMock (IEventDispatcher::class), $ this ->createMock (LoggerInterface::class));
6662 $ userBackend = new Dummy ();
6763 $ userBackend ->createUser ('u1 ' , '' );
6864 $ userBackend ->createUser ('u2 ' , '' );
6965 $ userBackend ->createUser ('u3 ' , '' );
7066 $ this ->userManager ->registerBackend ($ userBackend );
71- $ this ->cache = new \OC \Files \Config \UserMountCache ($ this ->connection , $ this ->userManager , $ this ->createMock (LoggerInterface::class), $ this ->createMock (IEventLogger::class));
67+
68+ $ this ->eventDispatcher = $ this ->createMock (IEventDispatcher::class);
69+
70+ $ this ->cache = new UserMountCache ($ this ->connection ,
71+ $ this ->userManager ,
72+ $ this ->createMock (LoggerInterface::class),
73+ $ this ->createMock (IEventLogger::class),
74+ $ this ->eventDispatcher ,
75+ );
7276 }
7377
7478 protected function tearDown (): void {
@@ -126,6 +130,11 @@ private function keyForMount(MountPoint $mount): string {
126130 }
127131
128132 public function testNewMounts (): void {
133+ $ this ->eventDispatcher
134+ ->expects ($ this ->once ())
135+ ->method ('dispatchTyped ' )
136+ ->with ($ this ->callback (fn (UserMountAddedEvent $ event ) => $ event ->mountPoint ->getMountPoint () === '/asd/ ' ));
137+
129138 $ user = $ this ->userManager ->get ('u1 ' );
130139
131140 [$ storage ] = $ this ->getStorage (10 );
@@ -146,6 +155,11 @@ public function testNewMounts(): void {
146155 }
147156
148157 public function testSameMounts (): void {
158+ $ this ->eventDispatcher
159+ ->expects ($ this ->once ())
160+ ->method ('dispatchTyped ' )
161+ ->with ($ this ->callback (fn (UserMountAddedEvent $ event ) => $ event ->mountPoint ->getMountPoint () === '/asd/ ' ));
162+
149163 $ user = $ this ->userManager ->get ('u1 ' );
150164
151165 [$ storage ] = $ this ->getStorage (10 );
@@ -170,6 +184,18 @@ public function testSameMounts(): void {
170184 }
171185
172186 public function testRemoveMounts (): void {
187+ $ operation = 0 ;
188+ $ this ->eventDispatcher
189+ ->expects ($ this ->exactly (2 ))
190+ ->method ('dispatchTyped ' )
191+ ->with ($ this ->callback (function (UserMountAddedEvent |UserMountRemovedEvent $ event ) use (&$ operation ) {
192+ return match (++$ operation ) {
193+ 1 => $ event instanceof UserMountAddedEvent && $ event ->mountPoint ->getMountPoint () === '/asd/ ' ,
194+ 2 => $ event instanceof UserMountRemovedEvent && $ event ->mountPoint ->getMountPoint () === '/asd/ ' ,
195+ default => false ,
196+ };
197+ }));
198+
173199 $ user = $ this ->userManager ->get ('u1 ' );
174200
175201 [$ storage ] = $ this ->getStorage (10 );
@@ -189,6 +215,19 @@ public function testRemoveMounts(): void {
189215 }
190216
191217 public function testChangeMounts (): void {
218+ $ operation = 0 ;
219+ $ this ->eventDispatcher
220+ ->expects ($ this ->exactly (3 ))
221+ ->method ('dispatchTyped ' )
222+ ->with ($ this ->callback (function (UserMountAddedEvent |UserMountRemovedEvent $ event ) use (&$ operation ) {
223+ return match (++$ operation ) {
224+ 1 => $ event instanceof UserMountAddedEvent && $ event ->mountPoint ->getMountPoint () === '/bar/ ' ,
225+ 2 => $ event instanceof UserMountAddedEvent && $ event ->mountPoint ->getMountPoint () === '/foo/ ' ,
226+ 3 => $ event instanceof UserMountRemovedEvent && $ event ->mountPoint ->getMountPoint () === '/bar/ ' ,
227+ default => false ,
228+ };
229+ }));
230+
192231 $ user = $ this ->userManager ->get ('u1 ' );
193232
194233 [$ storage ] = $ this ->getStorage (10 );
@@ -212,6 +251,18 @@ public function testChangeMounts(): void {
212251 }
213252
214253 public function testChangeMountId (): void {
254+ $ operation = 0 ;
255+ $ this ->eventDispatcher
256+ ->expects ($ this ->exactly (2 ))
257+ ->method ('dispatchTyped ' )
258+ ->with ($ this ->callback (function (UserMountAddedEvent |UserMountUpdatedEvent $ event ) use (&$ operation ) {
259+ return match (++$ operation ) {
260+ 1 => $ event instanceof UserMountAddedEvent && $ event ->mountPoint ->getMountPoint () === '/foo/ ' ,
261+ 2 => $ event instanceof UserMountUpdatedEvent && $ event ->oldMountPoint ->getMountId () === null && $ event ->newMountPoint ->getMountId () === 1 ,
262+ default => false ,
263+ };
264+ }));
265+
215266 $ user = $ this ->userManager ->get ('u1 ' );
216267
217268 [$ storage ] = $ this ->getStorage (10 );
0 commit comments