ImageExportPluginTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CardDAV;
  8. use OCA\DAV\CardDAV\AddressBook;
  9. use OCA\DAV\CardDAV\ImageExportPlugin;
  10. use OCA\DAV\CardDAV\PhotoCache;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\SimpleFS\ISimpleFile;
  13. use Sabre\CardDAV\Card;
  14. use Sabre\DAV\Node;
  15. use Sabre\DAV\Server;
  16. use Sabre\DAV\Tree;
  17. use Sabre\HTTP\RequestInterface;
  18. use Sabre\HTTP\ResponseInterface;
  19. use Test\TestCase;
  20. class ImageExportPluginTest extends TestCase {
  21. /** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
  22. private $response;
  23. /** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
  24. private $request;
  25. /** @var ImageExportPlugin|\PHPUnit\Framework\MockObject\MockObject */
  26. private $plugin;
  27. /** @var Server */
  28. private $server;
  29. /** @var Tree|\PHPUnit\Framework\MockObject\MockObject */
  30. private $tree;
  31. /** @var PhotoCache|\PHPUnit\Framework\MockObject\MockObject */
  32. private $cache;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->request = $this->createMock(RequestInterface::class);
  36. $this->response = $this->createMock(ResponseInterface::class);
  37. $this->server = $this->createMock(Server::class);
  38. $this->tree = $this->createMock(Tree::class);
  39. $this->server->tree = $this->tree;
  40. $this->cache = $this->createMock(PhotoCache::class);
  41. $this->plugin = $this->getMockBuilder(ImageExportPlugin::class)
  42. ->setMethods(['getPhoto'])
  43. ->setConstructorArgs([$this->cache])
  44. ->getMock();
  45. $this->plugin->initialize($this->server);
  46. }
  47. /**
  48. * @dataProvider providesQueryParams
  49. * @param $param
  50. */
  51. public function testQueryParams($param): void {
  52. $this->request->expects($this->once())->method('getQueryParameters')->willReturn($param);
  53. $result = $this->plugin->httpGet($this->request, $this->response);
  54. $this->assertTrue($result);
  55. }
  56. public function providesQueryParams() {
  57. return [
  58. [[]],
  59. [['1']],
  60. [['foo' => 'bar']],
  61. ];
  62. }
  63. public function testNoCard(): void {
  64. $this->request->method('getQueryParameters')
  65. ->willReturn([
  66. 'photo'
  67. ]);
  68. $this->request->method('getPath')
  69. ->willReturn('user/book/card');
  70. $node = $this->createMock(Node::class);
  71. $this->tree->method('getNodeForPath')
  72. ->with('user/book/card')
  73. ->willReturn($node);
  74. $result = $this->plugin->httpGet($this->request, $this->response);
  75. $this->assertTrue($result);
  76. }
  77. public function dataTestCard() {
  78. return [
  79. [null, false],
  80. [null, true],
  81. [32, false],
  82. [32, true],
  83. ];
  84. }
  85. /**
  86. * @dataProvider dataTestCard
  87. *
  88. * @param $size
  89. * @param bool $photo
  90. */
  91. public function testCard($size, $photo): void {
  92. $query = ['photo' => null];
  93. if ($size !== null) {
  94. $query['size'] = $size;
  95. }
  96. $this->request->method('getQueryParameters')
  97. ->willReturn($query);
  98. $this->request->method('getPath')
  99. ->willReturn('user/book/card');
  100. $card = $this->createMock(Card::class);
  101. $card->method('getETag')
  102. ->willReturn('"myEtag"');
  103. $card->method('getName')
  104. ->willReturn('card');
  105. $book = $this->createMock(AddressBook::class);
  106. $book->method('getResourceId')
  107. ->willReturn(1);
  108. $this->tree->method('getNodeForPath')
  109. ->willReturnCallback(function ($path) use ($card, $book) {
  110. if ($path === 'user/book/card') {
  111. return $card;
  112. } elseif ($path === 'user/book') {
  113. return $book;
  114. }
  115. $this->fail();
  116. });
  117. $size = $size === null ? -1 : $size;
  118. if ($photo) {
  119. $file = $this->createMock(ISimpleFile::class);
  120. $file->method('getMimeType')
  121. ->willReturn('image/jpeg');
  122. $file->method('getContent')
  123. ->willReturn('imgdata');
  124. $this->cache->method('get')
  125. ->with(1, 'card', $size, $card)
  126. ->willReturn($file);
  127. $this->response->expects($this->exactly(4))
  128. ->method('setHeader')
  129. ->withConsecutive(
  130. ['Cache-Control', 'private, max-age=3600, must-revalidate'],
  131. ['Etag', '"myEtag"'],
  132. ['Content-Type', 'image/jpeg'],
  133. ['Content-Disposition', 'attachment; filename=card.jpg'],
  134. );
  135. $this->response->expects($this->once())
  136. ->method('setStatus')
  137. ->with(200);
  138. $this->response->expects($this->once())
  139. ->method('setBody')
  140. ->with('imgdata');
  141. } else {
  142. $this->response->expects($this->exactly(2))
  143. ->method('setHeader')
  144. ->withConsecutive(
  145. ['Cache-Control', 'private, max-age=3600, must-revalidate'],
  146. ['Etag', '"myEtag"'],
  147. );
  148. $this->cache->method('get')
  149. ->with(1, 'card', $size, $card)
  150. ->willThrowException(new NotFoundException());
  151. $this->response->expects($this->once())
  152. ->method('setStatus')
  153. ->with(404);
  154. }
  155. $result = $this->plugin->httpGet($this->request, $this->response);
  156. $this->assertFalse($result);
  157. }
  158. }