ImageExportPluginTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\DAV\Tests\unit\CardDAV;
  22. use OCA\DAV\CardDAV\ImageExportPlugin;
  23. use OCP\ILogger;
  24. use Sabre\CardDAV\Card;
  25. use Sabre\DAV\Server;
  26. use Sabre\DAV\Tree;
  27. use Sabre\HTTP\RequestInterface;
  28. use Sabre\HTTP\ResponseInterface;
  29. use Test\TestCase;
  30. class ImageExportPluginTest extends TestCase {
  31. /** @var ResponseInterface | \PHPUnit_Framework_MockObject_MockObject */
  32. private $response;
  33. /** @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject */
  34. private $request;
  35. /** @var ImageExportPlugin | \PHPUnit_Framework_MockObject_MockObject */
  36. private $plugin;
  37. /** @var Server */
  38. private $server;
  39. /** @var Tree | \PHPUnit_Framework_MockObject_MockObject */
  40. private $tree;
  41. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
  42. private $logger;
  43. function setUp() {
  44. parent::setUp();
  45. $this->request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')->getMock();
  46. $this->response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')->getMock();
  47. $this->server = $this->getMockBuilder('Sabre\DAV\Server')->getMock();
  48. $this->tree = $this->getMockBuilder('Sabre\DAV\Tree')->disableOriginalConstructor()->getMock();
  49. $this->server->tree = $this->tree;
  50. $this->logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
  51. $this->plugin = $this->getMock('OCA\DAV\CardDAV\ImageExportPlugin', ['getPhoto'], [$this->logger]);
  52. $this->plugin->initialize($this->server);
  53. }
  54. /**
  55. * @dataProvider providesQueryParams
  56. * @param $param
  57. */
  58. public function testQueryParams($param) {
  59. $this->request->expects($this->once())->method('getQueryParameters')->willReturn($param);
  60. $result = $this->plugin->httpGet($this->request, $this->response);
  61. $this->assertTrue($result);
  62. }
  63. public function providesQueryParams() {
  64. return [
  65. [[]],
  66. [['1']],
  67. [['foo' => 'bar']],
  68. ];
  69. }
  70. public function testNotACard() {
  71. $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]);
  72. $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt');
  73. $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn(null);
  74. $result = $this->plugin->httpGet($this->request, $this->response);
  75. $this->assertTrue($result);
  76. }
  77. /**
  78. * @dataProvider providesCardWithOrWithoutPhoto
  79. * @param bool $expected
  80. * @param array $getPhotoResult
  81. */
  82. public function testCardWithOrWithoutPhoto($expected, $getPhotoResult) {
  83. $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]);
  84. $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt');
  85. $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock();
  86. $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn($card);
  87. $this->plugin->expects($this->once())->method('getPhoto')->willReturn($getPhotoResult);
  88. if (!$expected) {
  89. $this->response->expects($this->once())->method('setHeader');
  90. $this->response->expects($this->once())->method('setStatus');
  91. $this->response->expects($this->once())->method('setBody');
  92. }
  93. $result = $this->plugin->httpGet($this->request, $this->response);
  94. $this->assertEquals($expected, $result);
  95. }
  96. public function providesCardWithOrWithoutPhoto() {
  97. return [
  98. [true, null],
  99. [false, ['Content-Type' => 'image/jpeg', 'body' => '1234']],
  100. ];
  101. }
  102. /**
  103. * @dataProvider providesPhotoData
  104. * @param $expected
  105. * @param $cardData
  106. */
  107. public function testGetPhoto($expected, $cardData) {
  108. /** @var Card | \PHPUnit_Framework_MockObject_MockObject $card */
  109. $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock();
  110. $card->expects($this->once())->method('get')->willReturn($cardData);
  111. $this->plugin = new ImageExportPlugin($this->logger);
  112. $this->plugin->initialize($this->server);
  113. $result = $this->plugin->getPhoto($card);
  114. $this->assertEquals($expected, $result);
  115. }
  116. public function providesPhotoData() {
  117. return [
  118. 'empty vcard' => [false, ''],
  119. 'vcard without PHOTO' => [false, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n"],
  120. 'vcard 3 with PHOTO' => [['Content-Type' => 'image/jpeg', 'body' => '12345'], "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU=\r\nEND:VCARD\r\n"],
  121. //
  122. // TODO: these three below are not working - needs debugging
  123. //
  124. //'vcard 3 with PHOTO URL' => [['Content-Type' => 'image/jpeg', 'body' => '12345'], "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;TYPE=JPEG:http://example.org/photo.jpg\r\nEND:VCARD\r\n"],
  125. //'vcard 4 with PHOTO' => [['Content-Type' => 'image/jpeg', 'body' => '12345'], "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO:data:image/jpeg;MTIzNDU=\r\nEND:VCARD\r\n"],
  126. 'vcard 4 with PHOTO URL' => [['Content-Type' => 'image/jpeg', 'body' => 'http://example.org/photo.jpg'], "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject 3.5.0//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;MEDIATYPE=image/jpeg:http://example.org/photo.jpg\r\nEND:VCARD\r\n"],
  127. ];
  128. }
  129. }