SearchPluginTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Search;
  7. use OCA\DAV\CalDAV\CalendarHome;
  8. use OCA\DAV\CalDAV\Search\SearchPlugin;
  9. use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
  10. use Sabre\Xml\Service;
  11. use Test\TestCase;
  12. class SearchPluginTest extends TestCase {
  13. protected $server;
  14. /** @var SearchPlugin $plugin */
  15. protected $plugin;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  19. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  20. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  21. $this->server->xml = new Service();
  22. $this->plugin = new SearchPlugin();
  23. $this->plugin->initialize($this->server);
  24. }
  25. public function testGetFeatures(): void {
  26. $this->assertEquals(['nc-calendar-search'], $this->plugin->getFeatures());
  27. }
  28. public function testGetName(): void {
  29. $this->assertEquals('nc-calendar-search', $this->plugin->getPluginName());
  30. }
  31. public function testInitialize(): void {
  32. $server = $this->createMock(\Sabre\DAV\Server::class);
  33. $plugin = new SearchPlugin();
  34. $server->expects($this->once())
  35. ->method('on')
  36. ->with('report', [$plugin, 'report']);
  37. $server->xml = new Service();
  38. $plugin->initialize($server);
  39. $this->assertEquals(
  40. $server->xml->elementMap['{http://nextcloud.com/ns}calendar-search'],
  41. 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport'
  42. );
  43. }
  44. public function testReportUnknown(): void {
  45. $result = $this->plugin->report('{urn:ietf:params:xml:ns:caldav}calendar-query', 'REPORT', null);
  46. $this->assertEquals($result, null);
  47. $this->assertNotEquals($this->server->transactionType, 'report-nc-calendar-search');
  48. }
  49. public function testReport(): void {
  50. $report = $this->createMock(CalendarSearchReport::class);
  51. $report->filters = [];
  52. $calendarHome = $this->createMock(CalendarHome::class);
  53. $this->server->expects($this->once())
  54. ->method('getRequestUri')
  55. ->with()
  56. ->willReturn('/re/quest/u/r/i');
  57. $this->server->tree->expects($this->once())
  58. ->method('getNodeForPath')
  59. ->with('/re/quest/u/r/i')
  60. ->willReturn($calendarHome);
  61. $this->server->expects($this->once())
  62. ->method('getHTTPDepth')
  63. ->with(2)
  64. ->willReturn(2);
  65. $this->server
  66. ->method('getHTTPPrefer')
  67. ->willReturn([
  68. 'return' => null
  69. ]);
  70. $calendarHome->expects($this->once())
  71. ->method('calendarSearch')
  72. ->willReturn([]);
  73. $this->plugin->report('{http://nextcloud.com/ns}calendar-search', $report, '');
  74. }
  75. public function testSupportedReportSetNoCalendarHome(): void {
  76. $this->server->tree->expects($this->once())
  77. ->method('getNodeForPath')
  78. ->with('/foo/bar')
  79. ->willReturn(null);
  80. $reports = $this->plugin->getSupportedReportSet('/foo/bar');
  81. $this->assertEquals([], $reports);
  82. }
  83. public function testSupportedReportSet(): void {
  84. $calendarHome = $this->createMock(CalendarHome::class);
  85. $this->server->tree->expects($this->once())
  86. ->method('getNodeForPath')
  87. ->with('/bar/foo')
  88. ->willReturn($calendarHome);
  89. $reports = $this->plugin->getSupportedReportSet('/bar/foo');
  90. $this->assertEquals([
  91. '{http://nextcloud.com/ns}calendar-search'
  92. ], $reports);
  93. }
  94. }