CalDAVRemoveEmptyValueTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Tests\Unit\DAV\Migration;
  24. use OCA\DAV\CalDAV\CalDavBackend;
  25. use OCA\DAV\Migration\CalDAVRemoveEmptyValue;
  26. use OCP\ILogger;
  27. use OCP\Migration\IOutput;
  28. use Sabre\VObject\InvalidDataException;
  29. use Test\TestCase;
  30. /**
  31. * Class CalDAVRemoveEmptyValueTest
  32. *
  33. * @package OCA\DAV\Tests\Unit\DAV\Migration
  34. * @group DB
  35. */
  36. class CalDAVRemoveEmptyValueTest extends TestCase {
  37. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  38. private $logger;
  39. /** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject */
  40. private $backend;
  41. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject */
  42. private $output;
  43. /** @var string */
  44. private $invalid = 'BEGIN:VCALENDAR
  45. VERSION:2.0
  46. PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN
  47. CALSCALE:GREGORIAN
  48. BEGIN:VEVENT
  49. TRANSP:OPAQUE
  50. DTEND;VALUE=:20151223T223000Z
  51. LAST-MODIFIED:20151214T091032Z
  52. ORGANIZER;CN="User 1":mailto:user1@example.com
  53. UID:1234567890@example.com
  54. DTSTAMP:20151214T091032Z
  55. STATUS:CONFIRMED
  56. SEQUENCE:0
  57. SUMMARY:Ein Geburtstag
  58. DTSTART;VALUE=:20151223T173000Z
  59. X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
  60. CREATED;VALUE=:20151214T091032Z
  61. END:VEVENT
  62. END:VCALENDAR';
  63. /** @var string */
  64. private $valid = 'BEGIN:VCALENDAR
  65. VERSION:2.0
  66. PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN
  67. CALSCALE:GREGORIAN
  68. BEGIN:VEVENT
  69. TRANSP:OPAQUE
  70. DTEND:20151223T223000Z
  71. LAST-MODIFIED:20151214T091032Z
  72. ORGANIZER;CN="User 1":mailto:user1@example.com
  73. UID:1234567890@example.com
  74. DTSTAMP:20151214T091032Z
  75. STATUS:CONFIRMED
  76. SEQUENCE:0
  77. SUMMARY:Ein Geburtstag
  78. DTSTART:20151223T173000Z
  79. X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
  80. CREATED:20151214T091032Z
  81. END:VEVENT
  82. END:VCALENDAR';
  83. protected function setUp(): void {
  84. parent::setUp();
  85. $this->logger = $this->createMock(ILogger::class);
  86. $this->backend = $this->createMock(CalDavBackend::class);
  87. $this->output = $this->createMock(IOutput::class);
  88. }
  89. public function testRunAllValid() {
  90. /** @var CalDAVRemoveEmptyValue|\PHPUnit_Framework_MockObject_MockObject $step */
  91. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  92. ->setConstructorArgs([
  93. \OC::$server->getDatabaseConnection(),
  94. $this->backend,
  95. $this->logger
  96. ])
  97. ->setMethods(['getInvalidObjects'])
  98. ->getMock();
  99. $step->expects($this->once())
  100. ->method('getInvalidObjects')
  101. ->willReturn([]);
  102. $this->output->expects($this->once())
  103. ->method('startProgress')
  104. ->with(0);
  105. $this->output->expects($this->once())
  106. ->method('finishProgress');
  107. $step->run($this->output);
  108. }
  109. public function testRunInvalid() {
  110. /** @var CalDAVRemoveEmptyValue|\PHPUnit_Framework_MockObject_MockObject $step */
  111. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  112. ->setConstructorArgs([
  113. \OC::$server->getDatabaseConnection(),
  114. $this->backend,
  115. $this->logger
  116. ])
  117. ->setMethods(['getInvalidObjects'])
  118. ->getMock();
  119. $step->expects($this->once())
  120. ->method('getInvalidObjects')
  121. ->willReturn([
  122. ['calendarid' => '42', 'uri' => 'myuri'],
  123. ]);
  124. $this->output->expects($this->once())
  125. ->method('startProgress')
  126. ->with(1);
  127. $this->output->expects($this->once())
  128. ->method('finishProgress');
  129. $this->backend->expects($this->exactly(1))
  130. ->method('getCalendarObject')
  131. ->with(42, 'myuri')
  132. ->willReturn([
  133. 'calendardata' => $this->invalid
  134. ]);
  135. $this->output->expects($this->exactly(1))
  136. ->method('advance');
  137. $this->backend->expects($this->exactly(1))
  138. ->method('getDenormalizedData')
  139. ->with($this->valid);
  140. $this->backend->expects($this->exactly(1))
  141. ->method('updateCalendarObject')
  142. ->with(42, 'myuri', $this->valid);
  143. $step->run($this->output);
  144. }
  145. public function testRunValid() {
  146. /** @var CalDAVRemoveEmptyValue|\PHPUnit_Framework_MockObject_MockObject $step */
  147. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  148. ->setConstructorArgs([
  149. \OC::$server->getDatabaseConnection(),
  150. $this->backend,
  151. $this->logger
  152. ])
  153. ->setMethods(['getInvalidObjects'])
  154. ->getMock();
  155. $step->expects($this->once())
  156. ->method('getInvalidObjects')
  157. ->willReturn([
  158. ['calendarid' => '42', 'uri' => 'myuri'],
  159. ]);
  160. $this->output->expects($this->once())
  161. ->method('startProgress')
  162. ->with(1);
  163. $this->output->expects($this->once())
  164. ->method('finishProgress');
  165. $this->backend->expects($this->exactly(1))
  166. ->method('getCalendarObject')
  167. ->with(42, 'myuri')
  168. ->willReturn([
  169. 'calendardata' => $this->valid
  170. ]);
  171. $this->output->expects($this->never())
  172. ->method('advance');
  173. $this->backend->expects($this->never())
  174. ->method('getDenormalizedData');
  175. $this->backend->expects($this->never())
  176. ->method('updateCalendarObject');
  177. $step->run($this->output);
  178. }
  179. public function testRunStillInvalid() {
  180. /** @var CalDAVRemoveEmptyValue|\PHPUnit_Framework_MockObject_MockObject $step */
  181. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  182. ->setConstructorArgs([
  183. \OC::$server->getDatabaseConnection(),
  184. $this->backend,
  185. $this->logger
  186. ])
  187. ->setMethods(['getInvalidObjects'])
  188. ->getMock();
  189. $step->expects($this->once())
  190. ->method('getInvalidObjects')
  191. ->willReturn([
  192. ['calendarid' => '42', 'uri' => 'myuri'],
  193. ]);
  194. $this->output->expects($this->once())
  195. ->method('startProgress')
  196. ->with(1);
  197. $this->output->expects($this->once())
  198. ->method('finishProgress');
  199. $this->backend->expects($this->exactly(1))
  200. ->method('getCalendarObject')
  201. ->with(42, 'myuri')
  202. ->willReturn([
  203. 'calendardata' => $this->invalid
  204. ]);
  205. $this->output->expects($this->exactly(1))
  206. ->method('advance');
  207. $this->backend->expects($this->exactly(1))
  208. ->method('getDenormalizedData')
  209. ->with($this->valid)
  210. ->willThrowException(new InvalidDataException());
  211. $this->backend->expects($this->never())
  212. ->method('updateCalendarObject');
  213. $step->run($this->output);
  214. }
  215. }