3535use OCP \IGroupManager ;
3636use OCP \SystemTag \ISystemTagManager ;
3737use OCP \ITags ;
38+ use OCP \Files \FileInfo ;
39+ use OCP \IRequest ;
40+ use OCP \IConfig ;
3841
3942class FilesReportPluginTest extends \Test \TestCase {
4043 /** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */
@@ -76,13 +79,11 @@ public function setUp() {
7679 ->disableOriginalConstructor ()
7780 ->getMock ();
7881
79- $ this ->view = $ this ->getMockBuilder ('\OC\Files\View ' )
80- ->disableOriginalConstructor ()
81- ->getMock ();
82+ $ this ->view = new View ();
8283
8384 $ this ->server = $ this ->getMockBuilder ('\Sabre\DAV\Server ' )
8485 ->setConstructorArgs ([$ this ->tree ])
85- ->setMethods (['getRequestUri ' , 'getBaseUri ' ])
86+ ->setMethods (['getRequestUri ' , 'getBaseUri ' , ' generateMultiStatus ' ])
8687 ->getMock ();
8788
8889 $ this ->server ->expects ($ this ->any ())
@@ -121,6 +122,15 @@ public function setUp() {
121122 ->method ('getUser ' )
122123 ->will ($ this ->returnValue ($ user ));
123124
125+ // add FilesPlugin to test more properties
126+ $ this ->server ->addPlugin (
127+ new \OCA \DAV \Connector \Sabre \FilesPlugin (
128+ $ this ->tree ,
129+ $ this ->createMock (IConfig::class),
130+ $ this ->createMock (IRequest::class)
131+ )
132+ );
133+
124134 $ this ->plugin = new FilesReportPluginImplementation (
125135 $ this ->tree ,
126136 $ this ->view ,
@@ -177,7 +187,12 @@ public function testOnReport() {
177187 $ path = 'test ' ;
178188
179189 $ parameters = new FilterRequest ();
180- $ parameters ->properties = ['{DAV:}getcontentlength ' , '{http://owncloud.org/ns}size ' ];
190+ $ parameters ->properties = [
191+ '{DAV:}getcontentlength ' ,
192+ '{http://owncloud.org/ns}size ' ,
193+ '{http://owncloud.org/ns}fileid ' ,
194+ '{DAV:}resourcetype ' ,
195+ ];
181196 $ parameters ->filters = [
182197 'systemtag ' => [123 , 456 ],
183198 'favorite ' => null
@@ -196,14 +211,8 @@ public function testOnReport() {
196211 ->with ('456 ' , 'files ' )
197212 ->will ($ this ->returnValue (['111 ' , '222 ' , '333 ' ]));
198213
199- $ reportTargetNode = $ this ->getMockBuilder ('\OCA\DAV\Connector\Sabre\Directory ' )
200- ->disableOriginalConstructor ()
201- ->getMock ();
202-
203- $ response = $ this ->getMockBuilder ('Sabre\HTTP\ResponseInterface ' )
204- ->disableOriginalConstructor ()
205- ->getMock ();
206-
214+ $ reportTargetNode = $ this ->createMock (\OCA \DAV \Connector \Sabre \Directory::class);
215+ $ response = $ this ->createMock (\Sabre \HTTP \ResponseInterface::class);
207216 $ response ->expects ($ this ->once ())
208217 ->method ('setHeader ' )
209218 ->with ('Content-Type ' , 'application/xml; charset=utf-8 ' );
@@ -220,12 +229,16 @@ public function testOnReport() {
220229 ->with ('/ ' . $ path )
221230 ->will ($ this ->returnValue ($ reportTargetNode ));
222231
223- $ filesNode1 = $ this ->getMockBuilder ('\OCP\Files\Folder ' )
224- ->disableOriginalConstructor ()
225- ->getMock ();
226- $ filesNode2 = $ this ->getMockBuilder ('\OCP\Files\File ' )
227- ->disableOriginalConstructor ()
228- ->getMock ();
232+ $ filesNode1 = $ this ->createMock (\OCP \Files \Folder::class);
233+ $ filesNode1 ->method ('getId ' )->willReturn (111 );
234+ $ filesNode1 ->method ('getPath ' )->willReturn ('/node1 ' );
235+ $ filesNode1 ->method ('isReadable ' )->willReturn (true );
236+ $ filesNode1 ->method ('getSize ' )->willReturn (2048 );
237+ $ filesNode2 = $ this ->createMock (\OCP \Files \File::class);
238+ $ filesNode2 ->method ('getId ' )->willReturn (222 );
239+ $ filesNode2 ->method ('getPath ' )->willReturn ('/sub/node2 ' );
240+ $ filesNode2 ->method ('getSize ' )->willReturn (1024 );
241+ $ filesNode2 ->method ('isReadable ' )->willReturn (true );
229242
230243 $ this ->userFolder ->expects ($ this ->at (0 ))
231244 ->method ('getById ' )
@@ -242,7 +255,110 @@ public function testOnReport() {
242255 $ this ->server ->httpResponse = $ response ;
243256 $ this ->plugin ->initialize ($ this ->server );
244257
258+ $ responses = null ;
259+ $ this ->server ->expects ($ this ->once ())
260+ ->method ('generateMultiStatus ' )
261+ ->will ($ this ->returnCallback (function ($ responsesArg ) use (&$ responses ) {
262+ $ responses = $ responsesArg ;
263+ })
264+ );
265+
266+ $ this ->assertFalse ($ this ->plugin ->onReport (FilesReportPluginImplementation::REPORT_NAME , $ parameters , '/ ' . $ path ));
267+
268+ $ this ->assertCount (2 , $ responses );
269+
270+ $ this ->assertTrue (isset ($ responses [0 ][200 ]));
271+ $ this ->assertTrue (isset ($ responses [1 ][200 ]));
272+
273+ $ this ->assertEquals ('/test/node1 ' , $ responses [0 ]['href ' ]);
274+ $ this ->assertEquals ('/test/sub/node2 ' , $ responses [1 ]['href ' ]);
275+
276+ $ props1 = $ responses [0 ];
277+ $ this ->assertEquals ('111 ' , $ props1 [200 ]['{http://owncloud.org/ns}fileid ' ]);
278+ $ this ->assertNull ($ props1 [404 ]['{DAV:}getcontentlength ' ]);
279+ $ this ->assertInstanceOf ('\Sabre\DAV\Xml\Property\ResourceType ' , $ props1 [200 ]['{DAV:}resourcetype ' ]);
280+ $ resourceType1 = $ props1 [200 ]['{DAV:}resourcetype ' ]->getValue ();
281+ $ this ->assertEquals ('{DAV:}collection ' , $ resourceType1 [0 ]);
282+
283+ $ props2 = $ responses [1 ];
284+ $ this ->assertEquals ('1024 ' , $ props2 [200 ]['{DAV:}getcontentlength ' ]);
285+ $ this ->assertEquals ('222 ' , $ props2 [200 ]['{http://owncloud.org/ns}fileid ' ]);
286+ $ this ->assertInstanceOf ('\Sabre\DAV\Xml\Property\ResourceType ' , $ props2 [200 ]['{DAV:}resourcetype ' ]);
287+ $ this ->assertCount (0 , $ props2 [200 ]['{DAV:}resourcetype ' ]->getValue ());
288+ }
289+
290+ public function testOnReportPaginationFiltered () {
291+ $ path = 'test ' ;
292+
293+ $ parameters = new FilterRequest ();
294+ $ parameters ->properties = [
295+ '{DAV:}getcontentlength ' ,
296+ ];
297+ $ parameters ->filters = [
298+ 'systemtag ' => [],
299+ 'favorite ' => true
300+ ];
301+ $ parameters ->search = [
302+ 'offset ' => 2 ,
303+ 'limit ' => 3 ,
304+ ];
305+
306+ $ filesNodes = [];
307+ for ($ i = 0 ; $ i < 20 ; $ i ++) {
308+ $ filesNode = $ this ->createMock (\OCP \Files \File::class);
309+ $ filesNode ->method ('getId ' )->willReturn (1000 + $ i );
310+ $ filesNode ->method ('getPath ' )->willReturn ('/nodes/node ' . $ i );
311+ $ filesNode ->method ('isReadable ' )->willReturn (true );
312+ $ filesNodes [$ filesNode ->getId ()] = $ filesNode ;
313+ }
314+
315+ // return all above nodes as favorites
316+ $ this ->privateTags ->expects ($ this ->once ())
317+ ->method ('getFavorites ' )
318+ ->will ($ this ->returnValue (array_keys ($ filesNodes )));
319+
320+ $ reportTargetNode = $ this ->createMock (\OCA \DAV \Connector \Sabre \Directory::class);
321+
322+ $ this ->tree ->expects ($ this ->any ())
323+ ->method ('getNodeForPath ' )
324+ ->with ('/ ' . $ path )
325+ ->will ($ this ->returnValue ($ reportTargetNode ));
326+
327+ // getById must only be called for the required nodes
328+ $ this ->userFolder ->expects ($ this ->at (0 ))
329+ ->method ('getById ' )
330+ ->with (1002 )
331+ ->willReturn ([$ filesNodes [1002 ]]);
332+ $ this ->userFolder ->expects ($ this ->at (1 ))
333+ ->method ('getById ' )
334+ ->with (1003 )
335+ ->willReturn ([$ filesNodes [1003 ]]);
336+ $ this ->userFolder ->expects ($ this ->at (2 ))
337+ ->method ('getById ' )
338+ ->with (1004 )
339+ ->willReturn ([$ filesNodes [1004 ]]);
340+
341+ $ this ->server ->expects ($ this ->any ())
342+ ->method ('getRequestUri ' )
343+ ->will ($ this ->returnValue ($ path ));
344+
345+ $ this ->plugin ->initialize ($ this ->server );
346+
347+ $ responses = null ;
348+ $ this ->server ->expects ($ this ->once ())
349+ ->method ('generateMultiStatus ' )
350+ ->will ($ this ->returnCallback (function ($ responsesArg ) use (&$ responses ) {
351+ $ responses = $ responsesArg ;
352+ })
353+ );
354+
245355 $ this ->assertFalse ($ this ->plugin ->onReport (FilesReportPluginImplementation::REPORT_NAME , $ parameters , '/ ' . $ path ));
356+
357+ $ this ->assertCount (3 , $ responses );
358+
359+ $ this ->assertEquals ('/test/nodes/node2 ' , $ responses [0 ]['href ' ]);
360+ $ this ->assertEquals ('/test/nodes/node3 ' , $ responses [1 ]['href ' ]);
361+ $ this ->assertEquals ('/test/nodes/node4 ' , $ responses [2 ]['href ' ]);
246362 }
247363
248364 public function testFindNodesByFileIdsRoot () {
0 commit comments