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.

CalDAVRemoveEmptyValueTest.php 6.7KB

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