CalDAVRemoveEmptyValueTest.php 6.6 KB

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