CalDAVRemoveEmptyValueTest.php 6.6 KB

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