1717use OC \Files \Node \File ;
1818use OC \Files \Node \Folder ;
1919use OC \Files \Node \Node ;
20+ use OC \Files \Node \NonExistingFolder ;
2021use OC \Files \Node \Root ;
2122use OC \Files \Search \SearchBinaryOperator ;
2223use OC \Files \Search \SearchComparison ;
3738use OCP \Files \Search \ISearchComparison ;
3839use OCP \Files \Search \ISearchOrder ;
3940use OCP \Files \Storage \IStorage ;
41+ use PHPUnit \Framework \Attributes \DataProvider ;
4042use PHPUnit \Framework \MockObject \MockObject ;
4143
4244/**
4749 */
4850#[\PHPUnit \Framework \Attributes \Group('DB ' )]
4951class FolderTest extends NodeTestCase {
50- protected function createTestNode ($ root , $ view , $ path , array $ data = [], $ internalPath = '' , $ storage = null ) {
52+ protected function createTestNode (IRootFolder $ root , View & MockObject $ view , string $ path , array $ data = [], string $ internalPath = '' , ? IStorage $ storage = null ): Folder {
5153 $ view ->expects ($ this ->any ())
5254 ->method ('getRoot ' )
5355 ->willReturn ('' );
@@ -58,23 +60,20 @@ protected function createTestNode($root, $view, $path, array $data = [], $intern
5860 }
5961 }
6062
61- protected function getNodeClass () {
62- return ' \OC\Files\Node\ Folder' ;
63+ protected function getNodeClass (): string {
64+ return Folder::class ;
6365 }
6466
65- protected function getNonExistingNodeClass () {
66- return ' \OC\Files\Node\ NonExistingFolder' ;
67+ protected function getNonExistingNodeClass (): string {
68+ return NonExistingFolder::class ;
6769 }
6870
69- protected function getViewDeleteMethod () {
71+ protected function getViewDeleteMethod (): string {
7072 return 'rmdir ' ;
7173 }
7274
7375 public function testGetDirectoryContent (): void {
7476 $ manager = $ this ->createMock (Manager::class);
75- /**
76- * @var View|\PHPUnit\Framework\MockObject\MockObject $view
77- */
7877 $ root = $ this ->getMockBuilder (Root::class)
7978 ->setConstructorArgs ([$ manager , $ this ->view , $ this ->user , $ this ->userMountCache , $ this ->logger , $ this ->userManager , $ this ->eventDispatcher , $ this ->cacheFactory , $ this ->appConfig ])
8079 ->getMock ();
@@ -299,7 +298,6 @@ public function testSearch(): void {
299298 ->getMock ();
300299 $ root ->method ('getUser ' )
301300 ->willReturn ($ this ->user );
302- /** @var Storage\IStorage&MockObject $storage */
303301 $ storage = $ this ->createMock (IStorage::class);
304302 $ storage ->method ('getId ' )->willReturn ('test::1 ' );
305303 $ cache = new Cache ($ storage );
@@ -349,7 +347,6 @@ public function testSearchInRoot(): void {
349347 $ root ->expects ($ this ->any ())
350348 ->method ('getUser ' )
351349 ->willReturn ($ this ->user );
352- /** @var \PHPUnit\Framework\MockObject\MockObject|Storage $storage */
353350 $ storage = $ this ->createMock (IStorage::class);
354351 $ storage ->method ('getId ' )->willReturn ('test::2 ' );
355352 $ cache = new Cache ($ storage );
@@ -1041,4 +1038,87 @@ public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array
10411038 }, $ result );
10421039 $ this ->assertEquals ($ expectedPaths , $ ids );
10431040 }
1041+
1042+ public static function dataGetOrCreateFolder (): \Generator {
1043+ yield 'Create new folder ' => [0 ];
1044+ yield 'Get existing folder ' => [1 ];
1045+ yield 'Create new folder while a file with the same name already exists ' => [2 ];
1046+ }
1047+
1048+ #[DataProvider('dataGetOrCreateFolder ' )]
1049+ public function testGetOrCreateFolder (int $ case ): void {
1050+ $ folderName = 'asd ' ;
1051+
1052+ $ view = $ this ->getRootViewMock ();
1053+ $ manager = $ this ->createMock (Manager::class);
1054+ $ root = $ this ->getMockBuilder (Root::class)
1055+ ->setConstructorArgs ([$ manager , $ view , $ this ->user , $ this ->userMountCache , $ this ->logger , $ this ->userManager , $ this ->eventDispatcher , $ this ->cacheFactory , $ this ->appConfig ])
1056+ ->getMock ();
1057+ $ root ->expects ($ this ->any ())
1058+ ->method ('getUser ' )
1059+ ->willReturn ($ this ->user );
1060+
1061+ $ view ->method ('getFileInfo ' )
1062+ ->willReturnCallback (function (string $ path ) use ($ folderName ) {
1063+ if ($ path === '/bar/foo ' || $ path === '/bar/foo/ ' . $ folderName ) {
1064+ return $ this ->getFileInfo (['permissions ' => Constants::PERMISSION_ALL ]);
1065+ }
1066+ $ this ->fail ('Trying to get ' . $ path );
1067+ });
1068+
1069+ $ view ->method ('mkdir ' )
1070+ ->willReturn (true );
1071+
1072+ $ view ->method ('touch ' )
1073+ ->with ('/bar/foo/asd ' )
1074+ ->willReturn (true );
1075+
1076+ $ node = new Folder ($ root , $ view , '/bar/foo ' );
1077+
1078+ switch ($ case ) {
1079+ case 0 :
1080+ $ child = new Folder ($ root , $ view , '/bar/foo/ ' . $ folderName , null , $ node );
1081+
1082+ $ root ->expects ($ this ->any ())
1083+ ->method ('get ' )
1084+ ->willReturnCallback (function (string $ path ) use ($ root , $ view , $ folderName ) {
1085+ if ($ path === '/bar/foo/ ' ) {
1086+ return new Folder ($ root , $ view , '/bar/foo/ ' );
1087+ } elseif ($ path === '/bar/foo/ ' . $ folderName ) {
1088+ throw new NotFoundException ();
1089+ }
1090+ $ this ->fail ('Trying to get ' . $ path );
1091+ });
1092+
1093+ break ; // do nothing
1094+ case 1 :
1095+ $ child = new Folder ($ root , $ view , '/bar/foo/ ' . $ folderName , null , $ node );
1096+
1097+ $ root ->expects ($ this ->any ())
1098+ ->method ('get ' )
1099+ ->with ('/bar/foo/ ' . $ folderName )
1100+ ->willReturn ($ child );
1101+ $ node ->newFolder ($ folderName );
1102+ break ;
1103+ case 2 :
1104+ $ child = new Folder ($ root , $ view , '/bar/foo/ ' . $ folderName . ' (1) ' , null , $ node );
1105+ $ root ->expects ($ this ->any ())
1106+ ->method ('get ' )
1107+ ->willReturnCallback (function (string $ path ) use ($ root , $ view , $ folderName ) {
1108+ if ($ path === '/bar/foo/ ' ) {
1109+ return new Folder ($ root , $ view , '/bar/foo/ ' );
1110+ } elseif ($ path === '/bar/foo/ ' . $ folderName ) {
1111+ return new File ($ root , $ view , '/bar/foo/asd ' );
1112+ } elseif ($ path === '/bar/foo/ ' . $ folderName . ' (1) ' ) {
1113+ throw new NotFoundException ();
1114+ }
1115+ $ this ->fail ('Trying to get ' . $ path );
1116+ });
1117+ $ node ->newFile ($ folderName );
1118+ break ;
1119+ }
1120+
1121+ $ result = $ node ->getOrCreateFolder ($ folderName );
1122+ $ this ->assertEquals ($ child , $ result );
1123+ }
10441124}
0 commit comments