ImageExportPluginTest.php 5.4 KB

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