ElggCoreGetEntitiesTest.php 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. <?php
  2. /**
  3. * Test elgg_get_entities()
  4. */
  5. class ElggCoreGetEntitiesTest extends \ElggCoreGetEntitiesBaseTest {
  6. /***********************************
  7. * TYPE TESTS
  8. ***********************************
  9. * check for getting a valid type in all ways we can.
  10. * note that these aren't wonderful tests as there will be
  11. * existing entities so we can't test against the ones we just created.
  12. * So these just test that some are returned and match the type(s) requested.
  13. * It could definitely be the case that the first 10 entities retrieved are all
  14. * objects. Maybe best to limit to 4 and group by type.
  15. */
  16. public function testElggAPIGettersValidTypeUsingType() {
  17. $type_arr = $this->getRandomValidTypes();
  18. $type = $type_arr[0];
  19. $options = array(
  20. 'type' => $type,
  21. 'group_by' => 'e.type'
  22. );
  23. $es = elgg_get_entities($options);
  24. $this->assertIsA($es, 'array');
  25. // should only ever return one object because of group by
  26. $this->assertIdentical(count($es), 1);
  27. foreach ($es as $e) {
  28. $this->assertTrue(in_array($e->getType(), $type_arr));
  29. }
  30. }
  31. public function testElggAPIGettersValidTypeUsingTypesAsString() {
  32. $type_arr = $this->getRandomValidTypes();
  33. $type = $type_arr[0];
  34. $options = array(
  35. 'types' => $type,
  36. 'group_by' => 'e.type'
  37. );
  38. $es = elgg_get_entities($options);
  39. $this->assertIsA($es, 'array');
  40. // should only ever return one object because of group by
  41. $this->assertIdentical(count($es), 1);
  42. foreach ($es as $e) {
  43. $this->assertTrue(in_array($e->getType(), $type_arr));
  44. }
  45. }
  46. public function testElggAPIGettersValidTypeUsingTypesAsArray() {
  47. $type_arr = $this->getRandomValidTypes();
  48. $type = $type_arr[0];
  49. $options = array(
  50. 'types' => $type_arr,
  51. 'group_by' => 'e.type'
  52. );
  53. $es = elgg_get_entities($options);
  54. $this->assertIsA($es, 'array');
  55. // should only ever return one object because of group by
  56. $this->assertIdentical(count($es), 1);
  57. foreach ($es as $e) {
  58. $this->assertTrue(in_array($e->getType(), $type_arr));
  59. }
  60. }
  61. public function testElggAPIGettersValidTypeUsingTypesAsArrayPlural() {
  62. $num = 2;
  63. $types = $this->getRandomValidTypes($num);
  64. $options = array(
  65. 'types' => $types,
  66. 'group_by' => 'e.type'
  67. );
  68. $es = elgg_get_entities($options);
  69. $this->assertIsA($es, 'array');
  70. // one of object and one of group
  71. $this->assertIdentical(count($es), $num);
  72. foreach ($es as $e) {
  73. $this->assertTrue(in_array($e->getType(), $types));
  74. }
  75. }
  76. /*
  77. * Test mixed valid and invalid types.
  78. */
  79. public function testElggAPIGettersValidAndInvalidTypes() {
  80. //@todo replace this with $this->getRandomMixedTypes().
  81. $t = $this->getRandomValidTypes();
  82. $valid = $t[0];
  83. $t = $this->getRandomInvalids();
  84. $invalid = $t[0];
  85. $options = array(
  86. 'types' => array($invalid, $valid),
  87. 'group_by' => 'e.type'
  88. );
  89. $es = elgg_get_entities($options);
  90. $this->assertIsA($es, 'array');
  91. // should only ever return one object because of group by
  92. $this->assertIdentical(count($es), 1);
  93. $this->assertIdentical($es[0]->getType(), $valid);
  94. }
  95. public function testElggAPIGettersValidAndInvalidTypesPlural() {
  96. $valid_num = 2;
  97. $invalid_num = 3;
  98. $valid = $this->getRandomValidTypes($valid_num);
  99. $invalid = $this->getRandomInvalids($invalid_num);
  100. $types = array();
  101. foreach ($valid as $t) {
  102. $types[] = $t;
  103. }
  104. foreach ($invalid as $t) {
  105. $types[] = $t;
  106. }
  107. shuffle($types);
  108. $options = array(
  109. 'types' => $types,
  110. 'group_by' => 'e.type'
  111. );
  112. $es = elgg_get_entities($options);
  113. $this->assertIsA($es, 'array');
  114. // should only ever return one object because of group by
  115. $this->assertIdentical(count($es), $valid_num);
  116. foreach ($es as $e) {
  117. $this->assertTrue(in_array($e->getType(), $valid));
  118. }
  119. }
  120. /**************************************
  121. * SUBTYPE TESTS
  122. **************************************
  123. *
  124. * Here we can use the subtypes we created to test more finely.
  125. * Subtypes are bound to types, so we must pass a type.
  126. * This is where the fun logic starts.
  127. */
  128. public function testElggAPIGettersValidSubtypeUsingSubtypeSingularType() {
  129. $types = $this->getRandomValidTypes();
  130. $subtypes = $this->getRandomValidSubtypes($types);
  131. $subtype = $subtypes[0];
  132. $options = array(
  133. 'types' => $types,
  134. 'subtype' => $subtype
  135. );
  136. $es = elgg_get_entities($options);
  137. $this->assertIsA($es, 'array');
  138. $this->assertIdentical(count($es), 1);
  139. foreach ($es as $e) {
  140. $this->assertTrue(in_array($e->getType(), $types));
  141. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  142. }
  143. }
  144. public function testElggAPIGettersValidSubtypeUsingSubtypesAsStringSingularType() {
  145. $types = $this->getRandomValidTypes();
  146. $subtypes = $this->getRandomValidSubtypes($types);
  147. $subtype = $subtypes[0];
  148. $options = array(
  149. 'types' => $types,
  150. 'subtypes' => $subtype
  151. );
  152. $es = elgg_get_entities($options);
  153. $this->assertIsA($es, 'array');
  154. $this->assertIdentical(count($es), 1);
  155. foreach ($es as $e) {
  156. $this->assertTrue(in_array($e->getType(), $types));
  157. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  158. }
  159. }
  160. public function testElggAPIGettersValidSubtypeUsingSubtypesAsArraySingularType() {
  161. $types = $this->getRandomValidTypes();
  162. $subtypes = $this->getRandomValidSubtypes($types);
  163. $options = array(
  164. 'types' => $types,
  165. 'subtypes' => $subtypes
  166. );
  167. $es = elgg_get_entities($options);
  168. $this->assertIsA($es, 'array');
  169. $this->assertIdentical(count($es), 1);
  170. foreach ($es as $e) {
  171. $this->assertTrue(in_array($e->getType(), $types));
  172. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  173. }
  174. }
  175. public function testElggAPIGettersValidSubtypeUsingPluralSubtypesSingularType() {
  176. $subtype_num = 2;
  177. $types = $this->getRandomValidTypes();
  178. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  179. $options = array(
  180. 'types' => $types,
  181. 'subtypes' => $subtypes
  182. );
  183. $es = elgg_get_entities($options);
  184. $this->assertIsA($es, 'array');
  185. $this->assertIdentical(count($es), $subtype_num);
  186. foreach ($es as $e) {
  187. $this->assertTrue(in_array($e->getType(), $types));
  188. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  189. }
  190. }
  191. /*
  192. Because we're looking for type OR subtype (sorta)
  193. it's possible that we've pulled in entities that aren't
  194. of the subtype we've requested.
  195. THIS COMBINATION MAKES LITTLE SENSE.
  196. There is no mechanism in elgg to retrieve a subtype without a type, so
  197. this combo gets trimmed down to only including subtypes that are valid to
  198. each particular type.
  199. FOR THE LOVE OF ALL GOOD PLEASE JUST USE TYPE_SUBTYPE_PAIRS!
  200. */
  201. public function testElggAPIGettersValidSubtypeUsingPluralSubtypesPluralTypes() {
  202. $type_num = 2;
  203. $subtype_num = 2;
  204. $types = $this->getRandomValidTypes($type_num);
  205. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  206. $options = array(
  207. 'types' => $types,
  208. 'subtypes' => $subtypes
  209. );
  210. $es = elgg_get_entities($options);
  211. $this->assertIsA($es, 'array');
  212. // this will unset all invalid subtypes for each type that that only
  213. // one entity exists of each.
  214. $this->assertIdentical(count($es), $subtype_num);
  215. foreach ($es as $e) {
  216. // entities must at least be in the type.
  217. $this->assertTrue(in_array($e->getType(), $types));
  218. // test that this is a valid subtype for the entity type.
  219. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  220. }
  221. }
  222. /*
  223. * This combination will remove all invalid subtypes for this type.
  224. */
  225. public function testElggAPIGettersValidSubtypeUsingPluralMixedSubtypesSingleType() {
  226. $type_num = 1;
  227. $subtype_num = 2;
  228. $types = $this->getRandomValidTypes($type_num);
  229. //@todo replace this with $this->getRandomMixedSubtypes()
  230. // we want this to return an invalid subtype for the returned type.
  231. $subtype_types = $types;
  232. $i = 1;
  233. while ($i <= $subtype_num) {
  234. $type = $this->types[$i-1];
  235. if (!in_array($type, $subtype_types)) {
  236. $subtype_types[] = $type;
  237. }
  238. $i++;
  239. }
  240. $subtypes = $this->getRandomValidSubtypes($subtype_types, $type_num);
  241. $options = array(
  242. 'types' => $types,
  243. 'subtypes' => $subtypes
  244. );
  245. $es = elgg_get_entities($options);
  246. $this->assertIsA($es, 'array');
  247. // this will unset all invalid subtypes for each type that that only
  248. // one entity exists of each.
  249. $this->assertIdentical(count($es), $type_num);
  250. foreach ($es as $e) {
  251. // entities must at least be in the type.
  252. $this->assertTrue(in_array($e->getType(), $types));
  253. // test that this is a valid subtype for the entity type.
  254. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  255. }
  256. }
  257. /***************************
  258. * TYPE_SUBTYPE_PAIRS
  259. ***************************/
  260. /**
  261. * Valid type, valid subtype pairs
  262. */
  263. public function testElggAPIGettersTSPValidTypeValidSubtype() {
  264. $type_num = 1;
  265. $subtype_num = 1;
  266. $types = $this->getRandomValidTypes($type_num);
  267. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  268. $pair = array($types[0] => $subtypes[0]);
  269. $options = array(
  270. 'type_subtype_pairs' => $pair
  271. );
  272. $es = elgg_get_entities($options);
  273. $this->assertIsA($es, 'array');
  274. $this->assertIdentical(count($es), $type_num);
  275. foreach ($es as $e) {
  276. $this->assertTrue(in_array($e->getType(), $types));
  277. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  278. }
  279. }
  280. /**
  281. * Valid type, multiple valid subtypes
  282. */
  283. public function testElggAPIGettersTSPValidTypeValidPluralSubtype() {
  284. $type_num = 1;
  285. $subtype_num = 3;
  286. $types = $this->getRandomValidTypes($type_num);
  287. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  288. $pair = array($types[0] => $subtypes);
  289. $options = array(
  290. 'type_subtype_pairs' => $pair
  291. );
  292. $es = elgg_get_entities($options);
  293. $this->assertIsA($es, 'array');
  294. $this->assertIdentical(count($es), $subtype_num);
  295. foreach ($es as $e) {
  296. $this->assertTrue(in_array($e->getType(), $types));
  297. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  298. }
  299. }
  300. /**
  301. * Valid type, both valid and invalid subtypes
  302. */
  303. public function testElggAPIGettersTSPValidTypeMixedPluralSubtype() {
  304. $type_num = 1;
  305. $valid_subtype_num = 2;
  306. $types = $this->getRandomValidTypes($type_num);
  307. $valid = $this->getRandomValidSubtypes($types, $valid_subtype_num);
  308. $invalid = $this->getRandomInvalids();
  309. $subtypes = array_merge($valid, $invalid);
  310. shuffle($subtypes);
  311. $pair = array($types[0] => $subtypes);
  312. $options = array(
  313. 'type_subtype_pairs' => $pair
  314. );
  315. $es = elgg_get_entities($options);
  316. $this->assertIsA($es, 'array');
  317. $this->assertIdentical(count($es), $valid_subtype_num);
  318. foreach ($es as $e) {
  319. $this->assertTrue(in_array($e->getType(), $types));
  320. $this->assertTrue(in_array($e->getSubtype(), $valid));
  321. }
  322. }
  323. /****************************
  324. * false-RETURNING TESTS
  325. ****************************
  326. * The original bug returned
  327. * all entities when invalid subtypes were passed.
  328. * Because there's a huge numer of combinations that
  329. * return entities, I'm only writing tests for
  330. * things that should return false.
  331. *
  332. * I'm leaving the above in case anyone is inspired to
  333. * write out the rest of the possible combinations
  334. */
  335. /**
  336. * Test invalid types with singular 'type'.
  337. */
  338. public function testElggApiGettersInvalidTypeUsingType() {
  339. $type_arr = $this->getRandomInvalids();
  340. $type = $type_arr[0];
  341. $options = array(
  342. 'type' => $type
  343. );
  344. $es = elgg_get_entities($options);
  345. $this->assertFalse($es);
  346. }
  347. /**
  348. * Test invalid types with plural 'types'.
  349. */
  350. public function testElggApiGettersInvalidTypeUsingTypesAsString() {
  351. $type_arr = $this->getRandomInvalids();
  352. $type = $type_arr[0];
  353. $options = array(
  354. 'types' => $type
  355. );
  356. $es = elgg_get_entities($options);
  357. $this->assertFalse($es);
  358. }
  359. /**
  360. * Test invalid types with plural 'types' and an array of a single type
  361. */
  362. public function testElggApiGettersInvalidTypeUsingTypesAsArray() {
  363. $type_arr = $this->getRandomInvalids(1);
  364. $options = array(
  365. 'types' => $type_arr
  366. );
  367. $es = elgg_get_entities($options);
  368. $this->assertFalse($es);
  369. }
  370. /**
  371. * Test invalid types with plural 'types' and an array of a two types
  372. */
  373. public function testElggApiGettersInvalidTypes() {
  374. $type_arr = $this->getRandomInvalids(2);
  375. $options = array(
  376. 'types' => $type_arr
  377. );
  378. $es = elgg_get_entities($options);
  379. $this->assertFalse($es);
  380. }
  381. public function testElggApiGettersInvalidSubtypeValidType() {
  382. $type_num = 1;
  383. $subtype_num = 1;
  384. $types = $this->getRandomValidTypes($type_num);
  385. $subtypes = $this->getRandomInvalids($subtype_num);
  386. $options = array(
  387. 'types' => $types,
  388. 'subtypes' => $subtypes
  389. );
  390. $es = elgg_get_entities($options);
  391. $this->assertFalse($es);
  392. }
  393. public function testElggApiGettersInvalidSubtypeValidTypes() {
  394. $type_num = 2;
  395. $subtype_num = 1;
  396. $types = $this->getRandomValidTypes($type_num);
  397. $subtypes = $this->getRandomInvalids($subtype_num);
  398. $options = array(
  399. 'types' => $types,
  400. 'subtypes' => $subtypes
  401. );
  402. $es = elgg_get_entities($options);
  403. $this->assertFalse($es);
  404. }
  405. public function testElggApiGettersInvalidSubtypesValidType() {
  406. $type_num = 1;
  407. $subtype_num = 2;
  408. $types = $this->getRandomValidTypes($type_num);
  409. $subtypes = $this->getRandomInvalids($subtype_num);
  410. $options = array(
  411. 'types' => $types,
  412. 'subtypes' => $subtypes
  413. );
  414. $es = elgg_get_entities($options);
  415. $this->assertFalse($es);
  416. }
  417. public function testElggApiGettersInvalidSubtypesValidTypes() {
  418. $type_num = 2;
  419. $subtype_num = 2;
  420. $types = $this->getRandomValidTypes($type_num);
  421. $subtypes = $this->getRandomInvalids($subtype_num);
  422. $options = array(
  423. 'types' => $types,
  424. 'subtypes' => $subtypes
  425. );
  426. $es = elgg_get_entities($options);
  427. $this->assertFalse($es);
  428. }
  429. public function testElggApiGettersTSPInvalidType() {
  430. $type_num = 1;
  431. $types = $this->getRandomInvalids($type_num);
  432. $pair = array();
  433. foreach ($types as $type) {
  434. $pair[$type] = null;
  435. }
  436. $options = array(
  437. 'type_subtype_pairs' => $pair
  438. );
  439. $es = elgg_get_entities($options);
  440. $this->assertFalse($es);
  441. }
  442. public function testElggApiGettersTSPInvalidTypes() {
  443. $type_num = 2;
  444. $types = $this->getRandomInvalids($type_num);
  445. $pair = array();
  446. foreach ($types as $type) {
  447. $pair[$type] = null;
  448. }
  449. $options = array(
  450. 'type_subtype_pairs' => $pair
  451. );
  452. $es = elgg_get_entities($options);
  453. $this->assertFalse($es);
  454. }
  455. public function testElggApiGettersTSPValidTypeInvalidSubtype() {
  456. $type_num = 1;
  457. $subtype_num = 1;
  458. $types = $this->getRandomValidTypes($type_num);
  459. $subtypes = $this->getRandomInvalids($subtype_num);
  460. $pair = array($types[0] => $subtypes[0]);
  461. $options = array(
  462. 'type_subtype_pairs' => $pair
  463. );
  464. $es = elgg_get_entities($options);
  465. $this->assertFalse($es);
  466. }
  467. public function testElggApiGettersTSPValidTypeInvalidSubtypes() {
  468. $type_num = 1;
  469. $subtype_num = 2;
  470. $types = $this->getRandomValidTypes($type_num);
  471. $subtypes = $this->getRandomInvalids($subtype_num);
  472. $pair = array($types[0] => array($subtypes[0], $subtypes[0]));
  473. $options = array(
  474. 'type_subtype_pairs' => $pair
  475. );
  476. $es = elgg_get_entities($options);
  477. $this->assertFalse($es);
  478. }
  479. public function testElggApiGettersTSPValidTypesInvalidSubtypes() {
  480. $type_num = 2;
  481. $subtype_num = 2;
  482. $types = $this->getRandomValidTypes($type_num);
  483. $subtypes = $this->getRandomInvalids($subtype_num);
  484. $pair = array();
  485. foreach ($types as $type) {
  486. $pair[$type] = $subtypes;
  487. }
  488. $options = array(
  489. 'type_subtype_pairs' => $pair
  490. );
  491. $es = elgg_get_entities($options);
  492. $this->assertFalse($es);
  493. }
  494. public function testElggApiGettersEntityNoSubtype() {
  495. // create an entity we can later delete.
  496. // order by guid and limit by 1 should == this entity.
  497. $e = new \ElggObject();
  498. $e->save();
  499. $options = array(
  500. 'type' => 'object',
  501. 'limit' => 1,
  502. 'order_by' => 'guid desc'
  503. );
  504. // grab ourself again to fill out attributes.
  505. $e = get_entity($e->getGUID());
  506. $entities = elgg_get_entities($options);
  507. $this->assertEqual(count($entities), 1);
  508. foreach ($entities as $entity) {
  509. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  510. }
  511. $e->delete();
  512. }
  513. public function testElggApiGettersEntityNoValueSubtypeNotSet() {
  514. // create an entity we can later delete.
  515. // order by time created and limit by 1 should == this entity.
  516. $e = new \ElggObject();
  517. $e->save();
  518. $options = array(
  519. 'type' => 'object',
  520. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  521. 'limit' => 1,
  522. 'order_by' => 'guid desc'
  523. );
  524. // grab ourself again to fill out attributes.
  525. $e = get_entity($e->getGUID());
  526. $entities = elgg_get_entities($options);
  527. $this->assertEqual(count($entities), 1);
  528. foreach ($entities as $entity) {
  529. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  530. }
  531. $e->delete();
  532. }
  533. public function testElggApiGettersEntityNoValueSubtypeSet() {
  534. global $CONFIG;
  535. // create an entity we can later delete.
  536. // order by time created and limit by 1 should == this entity.
  537. $subtype = 'subtype_' . rand();
  538. $e_subtype = new \ElggObject();
  539. $e_subtype->subtype = $subtype;
  540. $e_subtype->save();
  541. $e = new \ElggObject();
  542. $e->save();
  543. $options = array(
  544. 'type' => 'object',
  545. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  546. 'limit' => 1,
  547. 'order_by' => 'guid desc'
  548. );
  549. // grab ourself again to fill out attributes.
  550. $e = get_entity($e->getGUID());
  551. $entities = elgg_get_entities($options);
  552. $this->assertEqual(count($entities), 1);
  553. // this entity should NOT be the entity we just created
  554. // and should have no subtype
  555. foreach ($entities as $entity) {
  556. $this->assertEqual($entity->subtype_id, 0);
  557. }
  558. $e_subtype->delete();
  559. $e->delete();
  560. $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype = '$subtype'";
  561. delete_data($q);
  562. }
  563. public function testElggApiGettersEntitySiteSingular() {
  564. global $CONFIG;
  565. $guids = array();
  566. $obj1 = new \ElggObject();
  567. $obj1->test_md = 'test';
  568. // luckily this is never checked.
  569. $obj1->site_guid = 2;
  570. $obj1->save();
  571. $guids[] = $obj1->guid;
  572. $right_guid = $obj1->guid;
  573. $obj2 = new \ElggObject();
  574. $obj2->test_md = 'test';
  575. $obj2->site_guid = $CONFIG->site->guid;
  576. $obj2->save();
  577. $guids[] = $obj2->guid;
  578. $options = array(
  579. 'metadata_name' => 'test_md',
  580. 'metadata_value' => 'test',
  581. 'site_guid' => 2
  582. );
  583. $es = elgg_get_entities_from_metadata($options);
  584. $this->assertTrue(is_array($es));
  585. $this->assertEqual(1, count($es));
  586. $this->assertEqual($right_guid, $es[0]->guid);
  587. foreach ($guids as $guid) {
  588. get_entity($guid)->delete();
  589. }
  590. }
  591. public function testElggApiGettersEntitySiteSingularAny() {
  592. global $CONFIG;
  593. $guids = array();
  594. $obj1 = new \ElggObject();
  595. $obj1->test_md = 'test';
  596. // luckily this is never checked.
  597. $obj1->site_guid = 2;
  598. $obj1->save();
  599. $guids[] = $obj1->guid;
  600. $obj2 = new \ElggObject();
  601. $obj2->test_md = 'test';
  602. $obj2->site_guid = $CONFIG->site->guid;
  603. $obj2->save();
  604. $guids[] = $obj2->guid;
  605. $options = array(
  606. 'metadata_name' => 'test_md',
  607. 'metadata_value' => 'test',
  608. 'site_guid' => ELGG_ENTITIES_ANY_VALUE,
  609. 'limit' => 2,
  610. 'order_by' => 'e.guid DESC'
  611. );
  612. $es = elgg_get_entities_from_metadata($options);
  613. $this->assertTrue(is_array($es));
  614. $this->assertEqual(2, count($es));
  615. foreach ($es as $e) {
  616. $this->assertTrue(in_array($e->guid, $guids));
  617. }
  618. foreach ($guids as $guid) {
  619. get_entity($guid)->delete();
  620. }
  621. }
  622. public function testElggApiGettersEntitySitePlural() {
  623. global $CONFIG;
  624. $guids = array();
  625. $obj1 = new \ElggObject();
  626. $obj1->test_md = 'test';
  627. // luckily this is never checked.
  628. $obj1->site_guid = 2;
  629. $obj1->save();
  630. $guids[] = $obj1->guid;
  631. $obj2 = new \ElggObject();
  632. $obj2->test_md = 'test';
  633. $obj2->site_guid = $CONFIG->site->guid;
  634. $obj2->save();
  635. $guids[] = $obj2->guid;
  636. $options = array(
  637. 'metadata_name' => 'test_md',
  638. 'metadata_value' => 'test',
  639. 'site_guids' => array($CONFIG->site->guid, 2),
  640. 'limit' => 2,
  641. 'order_by' => 'e.guid DESC'
  642. );
  643. $es = elgg_get_entities_from_metadata($options);
  644. $this->assertTrue(is_array($es));
  645. $this->assertEqual(2, count($es));
  646. foreach ($es as $e) {
  647. $this->assertTrue(in_array($e->guid, $guids));
  648. }
  649. foreach ($guids as $guid) {
  650. get_entity($guid)->delete();
  651. }
  652. }
  653. public function testElggApiGettersEntitySitePluralSomeInvalid() {
  654. global $CONFIG;
  655. $guids = array();
  656. $obj1 = new \ElggObject();
  657. $obj1->test_md = 'test';
  658. // luckily this is never checked.
  659. $obj1->site_guid = 2;
  660. $obj1->save();
  661. $guids[] = $obj1->guid;
  662. $obj2 = new \ElggObject();
  663. $obj2->test_md = 'test';
  664. $obj2->save();
  665. $guids[] = $obj2->guid;
  666. $right_guid = $obj2->guid;
  667. $options = array(
  668. 'metadata_name' => 'test_md',
  669. 'metadata_value' => 'test',
  670. // just created the first entity so nothing will be "sited" by it.
  671. 'site_guids' => array($CONFIG->site->guid, $guids[0]),
  672. 'limit' => 2,
  673. 'order_by' => 'e.guid DESC'
  674. );
  675. $es = elgg_get_entities_from_metadata($options);
  676. $this->assertTrue(is_array($es));
  677. $this->assertEqual(1, count($es));
  678. $this->assertEqual($es[0]->guid, $right_guid);
  679. foreach ($guids as $guid) {
  680. get_entity($guid)->delete();
  681. }
  682. }
  683. public function testElggApiGettersEntitySitePluralAllInvalid() {
  684. global $CONFIG;
  685. $guids = array();
  686. $obj1 = new \ElggObject();
  687. $obj1->test_md = 'test';
  688. // luckily this is never checked.
  689. $obj1->site_guid = 2;
  690. $obj1->save();
  691. $guids[] = $obj1->guid;
  692. $obj2 = new \ElggObject();
  693. $obj2->test_md = 'test';
  694. $obj2->save();
  695. $guids[] = $obj2->guid;
  696. $right_guid = $obj2->guid;
  697. $options = array(
  698. 'metadata_name' => 'test_md',
  699. 'metadata_value' => 'test',
  700. // just created the first entity so nothing will be "sited" by it.
  701. 'site_guids' => array($guids[0], $guids[1]),
  702. 'limit' => 2,
  703. 'order_by' => 'e.guid DESC'
  704. );
  705. $es = elgg_get_entities_from_metadata($options);
  706. $this->assertTrue(empty($es));
  707. foreach ($guids as $guid) {
  708. get_entity($guid)->delete();
  709. }
  710. }
  711. public function testElggGetEntitiesByGuidSingular() {
  712. foreach ($this->entities as $e) {
  713. $options = array(
  714. 'guid' => $e->guid
  715. );
  716. $es = elgg_get_entities($options);
  717. $this->assertEqual(count($es), 1);
  718. $this->assertEqual($es[0]->guid, $e->guid);
  719. }
  720. }
  721. public function testElggGetEntitiesByGuidPlural() {
  722. $guids = array();
  723. foreach ($this->entities as $e) {
  724. $guids[] = $e->guid;
  725. }
  726. $options = array(
  727. 'guids' => $guids,
  728. 'limit' => 100
  729. );
  730. $es = elgg_get_entities($options);
  731. $this->assertEqual(count($es), count($this->entities));
  732. foreach ($es as $e) {
  733. $this->assertTrue(in_array($e->guid, $guids));
  734. }
  735. }
  736. public function testElggGetEntitiesBadWheres() {
  737. $options = array(
  738. 'container_guid' => 'abc'
  739. );
  740. $entities = elgg_get_entities($options);
  741. $this->assertFalse($entities);
  742. }
  743. public function testEGEEmptySubtypePlurality() {
  744. $options = array(
  745. 'type' => 'user',
  746. 'subtypes' => ''
  747. );
  748. $entities = elgg_get_entities($options);
  749. $this->assertTrue(is_array($entities));
  750. $options = array(
  751. 'type' => 'user',
  752. 'subtype' => ''
  753. );
  754. $entities = elgg_get_entities($options);
  755. $this->assertTrue(is_array($entities));
  756. $options = array(
  757. 'type' => 'user',
  758. 'subtype' => array('')
  759. );
  760. $entities = elgg_get_entities($options);
  761. $this->assertTrue(is_array($entities));
  762. $options = array(
  763. 'type' => 'user',
  764. 'subtypes' => array('')
  765. );
  766. $entities = elgg_get_entities($options);
  767. $this->assertTrue(is_array($entities));
  768. }
  769. public function testDistinctCanBeDisabled() {
  770. $prefix = _elgg_services()->config->get('dbprefix');
  771. $options = array(
  772. 'callback' => '',
  773. 'joins' => array(
  774. "RIGHT JOIN {$prefix}metadata m ON (e.guid = m.entity_guid)"
  775. ),
  776. 'wheres' => array(
  777. 'm.entity_guid = ' . elgg_get_logged_in_user_guid(),
  778. ),
  779. );
  780. $users = elgg_get_entities($options);
  781. $this->assertEqual(1, count($users));
  782. $options['distinct'] = false;
  783. $users = elgg_get_entities($options);
  784. $this->assertTrue(count($users) > 1);
  785. }
  786. }