PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Parse/ParseACLTest.php

https://gitlab.com/yousafsyed/parse-php-sdk
PHP | 386 lines | 337 code | 49 blank | 0 comment | 0 complexity | ecbe34aa81d44989aaa748a322f84535 MD5 | raw file
  1. <?php
  2. namespace Parse\Test;
  3. use Exception;
  4. use Parse\ParseACL;
  5. use Parse\ParseException;
  6. use Parse\ParseObject;
  7. use Parse\ParseQuery;
  8. use Parse\ParseUser;
  9. class ParseACLTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public static function setUpBeforeClass()
  12. {
  13. Helper::setUp();
  14. }
  15. public function setUp()
  16. {
  17. Helper::clearClass('_User');
  18. Helper::clearClass('Object');
  19. }
  20. public function tearDown()
  21. {
  22. Helper::tearDown();
  23. }
  24. public function testACLAnObjectOwnedByOneUser()
  25. {
  26. $user = new ParseUser();
  27. $user->setUsername('alice');
  28. $user->setPassword('wonderland');
  29. $user->signUp();
  30. $object = ParseObject::create('Object');
  31. $acl = ParseACL::createACLWithUser($user);
  32. $object->setACL($acl);
  33. $object->save();
  34. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  35. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  36. $this->assertFalse($object->getACL()->getPublicReadAccess());
  37. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  38. $user->logOut();
  39. $query = new ParseQuery('Object');
  40. try {
  41. $query->get($object->getObjectId());
  42. $this->fail('public should be unable to get');
  43. } catch (ParseException $e) {
  44. }
  45. $this->assertEquals(0, count($query->find()));
  46. $object->set('foo', 'bar');
  47. try {
  48. $object->save();
  49. $this->fail('update should fail with object not found');
  50. } catch (ParseException $e) {
  51. }
  52. try {
  53. $object->destroy();
  54. $this->fail('delete should fail with object not found');
  55. } catch (ParseException $e) {
  56. }
  57. ParseUser::logIn('alice', 'wonderland');
  58. $result = $query->get($object->getObjectId());
  59. $this->assertNotNull($result);
  60. $this->assertTrue($result->getACL()->getUserReadAccess($user));
  61. $this->assertTrue($result->getACL()->getUserWriteAccess($user));
  62. $this->assertFalse($result->getACL()->getPublicReadAccess());
  63. $this->assertFalse($result->getACL()->getPublicWriteAccess());
  64. $this->assertEquals(1, count($query->find()));
  65. $object->save();
  66. $object->destroy();
  67. }
  68. public function testACLMakingAnObjectPubliclyReadable()
  69. {
  70. $user = new ParseUser();
  71. $user->setUsername('alice');
  72. $user->setPassword('wonderland');
  73. $user->signUp();
  74. $object = ParseObject::create('Object');
  75. $acl = ParseACL::createACLWithUser($user);
  76. $object->setACL($acl);
  77. $object->save();
  78. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  79. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  80. $this->assertFalse($object->getACL()->getPublicReadAccess());
  81. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  82. $acl->setPublicReadAccess(true);
  83. $object->setACL($acl);
  84. $object->save();
  85. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  86. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  87. $this->assertTrue($object->getACL()->getPublicReadAccess());
  88. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  89. $user->logOut();
  90. $query = new ParseQuery('Object');
  91. $result = $query->get($object->getObjectId());
  92. $this->assertNotNull($result);
  93. $this->assertTrue($result->getACL()->getUserReadAccess($user));
  94. $this->assertTrue($result->getACL()->getUserWriteAccess($user));
  95. $this->assertTrue($result->getACL()->getPublicReadAccess());
  96. $this->assertFalse($result->getACL()->getPublicWriteAccess());
  97. $this->assertEquals(1, count($query->find()));
  98. $object->set('foo', 'bar');
  99. try {
  100. $object->save();
  101. $this->fail('update should fail with object not found');
  102. } catch (ParseException $e) {
  103. }
  104. try {
  105. $object->destroy();
  106. $this->fail('delete should fail with object not found');
  107. } catch (ParseException $e) {
  108. }
  109. }
  110. public function testACLMakingAnObjectPubliclyWritable()
  111. {
  112. $user = new ParseUser();
  113. $user->setUsername('alice');
  114. $user->setPassword('wonderland');
  115. $user->signUp();
  116. $object = ParseObject::create('Object');
  117. $acl = ParseACL::createACLWithUser($user);
  118. $object->setACL($acl);
  119. $object->save();
  120. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  121. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  122. $this->assertFalse($object->getACL()->getPublicReadAccess());
  123. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  124. $acl->setPublicWriteAccess(true);
  125. $object->setACL($acl);
  126. $object->save();
  127. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  128. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  129. $this->assertFalse($object->getACL()->getPublicReadAccess());
  130. $this->assertTrue($object->getACL()->getPublicWriteAccess());
  131. $user->logOut();
  132. $query = new ParseQuery('Object');
  133. try {
  134. $query->get($object->getObjectId());
  135. $this->fail('public should be unable to get');
  136. } catch (ParseException $e) {
  137. }
  138. $this->assertEquals(0, count($query->find()));
  139. $object->set('foo', 'bar');
  140. $object->save();
  141. $object->destroy();
  142. }
  143. public function testACLSharingWithAnotherUser()
  144. {
  145. $bob = new ParseUser();
  146. $bob->setUsername('bob');
  147. $bob->setPassword('pass');
  148. $bob->signUp();
  149. $bob->logOut();
  150. $alice = new ParseUser();
  151. $alice->setUsername('alice');
  152. $alice->setPassword('wonderland');
  153. $alice->signUp();
  154. $object = ParseObject::create('Object');
  155. $acl = ParseACL::createACLWithUser($alice);
  156. $acl->setUserReadAccess($bob, true);
  157. $acl->setUserWriteAccess($bob, true);
  158. $object->setACL($acl);
  159. $object->save();
  160. $this->assertTrue($object->getACL()->getUserReadAccess($alice));
  161. $this->assertTrue($object->getACL()->getUserWriteAccess($alice));
  162. $this->assertTrue($object->getACL()->getUserReadAccess($bob));
  163. $this->assertTrue($object->getACL()->getUserWriteAccess($bob));
  164. $this->assertFalse($object->getACL()->getPublicReadAccess());
  165. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  166. ParseUser::logOut();
  167. $query = new ParseQuery('Object');
  168. try {
  169. $query->get($object->getObjectId());
  170. $this->fail('public should be unable to get');
  171. } catch (ParseException $e) {
  172. }
  173. $this->assertEquals(0, count($query->find()));
  174. $object->set('foo', 'bar');
  175. try {
  176. $object->save();
  177. $this->fail('update should fail with object not found');
  178. } catch (ParseException $e) {
  179. }
  180. try {
  181. $object->destroy();
  182. $this->fail('delete should fail with object not found');
  183. } catch (ParseException $e) {
  184. }
  185. ParseUser::logIn('bob', 'pass');
  186. $query = new ParseQuery('Object');
  187. $result = $query->get($object->getObjectId());
  188. $this->assertNotNull($result);
  189. $this->assertTrue($result->getACL()->getUserReadAccess($alice));
  190. $this->assertTrue($result->getACL()->getUserWriteAccess($alice));
  191. $this->assertTrue($result->getACL()->getUserReadAccess($bob));
  192. $this->assertTrue($result->getACL()->getUserWriteAccess($bob));
  193. $this->assertFalse($result->getACL()->getPublicReadAccess());
  194. $this->assertFalse($result->getACL()->getPublicWriteAccess());
  195. $this->assertEquals(1, count($query->find()));
  196. $object->set('foo', 'bar');
  197. $object->save();
  198. $object->destroy();
  199. }
  200. public function testACLSaveAllWithPermissions()
  201. {
  202. $alice = new ParseUser();
  203. $alice->setUsername('alice');
  204. $alice->setPassword('wonderland');
  205. $alice->signUp();
  206. $acl = ParseACL::createACLWithUser($alice);
  207. $object1 = ParseObject::create('Object');
  208. $object1->setACL($acl);
  209. $object1->save();
  210. $object2 = ParseObject::create('Object');
  211. $object2->setACL($acl);
  212. $object2->save();
  213. $this->assertTrue($object1->getACL()->getUserReadAccess($alice));
  214. $this->assertTrue($object1->getACL()->getUserWriteAccess($alice));
  215. $this->assertFalse($object1->getACL()->getPublicReadAccess());
  216. $this->assertFalse($object1->getACL()->getPublicWriteAccess());
  217. $this->assertTrue($object2->getACL()->getUserReadAccess($alice));
  218. $this->assertTrue($object2->getACL()->getUserWriteAccess($alice));
  219. $this->assertFalse($object2->getACL()->getPublicReadAccess());
  220. $this->assertFalse($object2->getACL()->getPublicWriteAccess());
  221. $object1->set('foo', 'bar');
  222. $object2->set('foo', 'bar');
  223. ParseObject::saveAll([$object1, $object2]);
  224. $query = new ParseQuery('Object');
  225. $query->equalTo('foo', 'bar');
  226. $this->assertEquals(2, count($query->find()));
  227. }
  228. public function testACLModifyingAfterLoad()
  229. {
  230. $user = new ParseUser();
  231. $user->setUsername('alice');
  232. $user->setPassword('wonderland');
  233. $user->signUp();
  234. $object = ParseObject::create('Object');
  235. $acl = ParseACL::createACLWithUser($user);
  236. $object->setACL($acl);
  237. $object->save();
  238. $this->assertTrue($object->getACL()->getUserReadAccess($user));
  239. $this->assertTrue($object->getACL()->getUserWriteAccess($user));
  240. $this->assertFalse($object->getACL()->getPublicReadAccess());
  241. $this->assertFalse($object->getACL()->getPublicWriteAccess());
  242. $query = new ParseQuery('Object');
  243. $objectAgain = $query->get($object->getObjectId());
  244. $objectAgain->getACL()->setPublicReadAccess(true);
  245. $this->assertTrue($objectAgain->getACL()->getUserReadAccess($user));
  246. $this->assertTrue($objectAgain->getACL()->getUserWriteAccess($user));
  247. $this->assertTrue($objectAgain->getACL()->getPublicReadAccess());
  248. $this->assertFalse($objectAgain->getACL()->getPublicWriteAccess());
  249. }
  250. public function testACLRequiresObjectId()
  251. {
  252. $acl = new ParseACL();
  253. try {
  254. $acl->setReadAccess(null, true);
  255. $this->fail('Exception should have thrown');
  256. } catch (Exception $e) {
  257. }
  258. try {
  259. $acl->getReadAccess(null);
  260. $this->fail('Exception should have thrown');
  261. } catch (Exception $e) {
  262. }
  263. try {
  264. $acl->setWriteAccess(null, true);
  265. $this->fail('Exception should have thrown');
  266. } catch (Exception $e) {
  267. }
  268. try {
  269. $acl->getWriteAccess(null);
  270. $this->fail('Exception should have thrown');
  271. } catch (Exception $e) {
  272. }
  273. $user = new ParseUser();
  274. try {
  275. $acl->setReadAccess($user, true);
  276. $this->fail('Exception should have thrown');
  277. } catch (Exception $e) {
  278. }
  279. try {
  280. $acl->getReadAccess($user);
  281. $this->fail('Exception should have thrown');
  282. } catch (Exception $e) {
  283. }
  284. try {
  285. $acl->setWriteAccess($user, true);
  286. $this->fail('Exception should have thrown');
  287. } catch (Exception $e) {
  288. }
  289. try {
  290. $acl->getWriteAccess($user);
  291. $this->fail('Exception should have thrown');
  292. } catch (Exception $e) {
  293. }
  294. }
  295. public function testIncludedObjectsGetACLs()
  296. {
  297. Helper::clearClass('Test');
  298. Helper::clearClass('Related');
  299. $object = ParseObject::create('Test');
  300. $acl = new ParseACL();
  301. $acl->setPublicReadAccess(true);
  302. $object->setACL($acl);
  303. $object->save();
  304. $this->assertTrue($object->getACL()->getPublicReadAccess());
  305. $related = ParseObject::create('Related');
  306. $related->set('test', $object);
  307. $related->save();
  308. $query = new ParseQuery('Related');
  309. $query->includeKey('test');
  310. $objectAgain = $query->first()->get('test');
  311. $this->assertTrue($objectAgain->getACL()->getPublicReadAccess());
  312. $this->assertFalse($objectAgain->getACL()->getPublicWriteAccess());
  313. }
  314. public function testIncludedObjectsGetACLWithDefaultACL()
  315. {
  316. Helper::clearClass('Test');
  317. Helper::clearClass('Related');
  318. $defaultACL = new ParseACL();
  319. $defaultACL->setPublicReadAccess(true);
  320. $defaultACL->setPublicWriteAccess(true);
  321. ParseACL::setDefaultACL($defaultACL, true);
  322. $object = ParseObject::create('Test');
  323. $acl = new ParseACL();
  324. $acl->setPublicReadAccess(true);
  325. $object->setACL($acl);
  326. $object->save();
  327. $this->assertTrue($object->getACL()->getPublicReadAccess());
  328. $related = ParseObject::create('Related');
  329. $related->set('test', $object);
  330. $related->save();
  331. $query = new ParseQuery('Related');
  332. $query->includeKey('test');
  333. $objectAgain = $query->first()->get('test');
  334. $this->assertTrue($objectAgain->getACL()->getPublicReadAccess());
  335. $this->assertFalse($objectAgain->getACL()->getPublicWriteAccess());
  336. }
  337. }