ImageExportPluginTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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): void {
  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(): void {
  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): void {
  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. $size = $size === null ? -1 : $size;
  137. if ($photo) {
  138. $file = $this->createMock(ISimpleFile::class);
  139. $file->method('getMimeType')
  140. ->willReturn('image/jpeg');
  141. $file->method('getContent')
  142. ->willReturn('imgdata');
  143. $this->cache->method('get')
  144. ->with(1, 'card', $size, $card)
  145. ->willReturn($file);
  146. $this->response->expects($this->exactly(4))
  147. ->method('setHeader')
  148. ->withConsecutive(
  149. ['Cache-Control', 'private, max-age=3600, must-revalidate'],
  150. ['Etag', '"myEtag"'],
  151. ['Content-Type', 'image/jpeg'],
  152. ['Content-Disposition', 'attachment; filename=card.jpg'],
  153. );
  154. $this->response->expects($this->once())
  155. ->method('setStatus')
  156. ->with(200);
  157. $this->response->expects($this->once())
  158. ->method('setBody')
  159. ->with('imgdata');
  160. } else {
  161. $this->response->expects($this->exactly(2))
  162. ->method('setHeader')
  163. ->withConsecutive(
  164. ['Cache-Control', 'private, max-age=3600, must-revalidate'],
  165. ['Etag', '"myEtag"'],
  166. );
  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. }