ElggRelationshipTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * test ElggRelationship
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggRelationshipTest extends ElggCoreUnitTest {
  9. /**
  10. * @var ElggEntity
  11. */
  12. protected $entity1;
  13. protected $entity2;
  14. protected $entity3;
  15. /**
  16. * Called before each test method.
  17. */
  18. public function setUp() {
  19. $this->original_events = _elgg_services()->events;
  20. _elgg_services()->events = new Elgg\EventsService();
  21. $this->entity1 = new ElggObject();
  22. $this->entity1->subtype = 'elgg_relationship_test';
  23. $this->entity1->access_id = ACCESS_PUBLIC;
  24. $this->entity1->save();
  25. $this->entity2 = new ElggObject();
  26. $this->entity2->subtype = 'elgg_relationship_test';
  27. $this->entity2->access_id = ACCESS_PUBLIC;
  28. $this->entity2->save();
  29. $this->entity3 = new ElggObject();
  30. $this->entity3->subtype = 'elgg_relationship_test';
  31. $this->entity3->access_id = ACCESS_PUBLIC;
  32. $this->entity3->save();
  33. }
  34. /**
  35. * Called after each test method.
  36. */
  37. public function tearDown() {
  38. if ($this->entity1) {
  39. $this->entity1->delete();
  40. }
  41. if ($this->entity2) {
  42. $this->entity2->delete();
  43. }
  44. if ($this->entity3) {
  45. $this->entity3->delete();
  46. }
  47. remove_subtype('object', 'elgg_relationship_test');
  48. _elgg_services()->events = $this->original_events;
  49. }
  50. /**
  51. * Tests
  52. */
  53. public function testAddRelationship() {
  54. // test adding a relationship
  55. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  56. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  57. $this->assertIsA($r, 'ElggRelationship');
  58. }
  59. public function testRemoveRelationship() {
  60. // test adding a relationship
  61. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  62. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  63. $this->assertIsA($r, 'ElggRelationship');
  64. $this->assertTrue(remove_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  65. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  66. }
  67. public function testPreventAddRelationship() {
  68. // test event handler - should prevent the addition of a relationship
  69. elgg_register_event_handler('create', 'relationship', 'Elgg\Values::getFalse');
  70. $this->assertFalse(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  71. elgg_unregister_event_handler('create', 'relationship', 'Elgg\Values::getFalse');
  72. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  73. }
  74. public function testPreventRemoveRelationship() {
  75. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  76. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  77. $this->assertIsA($r, 'ElggRelationship');
  78. elgg_register_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
  79. $this->assertFalse(remove_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  80. elgg_unregister_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
  81. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  82. $this->assertIsA($r, 'ElggRelationship');
  83. }
  84. public function testRelationshipSave() {
  85. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  86. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  87. $this->assertIsA($r, 'ElggRelationship');
  88. // note - string because that's how it's returned when getting a new object
  89. $r->guid_two = (string) $this->entity3->guid;
  90. $this->assertTrue($r->save());
  91. $test_r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
  92. $this->assertIsA($test_r, 'ElggRelationship');
  93. $this->assertIdentical($r->guid_one, $test_r->guid_one);
  94. $this->assertIdentical($r->guid_two, $test_r->guid_two);
  95. $this->assertIdentical($r->relationship, $test_r->relationship);
  96. // the original shouldn't exist anymore
  97. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  98. }
  99. public function testRelationshipDelete() {
  100. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  101. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  102. $this->assertIsA($r, 'ElggRelationship');
  103. $this->assertTrue($r->delete());
  104. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  105. }
  106. public function testRelationshipOnEntityDelete() {
  107. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  108. // test deleting entity in guid_one position
  109. $this->entity1->delete();
  110. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  111. $this->assertTrue(add_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity3->guid));
  112. // test deleting entity in guid_two position
  113. $this->entity3->delete();
  114. $this->assertFalse(check_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity3->guid));
  115. }
  116. public function testPreventRelationshipOnEntityDelete() {
  117. $this->assertTrue(add_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  118. $this->assertTrue(add_entity_relationship($this->entity2->guid, 'test_relationship', $this->entity1->guid));
  119. $guid = $this->entity1->guid;
  120. elgg_register_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
  121. $this->assertTrue($this->entity1->delete());
  122. elgg_unregister_event_handler('delete', 'relationship', 'Elgg\Values::getFalse');
  123. // relationships should still be gone as there is no entity
  124. // despite the fact we have a handler trying to prevent it
  125. $this->assertFalse(check_entity_relationship($guid, 'test_relationship', $this->entity2->guid));
  126. $this->assertFalse(check_entity_relationship($this->entity2->guid, 'test_relationship', $guid));
  127. }
  128. public function testEntityMethodAddRelationship() {
  129. $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
  130. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  131. $this->assertIsA($r, 'ElggRelationship');
  132. }
  133. public function testEntityMethodRemoveRelationship() {
  134. $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
  135. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  136. $this->assertIsA($r, 'ElggRelationship');
  137. $this->assertTrue($this->entity1->removeRelationship($this->entity2->guid, 'test_relationship'));
  138. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  139. }
  140. public function testEntityMethodDeleteRelationships() {
  141. $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
  142. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  143. $this->assertIsA($r, 'ElggRelationship');
  144. $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
  145. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
  146. $this->assertIsA($r, 'ElggRelationship');
  147. $this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
  148. $r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
  149. $this->assertIsA($r, 'ElggRelationship');
  150. $this->assertTrue($this->entity1->deleteRelationships('test_relationship'));
  151. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  152. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
  153. // inverse relationships should be gone too
  154. $this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
  155. // Repeat above test, but with no relationship - should remove all relationships
  156. $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship'));
  157. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid);
  158. $this->assertIsA($r, 'ElggRelationship');
  159. $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship'));
  160. $r = check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid);
  161. $this->assertIsA($r, 'ElggRelationship');
  162. $this->assertTrue($this->entity3->addRelationship($this->entity1->guid, 'test_relationship'));
  163. $r = check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid);
  164. $this->assertIsA($r, 'ElggRelationship');
  165. $this->assertTrue($this->entity1->addRelationship($this->entity2->guid, 'test_relationship2'));
  166. $r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid);
  167. $this->assertIsA($r, 'ElggRelationship');
  168. $this->assertTrue($this->entity1->addRelationship($this->entity3->guid, 'test_relationship2'));
  169. $r = check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid);
  170. $this->assertIsA($r, 'ElggRelationship');
  171. $this->assertTrue($this->entity1->deleteRelationships());
  172. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity2->guid));
  173. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship', $this->entity3->guid));
  174. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity2->guid));
  175. $this->assertFalse(check_entity_relationship($this->entity1->guid, 'test_relationship2', $this->entity3->guid));
  176. // inverse relationships should be gone too
  177. $this->assertFalse(check_entity_relationship($this->entity3->guid, 'test_relationship', $this->entity1->guid));
  178. }
  179. }