You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.9 KiB

  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->getMockBuilder('OCA\DAV\CardDAV\ImageExportPlugin')
  52. ->setMethods(['getPhoto'])
  53. ->setConstructorArgs([$this->logger])
  54. ->getMock();
  55. $this->plugin->initialize($this->server);
  56. }
  57. /**
  58. * @dataProvider providesQueryParams
  59. * @param $param
  60. */
  61. public function testQueryParams($param) {
  62. $this->request->expects($this->once())->method('getQueryParameters')->willReturn($param);
  63. $result = $this->plugin->httpGet($this->request, $this->response);
  64. $this->assertTrue($result);
  65. }
  66. public function providesQueryParams() {
  67. return [
  68. [[]],
  69. [['1']],
  70. [['foo' => 'bar']],
  71. ];
  72. }
  73. public function testNotACard() {
  74. $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]);
  75. $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt');
  76. $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn(null);
  77. $result = $this->plugin->httpGet($this->request, $this->response);
  78. $this->assertTrue($result);
  79. }
  80. /**
  81. * @dataProvider providesCardWithOrWithoutPhoto
  82. * @param bool $expected
  83. * @param array $getPhotoResult
  84. */
  85. public function testCardWithOrWithoutPhoto($expected, $getPhotoResult) {
  86. $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]);
  87. $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt');
  88. $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock();
  89. $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn($card);
  90. $this->plugin->expects($this->once())->method('getPhoto')->willReturn($getPhotoResult);
  91. if (!$expected) {
  92. $this->response->expects($this->once())->method('setHeader');
  93. $this->response->expects($this->once())->method('setStatus');
  94. $this->response->expects($this->once())->method('setBody');
  95. }
  96. $result = $this->plugin->httpGet($this->request, $this->response);
  97. $this->assertEquals($expected, $result);
  98. }
  99. public function providesCardWithOrWithoutPhoto() {
  100. return [
  101. [true, null],
  102. [false, ['Content-Type' => 'image/jpeg', 'body' => '1234']],
  103. ];
  104. }
  105. /**
  106. * @dataProvider providesPhotoData
  107. * @param $expected
  108. * @param $cardData
  109. */
  110. public function testGetPhoto($expected, $cardData) {
  111. /** @var Card | \PHPUnit_Framework_MockObject_MockObject $card */
  112. $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock();
  113. $card->expects($this->once())->method('get')->willReturn($cardData);
  114. $this->plugin = new ImageExportPlugin($this->logger);
  115. $this->plugin->initialize($this->server);
  116. $result = $this->plugin->getPhoto($card);
  117. $this->assertEquals($expected, $result);
  118. }
  119. public function providesPhotoData() {
  120. return [
  121. 'empty vcard' => [false, ''],
  122. '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"],
  123. '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"],
  124. 'vcard 3 with PHOTO URL' => [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\nPHOTO;TYPE=JPEG;VALUE=URI:http://example.com/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;base64,MTIzNDU=\r\nEND:VCARD\r\n"],
  126. 'vcard 4 with PHOTO URL' => [false, "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. }