SearchPluginTest.php 3.8 KB

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