ImageExportPluginTest.php 5.5 KB

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