PageRenderTime 108ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/tests/ElggCoreEntityGetterFunctionsTest.php

https://github.com/fragilbert/Elgg
PHP | 2884 lines | 2052 code | 585 blank | 247 comment | 44 complexity | ceb9e706e740237dbb48b3ff474e692b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Elgg Test Entity Getter Functions
  4. * @package Elgg
  5. * @subpackage Test
  6. */
  7. class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest {
  8. /**
  9. * Called before each test object.
  10. */
  11. public function __construct() {
  12. elgg_set_ignore_access(TRUE);
  13. $this->entities = array();
  14. $this->subtypes = array(
  15. 'object' => array(),
  16. 'user' => array(),
  17. 'group' => array(),
  18. //'site' => array()
  19. );
  20. // sites are a bit wonky. Don't use them just now.
  21. $this->types = array('object', 'user', 'group');
  22. // create some fun objects to play with.
  23. // 5 with random subtypes
  24. for ($i=0; $i<5; $i++) {
  25. $subtype = 'test_object_subtype_' . rand();
  26. $e = new ElggObject();
  27. $e->subtype = $subtype;
  28. $e->save();
  29. $this->entities[] = $e;
  30. $this->subtypes['object'][] = $subtype;
  31. }
  32. // and users
  33. for ($i=0; $i<5; $i++) {
  34. $subtype = "test_user_subtype_" . rand();
  35. $e = new ElggUser();
  36. $e->username = "test_user_" . rand();
  37. $e->subtype = $subtype;
  38. $e->save();
  39. $this->entities[] = $e;
  40. $this->subtypes['user'][] = $subtype;
  41. }
  42. // and groups
  43. for ($i=0; $i<5; $i++) {
  44. $subtype = "test_group_subtype_" . rand();
  45. $e = new ElggGroup();
  46. $e->subtype = $subtype;
  47. $e->save();
  48. $this->entities[] = $e;
  49. $this->subtypes['group'][] = $subtype;
  50. }
  51. parent::__construct();
  52. }
  53. /**
  54. * Called after each test method.
  55. */
  56. public function setUp() {
  57. return TRUE;
  58. }
  59. /**
  60. * Called after each test method.
  61. */
  62. public function tearDown() {
  63. return TRUE;
  64. }
  65. /**
  66. * Called after each test object.
  67. */
  68. public function __destruct() {
  69. global $CONFIG;
  70. $this->swallowErrors();
  71. foreach ($this->entities as $e) {
  72. $e->delete();
  73. }
  74. // manually remove subtype entries since there is no way
  75. // to using the API.
  76. $subtype_arr = array();
  77. foreach ($this->subtypes as $type => $subtypes) {
  78. foreach ($subtypes as $subtype) {
  79. $subtype_arr[] = "'$subtype'";
  80. }
  81. }
  82. $subtype_str = implode(',', $subtype_arr);
  83. $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype IN ($subtype_str)";
  84. delete_data($q);
  85. parent::__destruct();
  86. }
  87. /*************************************************
  88. * Helpers for getting random types and subtypes *
  89. *************************************************/
  90. /**
  91. * Get a random valid subtype
  92. *
  93. * @param int $num
  94. * @return array
  95. */
  96. public function getRandomValidTypes($num = 1) {
  97. $r = array();
  98. for ($i=1; $i<=$num; $i++) {
  99. do {
  100. $t = $this->types[array_rand($this->types)];
  101. } while (in_array($t, $r) && count($r) < count($this->types));
  102. $r[] = $t;
  103. }
  104. shuffle($r);
  105. return $r;
  106. }
  107. /**
  108. * Get a random valid subtype (that we just created)
  109. *
  110. * @param array $type Type of objects to return valid subtypes for.
  111. * @param int $num of subtypes.
  112. *
  113. * @return array
  114. */
  115. public function getRandomValidSubtypes(array $types, $num = 1) {
  116. $r = array();
  117. for ($i=1; $i<=$num; $i++) {
  118. do {
  119. // make sure at least one subtype of each type is returned.
  120. if ($i-1 < count($types)) {
  121. $type = $types[$i-1];
  122. } else {
  123. $type = $types[array_rand($types)];
  124. }
  125. $k = array_rand($this->subtypes[$type]);
  126. $t = $this->subtypes[$type][$k];
  127. } while (in_array($t, $r));
  128. $r[] = $t;
  129. }
  130. shuffle($r);
  131. return $r;
  132. }
  133. /**
  134. * Return an array of invalid strings for type or subtypes.
  135. *
  136. * @param int $num
  137. * @return arr
  138. */
  139. public function getRandomInvalids($num = 1) {
  140. $r = array();
  141. for ($i=1; $i<=$num; $i++) {
  142. $r[] = 'random_invalid_' . rand();
  143. }
  144. return $r;
  145. }
  146. /**
  147. * Get a mix of valid and invalid types
  148. *
  149. * @param int $num
  150. * @return array
  151. */
  152. public function getRandomMixedTypes($num = 2) {
  153. $have_valid = $have_invalid = false;
  154. $r = array();
  155. // need at least one of each type.
  156. $valid_n = rand(1, $num-1);
  157. $r = array_merge($r, $this->getRandomValidTypes($valid_n));
  158. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  159. shuffle($r);
  160. return $r;
  161. }
  162. /**
  163. * Get random mix of valid and invalid subtypes for types given.
  164. *
  165. * @param array $types
  166. * @param int $num
  167. * @return array
  168. */
  169. public function getRandomMixedSubtypes(array $types, $num = 2) {
  170. $types_c = count($types);
  171. $r = array();
  172. // this can be more efficient but I'm very sleepy...
  173. // want at least one of valid and invalid of each type sent.
  174. for ($i=0; $i < $types_c && $num > 0; $i++) {
  175. // make sure we have a valid and invalid for each type
  176. if (true) {
  177. $type = $types[$i];
  178. $r = array_merge($r, $this->getRandomValidSubtypes(array($type), 1));
  179. $r = array_merge($r, $this->getRandomInvalids(1));
  180. $num -= 2;
  181. }
  182. }
  183. if ($num > 0) {
  184. $valid_n = rand(1, $num);
  185. $r = array_merge($r, $this->getRandomValidSubtypes($types, $valid_n));
  186. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  187. }
  188. //shuffle($r);
  189. return $r;
  190. }
  191. /**
  192. * Creates random annotations on $entity
  193. *
  194. * @param ElggEntity $entity
  195. * @param int $max
  196. */
  197. public function createRandomAnnotations($entity, $max = 1) {
  198. $annotations = array();
  199. for ($i=0; $i<$max; $i++) {
  200. $name = 'test_annotation_name_' . rand();
  201. $value = rand();
  202. $id = create_annotation($entity->getGUID(), $name, $value, 'integer', $entity->getGUID());
  203. $annotations[] = elgg_get_annotation_from_id($id);
  204. }
  205. return $annotations;
  206. }
  207. /***********************************
  208. * TYPE TESTS
  209. ***********************************
  210. * check for getting a valid type in all ways we can.
  211. * note that these aren't wonderful tests as there will be
  212. * existing entities so we can't test against the ones we just created.
  213. * So these just test that some are returned and match the type(s) requested.
  214. * It could definitely be the case that the first 10 entities retrieved are all
  215. * objects. Maybe best to limit to 4 and group by type.
  216. */
  217. public function testElggAPIGettersValidTypeUsingType() {
  218. $type_arr = $this->getRandomValidTypes();
  219. $type = $type_arr[0];
  220. $options = array(
  221. 'type' => $type,
  222. 'group_by' => 'e.type'
  223. );
  224. $es = elgg_get_entities($options);
  225. $this->assertIsA($es, 'array');
  226. // should only ever return one object because of group by
  227. $this->assertIdentical(count($es), 1);
  228. foreach ($es as $e) {
  229. $this->assertTrue(in_array($e->getType(), $type_arr));
  230. }
  231. }
  232. public function testElggAPIGettersValidTypeUsingTypesAsString() {
  233. $type_arr = $this->getRandomValidTypes();
  234. $type = $type_arr[0];
  235. $options = array(
  236. 'types' => $type,
  237. 'group_by' => 'e.type'
  238. );
  239. $es = elgg_get_entities($options);
  240. $this->assertIsA($es, 'array');
  241. // should only ever return one object because of group by
  242. $this->assertIdentical(count($es), 1);
  243. foreach ($es as $e) {
  244. $this->assertTrue(in_array($e->getType(), $type_arr));
  245. }
  246. }
  247. public function testElggAPIGettersValidTypeUsingTypesAsArray() {
  248. $type_arr = $this->getRandomValidTypes();
  249. $type = $type_arr[0];
  250. $options = array(
  251. 'types' => $type_arr,
  252. 'group_by' => 'e.type'
  253. );
  254. $es = elgg_get_entities($options);
  255. $this->assertIsA($es, 'array');
  256. // should only ever return one object because of group by
  257. $this->assertIdentical(count($es), 1);
  258. foreach ($es as $e) {
  259. $this->assertTrue(in_array($e->getType(), $type_arr));
  260. }
  261. }
  262. public function testElggAPIGettersValidTypeUsingTypesAsArrayPlural() {
  263. $num = 2;
  264. $types = $this->getRandomValidTypes($num);
  265. $options = array(
  266. 'types' => $types,
  267. 'group_by' => 'e.type'
  268. );
  269. $es = elgg_get_entities($options);
  270. $this->assertIsA($es, 'array');
  271. // one of object and one of group
  272. $this->assertIdentical(count($es), $num);
  273. foreach ($es as $e) {
  274. $this->assertTrue(in_array($e->getType(), $types));
  275. }
  276. }
  277. /*
  278. * Test mixed valid and invalid types.
  279. */
  280. public function testElggAPIGettersValidAndInvalidTypes() {
  281. //@todo replace this with $this->getRandomMixedTypes().
  282. $t = $this->getRandomValidTypes();
  283. $valid = $t[0];
  284. $t = $this->getRandomInvalids();
  285. $invalid = $t[0];
  286. $options = array(
  287. 'types' => array($invalid, $valid),
  288. 'group_by' => 'e.type'
  289. );
  290. $es = elgg_get_entities($options);
  291. $this->assertIsA($es, 'array');
  292. // should only ever return one object because of group by
  293. $this->assertIdentical(count($es), 1);
  294. $this->assertIdentical($es[0]->getType(), $valid);
  295. }
  296. public function testElggAPIGettersValidAndInvalidTypesPlural() {
  297. $valid_num = 2;
  298. $invalid_num = 3;
  299. $valid = $this->getRandomValidTypes($valid_num);
  300. $invalid = $this->getRandomInvalids($invalid_num);
  301. $types = array();
  302. foreach ($valid as $t) {
  303. $types[] = $t;
  304. }
  305. foreach ($invalid as $t) {
  306. $types[] = $t;
  307. }
  308. shuffle($types);
  309. $options = array(
  310. 'types' => $types,
  311. 'group_by' => 'e.type'
  312. );
  313. $es = elgg_get_entities($options);
  314. $this->assertIsA($es, 'array');
  315. // should only ever return one object because of group by
  316. $this->assertIdentical(count($es), $valid_num);
  317. foreach ($es as $e) {
  318. $this->assertTrue(in_array($e->getType(), $valid));
  319. }
  320. }
  321. /**************************************
  322. * SUBTYPE TESTS
  323. **************************************
  324. *
  325. * Here we can use the subtypes we created to test more finely.
  326. * Subtypes are bound to types, so we must pass a type.
  327. * This is where the fun logic starts.
  328. */
  329. public function testElggAPIGettersValidSubtypeUsingSubtypeSingularType() {
  330. $types = $this->getRandomValidTypes();
  331. $subtypes = $this->getRandomValidSubtypes($types);
  332. $subtype = $subtypes[0];
  333. $options = array(
  334. 'types' => $types,
  335. 'subtype' => $subtype
  336. );
  337. $es = elgg_get_entities($options);
  338. $this->assertIsA($es, 'array');
  339. $this->assertIdentical(count($es), 1);
  340. foreach ($es as $e) {
  341. $this->assertTrue(in_array($e->getType(), $types));
  342. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  343. }
  344. }
  345. public function testElggAPIGettersValidSubtypeUsingSubtypesAsStringSingularType() {
  346. $types = $this->getRandomValidTypes();
  347. $subtypes = $this->getRandomValidSubtypes($types);
  348. $subtype = $subtypes[0];
  349. $options = array(
  350. 'types' => $types,
  351. 'subtype' => $subtype
  352. );
  353. $es = elgg_get_entities($options);
  354. $this->assertIsA($es, 'array');
  355. $this->assertIdentical(count($es), 1);
  356. foreach ($es as $e) {
  357. $this->assertTrue(in_array($e->getType(), $types));
  358. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  359. }
  360. }
  361. public function testElggAPIGettersValidSubtypeUsingSubtypesAsArraySingularType() {
  362. $types = $this->getRandomValidTypes();
  363. $subtypes = $this->getRandomValidSubtypes($types);
  364. $options = array(
  365. 'types' => $types,
  366. 'subtypes' => $subtypes
  367. );
  368. $es = elgg_get_entities($options);
  369. $this->assertIsA($es, 'array');
  370. $this->assertIdentical(count($es), 1);
  371. foreach ($es as $e) {
  372. $this->assertTrue(in_array($e->getType(), $types));
  373. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  374. }
  375. }
  376. public function testElggAPIGettersValidSubtypeUsingPluralSubtypesSingularType() {
  377. $subtype_num = 2;
  378. $types = $this->getRandomValidTypes();
  379. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  380. $options = array(
  381. 'types' => $types,
  382. 'subtypes' => $subtypes
  383. );
  384. $es = elgg_get_entities($options);
  385. $this->assertIsA($es, 'array');
  386. $this->assertIdentical(count($es), $subtype_num);
  387. foreach ($es as $e) {
  388. $this->assertTrue(in_array($e->getType(), $types));
  389. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  390. }
  391. }
  392. /*
  393. Because we're looking for type OR subtype (sorta)
  394. it's possible that we've pulled in entities that aren't
  395. of the subtype we've requested.
  396. THIS COMBINATION MAKES LITTLE SENSE.
  397. There is no mechanism in elgg to retrieve a subtype without a type, so
  398. this combo gets trimmed down to only including subtypes that are valid to
  399. each particular type.
  400. FOR THE LOVE OF ALL GOOD PLEASE JUST USE TYPE_SUBTYPE_PAIRS!
  401. */
  402. public function testElggAPIGettersValidSubtypeUsingPluralSubtypesPluralTypes() {
  403. $type_num = 2;
  404. $subtype_num = 2;
  405. $types = $this->getRandomValidTypes($type_num);
  406. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  407. $options = array(
  408. 'types' => $types,
  409. 'subtypes' => $subtypes
  410. );
  411. $es = elgg_get_entities($options);
  412. $this->assertIsA($es, 'array');
  413. // this will unset all invalid subtypes for each type that that only
  414. // one entity exists of each.
  415. $this->assertIdentical(count($es), $subtype_num);
  416. foreach ($es as $e) {
  417. // entities must at least be in the type.
  418. $this->assertTrue(in_array($e->getType(), $types));
  419. // test that this is a valid subtype for the entity type.
  420. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  421. }
  422. }
  423. /*
  424. * This combination will remove all invalid subtypes for this type.
  425. */
  426. public function testElggAPIGettersValidSubtypeUsingPluralMixedSubtypesSingleType() {
  427. $type_num = 1;
  428. $subtype_num = 2;
  429. $types = $this->getRandomValidTypes($type_num);
  430. //@todo replace this with $this->getRandomMixedSubtypes()
  431. // we want this to return an invalid subtype for the returned type.
  432. $subtype_types = $types;
  433. $i = 1;
  434. while ($i <= $subtype_num) {
  435. $type = $this->types[$i-1];
  436. if (!in_array($type, $subtype_types)) {
  437. $subtype_types[] = $type;
  438. }
  439. $i++;
  440. }
  441. $subtypes = $this->getRandomValidSubtypes($subtype_types, $type_num);
  442. $options = array(
  443. 'types' => $types,
  444. 'subtypes' => $subtypes
  445. );
  446. $es = elgg_get_entities($options);
  447. $this->assertIsA($es, 'array');
  448. // this will unset all invalid subtypes for each type that that only
  449. // one entity exists of each.
  450. $this->assertIdentical(count($es), $type_num);
  451. foreach ($es as $e) {
  452. // entities must at least be in the type.
  453. $this->assertTrue(in_array($e->getType(), $types));
  454. // test that this is a valid subtype for the entity type.
  455. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  456. }
  457. }
  458. /***************************
  459. * TYPE_SUBTYPE_PAIRS
  460. ***************************/
  461. /**
  462. * Valid type, valid subtype pairs
  463. */
  464. public function testElggAPIGettersTSPValidTypeValidSubtype() {
  465. $type_num = 1;
  466. $subtype_num = 1;
  467. $types = $this->getRandomValidTypes($type_num);
  468. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  469. $pair = array($types[0] => $subtypes[0]);
  470. $options = array(
  471. 'type_subtype_pairs' => $pair
  472. );
  473. $es = elgg_get_entities($options);
  474. $this->assertIsA($es, 'array');
  475. $this->assertIdentical(count($es), $type_num);
  476. foreach ($es as $e) {
  477. $this->assertTrue(in_array($e->getType(), $types));
  478. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  479. }
  480. }
  481. /**
  482. * Valid type, multiple valid subtypes
  483. */
  484. public function testElggAPIGettersTSPValidTypeValidPluralSubtype() {
  485. $type_num = 1;
  486. $subtype_num = 3;
  487. $types = $this->getRandomValidTypes($type_num);
  488. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  489. $pair = array($types[0] => $subtypes);
  490. $options = array(
  491. 'type_subtype_pairs' => $pair
  492. );
  493. $es = elgg_get_entities($options);
  494. $this->assertIsA($es, 'array');
  495. $this->assertIdentical(count($es), $subtype_num);
  496. foreach ($es as $e) {
  497. $this->assertTrue(in_array($e->getType(), $types));
  498. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  499. }
  500. }
  501. /**
  502. * Valid type, both valid and invalid subtypes
  503. */
  504. public function testElggAPIGettersTSPValidTypeMixedPluralSubtype() {
  505. $type_num = 1;
  506. $valid_subtype_num = 2;
  507. $types = $this->getRandomValidTypes($type_num);
  508. $valid = $this->getRandomValidSubtypes($types, $valid_subtype_num);
  509. $invalid = $this->getRandomInvalids();
  510. $subtypes = array_merge($valid, $invalid);
  511. shuffle($subtypes);
  512. $pair = array($types[0] => $subtypes);
  513. $options = array(
  514. 'type_subtype_pairs' => $pair
  515. );
  516. $es = elgg_get_entities($options);
  517. $this->assertIsA($es, 'array');
  518. $this->assertIdentical(count($es), $valid_subtype_num);
  519. foreach ($es as $e) {
  520. $this->assertTrue(in_array($e->getType(), $types));
  521. $this->assertTrue(in_array($e->getSubtype(), $valid));
  522. }
  523. }
  524. /****************************
  525. * FALSE-RETURNING TESTS
  526. ****************************
  527. * The original bug corrected returned
  528. * all entities when invalid subtypes were passed.
  529. * Because there's a huge numer of combinations that
  530. * return entities, I'm only writing tests for
  531. * things that should return false.
  532. *
  533. * I'm leaving the above in case anyone is inspired to
  534. * write out the rest of the possible combinations
  535. */
  536. /**
  537. * Test invalid types with singular 'type'.
  538. */
  539. public function testElggApiGettersInvalidTypeUsingType() {
  540. $type_arr = $this->getRandomInvalids();
  541. $type = $type_arr[0];
  542. $options = array(
  543. 'type' => $type
  544. );
  545. $es = elgg_get_entities($options);
  546. $this->assertFalse($es);
  547. }
  548. /**
  549. * Test invalid types with plural 'types'.
  550. */
  551. public function testElggApiGettersInvalidTypeUsingTypesAsString() {
  552. $type_arr = $this->getRandomInvalids();
  553. $type = $type_arr[0];
  554. $options = array(
  555. 'types' => $type
  556. );
  557. $es = elgg_get_entities($options);
  558. $this->assertFalse($es);
  559. }
  560. /**
  561. * Test invalid types with plural 'types' and an array of a single type
  562. */
  563. public function testElggApiGettersInvalidTypeUsingTypesAsArray() {
  564. $type_arr = $this->getRandomInvalids(1);
  565. $options = array(
  566. 'types' => $type_arr
  567. );
  568. $es = elgg_get_entities($options);
  569. $this->assertFalse($es);
  570. }
  571. /**
  572. * Test invalid types with plural 'types' and an array of a two types
  573. */
  574. public function testElggApiGettersInvalidTypes() {
  575. $type_arr = $this->getRandomInvalids(2);
  576. $options = array(
  577. 'types' => $type_arr
  578. );
  579. $es = elgg_get_entities($options);
  580. $this->assertFalse($es);
  581. }
  582. public function testElggApiGettersInvalidSubtypeValidType() {
  583. $type_num = 1;
  584. $subtype_num = 1;
  585. $types = $this->getRandomValidTypes($type_num);
  586. $subtypes = $this->getRandomInvalids($subtype_num);
  587. $options = array(
  588. 'types' => $types,
  589. 'subtypes' => $subtypes
  590. );
  591. $es = elgg_get_entities($options);
  592. $this->assertFalse($es);
  593. }
  594. public function testElggApiGettersInvalidSubtypeValidTypes() {
  595. $type_num = 2;
  596. $subtype_num = 1;
  597. $types = $this->getRandomValidTypes($type_num);
  598. $subtypes = $this->getRandomInvalids($subtype_num);
  599. $options = array(
  600. 'types' => $types,
  601. 'subtypes' => $subtypes
  602. );
  603. $es = elgg_get_entities($options);
  604. $this->assertFalse($es);
  605. }
  606. public function testElggApiGettersInvalidSubtypesValidType() {
  607. $type_num = 1;
  608. $subtype_num = 2;
  609. $types = $this->getRandomValidTypes($type_num);
  610. $subtypes = $this->getRandomInvalids($subtype_num);
  611. $options = array(
  612. 'types' => $types,
  613. 'subtypes' => $subtypes
  614. );
  615. $es = elgg_get_entities($options);
  616. $this->assertFalse($es);
  617. }
  618. public function testElggApiGettersInvalidSubtypesValidTypes() {
  619. $type_num = 2;
  620. $subtype_num = 2;
  621. $types = $this->getRandomValidTypes($type_num);
  622. $subtypes = $this->getRandomInvalids($subtype_num);
  623. $options = array(
  624. 'types' => $types,
  625. 'subtypes' => $subtypes
  626. );
  627. $es = elgg_get_entities($options);
  628. $this->assertFalse($es);
  629. }
  630. public function testElggApiGettersTSPInvalidType() {
  631. $type_num = 1;
  632. $types = $this->getRandomInvalids($type_num);
  633. $pair = array();
  634. foreach ($types as $type) {
  635. $pair[$type] = NULL;
  636. }
  637. $options = array(
  638. 'type_subtype_pairs' => $pair
  639. );
  640. $es = elgg_get_entities($options);
  641. $this->assertFalse($es);
  642. }
  643. public function testElggApiGettersTSPInvalidTypes() {
  644. $type_num = 2;
  645. $types = $this->getRandomInvalids($type_num);
  646. $pair = array();
  647. foreach ($types as $type) {
  648. $pair[$type] = NULL;
  649. }
  650. $options = array(
  651. 'type_subtype_pairs' => $pair
  652. );
  653. $es = elgg_get_entities($options);
  654. $this->assertFalse($es);
  655. }
  656. public function testElggApiGettersTSPValidTypeInvalidSubtype() {
  657. $type_num = 1;
  658. $subtype_num = 1;
  659. $types = $this->getRandomValidTypes($type_num);
  660. $subtypes = $this->getRandomInvalids($subtype_num);
  661. $pair = array($types[0] => $subtypes[0]);
  662. $options = array(
  663. 'type_subtype_pairs' => $pair
  664. );
  665. $es = elgg_get_entities($options);
  666. $this->assertFalse($es);
  667. }
  668. public function testElggApiGettersTSPValidTypeInvalidSubtypes() {
  669. $type_num = 1;
  670. $subtype_num = 2;
  671. $types = $this->getRandomValidTypes($type_num);
  672. $subtypes = $this->getRandomInvalids($subtype_num);
  673. $pair = array($types[0] => array($subtypes[0], $subtypes[0]));
  674. $options = array(
  675. 'type_subtype_pairs' => $pair
  676. );
  677. $es = elgg_get_entities($options);
  678. $this->assertFalse($es);
  679. }
  680. public function testElggApiGettersTSPValidTypesInvalidSubtypes() {
  681. $type_num = 2;
  682. $subtype_num = 2;
  683. $types = $this->getRandomValidTypes($type_num);
  684. $subtypes = $this->getRandomInvalids($subtype_num);
  685. $pair = array();
  686. foreach ($types as $type) {
  687. $pair[$type] = $subtypes;
  688. }
  689. $options = array(
  690. 'type_subtype_pairs' => $pair
  691. );
  692. $es = elgg_get_entities($options);
  693. $this->assertFalse($es);
  694. }
  695. public function testElggApiGettersEntityNoSubtype() {
  696. // create an entity we can later delete.
  697. // order by guid and limit by 1 should == this entity.
  698. $e = new ElggObject();
  699. $e->save();
  700. $options = array(
  701. 'type' => 'object',
  702. 'limit' => 1,
  703. 'order_by' => 'guid desc'
  704. );
  705. // grab ourself again to fill out attributes.
  706. $e = get_entity($e->getGUID());
  707. $entities = elgg_get_entities($options);
  708. $this->assertEqual(count($entities), 1);
  709. foreach ($entities as $entity) {
  710. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  711. }
  712. $e->delete();
  713. }
  714. public function testElggApiGettersEntityNoValueSubtypeNotSet() {
  715. // create an entity we can later delete.
  716. // order by time created and limit by 1 should == this entity.
  717. $e = new ElggObject();
  718. $e->save();
  719. $options = array(
  720. 'type' => 'object',
  721. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  722. 'limit' => 1,
  723. 'order_by' => 'guid desc'
  724. );
  725. // grab ourself again to fill out attributes.
  726. $e = get_entity($e->getGUID());
  727. $entities = elgg_get_entities($options);
  728. $this->assertEqual(count($entities), 1);
  729. foreach ($entities as $entity) {
  730. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  731. }
  732. $e->delete();
  733. }
  734. public function testElggApiGettersEntityNoValueSubtypeSet() {
  735. global $CONFIG;
  736. // create an entity we can later delete.
  737. // order by time created and limit by 1 should == this entity.
  738. $subtype = 'subtype_' . rand();
  739. $e_subtype = new ElggObject();
  740. $e_subtype->subtype = $subtype;
  741. $e_subtype->save();
  742. $e = new ElggObject();
  743. $e->save();
  744. $options = array(
  745. 'type' => 'object',
  746. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  747. 'limit' => 1,
  748. 'order_by' => 'guid desc'
  749. );
  750. // grab ourself again to fill out attributes.
  751. $e = get_entity($e->getGUID());
  752. $entities = elgg_get_entities($options);
  753. $this->assertEqual(count($entities), 1);
  754. // this entity should NOT be the entity we just created
  755. // and should have no subtype
  756. foreach ($entities as $entity) {
  757. $this->assertEqual($entity->subtype_id, 0);
  758. }
  759. $e_subtype->delete();
  760. $e->delete();
  761. $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype = '$subtype'";
  762. delete_data($q);
  763. }
  764. /************
  765. * METADATA
  766. ************/
  767. //names
  768. function testElggApiGettersEntityMetadataNameValidSingle() {
  769. // create a new entity with a subtype we know
  770. // use an existing type so it will clean up automatically
  771. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  772. $subtype = $subtypes[0];
  773. $md_name = 'test_metadata_name_' . rand();
  774. $md_value = 'test_metadata_value_' . rand();
  775. $e = new ElggObject();
  776. $e->subtype = $subtype;
  777. $e->$md_name = $md_value;
  778. $e->save();
  779. $options = array(
  780. 'type' => 'object',
  781. 'subtype' => $subtype,
  782. 'metadata_name' => $md_name
  783. );
  784. $entities = elgg_get_entities_from_metadata($options);
  785. $this->assertIsa($entities, 'array');
  786. $this->assertEqual(count($entities), 1);
  787. foreach ($entities as $entity) {
  788. $this->assertEqual($entity->getGUID(), $e->getGUID());
  789. $this->assertEqual($entity->$md_name, $md_value);
  790. }
  791. $e->delete();
  792. }
  793. function testElggApiGettersEntityMetadataNameValidMultiple() {
  794. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  795. $subtype = $subtypes[0];
  796. $md_names = array();
  797. $md_name = 'test_metadata_name_' . rand();
  798. $md_value = 'test_metadata_value_' . rand();
  799. $md_names[] = $md_name;
  800. $e_guids = array();
  801. $e = new ElggObject();
  802. $e->subtype = $subtype;
  803. $e->$md_name = $md_value;
  804. $e->save();
  805. $e_guids[] = $e->getGUID();
  806. $md_name = 'test_metadata_name_' . rand();
  807. $md_value = 'test_metadata_value_' . rand();
  808. $md_names[] = $md_name;
  809. $e = new ElggObject();
  810. $e->subtype = $subtype;
  811. $e->$md_name = $md_value;
  812. $e->save();
  813. $e_guids[] = $e->getGUID();
  814. $options = array(
  815. 'type' => 'object',
  816. 'subtype' => $subtype,
  817. 'metadata_names' => $md_names
  818. );
  819. $entities = elgg_get_entities_from_metadata($options);
  820. $this->assertIsa($entities, 'array');
  821. $this->assertEqual(count($entities), 2);
  822. foreach ($entities as $entity) {
  823. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  824. $entity->delete();
  825. }
  826. }
  827. function testElggApiGettersEntityMetadataNameInvalidSingle() {
  828. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  829. $subtype = $subtypes[0];
  830. $md_name = 'test_metadata_name_' . rand();
  831. $md_value = 'test_metadata_value_' . rand();
  832. $e = new ElggObject();
  833. $e->subtype = $subtype;
  834. $e->$md_name = $md_value;
  835. $e->save();
  836. $md_invalid_name = 'test_metadata_name_' . rand();
  837. $options = array(
  838. 'type' => 'object',
  839. 'subtype' => $subtype,
  840. 'metadata_name' => $md_invalid_name
  841. );
  842. $entities = elgg_get_entities_from_metadata($options);
  843. $this->assertIdentical(array(), $entities);
  844. $e->delete();
  845. }
  846. function testElggApiGettersEntityMetadataNameInvalidMultiple() {
  847. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  848. $subtype = $subtypes[0];
  849. $md_name = 'test_metadata_name_' . rand();
  850. $md_value = 'test_metadata_value_' . rand();
  851. $e = new ElggObject();
  852. $e->subtype = $subtype;
  853. $e->$md_name = $md_value;
  854. $e->save();
  855. $md_invalid_names = array();
  856. $md_invalid_names[] = 'test_metadata_name_' . rand();
  857. $md_invalid_names[] = 'test_metadata_name_' . rand();
  858. $options = array(
  859. 'type' => 'object',
  860. 'subtype' => $subtype,
  861. 'metadata_names' => $md_invalid_names
  862. );
  863. $entities = elgg_get_entities_from_metadata($options);
  864. $this->assertIdentical(array(), $entities);
  865. $e->delete();
  866. }
  867. function testElggApiGettersEntityMetadataNameMixedMultiple() {
  868. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  869. $subtype = $subtypes[0];
  870. $md_names = array();
  871. $md_name = 'test_metadata_name_' . rand();
  872. $md_value = 'test_metadata_value_' . rand();
  873. $md_names[] = $md_name;
  874. $e_guids = array();
  875. $valid = new ElggObject();
  876. $valid->subtype = $subtype;
  877. $valid->$md_name = $md_value;
  878. $valid->save();
  879. $e_guids[] = $valid->getGUID();
  880. $md_name = 'test_metadata_name_' . rand();
  881. $md_value = 'test_metadata_value_' . rand();
  882. // add a random invalid name.
  883. $md_names[] = 'test_metadata_name_' . rand();
  884. $e = new ElggObject();
  885. $e->subtype = $subtype;
  886. $e->$md_name = $md_value;
  887. $e->save();
  888. $e_guids[] = $e->getGUID();
  889. $options = array(
  890. 'type' => 'object',
  891. 'subtype' => $subtype,
  892. 'metadata_names' => $md_names
  893. );
  894. $entities = elgg_get_entities_from_metadata($options);
  895. $this->assertIsa($entities, 'array');
  896. $this->assertEqual(count($entities), 1);
  897. foreach ($entities as $entity) {
  898. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  899. }
  900. foreach ($e_guids as $guid) {
  901. if ($e = get_entity($guid)) {
  902. $e->delete();
  903. }
  904. }
  905. }
  906. // values
  907. function testElggApiGettersEntityMetadataValueValidSingle() {
  908. // create a new entity with a subtype we know
  909. // use an existing type so it will clean up automatically
  910. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  911. $subtype = $subtypes[0];
  912. $md_name = 'test_metadata_name_' . rand();
  913. $md_value = 'test_metadata_value_' . rand();
  914. $e = new ElggObject();
  915. $e->subtype = $subtype;
  916. $e->$md_name = $md_value;
  917. $e->save();
  918. $options = array(
  919. 'type' => 'object',
  920. 'subtype' => $subtype,
  921. 'metadata_value' => $md_value
  922. );
  923. $entities = elgg_get_entities_from_metadata($options);
  924. $this->assertIsa($entities, 'array');
  925. $this->assertEqual(count($entities), 1);
  926. foreach ($entities as $entity) {
  927. $this->assertEqual($entity->getGUID(), $e->getGUID());
  928. $this->assertEqual($entity->$md_name, $md_value);
  929. }
  930. $e->delete();
  931. }
  932. function testElggApiGettersEntityMetadataValueValidMultiple() {
  933. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  934. $subtype = $subtypes[0];
  935. $md_values = array();
  936. $md_name = 'test_metadata_name_' . rand();
  937. $md_value = 'test_metadata_value_' . rand();
  938. $md_values[] = $md_value;
  939. $e_guids = array();
  940. $e = new ElggObject();
  941. $e->subtype = $subtype;
  942. $e->$md_name = $md_value;
  943. $e->save();
  944. $e_guids[] = $e->getGUID();
  945. $md_name = 'test_metadata_name_' . rand();
  946. $md_value = 'test_metadata_value_' . rand();
  947. $md_values[] = $md_value;
  948. $e = new ElggObject();
  949. $e->subtype = $subtype;
  950. $e->$md_name = $md_value;
  951. $e->save();
  952. $e_guids[] = $e->getGUID();
  953. $options = array(
  954. 'type' => 'object',
  955. 'subtype' => $subtype,
  956. 'metadata_values' => $md_values
  957. );
  958. $entities = elgg_get_entities_from_metadata($options);
  959. $this->assertIsa($entities, 'array');
  960. $this->assertEqual(count($entities), 2);
  961. foreach ($entities as $entity) {
  962. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  963. $entity->delete();
  964. }
  965. }
  966. function testElggApiGettersEntityMetadataValueInvalidSingle() {
  967. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  968. $subtype = $subtypes[0];
  969. $md_name = 'test_metadata_name_' . rand();
  970. $md_value = 'test_metadata_value_' . rand();
  971. $e = new ElggObject();
  972. $e->subtype = $subtype;
  973. $e->$md_name = $md_value;
  974. $e->save();
  975. $md_invalid_value = 'test_metadata_value_' . rand();
  976. $options = array(
  977. 'type' => 'object',
  978. 'subtype' => $subtype,
  979. 'metadata_value' => $md_invalid_value
  980. );
  981. $entities = elgg_get_entities_from_metadata($options);
  982. $this->assertIdentical(array(), $entities);
  983. $e->delete();
  984. }
  985. function testElggApiGettersEntityMetadataValueInvalidMultiple() {
  986. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  987. $subtype = $subtypes[0];
  988. $md_name = 'test_metadata_name_' . rand();
  989. $md_value = 'test_metadata_value_' . rand();
  990. $e = new ElggObject();
  991. $e->subtype = $subtype;
  992. $e->$md_name = $md_value;
  993. $e->save();
  994. $md_invalid_values = array();
  995. $md_invalid_values[] = 'test_metadata_value_' . rand();
  996. $md_invalid_values[] = 'test_metadata_value_' . rand();
  997. $options = array(
  998. 'type' => 'object',
  999. 'subtype' => $subtype,
  1000. 'metadata_values' => $md_invalid_values
  1001. );
  1002. $entities = elgg_get_entities_from_metadata($options);
  1003. $this->assertIdentical(array(), $entities);
  1004. $e->delete();
  1005. }
  1006. function testElggApiGettersEntityMetadataValueMixedMultiple() {
  1007. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1008. $subtype = $subtypes[0];
  1009. $md_values = array();
  1010. $md_name = 'test_metadata_name_' . rand();
  1011. $md_value = 'test_metadata_value_' . rand();
  1012. $md_values[] = $md_value;
  1013. $e_guids = array();
  1014. $valid = new ElggObject();
  1015. $valid->subtype = $subtype;
  1016. $valid->$md_name = $md_value;
  1017. $valid->save();
  1018. $e_guids[] = $valid->getGUID();
  1019. $md_name = 'test_metadata_name_' . rand();
  1020. $md_value = 'test_metadata_value_' . rand();
  1021. // add a random invalid value.
  1022. $md_values[] = 'test_metadata_value_' . rand();
  1023. $e = new ElggObject();
  1024. $e->subtype = $subtype;
  1025. $e->$md_name = $md_value;
  1026. $e->save();
  1027. $e_guids[] = $e->getGUID();
  1028. $options = array(
  1029. 'type' => 'object',
  1030. 'subtype' => $subtype,
  1031. 'metadata_values' => $md_values
  1032. );
  1033. $entities = elgg_get_entities_from_metadata($options);
  1034. $this->assertIsa($entities, 'array');
  1035. $this->assertEqual(count($entities), 1);
  1036. foreach ($entities as $entity) {
  1037. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1038. }
  1039. foreach ($e_guids as $guid) {
  1040. if ($e = get_entity($guid)) {
  1041. $e->delete();
  1042. }
  1043. }
  1044. }
  1045. // name_value_pairs
  1046. function testElggApiGettersEntityMetadataNVPValidNValidVEquals() {
  1047. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1048. $subtype = $subtypes[0];
  1049. $md_name = 'test_metadata_name_' . rand();
  1050. $md_value = 'test_metadata_value_' . rand();
  1051. $guids = array();
  1052. // our target
  1053. $valid = new ElggObject();
  1054. $valid->subtype = $subtype;
  1055. $valid->$md_name = $md_value;
  1056. $valid->save();
  1057. $guids[] = $valid->getGUID();
  1058. // make some bad ones
  1059. $invalid_md_name = 'test_metadata_name_' . rand();
  1060. $e = new ElggObject();
  1061. $e->subtype = $subtype;
  1062. $e->$invalid_md_name = $md_value;
  1063. $e->save();
  1064. $guids[] = $e->getGUID();
  1065. $invalid_md_value = 'test_metadata_value_' . rand();
  1066. $e = new ElggObject();
  1067. $e->subtype = $subtype;
  1068. $e->$md_name = $invalid_md_value;
  1069. $e->save();
  1070. $guids[] = $e->getGUID();
  1071. $md_invalid_names = array();
  1072. $options = array(
  1073. 'type' => 'object',
  1074. 'subtype' => $subtype,
  1075. 'metadata_name_value_pairs' => array(array(
  1076. 'name' => $md_name,
  1077. 'value' => $md_value
  1078. ))
  1079. );
  1080. $entities = elgg_get_entities_from_metadata($options);
  1081. $this->assertIsa($entities, 'array');
  1082. $this->assertEqual(count($entities), 1);
  1083. foreach ($entities as $entity) {
  1084. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1085. $this->assertEqual($entity->$md_name, $md_value);
  1086. $entity->delete();
  1087. }
  1088. foreach ($guids as $guid) {
  1089. if ($e = get_entity($guid)) {
  1090. $e->delete();
  1091. }
  1092. }
  1093. }
  1094. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsTriple() {
  1095. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1096. $subtype = $subtypes[0];
  1097. $md_name = 'test_metadata_name_' . rand();
  1098. $md_value = 'test_metadata_value_' . rand();
  1099. $md_name2 = 'test_metadata_name_' . rand();
  1100. $md_value2 = 'test_metadata_value_' . rand();
  1101. $md_name3 = 'test_metadata_name_' . rand();
  1102. $md_value3 = 'test_metadata_value_' . rand();
  1103. $guids = array();
  1104. // our target
  1105. $valid = new ElggObject();
  1106. $valid->subtype = $subtype;
  1107. $valid->$md_name = $md_value;
  1108. $valid->$md_name2 = $md_value2;
  1109. $valid->$md_name3 = $md_value3;
  1110. $valid->save();
  1111. $guids[] = $valid->getGUID();
  1112. // make some bad ones
  1113. $invalid_md_name = 'test_metadata_name_' . rand();
  1114. $invalid_md_name2 = 'test_metadata_name_' . rand();
  1115. $invalid_md_name3 = 'test_metadata_name_' . rand();
  1116. $e = new ElggObject();
  1117. $e->subtype = $subtype;
  1118. $e->$invalid_md_name = $md_value;
  1119. $e->$invalid_md_name2 = $md_value2;
  1120. $e->$invalid_md_name3 = $md_value3;
  1121. $e->save();
  1122. $guids[] = $e->getGUID();
  1123. $invalid_md_value = 'test_metadata_value_' . rand();
  1124. $e = new ElggObject();
  1125. $e->subtype = $subtype;
  1126. $e->$md_name = $invalid_md_value;
  1127. $e->$md_name2 = $invalid_md_value;
  1128. $e->$md_name3 = $invalid_md_value;
  1129. $e->save();
  1130. $guids[] = $e->getGUID();
  1131. $md_invalid_names = array();
  1132. $options = array(
  1133. 'type' => 'object',
  1134. 'subtype' => $subtype,
  1135. 'metadata_name_value_pairs' => array(
  1136. array(
  1137. 'name' => $md_name,
  1138. 'value' => $md_value
  1139. ),
  1140. array(
  1141. 'name' => $md_name2,
  1142. 'value' => $md_value2
  1143. ),
  1144. array(
  1145. 'name' => $md_name3,
  1146. 'value' => $md_value3
  1147. )
  1148. )
  1149. );
  1150. $entities = elgg_get_entities_from_metadata($options);
  1151. $this->assertIsa($entities, 'array');
  1152. $this->assertEqual(count($entities), 1);
  1153. foreach ($entities as $entity) {
  1154. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1155. $this->assertEqual($entity->$md_name, $md_value);
  1156. $entity->delete();
  1157. }
  1158. foreach ($guids as $guid) {
  1159. if ($e = get_entity($guid)) {
  1160. $e->delete();
  1161. }
  1162. }
  1163. }
  1164. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsDouble() {
  1165. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1166. $subtype = $subtypes[0];
  1167. $md_name = 'test_metadata_name_' . rand();
  1168. $md_value = 'test_metadata_value_' . rand();
  1169. $md_name2 = 'test_metadata_name_' . rand();
  1170. $md_value2 = 'test_metadata_value_' . rand();
  1171. $guids = array();
  1172. // our target
  1173. $valid = new ElggObject();
  1174. $valid->subtype = $subtype;
  1175. $valid->$md_name = $md_value;
  1176. $valid->$md_name2 = $md_value2;
  1177. $valid->save();
  1178. $guids[] = $valid->getGUID();
  1179. // make some bad ones
  1180. $invalid_md_name = 'test_metadata_name_' . rand();
  1181. $invalid_md_name2 = 'test_metadata_name_' . rand();
  1182. $e = new ElggObject();
  1183. $e->subtype = $subtype;
  1184. $e->$invalid_md_name = $md_value;
  1185. $e->$invalid_md_name2 = $md_value2;
  1186. $e->save();
  1187. $guids[] = $e->getGUID();
  1188. $invalid_md_value = 'test_metadata_value_' . rand();
  1189. $e = new ElggObject();
  1190. $e->subtype = $subtype;
  1191. $e->$md_name = $invalid_md_value;
  1192. $e->$md_name2 = $invalid_md_value;
  1193. $e->save();
  1194. $guids[] = $e->getGUID();
  1195. $md_invalid_names = array();
  1196. $options = array(
  1197. 'type' => 'object',
  1198. 'subtype' => $subtype,
  1199. 'metadata_name_value_pairs' => array(
  1200. array(
  1201. 'name' => $md_name,
  1202. 'value' => $md_value
  1203. ),
  1204. array(
  1205. 'name' => $md_name2,
  1206. 'value' => $md_value2
  1207. )
  1208. )
  1209. );
  1210. $entities = elgg_get_entities_from_metadata($options);
  1211. $this->assertIsa($entities, 'array');
  1212. $this->assertEqual(count($entities), 1);
  1213. foreach ($entities as $entity) {
  1214. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1215. $this->assertEqual($entity->$md_name, $md_value);
  1216. $entity->delete();
  1217. }
  1218. foreach ($guids as $guid) {
  1219. if ($e = get_entity($guid)) {
  1220. $e->delete();
  1221. }
  1222. }
  1223. }
  1224. // this keeps locking up my database...
  1225. function xtestElggApiGettersEntityMetadataNVPValidNValidVEqualsStupid() {
  1226. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1227. $subtype = $subtypes[0];
  1228. $md_name = 'test_metadata_name_' . rand();
  1229. $md_value = 'test_metadata_value_' . rand();
  1230. $md_name2 = 'test_metadata_name_' . rand();
  1231. $md_value2 = 'test_metadata_value_' . rand();
  1232. $md_name3 = 'test_metadata_name_' . rand();
  1233. $md_value3 = 'test_metadata_value_' . rand();
  1234. $md_name3 = 'test_metadata_name_' . rand();
  1235. $md_value3 = 'test_metadata_value_' . rand();
  1236. $md_name4 = 'test_metadata_name_' . rand();
  1237. $md_value4 = 'test_metadata_value_' . rand();
  1238. $md_name5 = 'test_metadata_name_' . rand();
  1239. $md_value5 = 'test_metadata_value_' . rand();
  1240. $guids = array();
  1241. // our target
  1242. $valid = new ElggObject();
  1243. $valid->subtype = $subtype;
  1244. $valid->$md_name = $md_value;
  1245. $valid->$md_name2 = $md_value2;
  1246. $valid->$md_name3 = $md_value3;
  1247. $valid->$md_name4 = $md_value4;
  1248. $valid->$md_name5 = $md_value5;
  1249. $valid->save();
  1250. $guids[] = $valid->getGUID();
  1251. // make some bad ones
  1252. $invalid_md_name = 'test_metadata_name_' . rand();
  1253. $e = new ElggObject();
  1254. $e->subtype = $subtype;
  1255. $e->$invalid_md_name = $md_value;
  1256. $e->$md_name2 = $md_value2;
  1257. $e->$md_name3 = $md_value3;
  1258. $e->$md_name4 = $md_value4;
  1259. $e->$md_name5 = $md_value5;
  1260. $e->save();
  1261. $guids[] = $e->getGUID();
  1262. $invalid_md_value = 'test_metadata_value_' . rand();
  1263. $e = new ElggObject();
  1264. $e->subtype = $subtype;
  1265. $e->$md_name = $invalid_md_value;
  1266. $e->$md_name2 = $invalid_md_value;
  1267. $e->$md_name3 = $invalid_md_value;
  1268. $e->$md_name4 = $invalid_md_value;
  1269. $e->$md_name5 = $invalid_md_value;
  1270. $e->save();
  1271. $guids[] = $e->getGUID();
  1272. $md_invalid_names = array();
  1273. $options = array(
  1274. 'type' => 'object',
  1275. 'subtype' => $subtype,
  1276. 'metadata_name_value_pairs' => array(
  1277. array(
  1278. 'name' => $md_name,
  1279. 'value' => $md_value
  1280. ),
  1281. array(
  1282. 'name' => $md_name2,
  1283. 'value' => $md_value2
  1284. ),
  1285. array(
  1286. 'name' => $md_name3,
  1287. 'value' => $md_value3
  1288. ),
  1289. array(
  1290. 'name' => $md_name4,
  1291. 'value' => $md_value4
  1292. ),
  1293. array(
  1294. 'name' => $md_name5,
  1295. 'value' => $md_value5
  1296. ),
  1297. )
  1298. );
  1299. $entities = elgg_get_entities_from_metadata($options);
  1300. $this->assertIsa($entities, 'array');
  1301. $this->assertEqual(count($entities), 1);
  1302. foreach ($entities as $entity) {
  1303. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1304. $this->assertEqual($entity->$md_name, $md_value);
  1305. $entity->delete();
  1306. }
  1307. foreach ($guids as $guid) {
  1308. if ($e = get_entity($guid)) {
  1309. $e->delete();
  1310. }
  1311. }
  1312. }
  1313. /**
  1314. * Name value pair with valid name and invalid value
  1315. */
  1316. function testElggApiGettersEntityMetadataNVPValidNInvalidV() {
  1317. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1318. $subtype = $subtypes[0];
  1319. $md_name = 'test_metadata_name_' . rand();
  1320. $md_value = 'test_metadata_value_' . rand();
  1321. $guids = array();
  1322. // make some bad ones
  1323. $invalid_md_name = 'test_metadata_name_' . rand();
  1324. $e = new ElggObject();
  1325. $e->subtype = $subtype;
  1326. $e->$invalid_md_name = $md_value;
  1327. $e->save();
  1328. $guids[] = $e->getGUID();
  1329. $invalid_md_value = 'test_metadata_value_' . rand();
  1330. $e = new ElggObject();
  1331. $e->subtype = $subtype;
  1332. $e->$md_name = $invalid_md_value;
  1333. $e->save();
  1334. $guids[] = $e->getGUID();
  1335. $md_invalid_names = array();
  1336. $options = array(
  1337. 'type' => 'object',
  1338. 'subtype' => $subtype,
  1339. 'metadata_name_value_pairs' => array(array(
  1340. 'name' => $md_name,
  1341. 'value' => 'test_metadata_value_' . rand()
  1342. ))
  1343. );
  1344. $entities = elgg_get_entities_from_metadata($options);
  1345. $this->assertIdentical(array(), $entities);
  1346. foreach ($guids as $guid) {
  1347. if ($e = get_entity($guid)) {
  1348. $e->delete();
  1349. }
  1350. }
  1351. }
  1352. /**
  1353. * Name value pair with invalid name and valid value
  1354. */
  1355. function testElggApiGettersEntityMetadataNVPInvalidNValidV() {
  1356. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1357. $subtype = $subtypes[0];
  1358. $md_name = 'test_metadata_name_' . rand();
  1359. $md_value = 'test_metadata_value_' . rand();
  1360. $guids = array();
  1361. // make some bad ones
  1362. $invalid_md_name = 'test_metadata_name_' . rand();
  1363. $e = new ElggObject();
  1364. $e->subtype = $subtype;
  1365. $e->$invalid_md_name = $md_value;
  1366. $e->save();
  1367. $guids[] = $e->getGUID();
  1368. $invalid_md_value = 'test_metadata_value_' . rand();
  1369. $e = new ElggObject();
  1370. $e->subtype = $subtype;
  1371. $e->$md_name = $invalid_md_value;
  1372. $e->save();
  1373. $guids[] = $e->getGUID();
  1374. $md_invalid_names = array();
  1375. $options = array(
  1376. 'type' => 'object',
  1377. 'subtype' => $subtype,
  1378. 'metadata_name_value_pairs' => array(array(
  1379. 'name' => 'test_metadata_name_' . rand(),
  1380. 'value' => $md_value
  1381. ))
  1382. );
  1383. $entities = elgg_get_entities_from_metadata($options);
  1384. $this->assertIdentical(array(), $entities);
  1385. foreach ($guids as $guid) {
  1386. if ($e = get_entity($guid)) {
  1387. $e->delete();
  1388. }
  1389. }
  1390. }
  1391. function testElggApiGettersEntityMetadataNVPValidNValidVOperandIn() {
  1392. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1393. $subtype = $subtypes[0];
  1394. $md_name = 'test_metadata_name_' . rand();
  1395. $md_value = 'test_metadata_value_' . rand();
  1396. $guids = array();
  1397. $valid_guids = array();
  1398. // our targets
  1399. $valid = new ElggObject();
  1400. $valid->subtype = $subtype;
  1401. $valid->$md_name = $md_value;
  1402. $valid->save();
  1403. $guids[] = $valid->getGUID();
  1404. $valid_guids[] = $valid->getGUID();
  1405. $md_name2 = 'test_metadata_name_' . rand();
  1406. $md_value2 = 'test_metadata_value_' . rand();
  1407. $valid2 = new ElggObject();
  1408. $valid2->subtype = $subtype;
  1409. $valid2->$md_name2 = $md_value2;
  1410. $valid2->save();
  1411. $guids[] = $valid->getGUID();
  1412. $valid_guids[] = $valid2->getGUID();
  1413. // make some bad ones
  1414. $invalid_md_name = 'test_metadata_name_' . rand();
  1415. $e = new ElggObject();
  1416. $e->subtype = $subtype;
  1417. $e->$invalid_md_name = $md_value;
  1418. $e->save();
  1419. $guids[] = $e->getGUID();
  1420. $invalid_md_value = 'test_metadata_value_' . rand();
  1421. $e = new ElggObject();
  1422. $e->subtype = $subtype;
  1423. $e->$md_name = $invalid_md_value;
  1424. $e->save();
  1425. $guids[] = $e->getGUID();
  1426. $md_valid_values = "'$md_value', '$md_value2'";
  1427. $options = array(
  1428. 'type' => 'object',
  1429. 'subtype' => $subtype,
  1430. 'metadata_name_value_pairs' => array(
  1431. array(
  1432. 'name' => $md_name,
  1433. 'value' => $md_valid_values,
  1434. 'operand' => 'IN'
  1435. ),
  1436. array(
  1437. 'name' => $md_name2,
  1438. 'value' => $md_valid_values,
  1439. 'operand' => 'IN'
  1440. ),
  1441. ),
  1442. 'metadata_name_value_pairs_operator' => 'OR'
  1443. );
  1444. $entities = elgg_get_entities_from_metadata($options);
  1445. $this->assertIsa($entities, 'array');
  1446. $this->assertEqual(count($entities), 2);
  1447. foreach ($entities as $entity) {
  1448. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1449. $entity->delete();
  1450. }
  1451. foreach ($guids as $guid) {
  1452. if ($e = get_entity($guid)) {
  1453. $e->delete();
  1454. }
  1455. }
  1456. }
  1457. function testElggApiGettersEntityMetadataNVPValidNValidVPlural() {
  1458. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1459. $subtype = $subtypes[0];
  1460. $md_name = 'test_metadata_name_' . rand();
  1461. $md_value = 'test_metadata_value_' . rand();
  1462. $guids = array();
  1463. $valid_guids = array();
  1464. // our targets
  1465. $valid = new ElggObject();
  1466. $valid->subtype = $subtype;
  1467. $valid->$md_name = $md_value;
  1468. $valid->save();
  1469. $guids[] = $valid->getGUID();
  1470. $valid_guids[] = $valid->getGUID();
  1471. $md_name2 = 'test_metadata_name_' . rand();
  1472. $md_value2 = 'test_metadata_value_' . rand();
  1473. $valid2 = new ElggObject();
  1474. $valid2->subtype = $subtype;
  1475. $valid2->$md_name2 = $md_value2;
  1476. $valid2->save();
  1477. $guids[] = $valid->getGUID();
  1478. $valid_guids[] = $valid2->getGUID();
  1479. // make some bad ones
  1480. $invalid_md_name = 'test_metadata_name_' . rand();
  1481. $e = new ElggObject();
  1482. $e->subtype = $subtype;
  1483. $e->$invalid_md_name = $md_value;
  1484. $e->save();
  1485. $guids[] = $e->getGUID();
  1486. $invalid_md_value = 'test_metadata_value_' . rand();
  1487. $e = new ElggObject();
  1488. $e->subtype = $subtype;
  1489. $e->$md_name = $invalid_md_value;
  1490. $e->save();
  1491. $guids[] = $e->getGUID();
  1492. $md_valid_values = array($md_value, $md_value2);
  1493. $options = array(
  1494. 'type' => 'object',
  1495. 'subtype' => $subtype,
  1496. 'metadata_name_value_pairs' => array(
  1497. array(
  1498. 'name' => $md_name,
  1499. 'value' => $md_valid_values,
  1500. 'operand' => 'IN'
  1501. ),
  1502. array(
  1503. 'name' => $md_name2,
  1504. 'value' => $md_valid_values,
  1505. 'operand' => 'IN'
  1506. ),
  1507. ),
  1508. 'metadata_name_value_pairs_operator' => 'OR'
  1509. );
  1510. $entities = elgg_get_entities_from_metadata($options);
  1511. $this->assertIsa($entities, 'array');
  1512. $this->assertEqual(count($entities), 2);
  1513. foreach ($entities as $entity) {
  1514. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1515. $entity->delete();
  1516. }
  1517. foreach ($guids as $guid) {
  1518. if ($e = get_entity($guid)) {
  1519. $e->delete();
  1520. }
  1521. }
  1522. }
  1523. function testElggApiGettersEntityMetadataNVPOrderByMDText() {
  1524. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1525. $subtype = $subtypes[0];
  1526. $md_name = 'test_metadata_name_' . rand();
  1527. $guids = array();
  1528. $valid_guids = array();
  1529. // our targets
  1530. $valid = new ElggObject();
  1531. $valid->subtype = $subtype;
  1532. $valid->$md_name = 1;
  1533. $valid->save();
  1534. $guids[] = $valid->getGUID();
  1535. $valid_guids[] = $valid->getGUID();
  1536. $valid2 = new ElggObject();
  1537. $valid2->subtype = $subtype;
  1538. $valid2->$md_name = 2;
  1539. $valid2->save();
  1540. $guids[] = $valid->getGUID();
  1541. $valid_guids[] = $valid2->getGUID();
  1542. $valid3 = new ElggObject();
  1543. $valid3->subtype = $subtype;
  1544. $valid3->$md_name = 3;
  1545. $valid3->save();
  1546. $guids[] = $valid->getGUID();
  1547. $valid_guids[] = $valid3->getGUID();
  1548. $md_valid_values = array(1, 2, 3);
  1549. $options = array(
  1550. 'type' => 'object',
  1551. 'subtype' => $subtype,
  1552. //'metadata_name' => $md_name,
  1553. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  1554. );
  1555. $entities = elgg_get_entities_from_metadata($options);
  1556. $this->assertIsa($entities, 'array');
  1557. $this->assertEqual(count($entities), 3);
  1558. $i = 1;
  1559. foreach ($entities as $entity) {
  1560. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1561. $this->assertEqual($entity->$md_name, $i);
  1562. $i++;
  1563. $entity->delete();
  1564. }
  1565. foreach ($guids as $guid) {
  1566. if ($e = get_entity($guid)) {
  1567. $e->delete();
  1568. }
  1569. }
  1570. }
  1571. function testElggApiGettersEntityMetadataNVPOrderByMDString() {
  1572. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1573. $subtype = $subtypes[0];
  1574. $md_name = 'test_metadata_name_' . rand();
  1575. $guids = array();
  1576. $valid_guids = array();
  1577. // our targets
  1578. $valid = new ElggObject();
  1579. $valid->subtype = $subtype;
  1580. $valid->$md_name = 'a';
  1581. $valid->save();
  1582. $guids[] = $valid->getGUID();
  1583. $valid_guids[] = $valid->getGUID();
  1584. $valid2 = new ElggObject();
  1585. $valid2->subtype = $subtype;
  1586. $valid2->$md_name = 'b';
  1587. $valid2->save();
  1588. $guids[] = $valid->getGUID();
  1589. $valid_guids[] = $valid2->getGUID();
  1590. $valid3 = new ElggObject();
  1591. $valid3->subtype = $subtype;
  1592. $valid3->$md_name = 'c';
  1593. $valid3->save();
  1594. $guids[] = $valid->getGUID();
  1595. $valid_guids[] = $valid3->getGUID();
  1596. $md_valid_values = array('a', 'b', 'c');
  1597. $options = array(
  1598. 'type' => 'object',
  1599. 'subtype' => $subtype,
  1600. 'metadata_name' => $md_name,
  1601. 'order_by_metadata' => array('name' => $md_name, 'as' => 'text')
  1602. );
  1603. $entities = elgg_get_entities_from_metadata($options);
  1604. $this->assertIsa($entities, 'array');
  1605. $this->assertEqual(count($entities), 3);
  1606. $alpha = array('a', 'b', 'c');
  1607. $i = 0;
  1608. foreach ($entities as $entity) {
  1609. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1610. $this->assertEqual($entity->$md_name, $alpha[$i]);
  1611. $i++;
  1612. $entity->delete();
  1613. }
  1614. foreach ($guids as $guid) {
  1615. if ($e = get_entity($guid)) {
  1616. $e->delete();
  1617. }
  1618. }
  1619. }
  1620. /**
  1621. * Annotations
  1622. */
  1623. public function testElggApiGettersEntitiesFromAnnotation() {
  1624. // grab a few different users to annotation
  1625. // there will always be at least 2 here because of the construct.
  1626. $users = elgg_get_entities(array('type' => 'user', 'limit' => 2));
  1627. // create some test annotations
  1628. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1629. $subtype = $subtypes[0];
  1630. $annotation_name = 'test_annotation_name_' . rand();
  1631. $annotation_value = rand(1000, 9999);
  1632. $annotation_name2 = 'test_annotation_name_' . rand();
  1633. $annotation_value2 = rand(1000, 9999);
  1634. $guids = array();
  1635. // our targets
  1636. $valid = new ElggObject();
  1637. $valid->subtype = $subtype;
  1638. $valid->save();
  1639. $guids[] = $valid->getGUID();
  1640. create_annotation($valid->getGUID(), $annotation_name, $annotation_value, 'integer', $users[0]->getGUID());
  1641. $valid2 = new ElggObject();
  1642. $valid2->subtype = $subtype;
  1643. $valid2->save();
  1644. $guids[] = $valid2->getGUID();
  1645. create_annotation($valid2->getGUID(), $annotation_name2, $annotation_value2, 'integer', $users[1]->getGUID());
  1646. $options = array(
  1647. 'annotation_owner_guid' => $users[0]->getGUID(),
  1648. 'annotation_name' => $annotation_name
  1649. );
  1650. $entities = elgg_get_entities_from_annotations($options);
  1651. foreach ($entities as $entity) {
  1652. $this->assertTrue(in_array($entity->getGUID(), $guids));
  1653. $annotations = $entity->getAnnotations($annotation_name);
  1654. $this->assertEqual(count($annotations), 1);
  1655. $this->assertEqual($annotations[0]->name, $annotation_name);
  1656. $this->assertEqual($annotations[0]->value, $annotation_value);
  1657. $this->assertEqual($annotations[0]->owner_guid, $users[0]->getGUID());
  1658. }
  1659. foreach ($guids as $guid) {
  1660. if ($e = get_entity($guid)) {
  1661. $e->delete();
  1662. }
  1663. }
  1664. }
  1665. // Make sure metadata doesn't affect getting entities by relationship. See #2274
  1666. public function testElggApiGettersEntityRelationshipWithMetadata() {
  1667. $guids = array();
  1668. $obj1 = new ElggObject();
  1669. $obj1->test_md = 'test';
  1670. $obj1->save();
  1671. $guids[] = $obj1->guid;
  1672. $obj2 = new ElggObject();
  1673. $obj2->test_md = 'test';
  1674. $obj2->save();
  1675. $guids[] = $obj2->guid;
  1676. add_entity_relationship($guids[0], 'test', $guids[1]);
  1677. $options = array(
  1678. 'relationship' => 'test',
  1679. 'relationship_guid' => $guids[0]
  1680. );
  1681. $es = elgg_get_entities_from_relationship($options);
  1682. $this->assertTrue(is_array($es));
  1683. $this->assertIdentical(count($es), 1);
  1684. foreach ($es as $e) {
  1685. $this->assertEqual($guids[1], $e->guid);
  1686. }
  1687. foreach ($guids as $guid) {
  1688. $e = get_entity($guid);
  1689. $e->delete();
  1690. }
  1691. }
  1692. public function testElggApiGettersEntityRelationshipWithOutMetadata() {
  1693. $guids = array();
  1694. $obj1 = new ElggObject();
  1695. $obj1->save();
  1696. $guids[] = $obj1->guid;
  1697. $obj2 = new ElggObject();
  1698. $obj2->save();
  1699. $guids[] = $obj2->guid;
  1700. add_entity_relationship($guids[0], 'test', $guids[1]);
  1701. $options = array(
  1702. 'relationship' => 'test',
  1703. 'relationship_guid' => $guids[0]
  1704. );
  1705. $es = elgg_get_entities_from_relationship($options);
  1706. $this->assertTrue(is_array($es));
  1707. $this->assertIdentical(count($es), 1);
  1708. foreach ($es as $e) {
  1709. $this->assertEqual($guids[1], $e->guid);
  1710. }
  1711. foreach ($guids as $guid) {
  1712. $e = get_entity($guid);
  1713. $e->delete();
  1714. }
  1715. }
  1716. public function testElggApiGettersEntityRelationshipWithMetadataIncludingRealMetadata() {
  1717. $guids = array();
  1718. $obj1 = new ElggObject();
  1719. $obj1->test_md = 'test';
  1720. $obj1->save();
  1721. $guids[] = $obj1->guid;
  1722. $obj2 = new ElggObject();
  1723. $obj2->test_md = 'test';
  1724. $obj2->save();
  1725. $guids[] = $obj2->guid;
  1726. add_entity_relationship($guids[0], 'test', $guids[1]);
  1727. $options = array(
  1728. 'relationship' => 'test',
  1729. 'relationship_guid' => $guids[0],
  1730. 'metadata_name' => 'test_md',
  1731. 'metadata_value' => 'test',
  1732. );
  1733. $es = elgg_get_entities_from_relationship($options);
  1734. $this->assertTrue(is_array($es));
  1735. $this->assertIdentical(count($es), 1);
  1736. foreach ($es as $e) {
  1737. $this->assertEqual($guids[1], $e->guid);
  1738. }
  1739. foreach ($guids as $guid) {
  1740. $e = get_entity($guid);
  1741. $e->delete();
  1742. }
  1743. }
  1744. public function testElggApiGettersEntityRelationshipWithMetadataIncludingFakeMetadata() {
  1745. $guids = array();
  1746. $obj1 = new ElggObject();
  1747. $obj1->test_md = 'test';
  1748. $obj1->save();
  1749. $guids[] = $obj1->guid;
  1750. $obj2 = new ElggObject();
  1751. $obj2->test_md = 'test';
  1752. $obj2->save();
  1753. $guids[] = $obj2->guid;
  1754. add_entity_relationship($guids[0], 'test', $guids[1]);
  1755. $options = array(
  1756. 'relationship' => 'test',
  1757. 'relationship_guid' => $guids[0],
  1758. 'metadata_name' => 'test_md',
  1759. 'metadata_value' => 'invalid',
  1760. );
  1761. $es = elgg_get_entities_from_relationship($options);
  1762. $this->assertTrue(empty($es));
  1763. foreach ($guids as $guid) {
  1764. $e = get_entity($guid);
  1765. $e->delete();
  1766. }
  1767. }
  1768. public function testElggApiGettersEntitySiteSingular() {
  1769. global $CONFIG;
  1770. $guids = array();
  1771. $obj1 = new ElggObject();
  1772. $obj1->test_md = 'test';
  1773. // luckily this is never checked.
  1774. $obj1->site_guid = 2;
  1775. $obj1->save();
  1776. $guids[] = $obj1->guid;
  1777. $right_guid = $obj1->guid;
  1778. $obj2 = new ElggObject();
  1779. $obj2->test_md = 'test';
  1780. $obj2->site_guid = $CONFIG->site->guid;
  1781. $obj2->save();
  1782. $guids[] = $obj2->guid;
  1783. $options = array(
  1784. 'metadata_name' => 'test_md',
  1785. 'metadata_value' => 'test',
  1786. 'site_guid' => 2
  1787. );
  1788. $es = elgg_get_entities_from_metadata($options);
  1789. $this->assertTrue(is_array($es));
  1790. $this->assertEqual(1, count($es));
  1791. $this->assertEqual($right_guid, $es[0]->guid);
  1792. foreach ($guids as $guid) {
  1793. get_entity($guid)->delete();
  1794. }
  1795. }
  1796. public function testElggApiGettersEntitySiteSingularAny() {
  1797. global $CONFIG;
  1798. $guids = array();
  1799. $obj1 = new ElggObject();
  1800. $obj1->test_md = 'test';
  1801. // luckily this is never checked.
  1802. $obj1->site_guid = 2;
  1803. $obj1->save();
  1804. $guids[] = $obj1->guid;
  1805. $obj2 = new ElggObject();
  1806. $obj2->test_md = 'test';
  1807. $obj2->site_guid = $CONFIG->site->guid;
  1808. $obj2->save();
  1809. $guids[] = $obj2->guid;
  1810. $options = array(
  1811. 'metadata_name' => 'test_md',
  1812. 'metadata_value' => 'test',
  1813. 'site_guid' => ELGG_ENTITIES_ANY_VALUE,
  1814. 'limit' => 2,
  1815. 'order_by' => 'e.guid DESC'
  1816. );
  1817. $es = elgg_get_entities_from_metadata($options);
  1818. $this->assertTrue(is_array($es));
  1819. $this->assertEqual(2, count($es));
  1820. foreach ($es as $e) {
  1821. $this->assertTrue(in_array($e->guid, $guids));
  1822. }
  1823. foreach ($guids as $guid) {
  1824. get_entity($guid)->delete();
  1825. }
  1826. }
  1827. public function testElggApiGettersEntitySitePlural() {
  1828. global $CONFIG;
  1829. $guids = array();
  1830. $obj1 = new ElggObject();
  1831. $obj1->test_md = 'test';
  1832. // luckily this is never checked.
  1833. $obj1->site_guid = 2;
  1834. $obj1->save();
  1835. $guids[] = $obj1->guid;
  1836. $obj2 = new ElggObject();
  1837. $obj2->test_md = 'test';
  1838. $obj2->site_guid = $CONFIG->site->guid;
  1839. $obj2->save();
  1840. $guids[] = $obj2->guid;
  1841. $options = array(
  1842. 'metadata_name' => 'test_md',
  1843. 'metadata_value' => 'test',
  1844. 'site_guids' => array($CONFIG->site->guid, 2),
  1845. 'limit' => 2,
  1846. 'order_by' => 'e.guid DESC'
  1847. );
  1848. $es = elgg_get_entities_from_metadata($options);
  1849. $this->assertTrue(is_array($es));
  1850. $this->assertEqual(2, count($es));
  1851. foreach ($es as $e) {
  1852. $this->assertTrue(in_array($e->guid, $guids));
  1853. }
  1854. foreach ($guids as $guid) {
  1855. get_entity($guid)->delete();
  1856. }
  1857. }
  1858. public function testElggApiGettersEntitySitePluralSomeInvalid() {
  1859. global $CONFIG;
  1860. $guids = array();
  1861. $obj1 = new ElggObject();
  1862. $obj1->test_md = 'test';
  1863. // luckily this is never checked.
  1864. $obj1->site_guid = 2;
  1865. $obj1->save();
  1866. $guids[] = $obj1->guid;
  1867. $obj2 = new ElggObject();
  1868. $obj2->test_md = 'test';
  1869. $obj2->save();
  1870. $guids[] = $obj2->guid;
  1871. $right_guid = $obj2->guid;
  1872. $options = array(
  1873. 'metadata_name' => 'test_md',
  1874. 'metadata_value' => 'test',
  1875. // just created the first entity so nothing will be "sited" by it.
  1876. 'site_guids' => array($CONFIG->site->guid, $guids[0]),
  1877. 'limit' => 2,
  1878. 'order_by' => 'e.guid DESC'
  1879. );
  1880. $es = elgg_get_entities_from_metadata($options);
  1881. $this->assertTrue(is_array($es));
  1882. $this->assertEqual(1, count($es));
  1883. $this->assertEqual($es[0]->guid, $right_guid);
  1884. foreach ($guids as $guid) {
  1885. get_entity($guid)->delete();
  1886. }
  1887. }
  1888. public function testElggApiGettersEntitySitePluralAllInvalid() {
  1889. global $CONFIG;
  1890. $guids = array();
  1891. $obj1 = new ElggObject();
  1892. $obj1->test_md = 'test';
  1893. // luckily this is never checked.
  1894. $obj1->site_guid = 2;
  1895. $obj1->save();
  1896. $guids[] = $obj1->guid;
  1897. $obj2 = new ElggObject();
  1898. $obj2->test_md = 'test';
  1899. $obj2->save();
  1900. $guids[] = $obj2->guid;
  1901. $right_guid = $obj2->guid;
  1902. $options = array(
  1903. 'metadata_name' => 'test_md',
  1904. 'metadata_value' => 'test',
  1905. // just created the first entity so nothing will be "sited" by it.
  1906. 'site_guids' => array($guids[0], $guids[1]),
  1907. 'limit' => 2,
  1908. 'order_by' => 'e.guid DESC'
  1909. );
  1910. $es = elgg_get_entities_from_metadata($options);
  1911. $this->assertTrue(empty($es));
  1912. foreach ($guids as $guid) {
  1913. get_entity($guid)->delete();
  1914. }
  1915. }
  1916. /**
  1917. * Private settings
  1918. */
  1919. public function testElggApiGettersEntitiesFromPrivateSettings() {
  1920. // create some test private settings
  1921. $setting_name = 'test_setting_name_' . rand();
  1922. $setting_value = rand(1000, 9999);
  1923. $setting_name2 = 'test_setting_name_' . rand();
  1924. $setting_value2 = rand(1000, 9999);
  1925. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1926. $subtype = $subtypes[0];
  1927. $guids = array();
  1928. // our targets
  1929. $valid = new ElggObject();
  1930. $valid->subtype = $subtype;
  1931. $valid->save();
  1932. $guids[] = $valid->getGUID();
  1933. set_private_setting($valid->getGUID(), $setting_name, $setting_value);
  1934. set_private_setting($valid->getGUID(), $setting_name2, $setting_value2);
  1935. $valid2 = new ElggObject();
  1936. $valid2->subtype = $subtype;
  1937. $valid2->save();
  1938. $guids[] = $valid2->getGUID();
  1939. set_private_setting($valid2->getGUID(), $setting_name, $setting_value);
  1940. set_private_setting($valid2->getGUID(), $setting_name2, $setting_value2);
  1941. // simple test with name
  1942. $options = array(
  1943. 'private_setting_name' => $setting_name
  1944. );
  1945. $entities = elgg_get_entities_from_private_settings($options);
  1946. foreach ($entities as $entity) {
  1947. $this->assertTrue(in_array($entity->getGUID(), $guids));
  1948. $value = get_private_setting($entity->getGUID(), $setting_name);
  1949. $this->assertEqual($value, $setting_value);
  1950. }
  1951. // simple test with value
  1952. $options = array(
  1953. 'private_setting_value' => $setting_value
  1954. );
  1955. $entities = elgg_get_entities_from_private_settings($options);
  1956. foreach ($entities as $entity) {
  1957. $this->assertTrue(in_array($entity->getGUID(), $guids));
  1958. $value = get_private_setting($entity->getGUID(), $setting_name);
  1959. $this->assertEqual($value, $setting_value);
  1960. }
  1961. // test pairs
  1962. $options = array(
  1963. 'type' => 'object',
  1964. 'subtype' => $subtype,
  1965. 'private_setting_name_value_pairs' => array(
  1966. array(
  1967. 'name' => $setting_name,
  1968. 'value' => $setting_value
  1969. ),
  1970. array(
  1971. 'name' => $setting_name2,
  1972. 'value' => $setting_value2
  1973. )
  1974. )
  1975. );
  1976. $entities = elgg_get_entities_from_private_settings($options);
  1977. $this->assertEqual(2, count($entities));
  1978. foreach ($entities as $entity) {
  1979. $this->assertTrue(in_array($entity->getGUID(), $guids));
  1980. }
  1981. foreach ($guids as $guid) {
  1982. if ($e = get_entity($guid)) {
  1983. $e->delete();
  1984. }
  1985. }
  1986. }
  1987. /**
  1988. * Location
  1989. */
  1990. public function testElggApiGettersEntitiesFromLocation() {
  1991. // a test location that is out of this world
  1992. $lat = 500;
  1993. $long = 500;
  1994. $delta = 5;
  1995. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1996. $subtype = $subtypes[0];
  1997. $guids = array();
  1998. // our objects
  1999. $valid = new ElggObject();
  2000. $valid->subtype = $subtype;
  2001. $valid->save();
  2002. $guids[] = $valid->getGUID();
  2003. $valid->setLatLong($lat, $long);
  2004. $valid2 = new ElggObject();
  2005. $valid2->subtype = $subtype;
  2006. $valid2->save();
  2007. $guids[] = $valid2->getGUID();
  2008. $valid2->setLatLong($lat + 2 * $delta, $long + 2 * $delta);
  2009. // limit to first object
  2010. $options = array(
  2011. 'latitude' => $lat,
  2012. 'longitude' => $long,
  2013. 'distance' => $delta
  2014. );
  2015. //global $CONFIG;
  2016. //$CONFIG->debug = 'NOTICE';
  2017. $entities = elgg_get_entities_from_location($options);
  2018. //unset($CONFIG->debug);
  2019. $this->assertEqual(1, count($entities));
  2020. $this->assertEqual($entities[0]->getGUID(), $valid->getGUID());
  2021. // get both objects
  2022. $options = array(
  2023. 'latitude' => $lat,
  2024. 'longitude' => $long,
  2025. 'distance' => array('latitude' => 2 * $delta, 'longitude' => 2 * $delta)
  2026. );
  2027. $entities = elgg_get_entities_from_location($options);
  2028. $this->assertEqual(2, count($entities));
  2029. foreach ($entities as $entity) {
  2030. $this->assertTrue(in_array($entity->getGUID(), $guids));
  2031. }
  2032. foreach ($guids as $guid) {
  2033. if ($e = get_entity($guid)) {
  2034. $e->delete();
  2035. }
  2036. }
  2037. }
  2038. public function testElggGetEntitiesFromRelationshipCount() {
  2039. $entities = $this->entities;
  2040. $relationships = array();
  2041. $count = count($entities);
  2042. $max = $count - 1;
  2043. $relationship_name = 'test_relationship_' . rand(0, 1000);
  2044. for ($i = 0; $i < $count; $i++) {
  2045. do {
  2046. $popular_entity = $entities[array_rand($entities)];
  2047. } while (array_key_exists($popular_entity->guid, $relationships));
  2048. $relationships[$popular_entity->guid] = array();
  2049. for ($c = 0; $c < $max; $c++) {
  2050. do {
  2051. $fan_entity = $entities[array_rand($entities)];
  2052. } while ($fan_entity->guid == $popular_entity->guid || in_array($fan_entity->guid, $relationships[$popular_entity->guid]));
  2053. $relationships[$popular_entity->guid][] = $fan_entity->guid;
  2054. add_entity_relationship($fan_entity->guid, $relationship_name, $popular_entity->guid);
  2055. }
  2056. $max--;
  2057. }
  2058. $options = array(
  2059. 'relationship' => $relationship_name,
  2060. 'limit' => $count
  2061. );
  2062. $entities = elgg_get_entities_from_relationship_count($options);
  2063. foreach ($entities as $e) {
  2064. $options = array(
  2065. 'relationship' => $relationship_name,
  2066. 'limit' => 100,
  2067. 'relationship_guid' => $e->guid,
  2068. 'inverse_relationship' => true
  2069. );
  2070. $fan_entities = elgg_get_entities_from_relationship($options);
  2071. $this->assertEqual(count($fan_entities), count($relationships[$e->guid]));
  2072. foreach ($fan_entities as $fan_entity) {
  2073. $this->assertTrue(in_array($fan_entity->guid, $relationships[$e->guid]));
  2074. $this->assertNotIdentical(false, check_entity_relationship($fan_entity->guid, $relationship_name, $e->guid));
  2075. }
  2076. }
  2077. }
  2078. public function testElggGetEntitiesByGuidSingular() {
  2079. foreach ($this->entities as $e) {
  2080. $options = array(
  2081. 'guid' => $e->guid
  2082. );
  2083. $es = elgg_get_entities($options);
  2084. $this->assertEqual(count($es), 1);
  2085. $this->assertEqual($es[0]->guid, $e->guid);
  2086. }
  2087. }
  2088. public function testElggGetEntitiesByGuidPlural() {
  2089. $guids = array();
  2090. foreach ($this->entities as $e) {
  2091. $guids[] = $e->guid;
  2092. }
  2093. $options = array(
  2094. 'guids' => $guids,
  2095. 'limit' => 100
  2096. );
  2097. $es = elgg_get_entities($options);
  2098. $this->assertEqual(count($es), count($this->entities));
  2099. foreach ($es as $e) {
  2100. $this->assertTrue(in_array($e->guid, $guids));
  2101. }
  2102. }
  2103. public function testElggGetEntitiesFromAnnotationsCalculateX() {
  2104. $types = array(
  2105. 'sum',
  2106. 'avg',
  2107. 'min',
  2108. 'max'
  2109. );
  2110. foreach ($types as $type) {
  2111. $subtypes = $this->getRandomValidSubtypes(array('object'), 5);
  2112. $name = 'test_annotation_' . rand(0, 9999);
  2113. $values = array();
  2114. $options = array(
  2115. 'type' => 'object',
  2116. 'subtypes' => $subtypes,
  2117. 'limit' => 5
  2118. );
  2119. $es = elgg_get_entities($options);
  2120. foreach ($es as $e) {
  2121. $value = rand(0,9999);
  2122. $e->annotate($name, $value);
  2123. $value2 = rand(0,9999);
  2124. $e->annotate($name, $value2);
  2125. switch ($type) {
  2126. case 'sum':
  2127. $calc_value = $value + $value2;
  2128. break;
  2129. case 'avg':
  2130. $calc_value = ($value + $value2) / 2;
  2131. break;
  2132. case 'min':
  2133. $calc_value = min(array($value, $value2));
  2134. break;
  2135. case 'max':
  2136. $calc_value = max(array($value, $value2));
  2137. break;
  2138. }
  2139. $values[$e->guid] = $calc_value;
  2140. }
  2141. arsort($values);
  2142. $order = array_keys($values);
  2143. $options = array(
  2144. 'type' => 'object',
  2145. 'subtypes' => $subtypes,
  2146. 'limit' => 5,
  2147. 'annotation_name' => $name,
  2148. 'calculation' => $type
  2149. );
  2150. $es = elgg_get_entities_from_annotation_calculation($options);
  2151. foreach ($es as $i => $e) {
  2152. $value = 0;
  2153. $as = $e->getAnnotations($name);
  2154. // should only ever be 2
  2155. $this->assertEqual(2, count($as));
  2156. $value = $as[0]->value;
  2157. $value2 = $as[1]->value;
  2158. switch ($type) {
  2159. case 'sum':
  2160. $calc_value = $value + $value2;
  2161. break;
  2162. case 'avg':
  2163. $calc_value = ($value + $value2) / 2;
  2164. break;
  2165. case 'min':
  2166. $calc_value = min(array($value, $value2));
  2167. break;
  2168. case 'max':
  2169. $calc_value = max(array($value, $value2));
  2170. break;
  2171. }
  2172. $this->assertEqual($e->guid, $order[$i]);
  2173. $this->assertEqual($values[$e->guid], $calc_value);
  2174. }
  2175. }
  2176. }
  2177. public function testElggGetEntitiesFromAnnotationCalculationCount() {
  2178. // add two annotations with a unique name to an entity
  2179. // then count the number of entities with that annotation name
  2180. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  2181. $name = 'test_annotation_' . rand(0, 9999);
  2182. $values = array();
  2183. $options = array(
  2184. 'type' => 'object',
  2185. 'subtypes' => $subtypes,
  2186. 'limit' => 1
  2187. );
  2188. $es = elgg_get_entities($options);
  2189. $entity = $es[0];
  2190. $value = rand(0, 9999);
  2191. $entity->annotate($name, $value);
  2192. $value = rand(0, 9999);
  2193. $entity->annotate($name, $value);
  2194. $options = array(
  2195. 'type' => 'object',
  2196. 'subtypes' => $subtypes,
  2197. 'annotation_name' => $name,
  2198. 'calculation' => 'count',
  2199. 'count' => true,
  2200. );
  2201. $count = (int)elgg_get_entities_from_annotation_calculation($options);
  2202. $this->assertEqual(1, $count);
  2203. }
  2204. public function testElggGetAnnotationsAnnotationNames() {
  2205. $options = array('annotation_names' => array());
  2206. $a_e_map = array();
  2207. // create test annotations on a few entities.
  2208. for ($i=0; $i<3; $i++) {
  2209. do {
  2210. $e = $this->entities[array_rand($this->entities)];
  2211. } while(in_array($e->guid, $a_e_map));
  2212. $annotations = $this->createRandomAnnotations($e);
  2213. foreach($annotations as $a) {
  2214. $options['annotation_names'][] = $a->name;
  2215. $a_e_map[$a->id] = $e->guid;
  2216. }
  2217. }
  2218. $as = elgg_get_annotations($options);
  2219. $this->assertEqual(count($a_e_map), count($as));
  2220. foreach ($as as $a) {
  2221. $this->assertEqual($a_e_map[$a->id], $a->entity_guid);
  2222. }
  2223. }
  2224. public function testElggGetAnnotationsAnnotationValues() {
  2225. $options = array('annotation_values' => array());
  2226. $a_e_map = array();
  2227. // create test annotations on a few entities.
  2228. for ($i=0; $i<3; $i++) {
  2229. do {
  2230. $e = $this->entities[array_rand($this->entities)];
  2231. } while(in_array($e->guid, $a_e_map));
  2232. $annotations = $this->createRandomAnnotations($e);
  2233. foreach($annotations as $a) {
  2234. $options['annotation_values'][] = $a->value;
  2235. $a_e_map[$a->id] = $e->guid;
  2236. }
  2237. }
  2238. $as = elgg_get_annotations($options);
  2239. $this->assertEqual(count($a_e_map), count($as));
  2240. foreach ($as as $a) {
  2241. $this->assertEqual($a_e_map[$a->id], $a->entity_guid);
  2242. }
  2243. }
  2244. public function testElggGetAnnotationsAnnotationOwnerGuids() {
  2245. $options = array('annotation_owner_guids' => array());
  2246. $a_e_map = array();
  2247. // create test annotations on a single entity
  2248. for ($i=0; $i<3; $i++) {
  2249. do {
  2250. $e = $this->entities[array_rand($this->entities)];
  2251. } while(in_array($e->guid, $a_e_map));
  2252. // remove annotations left over from previous tests.
  2253. elgg_delete_annotations(array('annotation_owner_guid' => $e->guid));
  2254. $annotations = $this->createRandomAnnotations($e);
  2255. foreach($annotations as $a) {
  2256. $options['annotation_owner_guids'][] = $e->guid;
  2257. $a_e_map[$a->id] = $e->guid;
  2258. }
  2259. }
  2260. $as = elgg_get_annotations($options);
  2261. $this->assertEqual(count($a_e_map), count($as));
  2262. foreach ($as as $a) {
  2263. $this->assertEqual($a_e_map[$a->id], $a->owner_guid);
  2264. }
  2265. }
  2266. public function testElggGetEntitiesBadWheres() {
  2267. $options = array(
  2268. 'container_guid' => 'abc'
  2269. );
  2270. $entities = elgg_get_entities($options);
  2271. $this->assertFalse($entities);
  2272. }
  2273. public function testEGEEmptySubtypePlurality() {
  2274. $options = array(
  2275. 'type' => 'user',
  2276. 'subtypes' => ''
  2277. );
  2278. $entities = elgg_get_entities($options);
  2279. $this->assertTrue(is_array($entities));
  2280. $options = array(
  2281. 'type' => 'user',
  2282. 'subtype' => ''
  2283. );
  2284. $entities = elgg_get_entities($options);
  2285. $this->assertTrue(is_array($entities));
  2286. $options = array(
  2287. 'type' => 'user',
  2288. 'subtype' => array('')
  2289. );
  2290. $entities = elgg_get_entities($options);
  2291. $this->assertTrue(is_array($entities));
  2292. $options = array(
  2293. 'type' => 'user',
  2294. 'subtypes' => array('')
  2295. );
  2296. $entities = elgg_get_entities($options);
  2297. $this->assertTrue(is_array($entities));
  2298. }
  2299. }