PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Sabre/CardDAV/Backend/AbstractPDOTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 343 lines | 234 code | 87 blank | 22 comment | 2 complexity | 0913379230953ecaa1af96c47f6cc1a6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\CardDAV\Backend;
  3. use Sabre\CardDAV;
  4. abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
  5. /**
  6. * @var CardDAV\Backend\PDO
  7. */
  8. protected $backend;
  9. /**
  10. * @abstract
  11. * @return PDO
  12. */
  13. abstract function getPDO();
  14. public function setUp() {
  15. $pdo = $this->getPDO();
  16. $this->backend = new PDO($pdo);
  17. $pdo->exec('INSERT INTO addressbooks (principaluri, displayname, uri, description, synctoken) VALUES ("principals/user1", "book1", "book1", "addressbook 1", 1)');
  18. $pdo->exec('INSERT INTO cards (addressbookid, carddata, uri, lastmodified) VALUES (1, "card1", "card1", 0)');
  19. }
  20. public function testGetAddressBooksForUser() {
  21. $result = $this->backend->getAddressBooksForUser('principals/user1');
  22. $expected = array(
  23. array(
  24. 'id' => 1,
  25. 'uri' => 'book1',
  26. 'principaluri' => 'principals/user1',
  27. '{DAV:}displayname' => 'book1',
  28. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
  29. '{http://calendarserver.org/ns/}getctag' => 1,
  30. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  31. '{DAV:}sync-token' => 1
  32. )
  33. );
  34. $this->assertEquals($expected, $result);
  35. }
  36. public function testUpdateAddressBookInvalidProp() {
  37. $result = $this->backend->updateAddressBook(1, array(
  38. '{DAV:}displayname' => 'updated',
  39. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
  40. '{DAV:}foo' => 'bar',
  41. ));
  42. $this->assertFalse($result);
  43. $result = $this->backend->getAddressBooksForUser('principals/user1');
  44. $expected = array(
  45. array(
  46. 'id' => 1,
  47. 'uri' => 'book1',
  48. 'principaluri' => 'principals/user1',
  49. '{DAV:}displayname' => 'book1',
  50. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
  51. '{http://calendarserver.org/ns/}getctag' => 1,
  52. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  53. '{DAV:}sync-token' => 1
  54. )
  55. );
  56. $this->assertEquals($expected, $result);
  57. }
  58. public function testUpdateAddressBookNoProps() {
  59. $result = $this->backend->updateAddressBook(1, array());
  60. $this->assertFalse($result);
  61. $result = $this->backend->getAddressBooksForUser('principals/user1');
  62. $expected = array(
  63. array(
  64. 'id' => 1,
  65. 'uri' => 'book1',
  66. 'principaluri' => 'principals/user1',
  67. '{DAV:}displayname' => 'book1',
  68. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
  69. '{http://calendarserver.org/ns/}getctag' => 1,
  70. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  71. '{DAV:}sync-token' => 1
  72. )
  73. );
  74. $this->assertEquals($expected, $result);
  75. }
  76. public function testUpdateAddressBookSuccess() {
  77. $result = $this->backend->updateAddressBook(1, array(
  78. '{DAV:}displayname' => 'updated',
  79. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
  80. ));
  81. $this->assertTrue($result);
  82. $result = $this->backend->getAddressBooksForUser('principals/user1');
  83. $expected = array(
  84. array(
  85. 'id' => 1,
  86. 'uri' => 'book1',
  87. 'principaluri' => 'principals/user1',
  88. '{DAV:}displayname' => 'updated',
  89. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'updated',
  90. '{http://calendarserver.org/ns/}getctag' => 2,
  91. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  92. '{DAV:}sync-token' => 2
  93. )
  94. );
  95. $this->assertEquals($expected, $result);
  96. }
  97. public function testDeleteAddressBook() {
  98. $this->backend->deleteAddressBook(1);
  99. $this->assertEquals(array(), $this->backend->getAddressBooksForUser('principals/user1'));
  100. }
  101. /**
  102. * @expectedException Sabre\DAV\Exception\BadRequest
  103. */
  104. public function testCreateAddressBookUnsupportedProp() {
  105. $this->backend->createAddressBook('principals/user1','book2', array(
  106. '{DAV:}foo' => 'bar',
  107. ));
  108. }
  109. public function testCreateAddressBookSuccess() {
  110. $this->backend->createAddressBook('principals/user1','book2', array(
  111. '{DAV:}displayname' => 'book2',
  112. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
  113. ));
  114. $expected = array(
  115. array(
  116. 'id' => 1,
  117. 'uri' => 'book1',
  118. 'principaluri' => 'principals/user1',
  119. '{DAV:}displayname' => 'book1',
  120. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 1',
  121. '{http://calendarserver.org/ns/}getctag' => 1,
  122. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  123. '{DAV:}sync-token' => 1,
  124. ),
  125. array(
  126. 'id' => 2,
  127. 'uri' => 'book2',
  128. 'principaluri' => 'principals/user1',
  129. '{DAV:}displayname' => 'book2',
  130. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => 'addressbook 2',
  131. '{http://calendarserver.org/ns/}getctag' => 1,
  132. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' => new CardDAV\Property\SupportedAddressData(),
  133. '{DAV:}sync-token' => 1,
  134. )
  135. );
  136. $result = $this->backend->getAddressBooksForUser('principals/user1');
  137. $this->assertEquals($expected, $result);
  138. }
  139. public function testGetCards() {
  140. $result = $this->backend->getCards(1);
  141. $expected = array(
  142. array(
  143. 'id' => 1,
  144. 'uri' => 'card1',
  145. 'carddata' => 'card1',
  146. 'lastmodified' => 0,
  147. )
  148. );
  149. $this->assertEquals($expected, $result);
  150. }
  151. public function testGetCard() {
  152. $result = $this->backend->getCard(1,'card1');
  153. $expected = array(
  154. 'id' => 1,
  155. 'uri' => 'card1',
  156. 'carddata' => 'card1',
  157. 'lastmodified' => 0,
  158. );
  159. $this->assertEquals($expected, $result);
  160. }
  161. /**
  162. * @depends testGetCard
  163. */
  164. public function testCreateCard() {
  165. $result = $this->backend->createCard(1, 'card2', 'data2');
  166. $this->assertEquals('"' . md5('data2') . '"', $result);
  167. $result = $this->backend->getCard(1,'card2');
  168. $this->assertEquals(2, $result['id']);
  169. $this->assertEquals('card2', $result['uri']);
  170. $this->assertEquals('data2', $result['carddata']);
  171. }
  172. /**
  173. * @depends testCreateCard
  174. */
  175. public function testGetMultiple() {
  176. $result = $this->backend->createCard(1, 'card2', 'data2');
  177. $result = $this->backend->createCard(1, 'card3', 'data3');
  178. $check = [
  179. [
  180. 'id' => 1,
  181. 'uri' => 'card1',
  182. 'carddata' => 'card1',
  183. 'lastmodified' => 0,
  184. ],
  185. [
  186. 'id' => 2,
  187. 'uri' => 'card2',
  188. 'carddata' => 'data2',
  189. 'lastmodified' => time(),
  190. ],
  191. [
  192. 'id' => 3,
  193. 'uri' => 'card3',
  194. 'carddata' => 'data3',
  195. 'lastmodified' => time(),
  196. ],
  197. ];
  198. $result = $this->backend->getMultipleCards(1, ['card1','card2','card3']);
  199. foreach($check as $index=>$node) {
  200. foreach($node as $k=>$v) {
  201. if ($k!=='lastmodified') {
  202. $this->assertEquals($v, $result[$index][$k]);
  203. } else {
  204. $this->assertTrue(isset($result[$index][$k]));
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * @depends testGetCard
  211. */
  212. public function testUpdateCard() {
  213. $result = $this->backend->updateCard(1, 'card1', 'newdata');
  214. $this->assertEquals('"' . md5('newdata') . '"', $result);
  215. $result = $this->backend->getCard(1,'card1');
  216. $this->assertEquals(1, $result['id']);
  217. $this->assertEquals('newdata', $result['carddata']);
  218. }
  219. /**
  220. * @depends testGetCard
  221. */
  222. public function testDeleteCard() {
  223. $this->backend->deleteCard(1, 'card1');
  224. $result = $this->backend->getCard(1,'card1');
  225. $this->assertFalse($result);
  226. }
  227. function testGetChanges() {
  228. $backend = $this->backend;
  229. $id = $backend->createAddressBook(
  230. 'principals/user1',
  231. 'bla',
  232. []
  233. );
  234. $result = $backend->getChangesForAddressBook($id, null, 1);
  235. $this->assertEquals([
  236. 'syncToken' => 1,
  237. "added" => [],
  238. 'modified' => [],
  239. 'deleted' => [],
  240. ], $result);
  241. $currentToken = $result['syncToken'];
  242. $dummyCard = "BEGIN:VCARD\r\nEND:VCARD\r\n";
  243. $backend->createCard($id, "card1.ics", $dummyCard);
  244. $backend->createCard($id, "card2.ics", $dummyCard);
  245. $backend->createCard($id, "card3.ics", $dummyCard);
  246. $backend->updateCard($id, "card1.ics", $dummyCard);
  247. $backend->deleteCard($id, "card2.ics");
  248. $result = $backend->getChangesForAddressBook($id, $currentToken, 1);
  249. $this->assertEquals([
  250. 'syncToken' => 6,
  251. 'modified' => ["card1.ics"],
  252. 'deleted' => ["card2.ics"],
  253. "added" => ["card3.ics"],
  254. ], $result);
  255. }
  256. }