SearchPluginTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV\Search;
  26. use OCA\DAV\CalDAV\CalendarHome;
  27. use OCA\DAV\CalDAV\Search\SearchPlugin;
  28. use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
  29. use Sabre\Xml\Service;
  30. use Test\TestCase;
  31. class SearchPluginTest extends TestCase {
  32. protected $server;
  33. /** @var \OCA\DAV\CalDAV\Search\SearchPlugin $plugin */
  34. protected $plugin;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  38. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  39. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  40. $this->server->xml = new Service();
  41. $this->plugin = new SearchPlugin();
  42. $this->plugin->initialize($this->server);
  43. }
  44. public function testGetFeatures(): void {
  45. $this->assertEquals(['nc-calendar-search'], $this->plugin->getFeatures());
  46. }
  47. public function testGetName(): void {
  48. $this->assertEquals('nc-calendar-search', $this->plugin->getPluginName());
  49. }
  50. public function testInitialize(): void {
  51. $server = $this->createMock(\Sabre\DAV\Server::class);
  52. $plugin = new SearchPlugin();
  53. $server->expects($this->once())
  54. ->method('on')
  55. ->with('report', [$plugin, 'report']);
  56. $server->xml = new Service();
  57. $plugin->initialize($server);
  58. $this->assertEquals(
  59. $server->xml->elementMap['{http://nextcloud.com/ns}calendar-search'],
  60. 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport'
  61. );
  62. }
  63. public function testReportUnknown(): void {
  64. $result = $this->plugin->report('{urn:ietf:params:xml:ns:caldav}calendar-query', 'REPORT', null);
  65. $this->assertEquals($result, null);
  66. $this->assertNotEquals($this->server->transactionType, 'report-nc-calendar-search');
  67. }
  68. public function testReport(): void {
  69. $report = $this->createMock(CalendarSearchReport::class);
  70. $report->filters = [];
  71. $calendarHome = $this->createMock(CalendarHome::class);
  72. $this->server->expects($this->once())
  73. ->method('getRequestUri')
  74. ->with()
  75. ->willReturn('/re/quest/u/r/i');
  76. $this->server->tree->expects($this->once())
  77. ->method('getNodeForPath')
  78. ->with('/re/quest/u/r/i')
  79. ->willReturn($calendarHome);
  80. $this->server->expects($this->once())
  81. ->method('getHTTPDepth')
  82. ->with(2)
  83. ->willReturn(2);
  84. $this->server
  85. ->method('getHTTPPrefer')
  86. ->willReturn([
  87. 'return' => null
  88. ]);
  89. $calendarHome->expects($this->once())
  90. ->method('calendarSearch')
  91. ->willReturn([]);
  92. $this->plugin->report('{http://nextcloud.com/ns}calendar-search', $report, '');
  93. }
  94. public function testSupportedReportSetNoCalendarHome(): void {
  95. $this->server->tree->expects($this->once())
  96. ->method('getNodeForPath')
  97. ->with('/foo/bar')
  98. ->willReturn(null);
  99. $reports = $this->plugin->getSupportedReportSet('/foo/bar');
  100. $this->assertEquals([], $reports);
  101. }
  102. public function testSupportedReportSet(): void {
  103. $calendarHome = $this->createMock(CalendarHome::class);
  104. $this->server->tree->expects($this->once())
  105. ->method('getNodeForPath')
  106. ->with('/bar/foo')
  107. ->willReturn($calendarHome);
  108. $reports = $this->plugin->getSupportedReportSet('/bar/foo');
  109. $this->assertEquals([
  110. '{http://nextcloud.com/ns}calendar-search'
  111. ], $reports);
  112. }
  113. }