77
88namespace Test \Files \Mount ;
99
10+ use OC \Files \Mount \Manager ;
11+ use OC \Files \Mount \MountPoint ;
1012use OC \Files \SetupManagerFactory ;
1113use OC \Files \Storage \Temporary ;
14+ use OCP \EventDispatcher \IEventDispatcher ;
15+ use OCP \Files \Mount \Event \MountAddedEvent ;
16+ use OCP \Files \Mount \Event \MountMovedEvent ;
17+ use OCP \Files \Mount \Event \MountRemovedEvent ;
18+ use Test \TestCase ;
1219
1320class LongId extends Temporary {
1421 public function getId (): string {
1522 return 'long: ' . str_repeat ('foo ' , 50 ) . parent ::getId ();
1623 }
1724}
1825
19- class ManagerTest extends \Test \TestCase {
20- /**
21- * @var \OC\Files\Mount\Manager
22- */
23- private $ manager ;
26+ class ManagerTest extends TestCase {
27+ private IEventDispatcher $ eventDispatcher ;
28+ private Manager $ manager ;
2429
2530 protected function setUp (): void {
2631 parent ::setUp ();
27- $ this ->manager = new \OC \Files \Mount \Manager ($ this ->createMock (SetupManagerFactory::class));
32+
33+ $ this ->eventDispatcher = $ this ->createMock (IEventDispatcher::class);
34+
35+ $ this ->manager = new Manager (
36+ $ this ->createMock (SetupManagerFactory::class),
37+ $ this ->eventDispatcher ,
38+ );
2839 }
2940
3041 public function testFind (): void {
31- $ rootMount = new \ OC \ Files \ Mount \ MountPoint (new Temporary ([]), '/ ' );
42+ $ rootMount = new MountPoint (new Temporary ([]), '/ ' );
3243 $ this ->manager ->addMount ($ rootMount );
3344 $ this ->assertEquals ($ rootMount , $ this ->manager ->find ('/ ' ));
3445 $ this ->assertEquals ($ rootMount , $ this ->manager ->find ('/foo/bar ' ));
3546
3647 $ storage = new Temporary ([]);
37- $ mount1 = new \ OC \ Files \ Mount \ MountPoint ($ storage , '/foo ' );
48+ $ mount1 = new MountPoint ($ storage , '/foo ' );
3849 $ this ->manager ->addMount ($ mount1 );
3950 $ this ->assertEquals ($ rootMount , $ this ->manager ->find ('/ ' ));
4051 $ this ->assertEquals ($ mount1 , $ this ->manager ->find ('/foo/bar ' ));
4152
4253 $ this ->assertEquals (1 , count ($ this ->manager ->findIn ('/ ' )));
43- $ mount2 = new \ OC \ Files \ Mount \ MountPoint (new Temporary ([]), '/bar ' );
54+ $ mount2 = new MountPoint (new Temporary ([]), '/bar ' );
4455 $ this ->manager ->addMount ($ mount2 );
4556 $ this ->assertEquals (2 , count ($ this ->manager ->findIn ('/ ' )));
4657
4758 $ id = $ mount1 ->getStorageId ();
4859 $ this ->assertEquals ([$ mount1 ], $ this ->manager ->findByStorageId ($ id ));
4960
50- $ mount3 = new \ OC \ Files \ Mount \ MountPoint ($ storage , '/foo/bar ' );
61+ $ mount3 = new MountPoint ($ storage , '/foo/bar ' );
5162 $ this ->manager ->addMount ($ mount3 );
5263 $ this ->assertEquals ([$ mount1 , $ mount3 ], $ this ->manager ->findByStorageId ($ id ));
5364 }
5465
5566 public function testLong (): void {
5667 $ storage = new LongId ([]);
57- $ mount = new \ OC \ Files \ Mount \ MountPoint ($ storage , '/foo ' );
68+ $ mount = new MountPoint ($ storage , '/foo ' );
5869 $ this ->manager ->addMount ($ mount );
5970
6071 $ id = $ mount ->getStorageId ();
@@ -63,4 +74,37 @@ public function testLong(): void {
6374 $ this ->assertEquals ([$ mount ], $ this ->manager ->findByStorageId ($ storageId ));
6475 $ this ->assertEquals ([$ mount ], $ this ->manager ->findByStorageId (md5 ($ storageId )));
6576 }
77+
78+ public function testAddMountEvent (): void {
79+ $ this ->eventDispatcher
80+ ->expects ($ this ->once ())
81+ ->method ('dispatchTyped ' )
82+ ->with ($ this ->callback (fn (MountAddedEvent $ event ) => $ event ->mountPoint ->getMountPoint () === '/foo/ ' ));
83+
84+ $ this ->manager ->addMount (new MountPoint (new Temporary ([]), '/foo ' ));
85+ }
86+
87+ public function testRemoveMountEvent (): void {
88+ $ this ->eventDispatcher
89+ ->expects ($ this ->exactly (2 ))
90+ ->method ('dispatchTyped ' )
91+ ->with ($ this ->callback (fn (MountAddedEvent |MountRemovedEvent $ event ) => $ event ->mountPoint ->getMountPoint () === '/foo/ ' ));
92+
93+ $ this ->manager ->addMount (new MountPoint (new Temporary ([]), '/foo ' ));
94+ $ this ->manager ->removeMount ('/foo ' );
95+ }
96+
97+ public function testMoveMountEvent (): void {
98+ $ this ->eventDispatcher
99+ ->expects ($ this ->exactly (2 ))
100+ ->method ('dispatchTyped ' )
101+ ->with ($ this ->callback (fn (MountAddedEvent |MountMovedEvent $ event ) =>
102+ ($ event instanceof MountAddedEvent && $ event ->mountPoint ->getMountPoint () === '/foo/ ' )
103+ // The getMountPoint() still returns the old path in this test because it is updated outside the MountManager before calling moveMount().
104+ || ($ event instanceof MountMovedEvent && $ event ->mountPoint ->getMountPoint () === '/foo/ ' && $ event ->oldPath === '/foo/ ' && $ event ->newPath === '/bar/ ' ))
105+ );
106+
107+ $ this ->manager ->addMount (new MountPoint (new Temporary ([]), '/foo ' ));
108+ $ this ->manager ->moveMount ('/foo/ ' , '/bar/ ' );
109+ }
66110}
0 commit comments