ElggCoreGetEntitiesFromRelationshipTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Test elgg_get_entities_from_relationship() and
  4. * elgg_get_entities_from_relationship_count()
  5. */
  6. class ElggCoreGetEntitiesFromRelationshipTest extends \ElggCoreGetEntitiesBaseTest {
  7. // Make sure metadata doesn't affect getting entities by relationship. See #2274
  8. public function testElggApiGettersEntityRelationshipWithMetadata() {
  9. $guids = array();
  10. $obj1 = new \ElggObject();
  11. $obj1->test_md = 'test';
  12. $obj1->save();
  13. $guids[] = $obj1->guid;
  14. $obj2 = new \ElggObject();
  15. $obj2->test_md = 'test';
  16. $obj2->save();
  17. $guids[] = $obj2->guid;
  18. add_entity_relationship($guids[0], 'test', $guids[1]);
  19. $options = array(
  20. 'relationship' => 'test',
  21. 'relationship_guid' => $guids[0]
  22. );
  23. $es = elgg_get_entities_from_relationship($options);
  24. $this->assertTrue(is_array($es));
  25. $this->assertIdentical(count($es), 1);
  26. foreach ($es as $e) {
  27. $this->assertEqual($guids[1], $e->guid);
  28. }
  29. foreach ($guids as $guid) {
  30. $e = get_entity($guid);
  31. $e->delete();
  32. }
  33. }
  34. public function testElggApiGettersEntityRelationshipWithOutMetadata() {
  35. $guids = array();
  36. $obj1 = new \ElggObject();
  37. $obj1->save();
  38. $guids[] = $obj1->guid;
  39. $obj2 = new \ElggObject();
  40. $obj2->save();
  41. $guids[] = $obj2->guid;
  42. add_entity_relationship($guids[0], 'test', $guids[1]);
  43. $options = array(
  44. 'relationship' => 'test',
  45. 'relationship_guid' => $guids[0]
  46. );
  47. $es = elgg_get_entities_from_relationship($options);
  48. $this->assertTrue(is_array($es));
  49. $this->assertIdentical(count($es), 1);
  50. foreach ($es as $e) {
  51. $this->assertEqual($guids[1], $e->guid);
  52. }
  53. foreach ($guids as $guid) {
  54. $e = get_entity($guid);
  55. $e->delete();
  56. }
  57. }
  58. public function testElggApiGettersEntityRelationshipWithMetadataIncludingRealMetadata() {
  59. $guids = array();
  60. $obj1 = new \ElggObject();
  61. $obj1->test_md = 'test';
  62. $obj1->save();
  63. $guids[] = $obj1->guid;
  64. $obj2 = new \ElggObject();
  65. $obj2->test_md = 'test';
  66. $obj2->save();
  67. $guids[] = $obj2->guid;
  68. add_entity_relationship($guids[0], 'test', $guids[1]);
  69. $options = array(
  70. 'relationship' => 'test',
  71. 'relationship_guid' => $guids[0],
  72. 'metadata_name' => 'test_md',
  73. 'metadata_value' => 'test',
  74. );
  75. $es = elgg_get_entities_from_relationship($options);
  76. $this->assertTrue(is_array($es));
  77. $this->assertIdentical(count($es), 1);
  78. foreach ($es as $e) {
  79. $this->assertEqual($guids[1], $e->guid);
  80. }
  81. foreach ($guids as $guid) {
  82. $e = get_entity($guid);
  83. $e->delete();
  84. }
  85. }
  86. public function testElggApiGettersEntityRelationshipWithMetadataIncludingFakeMetadata() {
  87. $guids = array();
  88. $obj1 = new \ElggObject();
  89. $obj1->test_md = 'test';
  90. $obj1->save();
  91. $guids[] = $obj1->guid;
  92. $obj2 = new \ElggObject();
  93. $obj2->test_md = 'test';
  94. $obj2->save();
  95. $guids[] = $obj2->guid;
  96. add_entity_relationship($guids[0], 'test', $guids[1]);
  97. $options = array(
  98. 'relationship' => 'test',
  99. 'relationship_guid' => $guids[0],
  100. 'metadata_name' => 'test_md',
  101. 'metadata_value' => 'invalid',
  102. );
  103. $es = elgg_get_entities_from_relationship($options);
  104. $this->assertTrue(empty($es));
  105. foreach ($guids as $guid) {
  106. $e = get_entity($guid);
  107. $e->delete();
  108. }
  109. }
  110. public function testElggGetEntitiesFromRelationshipCount() {
  111. $entities = $this->entities;
  112. $relationships = array();
  113. $count = count($entities);
  114. $max = $count - 1;
  115. $relationship_name = 'test_relationship_' . rand(0, 1000);
  116. for ($i = 0; $i < $count; $i++) {
  117. do {
  118. $popular_entity = $entities[array_rand($entities)];
  119. } while (array_key_exists($popular_entity->guid, $relationships));
  120. $relationships[$popular_entity->guid] = array();
  121. for ($c = 0; $c < $max; $c++) {
  122. do {
  123. $fan_entity = $entities[array_rand($entities)];
  124. } while ($fan_entity->guid == $popular_entity->guid || in_array($fan_entity->guid, $relationships[$popular_entity->guid]));
  125. $relationships[$popular_entity->guid][] = $fan_entity->guid;
  126. add_entity_relationship($fan_entity->guid, $relationship_name, $popular_entity->guid);
  127. }
  128. $max--;
  129. }
  130. $options = array(
  131. 'relationship' => $relationship_name,
  132. 'limit' => $count
  133. );
  134. $entities = elgg_get_entities_from_relationship_count($options);
  135. foreach ($entities as $e) {
  136. $options = array(
  137. 'relationship' => $relationship_name,
  138. 'limit' => 100,
  139. 'relationship_guid' => $e->guid,
  140. 'inverse_relationship' => true
  141. );
  142. $fan_entities = elgg_get_entities_from_relationship($options);
  143. $this->assertEqual(count($fan_entities), count($relationships[$e->guid]));
  144. foreach ($fan_entities as $fan_entity) {
  145. $this->assertTrue(in_array($fan_entity->guid, $relationships[$e->guid]));
  146. $this->assertNotIdentical(false, check_entity_relationship($fan_entity->guid, $relationship_name, $e->guid));
  147. }
  148. }
  149. }
  150. /**
  151. * Make sure elgg_get_entities_from_relationship() returns distinct (unique) results when relationship_guid is not set
  152. * See #5775
  153. * @covers elgg_get_entities_from_relationship()
  154. */
  155. public function testElggApiGettersEntityRelationshipDistinctResult() {
  156. $obj1 = new ElggObject();
  157. $obj1->save();
  158. $obj2 = new ElggObject();
  159. $obj2->save();
  160. $obj3 = new ElggObject();
  161. $obj3->save();
  162. add_entity_relationship($obj2->guid, 'test_5775', $obj1->guid);
  163. add_entity_relationship($obj3->guid, 'test_5775', $obj1->guid);
  164. $options = array(
  165. 'relationship' => 'test_5775',
  166. 'inverse_relationship' => false,
  167. 'count' => true,
  168. );
  169. $count = elgg_get_entities_from_relationship($options);
  170. $this->assertIdentical($count, 1);
  171. unset($options['count']);
  172. $objects = elgg_get_entities_from_relationship($options);
  173. $this->assertTrue(is_array($objects));
  174. $this->assertIdentical(count($objects), 1);
  175. $obj1->delete();
  176. $obj2->delete();
  177. $obj3->delete();
  178. }
  179. /**
  180. * Make sure changes related to #5775 do not affect inverse relationship queries
  181. * @covers elgg_get_entities_from_relationship()
  182. */
  183. public function testElggApiGettersEntityRelationshipDistinctResultInverse() {
  184. $obj1 = new ElggObject();
  185. $obj1->save();
  186. $obj2 = new ElggObject();
  187. $obj2->save();
  188. $obj3 = new ElggObject();
  189. $obj3->save();
  190. add_entity_relationship($obj2->guid, 'test_5775_inverse', $obj1->guid);
  191. add_entity_relationship($obj3->guid, 'test_5775_inverse', $obj1->guid);
  192. $options = array(
  193. 'relationship' => 'test_5775_inverse',
  194. 'inverse_relationship' => true,
  195. 'count' => true,
  196. );
  197. $count = elgg_get_entities_from_relationship($options);
  198. $this->assertIdentical($count, 2);
  199. unset($options['count']);
  200. $objects = elgg_get_entities_from_relationship($options);
  201. $this->assertTrue(is_array($objects));
  202. $this->assertIdentical(count($objects), 2);
  203. $obj1->delete();
  204. $obj2->delete();
  205. $obj3->delete();
  206. }
  207. }