PluginTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\WebcalCaching;
  7. use OCA\DAV\CalDAV\WebcalCaching\Plugin;
  8. use OCP\IRequest;
  9. use Sabre\DAV\Server;
  10. use Sabre\DAV\Tree;
  11. use Sabre\HTTP\Request;
  12. use Sabre\HTTP\Response;
  13. class PluginTest extends \Test\TestCase {
  14. public function testDisabled(): void {
  15. $request = $this->createMock(IRequest::class);
  16. $request->expects($this->once())
  17. ->method('isUserAgent')
  18. ->with(Plugin::ENABLE_FOR_CLIENTS)
  19. ->willReturn(false);
  20. $request->expects($this->once())
  21. ->method('getHeader')
  22. ->with('X-NC-CalDAV-Webcal-Caching')
  23. ->willReturn('');
  24. $plugin = new Plugin($request);
  25. $this->assertEquals(false, $plugin->isCachingEnabledForThisRequest());
  26. }
  27. public function testEnabledUserAgent(): void {
  28. $request = $this->createMock(IRequest::class);
  29. $request->expects($this->once())
  30. ->method('isUserAgent')
  31. ->with(Plugin::ENABLE_FOR_CLIENTS)
  32. ->willReturn(true);
  33. $request->expects($this->once())
  34. ->method('getHeader')
  35. ->with('X-NC-CalDAV-Webcal-Caching')
  36. ->willReturn('');
  37. $request->expects($this->once())
  38. ->method('getMethod')
  39. ->willReturn('REPORT');
  40. $request->expects($this->never())
  41. ->method('getParams');
  42. $plugin = new Plugin($request);
  43. $this->assertEquals(true, $plugin->isCachingEnabledForThisRequest());
  44. }
  45. public function testEnabledWebcalCachingHeader(): void {
  46. $request = $this->createMock(IRequest::class);
  47. $request->expects($this->once())
  48. ->method('isUserAgent')
  49. ->with(Plugin::ENABLE_FOR_CLIENTS)
  50. ->willReturn(false);
  51. $request->expects($this->once())
  52. ->method('getHeader')
  53. ->with('X-NC-CalDAV-Webcal-Caching')
  54. ->willReturn('On');
  55. $request->expects($this->once())
  56. ->method('getMethod')
  57. ->willReturn('REPORT');
  58. $request->expects($this->never())
  59. ->method('getParams');
  60. $plugin = new Plugin($request);
  61. $this->assertEquals(true, $plugin->isCachingEnabledForThisRequest());
  62. }
  63. public function testEnabledExportRequest(): void {
  64. $request = $this->createMock(IRequest::class);
  65. $request->expects($this->once())
  66. ->method('isUserAgent')
  67. ->with(Plugin::ENABLE_FOR_CLIENTS)
  68. ->willReturn(false);
  69. $request->expects($this->once())
  70. ->method('getHeader')
  71. ->with('X-NC-CalDAV-Webcal-Caching')
  72. ->willReturn('');
  73. $request->expects($this->once())
  74. ->method('getMethod')
  75. ->willReturn('GET');
  76. $request->expects($this->once())
  77. ->method('getParams')
  78. ->willReturn(['export' => '']);
  79. $plugin = new Plugin($request);
  80. $this->assertEquals(true, $plugin->isCachingEnabledForThisRequest());
  81. }
  82. public function testSkipNonCalendarRequest(): void {
  83. $request = $this->createMock(IRequest::class);
  84. $request->expects($this->once())
  85. ->method('isUserAgent')
  86. ->with(Plugin::ENABLE_FOR_CLIENTS)
  87. ->willReturn(false);
  88. $request->expects($this->once())
  89. ->method('getHeader')
  90. ->with('X-NC-CalDAV-Webcal-Caching')
  91. ->willReturn('On');
  92. $sabreRequest = new Request('REPORT', '/remote.php/dav/principals/users/admin/');
  93. $sabreRequest->setBaseUrl('/remote.php/dav/');
  94. $tree = $this->createMock(Tree::class);
  95. $tree->expects($this->never())
  96. ->method('getNodeForPath');
  97. $server = new Server($tree);
  98. $plugin = new Plugin($request);
  99. $plugin->initialize($server);
  100. $plugin->beforeMethod($sabreRequest, new Response());
  101. }
  102. public function testProcessCalendarRequest(): void {
  103. $request = $this->createMock(IRequest::class);
  104. $request->expects($this->once())
  105. ->method('isUserAgent')
  106. ->with(Plugin::ENABLE_FOR_CLIENTS)
  107. ->willReturn(false);
  108. $request->expects($this->once())
  109. ->method('getHeader')
  110. ->with('X-NC-CalDAV-Webcal-Caching')
  111. ->willReturn('On');
  112. $sabreRequest = new Request('REPORT', '/remote.php/dav/calendars/admin/personal/');
  113. $sabreRequest->setBaseUrl('/remote.php/dav/');
  114. $tree = $this->createMock(Tree::class);
  115. $tree->expects($this->once())
  116. ->method('getNodeForPath');
  117. $server = new Server($tree);
  118. $plugin = new Plugin($request);
  119. $plugin->initialize($server);
  120. $plugin->beforeMethod($sabreRequest, new Response());
  121. }
  122. }