CalDAVRemoveEmptyValueTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\Unit\DAV\Migration;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCA\DAV\Migration\CalDAVRemoveEmptyValue;
  9. use OCP\Migration\IOutput;
  10. use Psr\Log\LoggerInterface;
  11. use Sabre\VObject\InvalidDataException;
  12. use Test\TestCase;
  13. /**
  14. * Class CalDAVRemoveEmptyValueTest
  15. *
  16. * @package OCA\DAV\Tests\Unit\DAV\Migration
  17. * @group DB
  18. */
  19. class CalDAVRemoveEmptyValueTest extends TestCase {
  20. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  21. private $logger;
  22. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject */
  23. private $backend;
  24. /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject */
  25. private $output;
  26. /** @var string */
  27. private $invalid = 'BEGIN:VCALENDAR
  28. VERSION:2.0
  29. PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN
  30. CALSCALE:GREGORIAN
  31. BEGIN:VEVENT
  32. TRANSP:OPAQUE
  33. DTEND;VALUE=:20151223T223000Z
  34. LAST-MODIFIED:20151214T091032Z
  35. ORGANIZER;CN="User 1":mailto:user1@example.com
  36. UID:1234567890@example.com
  37. DTSTAMP:20151214T091032Z
  38. STATUS:CONFIRMED
  39. SEQUENCE:0
  40. SUMMARY:Ein Geburtstag
  41. DTSTART;VALUE=:20151223T173000Z
  42. X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
  43. CREATED;VALUE=:20151214T091032Z
  44. END:VEVENT
  45. END:VCALENDAR';
  46. /** @var string */
  47. private $valid = 'BEGIN:VCALENDAR
  48. VERSION:2.0
  49. PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN
  50. CALSCALE:GREGORIAN
  51. BEGIN:VEVENT
  52. TRANSP:OPAQUE
  53. DTEND:20151223T223000Z
  54. LAST-MODIFIED:20151214T091032Z
  55. ORGANIZER;CN="User 1":mailto:user1@example.com
  56. UID:1234567890@example.com
  57. DTSTAMP:20151214T091032Z
  58. STATUS:CONFIRMED
  59. SEQUENCE:0
  60. SUMMARY:Ein Geburtstag
  61. DTSTART:20151223T173000Z
  62. X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
  63. CREATED:20151214T091032Z
  64. END:VEVENT
  65. END:VCALENDAR';
  66. protected function setUp(): void {
  67. parent::setUp();
  68. $this->logger = $this->createMock(LoggerInterface::class);
  69. $this->backend = $this->createMock(CalDavBackend::class);
  70. $this->output = $this->createMock(IOutput::class);
  71. }
  72. public function testRunAllValid(): void {
  73. /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */
  74. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  75. ->setConstructorArgs([
  76. \OC::$server->getDatabaseConnection(),
  77. $this->backend,
  78. $this->logger
  79. ])
  80. ->setMethods(['getInvalidObjects'])
  81. ->getMock();
  82. $step->expects($this->once())
  83. ->method('getInvalidObjects')
  84. ->willReturn([]);
  85. $this->output->expects($this->once())
  86. ->method('startProgress')
  87. ->with(0);
  88. $this->output->expects($this->once())
  89. ->method('finishProgress');
  90. $step->run($this->output);
  91. }
  92. public function testRunInvalid(): void {
  93. /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */
  94. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  95. ->setConstructorArgs([
  96. \OC::$server->getDatabaseConnection(),
  97. $this->backend,
  98. $this->logger
  99. ])
  100. ->setMethods(['getInvalidObjects'])
  101. ->getMock();
  102. $step->expects($this->once())
  103. ->method('getInvalidObjects')
  104. ->willReturn([
  105. ['calendarid' => '42', 'uri' => 'myuri'],
  106. ]);
  107. $this->output->expects($this->once())
  108. ->method('startProgress')
  109. ->with(1);
  110. $this->output->expects($this->once())
  111. ->method('finishProgress');
  112. $this->backend->expects($this->exactly(1))
  113. ->method('getCalendarObject')
  114. ->with(42, 'myuri')
  115. ->willReturn([
  116. 'calendardata' => $this->invalid
  117. ]);
  118. $this->output->expects($this->exactly(1))
  119. ->method('advance');
  120. $this->backend->expects($this->exactly(1))
  121. ->method('getDenormalizedData')
  122. ->with($this->valid);
  123. $this->backend->expects($this->exactly(1))
  124. ->method('updateCalendarObject')
  125. ->with(42, 'myuri', $this->valid);
  126. $step->run($this->output);
  127. }
  128. public function testRunValid(): void {
  129. /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */
  130. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  131. ->setConstructorArgs([
  132. \OC::$server->getDatabaseConnection(),
  133. $this->backend,
  134. $this->logger
  135. ])
  136. ->setMethods(['getInvalidObjects'])
  137. ->getMock();
  138. $step->expects($this->once())
  139. ->method('getInvalidObjects')
  140. ->willReturn([
  141. ['calendarid' => '42', 'uri' => 'myuri'],
  142. ]);
  143. $this->output->expects($this->once())
  144. ->method('startProgress')
  145. ->with(1);
  146. $this->output->expects($this->once())
  147. ->method('finishProgress');
  148. $this->backend->expects($this->exactly(1))
  149. ->method('getCalendarObject')
  150. ->with(42, 'myuri')
  151. ->willReturn([
  152. 'calendardata' => $this->valid
  153. ]);
  154. $this->output->expects($this->never())
  155. ->method('advance');
  156. $this->backend->expects($this->never())
  157. ->method('getDenormalizedData');
  158. $this->backend->expects($this->never())
  159. ->method('updateCalendarObject');
  160. $step->run($this->output);
  161. }
  162. public function testRunStillInvalid(): void {
  163. /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */
  164. $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class)
  165. ->setConstructorArgs([
  166. \OC::$server->getDatabaseConnection(),
  167. $this->backend,
  168. $this->logger
  169. ])
  170. ->setMethods(['getInvalidObjects'])
  171. ->getMock();
  172. $step->expects($this->once())
  173. ->method('getInvalidObjects')
  174. ->willReturn([
  175. ['calendarid' => '42', 'uri' => 'myuri'],
  176. ]);
  177. $this->output->expects($this->once())
  178. ->method('startProgress')
  179. ->with(1);
  180. $this->output->expects($this->once())
  181. ->method('finishProgress');
  182. $this->backend->expects($this->exactly(1))
  183. ->method('getCalendarObject')
  184. ->with(42, 'myuri')
  185. ->willReturn([
  186. 'calendardata' => $this->invalid
  187. ]);
  188. $this->output->expects($this->exactly(1))
  189. ->method('advance');
  190. $this->backend->expects($this->exactly(1))
  191. ->method('getDenormalizedData')
  192. ->with($this->valid)
  193. ->willThrowException(new InvalidDataException());
  194. $this->backend->expects($this->never())
  195. ->method('updateCalendarObject');
  196. $step->run($this->output);
  197. }
  198. }