ImageExportPluginTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CardDAV;
  26. use OCA\DAV\CardDAV\AddressBook;
  27. use OCA\DAV\CardDAV\ImageExportPlugin;
  28. use OCA\DAV\CardDAV\PhotoCache;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use Sabre\CardDAV\Card;
  32. use Sabre\DAV\Node;
  33. use Sabre\DAV\Server;
  34. use Sabre\DAV\Tree;
  35. use Sabre\HTTP\RequestInterface;
  36. use Sabre\HTTP\ResponseInterface;
  37. use Test\TestCase;
  38. class ImageExportPluginTest extends TestCase {
  39. /** @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  40. private $response;
  41. /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  42. private $request;
  43. /** @var ImageExportPlugin|\PHPUnit_Framework_MockObject_MockObject */
  44. private $plugin;
  45. /** @var Server */
  46. private $server;
  47. /** @var Tree|\PHPUnit_Framework_MockObject_MockObject */
  48. private $tree;
  49. /** @var PhotoCache|\PHPUnit_Framework_MockObject_MockObject */
  50. private $cache;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->request = $this->createMock(RequestInterface::class);
  54. $this->response = $this->createMock(ResponseInterface::class);
  55. $this->server = $this->createMock(Server::class);
  56. $this->tree = $this->createMock(Tree::class);
  57. $this->server->tree = $this->tree;
  58. $this->cache = $this->createMock(PhotoCache::class);
  59. $this->plugin = $this->getMockBuilder(ImageExportPlugin::class)
  60. ->setMethods(['getPhoto'])
  61. ->setConstructorArgs([$this->cache])
  62. ->getMock();
  63. $this->plugin->initialize($this->server);
  64. }
  65. /**
  66. * @dataProvider providesQueryParams
  67. * @param $param
  68. */
  69. public function testQueryParams($param) {
  70. $this->request->expects($this->once())->method('getQueryParameters')->willReturn($param);
  71. $result = $this->plugin->httpGet($this->request, $this->response);
  72. $this->assertTrue($result);
  73. }
  74. public function providesQueryParams() {
  75. return [
  76. [[]],
  77. [['1']],
  78. [['foo' => 'bar']],
  79. ];
  80. }
  81. public function testNoCard() {
  82. $this->request->method('getQueryParameters')
  83. ->willReturn([
  84. 'photo'
  85. ]);
  86. $this->request->method('getPath')
  87. ->willReturn('user/book/card');
  88. $node = $this->createMock(Node::class);
  89. $this->tree->method('getNodeForPath')
  90. ->with('user/book/card')
  91. ->willReturn($node);
  92. $result = $this->plugin->httpGet($this->request, $this->response);
  93. $this->assertTrue($result);
  94. }
  95. public function dataTestCard() {
  96. return [
  97. [null, false],
  98. [null, true],
  99. [32, false],
  100. [32, true],
  101. ];
  102. }
  103. /**
  104. * @dataProvider dataTestCard
  105. *
  106. * @param $size
  107. * @param bool $photo
  108. */
  109. public function testCard($size, $photo) {
  110. $query = ['photo' => null];
  111. if ($size !== null) {
  112. $query['size'] = $size;
  113. }
  114. $this->request->method('getQueryParameters')
  115. ->willReturn($query);
  116. $this->request->method('getPath')
  117. ->willReturn('user/book/card');
  118. $card = $this->createMock(Card::class);
  119. $card->method('getETag')
  120. ->willReturn('"myEtag"');
  121. $card->method('getName')
  122. ->willReturn('card');
  123. $book = $this->createMock(AddressBook::class);
  124. $book->method('getResourceId')
  125. ->willReturn(1);
  126. $this->tree->method('getNodeForPath')
  127. ->willReturnCallback(function ($path) use ($card, $book) {
  128. if ($path === 'user/book/card') {
  129. return $card;
  130. } elseif ($path === 'user/book') {
  131. return $book;
  132. }
  133. $this->fail();
  134. });
  135. $this->response->expects($this->at(0))
  136. ->method('setHeader')
  137. ->with('Cache-Control', 'private, max-age=3600, must-revalidate');
  138. $this->response->expects($this->at(1))
  139. ->method('setHeader')
  140. ->with('Etag', '"myEtag"');
  141. $this->response->expects($this->at(2))
  142. ->method('setHeader')
  143. ->with('Pragma', 'public');
  144. $size = $size === null ? -1 : $size;
  145. if ($photo) {
  146. $file = $this->createMock(ISimpleFile::class);
  147. $file->method('getMimeType')
  148. ->willReturn('imgtype');
  149. $file->method('getContent')
  150. ->willReturn('imgdata');
  151. $this->cache->method('get')
  152. ->with(1, 'card', $size, $card)
  153. ->willReturn($file);
  154. $this->response->expects($this->at(3))
  155. ->method('setHeader')
  156. ->with('Content-Type', 'imgtype');
  157. $this->response->expects($this->at(4))
  158. ->method('setHeader')
  159. ->with('Content-Disposition', 'attachment');
  160. $this->response->expects($this->once())
  161. ->method('setStatus')
  162. ->with(200);
  163. $this->response->expects($this->once())
  164. ->method('setBody')
  165. ->with('imgdata');
  166. } else {
  167. $this->cache->method('get')
  168. ->with(1, 'card', $size, $card)
  169. ->willThrowException(new NotFoundException());
  170. $this->response->expects($this->once())
  171. ->method('setStatus')
  172. ->with(404);
  173. }
  174. $result = $this->plugin->httpGet($this->request, $this->response);
  175. $this->assertFalse($result);
  176. }
  177. }