PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/tests/api/entity_getter_functions.php

https://bitbucket.org/cash/elgg
PHP | 2044 lines | 1440 code | 420 blank | 184 comment | 30 complexity | 5470aafdd002a02b072e4f9548e73472 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * Elgg Test Entity Getter Functions
  4. * @package Elgg
  5. * @subpackage Test
  6. * @author Curverider Ltd
  7. * @link http://elgg.org/
  8. */
  9. class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest {
  10. /**
  11. * Called before each test object.
  12. */
  13. public function __construct() {
  14. elgg_set_ignore_access(TRUE);
  15. $this->entities = array();
  16. $this->subtypes = array(
  17. 'object' => array(),
  18. 'user' => array(),
  19. 'group' => array(),
  20. //'site' => array()
  21. );
  22. // sites are a bit wonky. Don't use them just now.
  23. $this->types = array('object', 'user', 'group');
  24. // create some fun objects to play with.
  25. // 5 with random subtypes
  26. for ($i=0; $i<5; $i++) {
  27. $subtype = 'test_object_subtype_' . rand();
  28. $e = new ElggObject();
  29. $e->subtype = $subtype;
  30. $e->save();
  31. $this->entities[] = $e;
  32. $this->subtypes['object'][] = $subtype;
  33. }
  34. // and users
  35. for ($i=0; $i<5; $i++) {
  36. $subtype = "test_user_subtype_" . rand();
  37. $e = new ElggUser();
  38. $e->username = "test_user_" . rand();
  39. $e->subtype = $subtype;
  40. $e->save();
  41. $this->entities[] = $e;
  42. $this->subtypes['user'][] = $subtype;
  43. }
  44. // and groups
  45. for ($i=0; $i<5; $i++) {
  46. $subtype = "test_group_subtype_" . rand();
  47. $e = new ElggGroup();
  48. $e->subtype = $subtype;
  49. $e->save();
  50. $this->entities[] = $e;
  51. $this->subtypes['group'][] = $subtype;
  52. }
  53. parent::__construct();
  54. }
  55. /**
  56. * Called after each test method.
  57. */
  58. public function setUp() {
  59. return TRUE;
  60. }
  61. /**
  62. * Called after each test method.
  63. */
  64. public function tearDown() {
  65. return TRUE;
  66. }
  67. /**
  68. * Called after each test object.
  69. */
  70. public function __destruct() {
  71. global $CONFIG;
  72. $this->swallowErrors();
  73. foreach ($this->entities as $e) {
  74. $e->delete();
  75. }
  76. // manually remove subtype entries since there is no way
  77. // to using the API.
  78. $subtype_arr = array();
  79. foreach ($this->subtypes as $type => $subtypes) {
  80. foreach ($subtypes as $subtype) {
  81. $subtype_arr[] = "'$subtype'";
  82. }
  83. }
  84. $subtype_str = implode(',', $subtype_arr);
  85. $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype IN ($subtype_str)";
  86. delete_data($q);
  87. parent::__destruct();
  88. }
  89. /*************************************************
  90. * Helpers for getting random types and subtypes *
  91. *************************************************/
  92. /**
  93. * Get a random valid subtype
  94. *
  95. * @param int $num
  96. * @return array
  97. */
  98. public function getRandomValidTypes($num = 1) {
  99. $r = array();
  100. for ($i=1; $i<=$num; $i++) {
  101. do {
  102. $t = $this->types[array_rand($this->types)];
  103. } while (in_array($t, $r) && count($r) < count($this->types));
  104. $r[] = $t;
  105. }
  106. shuffle($r);
  107. return $r;
  108. }
  109. /**
  110. * Get a random valid subtype (that we just created)
  111. *
  112. * @param array $type Type of objects to return valid subtypes for.
  113. * @param int $num of subtypes.
  114. *
  115. * @return array
  116. */
  117. public function getRandomValidSubtypes(array $types, $num = 1) {
  118. $r = array();
  119. for ($i=1; $i<=$num; $i++) {
  120. do {
  121. // make sure at least one subtype of each type is returned.
  122. if ($i-1 < count($types)) {
  123. $type = $types[$i-1];
  124. } else {
  125. $type = $types[array_rand($types)];
  126. }
  127. $k = array_rand($this->subtypes[$type]);
  128. $t = $this->subtypes[$type][$k];
  129. } while (in_array($t, $r));
  130. $r[] = $t;
  131. }
  132. shuffle($r);
  133. return $r;
  134. }
  135. /**
  136. * Return an array of invalid strings for type or subtypes.
  137. *
  138. * @param int $num
  139. * @return arr
  140. */
  141. public function getRandomInvalids($num = 1) {
  142. $r = array();
  143. for ($i=1; $i<=$num; $i++) {
  144. $r[] = 'random_invalid_' . rand();
  145. }
  146. return $r;
  147. }
  148. /**
  149. *
  150. * @param unknown_type $num
  151. * @return unknown_type
  152. */
  153. public function getRandomMixedTypes($num = 2) {
  154. $have_valid = $have_invalid = false;
  155. $r = array();
  156. // need at least one of each type.
  157. $valid_n = rand(1, $num-1);
  158. $r = array_merge($r, $this->getRandomValidTypes($valid_n));
  159. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  160. shuffle($r);
  161. return $r;
  162. }
  163. /**
  164. * Get random mix of valid and invalid subtypes for types given.
  165. *
  166. * @param array $types
  167. * @param unknown_type $num
  168. * @return unknown_type
  169. */
  170. public function getRandomMixedSubtypes(array $types, $num = 2) {
  171. $types_c = count($types);
  172. $r = array();
  173. // this can be more efficient but I'm very sleepy...
  174. // want at least one of valid and invalid of each type sent.
  175. for ($i=0; $i < $types_c && $num > 0; $i++) {
  176. // make sure we have a valid and invalid for each type
  177. if (true) {
  178. $type = $types[$i];
  179. $r = array_merge($r, $this->getRandomValidSubtypes(array($type), 1));
  180. $r = array_merge($r, $this->getRandomInvalids(1));
  181. $num -= 2;
  182. }
  183. }
  184. if ($num > 0) {
  185. $valid_n = rand(1, $num);
  186. $r = array_merge($r, $this->getRandomValidSubtypes($types, $valid_n));
  187. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  188. }
  189. //shuffle($r);
  190. return $r;
  191. }
  192. /***********************************
  193. * TYPE TESTS
  194. ***********************************
  195. * check for getting a valid type in all ways we can.
  196. * note that these aren't wonderful tests as there will be
  197. * existing entities so we can't test against the ones we just created.
  198. * So these just test that some are returned and match the type(s) requested.
  199. * It could definitely be the case that the first 10 entities retrieved are all
  200. * objects. Maybe best to limit to 4 and group by type.
  201. */
  202. public function testElggAPIGettersValidTypeUsingType() {
  203. $type_arr = $this->getRandomValidTypes();
  204. $type = $type_arr[0];
  205. $options = array(
  206. 'type' => $type,
  207. 'group_by' => 'e.type'
  208. );
  209. $es = elgg_get_entities($options);
  210. $this->assertIsA($es, 'array');
  211. // should only ever return one object because of group by
  212. $this->assertIdentical(count($es), 1);
  213. foreach ($es as $e) {
  214. $this->assertTrue(in_array($e->getType(), $type_arr));
  215. }
  216. }
  217. public function testElggAPIGettersValidTypeUsingTypesAsString() {
  218. $type_arr = $this->getRandomValidTypes();
  219. $type = $type_arr[0];
  220. $options = array(
  221. 'types' => $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 testElggAPIGettersValidTypeUsingTypesAsArray() {
  233. $type_arr = $this->getRandomValidTypes();
  234. $type = $type_arr[0];
  235. $options = array(
  236. 'types' => $type_arr,
  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 testElggAPIGettersValidTypeUsingTypesAsArrayPlural() {
  248. $num = 2;
  249. $types = $this->getRandomValidTypes($num);
  250. $options = array(
  251. 'types' => $types,
  252. 'group_by' => 'e.type'
  253. );
  254. $es = elgg_get_entities($options);
  255. $this->assertIsA($es, 'array');
  256. // one of object and one of group
  257. $this->assertIdentical(count($es), $num);
  258. foreach ($es as $e) {
  259. $this->assertTrue(in_array($e->getType(), $types));
  260. }
  261. }
  262. /*
  263. * Test mixed valid and invalid types.
  264. */
  265. public function testElggAPIGettersValidAndInvalidTypes() {
  266. //@todo replace this with $this->getRandomMixedTypes().
  267. $t = $this->getRandomValidTypes();
  268. $valid = $t[0];
  269. $t = $this->getRandomInvalids();
  270. $invalid = $t[0];
  271. $options = array(
  272. 'types' => array($invalid, $valid),
  273. 'group_by' => 'e.type'
  274. );
  275. $es = elgg_get_entities($options);
  276. $this->assertIsA($es, 'array');
  277. // should only ever return one object because of group by
  278. $this->assertIdentical(count($es), 1);
  279. $this->assertIdentical($es[0]->getType(), $valid);
  280. }
  281. public function testElggAPIGettersValidAndInvalidTypesPlural() {
  282. $valid_num = 2;
  283. $invalid_num = 3;
  284. $valid = $this->getRandomValidTypes($valid_num);
  285. $invalid = $this->getRandomInvalids($invalid_num);
  286. $types = array();
  287. foreach ($valid as $t) {
  288. $types[] = $t;
  289. }
  290. foreach ($invalid as $t) {
  291. $types[] = $t;
  292. }
  293. shuffle($types);
  294. $options = array(
  295. 'types' => $types,
  296. 'group_by' => 'e.type'
  297. );
  298. $es = elgg_get_entities($options);
  299. $this->assertIsA($es, 'array');
  300. // should only ever return one object because of group by
  301. $this->assertIdentical(count($es), $valid_num);
  302. foreach ($es as $e) {
  303. $this->assertTrue(in_array($e->getType(), $valid));
  304. }
  305. }
  306. /**************************************
  307. * SUBTYPE TESTS
  308. **************************************
  309. *
  310. * Here we can use the subtypes we created to test more finely.
  311. * Subtypes are bound to types, so we must pass a type.
  312. * This is where the fun logic starts.
  313. */
  314. public function testElggAPIGettersValidSubtypeUsingSubtypeSingularType() {
  315. $types = $this->getRandomValidTypes();
  316. $subtypes = $this->getRandomValidSubtypes($types);
  317. $subtype = $subtypes[0];
  318. $options = array(
  319. 'types' => $types,
  320. 'subtype' => $subtype
  321. );
  322. $es = elgg_get_entities($options);
  323. $this->assertIsA($es, 'array');
  324. $this->assertIdentical(count($es), 1);
  325. foreach ($es as $e) {
  326. $this->assertTrue(in_array($e->getType(), $types));
  327. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  328. }
  329. }
  330. public function testElggAPIGettersValidSubtypeUsingSubtypesAsStringSingularType() {
  331. $types = $this->getRandomValidTypes();
  332. $subtypes = $this->getRandomValidSubtypes($types);
  333. $subtype = $subtypes[0];
  334. $options = array(
  335. 'types' => $types,
  336. 'subtypes' => $subtype
  337. );
  338. $es = elgg_get_entities($options);
  339. $this->assertIsA($es, 'array');
  340. $this->assertIdentical(count($es), 1);
  341. foreach ($es as $e) {
  342. $this->assertTrue(in_array($e->getType(), $types));
  343. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  344. }
  345. }
  346. public function testElggAPIGettersValidSubtypeUsingSubtypesAsArraySingularType() {
  347. $types = $this->getRandomValidTypes();
  348. $subtypes = $this->getRandomValidSubtypes($types);
  349. $options = array(
  350. 'types' => $types,
  351. 'subtypes' => $subtypes
  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 testElggAPIGettersValidSubtypeUsingPluralSubtypesSingularType() {
  362. $subtype_num = 2;
  363. $types = $this->getRandomValidTypes();
  364. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  365. $options = array(
  366. 'types' => $types,
  367. 'subtypes' => $subtypes
  368. );
  369. $es = elgg_get_entities($options);
  370. $this->assertIsA($es, 'array');
  371. $this->assertIdentical(count($es), $subtype_num);
  372. foreach ($es as $e) {
  373. $this->assertTrue(in_array($e->getType(), $types));
  374. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  375. }
  376. }
  377. /*
  378. Because we're looking for type OR subtype (sorta)
  379. it's possible that we've pulled in entities that aren't
  380. of the subtype we've requested.
  381. THIS COMBINATION MAKES LITTLE SENSE.
  382. There is no mechanism in elgg to retrieve a subtype without a type, so
  383. this combo gets trimmed down to only including subtypes that are valid to
  384. each particular type.
  385. FOR THE LOVE OF ALL GOOD PLEASE JUST USE TYPE_SUBTYPE_PAIRS!
  386. */
  387. public function testElggAPIGettersValidSubtypeUsingPluralSubtypesPluralTypes() {
  388. $type_num = 2;
  389. $subtype_num = 2;
  390. $types = $this->getRandomValidTypes($type_num);
  391. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  392. $options = array(
  393. 'types' => $types,
  394. 'subtypes' => $subtypes
  395. );
  396. $es = elgg_get_entities($options);
  397. $this->assertIsA($es, 'array');
  398. // this will unset all invalid subtypes for each type that that only
  399. // one entity exists of each.
  400. $this->assertIdentical(count($es), $subtype_num);
  401. foreach ($es as $e) {
  402. // entities must at least be in the type.
  403. $this->assertTrue(in_array($e->getType(), $types));
  404. // test that this is a valid subtype for the entity type.
  405. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  406. }
  407. }
  408. /*
  409. * This combination will remove all invalid subtypes for this type.
  410. */
  411. public function testElggAPIGettersValidSubtypeUsingPluralMixedSubtypesSingleType() {
  412. $type_num = 1;
  413. $subtype_num = 2;
  414. $types = $this->getRandomValidTypes($type_num);
  415. //@todo replace this with $this->getRandomMixedSubtypes()
  416. // we want this to return an invalid subtype for the returned type.
  417. $subtype_types = $types;
  418. $i = 1;
  419. while ($i <= $subtype_num) {
  420. $type = $this->types[$i-1];
  421. if (!in_array($type, $subtype_types)) {
  422. $subtype_types[] = $type;
  423. }
  424. $i++;
  425. }
  426. $subtypes = $this->getRandomValidSubtypes($subtype_types, $type_num);
  427. $options = array(
  428. 'types' => $types,
  429. 'subtypes' => $subtypes
  430. );
  431. $es = elgg_get_entities($options);
  432. $this->assertIsA($es, 'array');
  433. // this will unset all invalid subtypes for each type that that only
  434. // one entity exists of each.
  435. $this->assertIdentical(count($es), $type_num);
  436. foreach ($es as $e) {
  437. // entities must at least be in the type.
  438. $this->assertTrue(in_array($e->getType(), $types));
  439. // test that this is a valid subtype for the entity type.
  440. $this->assertTrue(in_array($e->getSubtype(), $this->subtypes[$e->getType()]));
  441. }
  442. }
  443. /***************************
  444. * TYPE_SUBTYPE_PAIRS
  445. ***************************/
  446. public function testElggAPIGettersTSPValidTypeValidSubtype() {
  447. $type_num = 1;
  448. $subtype_num = 1;
  449. $types = $this->getRandomValidTypes($type_num);
  450. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  451. $pair = array($types[0] => $subtypes[0]);
  452. $options = array(
  453. 'type_subtype_pairs' => $pair
  454. );
  455. $es = elgg_get_entities($options);
  456. $this->assertIsA($es, 'array');
  457. $this->assertIdentical(count($es), $type_num);
  458. foreach ($es as $e) {
  459. $this->assertTrue(in_array($e->getType(), $types));
  460. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  461. }
  462. }
  463. public function testElggAPIGettersTSPValidTypeValidPluralSubtype() {
  464. $type_num = 1;
  465. $subtype_num = 3;
  466. $types = $this->getRandomValidTypes($type_num);
  467. $subtypes = $this->getRandomValidSubtypes($types, $subtype_num);
  468. $pair = array($types[0] => $subtypes);
  469. $options = array(
  470. 'type_subtype_pairs' => $pair
  471. );
  472. $es = elgg_get_entities($options);
  473. $this->assertIsA($es, 'array');
  474. $this->assertIdentical(count($es), $subtype_num);
  475. foreach ($es as $e) {
  476. $this->assertTrue(in_array($e->getType(), $types));
  477. $this->assertTrue(in_array($e->getSubtype(), $subtypes));
  478. }
  479. }
  480. public function testElggAPIGettersTSPValidTypeMixedPluralSubtype() {
  481. $type_num = 1;
  482. $valid_subtype_num = 2;
  483. $types = $this->getRandomValidTypes($type_num);
  484. $valid = $this->getRandomValidSubtypes($types, $valid_subtype_num);
  485. $invalid = $this->getRandomInvalids();
  486. $subtypes = array_merge($valid, $invalid);
  487. shuffle($subtypes);
  488. $pair = array($types[0] => $subtypes);
  489. $options = array(
  490. 'type_subtype_pairs' => $pair
  491. );
  492. $es = elgg_get_entities($options);
  493. $this->assertIsA($es, 'array');
  494. $this->assertIdentical(count($es), $valid_subtype_num);
  495. foreach ($es as $e) {
  496. $this->assertTrue(in_array($e->getType(), $types));
  497. $this->assertTrue(in_array($e->getSubtype(), $valid));
  498. }
  499. }
  500. /****************************
  501. * FALSE-RETURNING TESTS
  502. ****************************
  503. * The original bug corrected returned
  504. * all entities when invalid subtypes were passed.
  505. * Because there's a huge numer of combinations that
  506. * return entities, I'm only writing tests for
  507. * things that should return false.
  508. *
  509. * I'm leaving the above in case anyone is inspired to
  510. * write out the rest of the possible combinations
  511. */
  512. /*
  513. * Test invalid types.
  514. */
  515. public function testElggApiGettersInvalidTypeUsingType() {
  516. $type_arr = $this->getRandomInvalids();
  517. $type = $type_arr[0];
  518. $options = array(
  519. 'type' => $type
  520. );
  521. $es = elgg_get_entities($options);
  522. $this->assertFalse($es);
  523. }
  524. public function testElggApiGettersInvalidTypeUsingTypesAsString() {
  525. $type_arr = $this->getRandomInvalids();
  526. $type = $type_arr[0];
  527. $options = array(
  528. 'types' => $type
  529. );
  530. $es = elgg_get_entities($options);
  531. $this->assertFalse($es);
  532. }
  533. public function testElggApiGettersInvalidTypeUsingTypesAsArray() {
  534. $type_arr = $this->getRandomInvalids();
  535. $options = array(
  536. 'types' => $type_arr
  537. );
  538. $es = elgg_get_entities($options);
  539. $this->assertFalse($es);
  540. }
  541. public function testElggApiGettersInvalidTypes() {
  542. $type_arr = $this->getRandomInvalids(2);
  543. $options = array(
  544. 'types' => $type_arr
  545. );
  546. $es = elgg_get_entities($options);
  547. $this->assertFalse($es);
  548. }
  549. public function testElggApiGettersInvalidSubtypeValidType() {
  550. $type_num = 1;
  551. $subtype_num = 1;
  552. $types = $this->getRandomValidTypes($type_num);
  553. $subtypes = $this->getRandomInvalids($subtype_num);
  554. $options = array(
  555. 'types' => $types,
  556. 'subtypes' => $subtypes
  557. );
  558. $es = elgg_get_entities($options);
  559. $this->assertFalse($es);
  560. }
  561. public function testElggApiGettersInvalidSubtypeValidTypes() {
  562. $type_num = 2;
  563. $subtype_num = 1;
  564. $types = $this->getRandomValidTypes($type_num);
  565. $subtypes = $this->getRandomInvalids($subtype_num);
  566. $options = array(
  567. 'types' => $types,
  568. 'subtypes' => $subtypes
  569. );
  570. $es = elgg_get_entities($options);
  571. $this->assertFalse($es);
  572. }
  573. public function testElggApiGettersInvalidSubtypesValidType() {
  574. $type_num = 1;
  575. $subtype_num = 2;
  576. $types = $this->getRandomValidTypes($type_num);
  577. $subtypes = $this->getRandomInvalids($subtype_num);
  578. $options = array(
  579. 'types' => $types,
  580. 'subtypes' => $subtypes
  581. );
  582. $es = elgg_get_entities($options);
  583. $this->assertFalse($es);
  584. }
  585. public function testElggApiGettersInvalidSubtypesValidTypes() {
  586. $type_num = 2;
  587. $subtype_num = 2;
  588. $types = $this->getRandomValidTypes($type_num);
  589. $subtypes = $this->getRandomInvalids($subtype_num);
  590. $options = array(
  591. 'types' => $types,
  592. 'subtypes' => $subtypes
  593. );
  594. $es = elgg_get_entities($options);
  595. $this->assertFalse($es);
  596. }
  597. public function testElggApiGettersTSPInvalidType() {
  598. $type_num = 1;
  599. $types = $this->getRandomInvalids($type_num);
  600. $pair = array();
  601. foreach ($types as $type) {
  602. $pair[$type] = NULL;
  603. }
  604. $options = array(
  605. 'type_subtype_pairs' => $pair
  606. );
  607. $es = elgg_get_entities($options);
  608. $this->assertFalse($es);
  609. }
  610. public function testElggApiGettersTSPInvalidTypes() {
  611. $type_num = 2;
  612. $types = $this->getRandomInvalids($type_num);
  613. $pair = array();
  614. foreach ($types as $type) {
  615. $pair[$type] = NULL;
  616. }
  617. $options = array(
  618. 'type_subtype_pairs' => $pair
  619. );
  620. $es = elgg_get_entities($options);
  621. $this->assertFalse($es);
  622. }
  623. public function testElggApiGettersTSPValidTypeInvalidSubtype() {
  624. $type_num = 1;
  625. $subtype_num = 1;
  626. $types = $this->getRandomValidTypes($type_num);
  627. $subtypes = $this->getRandomInvalids($subtype_num);
  628. $pair = array($types[0] => $subtypes[0]);
  629. $options = array(
  630. 'type_subtype_pairs' => $pair
  631. );
  632. $es = elgg_get_entities($options);
  633. $this->assertFalse($es);
  634. }
  635. public function testElggApiGettersTSPValidTypeInvalidSubtypes() {
  636. $type_num = 1;
  637. $subtype_num = 2;
  638. $types = $this->getRandomValidTypes($type_num);
  639. $subtypes = $this->getRandomInvalids($subtype_num);
  640. $pair = array($types[0] => array($subtypes[0], $subtypes[0]));
  641. $options = array(
  642. 'type_subtype_pairs' => $pair
  643. );
  644. $es = elgg_get_entities($options);
  645. $this->assertFalse($es);
  646. }
  647. public function testElggApiGettersTSPValidTypesInvalidSubtypes() {
  648. $type_num = 2;
  649. $subtype_num = 2;
  650. $types = $this->getRandomValidTypes($type_num);
  651. $subtypes = $this->getRandomInvalids($subtype_num);
  652. $pair = array();
  653. foreach ($types as $type) {
  654. $pair[$type] = $subtypes;
  655. }
  656. $options = array(
  657. 'type_subtype_pairs' => $pair
  658. );
  659. $es = elgg_get_entities($options);
  660. $this->assertFalse($es);
  661. }
  662. public function testElggApiGettersEntityNoSubtype() {
  663. // create an entity we can later delete.
  664. // order by time created and limit by 1 should == this entity.
  665. $e = new ElggObject();
  666. $e->save();
  667. $options = array(
  668. 'type' => 'object',
  669. 'limit' => 1,
  670. 'order_by' => 'guid desc'
  671. );
  672. // grab ourself again to fill out attributes.
  673. $e = get_entity($e->getGUID());
  674. $entities = elgg_get_entities($options);
  675. $this->assertEqual(count($entities), 1);
  676. foreach ($entities as $entity) {
  677. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  678. }
  679. $e->delete();
  680. }
  681. public function testElggApiGettersEntityNoValueSubtypeNotSet() {
  682. // create an entity we can later delete.
  683. // order by time created and limit by 1 should == this entity.
  684. $e = new ElggObject();
  685. $e->save();
  686. $options = array(
  687. 'type' => 'object',
  688. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  689. 'limit' => 1,
  690. 'order_by' => 'guid desc'
  691. );
  692. // grab ourself again to fill out attributes.
  693. $e = get_entity($e->getGUID());
  694. $entities = elgg_get_entities($options);
  695. $this->assertEqual(count($entities), 1);
  696. foreach ($entities as $entity) {
  697. $this->assertIdentical($e->getGUID(), $entity->getGUID());
  698. }
  699. $e->delete();
  700. }
  701. public function testElggApiGettersEntityNoValueSubtypeSet() {
  702. global $CONFIG;
  703. // create an entity we can later delete.
  704. // order by time created and limit by 1 should == this entity.
  705. $subtype = 'subtype_' . rand();
  706. $e_subtype = new ElggObject();
  707. $e_subtype->subtype = $subtype;
  708. $e_subtype->save();
  709. $e = new ElggObject();
  710. $e->save();
  711. $options = array(
  712. 'type' => 'object',
  713. 'subtype' => ELGG_ENTITIES_NO_VALUE,
  714. 'limit' => 1,
  715. 'order_by' => 'guid desc'
  716. );
  717. // grab ourself again to fill out attributes.
  718. $e = get_entity($e->getGUID());
  719. $entities = elgg_get_entities($options);
  720. $this->assertEqual(count($entities), 1);
  721. // this entity should NOT be the entity we just created
  722. // and should have no subtype
  723. foreach ($entities as $entity) {
  724. $this->assertEqual($entity->subtype_id, 0);
  725. }
  726. $e_subtype->delete();
  727. $e->delete();
  728. $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype = '$subtype'";
  729. delete_data($q);
  730. }
  731. /************
  732. * METADATA
  733. ************/
  734. //names
  735. function testElggApiGettersEntityMetadataNameValidSingle() {
  736. // create a new entity with a subtype we know
  737. // use an existing type so it will clean up automatically
  738. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  739. $subtype = $subtypes[0];
  740. $md_name = 'test_metadata_name_' . rand();
  741. $md_value = 'test_metadata_value_' . rand();
  742. $e = new ElggObject();
  743. $e->subtype = $subtype;
  744. $e->$md_name = $md_value;
  745. $e->save();
  746. $options = array(
  747. 'type' => 'object',
  748. 'subtype' => $subtype,
  749. 'metadata_name' => $md_name
  750. );
  751. $entities = elgg_get_entities_from_metadata($options);
  752. $this->assertIsa($entities, 'array');
  753. $this->assertEqual(count($entities), 1);
  754. foreach ($entities as $entity) {
  755. $this->assertEqual($entity->getGUID(), $e->getGUID());
  756. $this->assertEqual($entity->$md_name, $md_value);
  757. }
  758. $e->delete();
  759. }
  760. function testElggApiGettersEntityMetadataNameValidMultiple() {
  761. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  762. $subtype = $subtypes[0];
  763. $md_names = array();
  764. $md_name = 'test_metadata_name_' . rand();
  765. $md_value = 'test_metadata_value_' . rand();
  766. $md_names[] = $md_name;
  767. $e_guids = array();
  768. $e = new ElggObject();
  769. $e->subtype = $subtype;
  770. $e->$md_name = $md_value;
  771. $e->save();
  772. $e_guids[] = $e->getGUID();
  773. $md_name = 'test_metadata_name_' . rand();
  774. $md_value = 'test_metadata_value_' . rand();
  775. $md_names[] = $md_name;
  776. $e = new ElggObject();
  777. $e->subtype = $subtype;
  778. $e->$md_name = $md_value;
  779. $e->save();
  780. $e_guids[] = $e->getGUID();
  781. $options = array(
  782. 'type' => 'object',
  783. 'subtype' => $subtype,
  784. 'metadata_names' => $md_names
  785. );
  786. $entities = elgg_get_entities_from_metadata($options);
  787. $this->assertIsa($entities, 'array');
  788. $this->assertEqual(count($entities), 2);
  789. foreach ($entities as $entity) {
  790. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  791. $entity->delete();
  792. }
  793. }
  794. function testElggApiGettersEntityMetadataNameInvalidSingle() {
  795. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  796. $subtype = $subtypes[0];
  797. $md_name = 'test_metadata_name_' . rand();
  798. $md_value = 'test_metadata_value_' . rand();
  799. $e = new ElggObject();
  800. $e->subtype = $subtype;
  801. $e->$md_name = $md_value;
  802. $e->save();
  803. $md_invalid_name = 'test_metadata_name_' . rand();
  804. $options = array(
  805. 'type' => 'object',
  806. 'subtype' => $subtype,
  807. 'metadata_name' => $md_invalid_name
  808. );
  809. $entities = elgg_get_entities_from_metadata($options);
  810. $this->assertFalse($entities);
  811. $e->delete();
  812. }
  813. function testElggApiGettersEntityMetadataNameInvalidMultiple() {
  814. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  815. $subtype = $subtypes[0];
  816. $md_name = 'test_metadata_name_' . rand();
  817. $md_value = 'test_metadata_value_' . rand();
  818. $e = new ElggObject();
  819. $e->subtype = $subtype;
  820. $e->$md_name = $md_value;
  821. $e->save();
  822. $md_invalid_names = array();
  823. $md_invalid_names[] = 'test_metadata_name_' . rand();
  824. $md_invalid_names[] = 'test_metadata_name_' . rand();
  825. $options = array(
  826. 'type' => 'object',
  827. 'subtype' => $subtype,
  828. 'metadata_names' => $md_invalid_names
  829. );
  830. $entities = elgg_get_entities_from_metadata($options);
  831. $this->assertFalse($entities);
  832. $e->delete();
  833. }
  834. function testElggApiGettersEntityMetadataNameMixedMultiple() {
  835. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  836. $subtype = $subtypes[0];
  837. $md_names = array();
  838. $md_name = 'test_metadata_name_' . rand();
  839. $md_value = 'test_metadata_value_' . rand();
  840. $md_names[] = $md_name;
  841. $e_guids = array();
  842. $valid = new ElggObject();
  843. $valid->subtype = $subtype;
  844. $valid->$md_name = $md_value;
  845. $valid->save();
  846. $e_guids[] = $valid->getGUID();
  847. $md_name = 'test_metadata_name_' . rand();
  848. $md_value = 'test_metadata_value_' . rand();
  849. // add a random invalid name.
  850. $md_names[] = 'test_metadata_name_' . rand();
  851. $e = new ElggObject();
  852. $e->subtype = $subtype;
  853. $e->$md_name = $md_value;
  854. $e->save();
  855. $e_guids[] = $e->getGUID();
  856. $options = array(
  857. 'type' => 'object',
  858. 'subtype' => $subtype,
  859. 'metadata_names' => $md_names
  860. );
  861. $entities = elgg_get_entities_from_metadata($options);
  862. $this->assertIsa($entities, 'array');
  863. $this->assertEqual(count($entities), 1);
  864. foreach ($entities as $entity) {
  865. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  866. }
  867. foreach ($e_guids as $guid) {
  868. if ($e = get_entity($guid)) {
  869. $e->delete();
  870. }
  871. }
  872. }
  873. // values
  874. function testElggApiGettersEntityMetadataValueValidSingle() {
  875. // create a new entity with a subtype we know
  876. // use an existing type so it will clean up automatically
  877. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  878. $subtype = $subtypes[0];
  879. $md_name = 'test_metadata_name_' . rand();
  880. $md_value = 'test_metadata_value_' . rand();
  881. $e = new ElggObject();
  882. $e->subtype = $subtype;
  883. $e->$md_name = $md_value;
  884. $e->save();
  885. $options = array(
  886. 'type' => 'object',
  887. 'subtype' => $subtype,
  888. 'metadata_value' => $md_value
  889. );
  890. $entities = elgg_get_entities_from_metadata($options);
  891. $this->assertIsa($entities, 'array');
  892. $this->assertEqual(count($entities), 1);
  893. foreach ($entities as $entity) {
  894. $this->assertEqual($entity->getGUID(), $e->getGUID());
  895. $this->assertEqual($entity->$md_name, $md_value);
  896. }
  897. $e->delete();
  898. }
  899. function testElggApiGettersEntityMetadataValueValidMultiple() {
  900. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  901. $subtype = $subtypes[0];
  902. $md_values = array();
  903. $md_name = 'test_metadata_name_' . rand();
  904. $md_value = 'test_metadata_value_' . rand();
  905. $md_values[] = $md_value;
  906. $e_guids = array();
  907. $e = new ElggObject();
  908. $e->subtype = $subtype;
  909. $e->$md_name = $md_value;
  910. $e->save();
  911. $e_guids[] = $e->getGUID();
  912. $md_name = 'test_metadata_name_' . rand();
  913. $md_value = 'test_metadata_value_' . rand();
  914. $md_values[] = $md_value;
  915. $e = new ElggObject();
  916. $e->subtype = $subtype;
  917. $e->$md_name = $md_value;
  918. $e->save();
  919. $e_guids[] = $e->getGUID();
  920. $options = array(
  921. 'type' => 'object',
  922. 'subtype' => $subtype,
  923. 'metadata_values' => $md_values
  924. );
  925. $entities = elgg_get_entities_from_metadata($options);
  926. $this->assertIsa($entities, 'array');
  927. $this->assertEqual(count($entities), 2);
  928. foreach ($entities as $entity) {
  929. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  930. $entity->delete();
  931. }
  932. }
  933. function testElggApiGettersEntityMetadatavalueInvalidSingle() {
  934. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  935. $subtype = $subtypes[0];
  936. $md_name = 'test_metadata_name_' . rand();
  937. $md_value = 'test_metadata_value_' . rand();
  938. $e = new ElggObject();
  939. $e->subtype = $subtype;
  940. $e->$md_name = $md_value;
  941. $e->save();
  942. $md_invalid_value = 'test_metadata_value_' . rand();
  943. $options = array(
  944. 'type' => 'object',
  945. 'subtype' => $subtype,
  946. 'metadata_value' => $md_invalid_value
  947. );
  948. $entities = elgg_get_entities_from_metadata($options);
  949. $this->assertFalse($entities);
  950. $e->delete();
  951. }
  952. function testElggApiGettersEntityMetadataValueInvalidMultiple() {
  953. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  954. $subtype = $subtypes[0];
  955. $md_name = 'test_metadata_name_' . rand();
  956. $md_value = 'test_metadata_value_' . rand();
  957. $e = new ElggObject();
  958. $e->subtype = $subtype;
  959. $e->$md_name = $md_value;
  960. $e->save();
  961. $md_invalid_values = array();
  962. $md_invalid_values[] = 'test_metadata_value_' . rand();
  963. $md_invalid_values[] = 'test_metadata_value_' . rand();
  964. $options = array(
  965. 'type' => 'object',
  966. 'subtype' => $subtype,
  967. 'metadata_values' => $md_invalid_values
  968. );
  969. $entities = elgg_get_entities_from_metadata($options);
  970. $this->assertFalse($entities);
  971. $e->delete();
  972. }
  973. function testElggApiGettersEntityMetadataValueMixedMultiple() {
  974. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  975. $subtype = $subtypes[0];
  976. $md_values = array();
  977. $md_name = 'test_metadata_name_' . rand();
  978. $md_value = 'test_metadata_value_' . rand();
  979. $md_values[] = $md_value;
  980. $e_guids = array();
  981. $valid = new ElggObject();
  982. $valid->subtype = $subtype;
  983. $valid->$md_name = $md_value;
  984. $valid->save();
  985. $e_guids[] = $valid->getGUID();
  986. $md_name = 'test_metadata_name_' . rand();
  987. $md_value = 'test_metadata_value_' . rand();
  988. // add a random invalid value.
  989. $md_values[] = 'test_metadata_value_' . rand();
  990. $e = new ElggObject();
  991. $e->subtype = $subtype;
  992. $e->$md_name = $md_value;
  993. $e->save();
  994. $e_guids[] = $e->getGUID();
  995. $options = array(
  996. 'type' => 'object',
  997. 'subtype' => $subtype,
  998. 'metadata_values' => $md_values
  999. );
  1000. $entities = elgg_get_entities_from_metadata($options);
  1001. $this->assertIsa($entities, 'array');
  1002. $this->assertEqual(count($entities), 1);
  1003. foreach ($entities as $entity) {
  1004. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1005. }
  1006. foreach ($e_guids as $guid) {
  1007. if ($e = get_entity($guid)) {
  1008. $e->delete();
  1009. }
  1010. }
  1011. }
  1012. // name_value_pairs
  1013. function testElggApiGettersEntityMetadataNVPValidNValidVEquals() {
  1014. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1015. $subtype = $subtypes[0];
  1016. $md_name = 'test_metadata_name_' . rand();
  1017. $md_value = 'test_metadata_value_' . rand();
  1018. $guids = array();
  1019. // our target
  1020. $valid = new ElggObject();
  1021. $valid->subtype = $subtype;
  1022. $valid->$md_name = $md_value;
  1023. $valid->save();
  1024. $guids[] = $valid->getGUID();
  1025. // make some bad ones
  1026. $invalid_md_name = 'test_metadata_name_' . rand();
  1027. $e = new ElggObject();
  1028. $e->subtype = $subtype;
  1029. $e->$md_name = $invalid_md_value;
  1030. $e->save();
  1031. $guids[] = $e->getGUID();
  1032. $invalid_md_value = 'test_metadata_value_' . rand();
  1033. $e = new ElggObject();
  1034. $e->subtype = $subtype;
  1035. $e->$md_name = $invalid_md_value;
  1036. $e->save();
  1037. $guids[] = $e->getGUID();
  1038. $md_invalid_names = array();
  1039. $options = array(
  1040. 'type' => 'object',
  1041. 'subtype' => $subtype,
  1042. 'metadata_name_value_pairs' => array(array(
  1043. 'name' => $md_name,
  1044. 'value' => $md_value
  1045. ))
  1046. );
  1047. $entities = elgg_get_entities_from_metadata($options);
  1048. $this->assertIsa($entities, 'array');
  1049. $this->assertEqual(count($entities), 1);
  1050. foreach ($entities as $entity) {
  1051. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1052. $this->assertEqual($entity->$md_name, $md_value);
  1053. $entity->delete();
  1054. }
  1055. foreach ($guids as $guid) {
  1056. if ($e = get_entity($guid)) {
  1057. $e->delete();
  1058. }
  1059. }
  1060. }
  1061. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsTriple() {
  1062. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1063. $subtype = $subtypes[0];
  1064. $md_name = 'test_metadata_name_' . rand();
  1065. $md_value = 'test_metadata_value_' . rand();
  1066. $md_name2 = 'test_metadata_name_' . rand();
  1067. $md_value2 = 'test_metadata_value_' . rand();
  1068. $md_name3 = 'test_metadata_name_' . rand();
  1069. $md_value3 = 'test_metadata_value_' . rand();
  1070. $guids = array();
  1071. // our target
  1072. $valid = new ElggObject();
  1073. $valid->subtype = $subtype;
  1074. $valid->$md_name = $md_value;
  1075. $valid->$md_name2 = $md_value2;
  1076. $valid->$md_name3 = $md_value3;
  1077. $valid->save();
  1078. $guids[] = $valid->getGUID();
  1079. // make some bad ones
  1080. $invalid_md_name = 'test_metadata_name_' . rand();
  1081. $e = new ElggObject();
  1082. $e->subtype = $subtype;
  1083. $e->$md_name = $invalid_md_value;
  1084. $e->$md_name2 = $invalid_md_value;
  1085. $e->$md_name3 = $invalid_md_value;
  1086. $e->save();
  1087. $guids[] = $e->getGUID();
  1088. $invalid_md_value = 'test_metadata_value_' . rand();
  1089. $e = new ElggObject();
  1090. $e->subtype = $subtype;
  1091. $e->$md_name = $invalid_md_value;
  1092. $e->$md_name2 = $invalid_md_value;
  1093. $e->$md_name3 = $invalid_md_value;
  1094. $e->save();
  1095. $guids[] = $e->getGUID();
  1096. $md_invalid_names = array();
  1097. $options = array(
  1098. 'type' => 'object',
  1099. 'subtype' => $subtype,
  1100. 'metadata_name_value_pairs' => array(
  1101. array(
  1102. 'name' => $md_name,
  1103. 'value' => $md_value
  1104. ),
  1105. array(
  1106. 'name' => $md_name2,
  1107. 'value' => $md_value2
  1108. ),
  1109. array(
  1110. 'name' => $md_name3,
  1111. 'value' => $md_value3
  1112. )
  1113. )
  1114. );
  1115. $entities = elgg_get_entities_from_metadata($options);
  1116. $this->assertIsa($entities, 'array');
  1117. $this->assertEqual(count($entities), 1);
  1118. foreach ($entities as $entity) {
  1119. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1120. $this->assertEqual($entity->$md_name, $md_value);
  1121. $entity->delete();
  1122. }
  1123. foreach ($guids as $guid) {
  1124. if ($e = get_entity($guid)) {
  1125. $e->delete();
  1126. }
  1127. }
  1128. }
  1129. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsDouble() {
  1130. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1131. $subtype = $subtypes[0];
  1132. $md_name = 'test_metadata_name_' . rand();
  1133. $md_value = 'test_metadata_value_' . rand();
  1134. $md_name2 = 'test_metadata_name_' . rand();
  1135. $md_value2 = 'test_metadata_value_' . rand();
  1136. $guids = array();
  1137. // our target
  1138. $valid = new ElggObject();
  1139. $valid->subtype = $subtype;
  1140. $valid->$md_name = $md_value;
  1141. $valid->$md_name2 = $md_value2;
  1142. $valid->save();
  1143. $guids[] = $valid->getGUID();
  1144. // make some bad ones
  1145. $invalid_md_name = 'test_metadata_name_' . rand();
  1146. $e = new ElggObject();
  1147. $e->subtype = $subtype;
  1148. $e->$md_name = $invalid_md_value;
  1149. $e->$md_name2 = $invalid_md_value;
  1150. $e->save();
  1151. $guids[] = $e->getGUID();
  1152. $invalid_md_value = 'test_metadata_value_' . rand();
  1153. $e = new ElggObject();
  1154. $e->subtype = $subtype;
  1155. $e->$md_name = $invalid_md_value;
  1156. $e->$md_name2 = $invalid_md_value;
  1157. $e->save();
  1158. $guids[] = $e->getGUID();
  1159. $md_invalid_names = array();
  1160. $options = array(
  1161. 'type' => 'object',
  1162. 'subtype' => $subtype,
  1163. 'metadata_name_value_pairs' => array(
  1164. array(
  1165. 'name' => $md_name,
  1166. 'value' => $md_value
  1167. ),
  1168. array(
  1169. 'name' => $md_name2,
  1170. 'value' => $md_value2
  1171. )
  1172. )
  1173. );
  1174. $entities = elgg_get_entities_from_metadata($options);
  1175. $this->assertIsa($entities, 'array');
  1176. $this->assertEqual(count($entities), 1);
  1177. foreach ($entities as $entity) {
  1178. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1179. $this->assertEqual($entity->$md_name, $md_value);
  1180. $entity->delete();
  1181. }
  1182. foreach ($guids as $guid) {
  1183. if ($e = get_entity($guid)) {
  1184. $e->delete();
  1185. }
  1186. }
  1187. }
  1188. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsStupid() {
  1189. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1190. $subtype = $subtypes[0];
  1191. $md_name = 'test_metadata_name_' . rand();
  1192. $md_value = 'test_metadata_value_' . rand();
  1193. $md_name2 = 'test_metadata_name_' . rand();
  1194. $md_value2 = 'test_metadata_value_' . rand();
  1195. $md_name3 = 'test_metadata_name_' . rand();
  1196. $md_value3 = 'test_metadata_value_' . rand();
  1197. $md_name3 = 'test_metadata_name_' . rand();
  1198. $md_value3 = 'test_metadata_value_' . rand();
  1199. $md_name4 = 'test_metadata_name_' . rand();
  1200. $md_value4 = 'test_metadata_value_' . rand();
  1201. $md_name5 = 'test_metadata_name_' . rand();
  1202. $md_value5 = 'test_metadata_value_' . rand();
  1203. $guids = array();
  1204. // our target
  1205. $valid = new ElggObject();
  1206. $valid->subtype = $subtype;
  1207. $valid->$md_name = $md_value;
  1208. $valid->$md_name2 = $md_value2;
  1209. $valid->$md_name3 = $md_value3;
  1210. $valid->$md_name4 = $md_value4;
  1211. $valid->$md_name5 = $md_value5;
  1212. $valid->save();
  1213. $guids[] = $valid->getGUID();
  1214. // make some bad ones
  1215. $invalid_md_name = 'test_metadata_name_' . rand();
  1216. $e = new ElggObject();
  1217. $e->subtype = $subtype;
  1218. $e->$md_name = $invalid_md_value;
  1219. $e->$md_name2 = $invalid_md_value;
  1220. $e->$md_name3 = $invalid_md_value;
  1221. $e->$md_name4 = $invalid_md_value;
  1222. $e->$md_name5 = $invalid_md_value;
  1223. $e->save();
  1224. $guids[] = $e->getGUID();
  1225. $invalid_md_value = 'test_metadata_value_' . rand();
  1226. $e = new ElggObject();
  1227. $e->subtype = $subtype;
  1228. $e->$md_name = $invalid_md_value;
  1229. $e->$md_name2 = $invalid_md_value;
  1230. $e->$md_name3 = $invalid_md_value;
  1231. $e->$md_name4 = $invalid_md_value;
  1232. $e->$md_name5 = $invalid_md_value;
  1233. $e->save();
  1234. $guids[] = $e->getGUID();
  1235. $md_invalid_names = array();
  1236. $options = array(
  1237. 'type' => 'object',
  1238. 'subtype' => $subtype,
  1239. 'metadata_name_value_pairs' => array(
  1240. array(
  1241. 'name' => $md_name,
  1242. 'value' => $md_value
  1243. ),
  1244. array(
  1245. 'name' => $md_name2,
  1246. 'value' => $md_value2
  1247. ),
  1248. array(
  1249. 'name' => $md_name3,
  1250. 'value' => $md_value3
  1251. ),
  1252. array(
  1253. 'name' => $md_name4,
  1254. 'value' => $md_value4
  1255. ),
  1256. array(
  1257. 'name' => $md_name5,
  1258. 'value' => $md_value5
  1259. ),
  1260. )
  1261. );
  1262. $entities = elgg_get_entities_from_metadata($options);
  1263. $this->assertIsa($entities, 'array');
  1264. $this->assertEqual(count($entities), 1);
  1265. foreach ($entities as $entity) {
  1266. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  1267. $this->assertEqual($entity->$md_name, $md_value);
  1268. $entity->delete();
  1269. }
  1270. foreach ($guids as $guid) {
  1271. if ($e = get_entity($guid)) {
  1272. $e->delete();
  1273. }
  1274. }
  1275. }
  1276. function testElggApiGettersEntityMetadataNVPValidNInvalidV() {
  1277. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1278. $subtype = $subtypes[0];
  1279. $md_name = 'test_metadata_name_' . rand();
  1280. $md_value = 'test_metadata_value_' . rand();
  1281. $guids = array();
  1282. // make some bad ones
  1283. $invalid_md_name = 'test_metadata_name_' . rand();
  1284. $e = new ElggObject();
  1285. $e->subtype = $subtype;
  1286. $e->$md_name = $invalid_md_value;
  1287. $e->save();
  1288. $guids[] = $e->getGUID();
  1289. $invalid_md_value = 'test_metadata_value_' . rand();
  1290. $e = new ElggObject();
  1291. $e->subtype = $subtype;
  1292. $e->$md_name = $invalid_md_value;
  1293. $e->save();
  1294. $guids[] = $e->getGUID();
  1295. $md_invalid_names = array();
  1296. $options = array(
  1297. 'type' => 'object',
  1298. 'subtype' => $subtype,
  1299. 'metadata_name_value_pairs' => array(array(
  1300. 'name' => $md_name,
  1301. 'value' => 'test_metadata_value_' . rand()
  1302. ))
  1303. );
  1304. $entities = elgg_get_entities_from_metadata($options);
  1305. $this->assertFalse($entities);
  1306. foreach ($guids as $guid) {
  1307. if ($e = get_entity($guid)) {
  1308. $e->delete();
  1309. }
  1310. }
  1311. }
  1312. function testElggApiGettersEntityMetadataNVPInvalidNValidV() {
  1313. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1314. $subtype = $subtypes[0];
  1315. $md_name = 'test_metadata_name_' . rand();
  1316. $md_value = 'test_metadata_value_' . rand();
  1317. $guids = array();
  1318. // make some bad ones
  1319. $invalid_md_name = 'test_metadata_name_' . rand();
  1320. $e = new ElggObject();
  1321. $e->subtype = $subtype;
  1322. $e->$md_name = $invalid_md_value;
  1323. $e->save();
  1324. $guids[] = $e->getGUID();
  1325. $invalid_md_value = 'test_metadata_value_' . rand();
  1326. $e = new ElggObject();
  1327. $e->subtype = $subtype;
  1328. $e->$md_name = $invalid_md_value;
  1329. $e->save();
  1330. $guids[] = $e->getGUID();
  1331. $md_invalid_names = array();
  1332. $options = array(
  1333. 'type' => 'object',
  1334. 'subtype' => $subtype,
  1335. 'metadata_name_value_pairs' => array(array(
  1336. 'name' => 'test_metadata_name_' . rand(),
  1337. 'value' => $md_value
  1338. ))
  1339. );
  1340. $entities = elgg_get_entities_from_metadata($options);
  1341. $this->assertFalse($entities);
  1342. foreach ($guids as $guid) {
  1343. if ($e = get_entity($guid)) {
  1344. $e->delete();
  1345. }
  1346. }
  1347. }
  1348. function testElggApiGettersEntityMetadataNVPValidNValidVOperandIn() {
  1349. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1350. $subtype = $subtypes[0];
  1351. $md_name = 'test_metadata_name_' . rand();
  1352. $md_value = 'test_metadata_value_' . rand();
  1353. $guids = array();
  1354. $valid_guids = array();
  1355. // our targets
  1356. $valid = new ElggObject();
  1357. $valid->subtype = $subtype;
  1358. $valid->$md_name = $md_value;
  1359. $valid->save();
  1360. $guids[] = $valid->getGUID();
  1361. $valid_guids[] = $valid->getGUID();
  1362. $md_name2 = 'test_metadata_name_' . rand();
  1363. $md_value2 = 'test_metadata_value_' . rand();
  1364. $valid2 = new ElggObject();
  1365. $valid2->subtype = $subtype;
  1366. $valid2->$md_name2 = $md_value2;
  1367. $valid2->save();
  1368. $guids[] = $valid->getGUID();
  1369. $valid_guids[] = $valid2->getGUID();
  1370. // make some bad ones
  1371. $invalid_md_name = 'test_metadata_name_' . rand();
  1372. $e = new ElggObject();
  1373. $e->subtype = $subtype;
  1374. $e->$md_name = $invalid_md_value;
  1375. $e->save();
  1376. $guids[] = $e->getGUID();
  1377. $invalid_md_value = 'test_metadata_value_' . rand();
  1378. $e = new ElggObject();
  1379. $e->subtype = $subtype;
  1380. $e->$md_name = $invalid_md_value;
  1381. $e->save();
  1382. $guids[] = $e->getGUID();
  1383. $md_valid_values = "'$md_value', '$md_value2'";
  1384. $options = array(
  1385. 'type' => 'object',
  1386. 'subtype' => $subtype,
  1387. 'metadata_name_value_pairs' => array(
  1388. array(
  1389. 'name' => $md_name,
  1390. 'value' => $md_valid_values,
  1391. 'operand' => 'IN'
  1392. ),
  1393. array(
  1394. 'name' => $md_name2,
  1395. 'value' => $md_valid_values,
  1396. 'operand' => 'IN'
  1397. ),
  1398. ),
  1399. 'metadata_name_value_pairs_operator' => 'OR'
  1400. );
  1401. $entities = elgg_get_entities_from_metadata($options);
  1402. $this->assertIsa($entities, 'array');
  1403. $this->assertEqual(count($entities), 2);
  1404. foreach ($entities as $entity) {
  1405. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1406. $entity->delete();
  1407. }
  1408. foreach ($guids as $guid) {
  1409. if ($e = get_entity($guid)) {
  1410. $e->delete();
  1411. }
  1412. }
  1413. }
  1414. function testElggApiGettersEntityMetadataNVPValidNValidVPlural() {
  1415. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1416. $subtype = $subtypes[0];
  1417. $md_name = 'test_metadata_name_' . rand();
  1418. $md_value = 'test_metadata_value_' . rand();
  1419. $guids = array();
  1420. $valid_guids = array();
  1421. // our targets
  1422. $valid = new ElggObject();
  1423. $valid->subtype = $subtype;
  1424. $valid->$md_name = $md_value;
  1425. $valid->save();
  1426. $guids[] = $valid->getGUID();
  1427. $valid_guids[] = $valid->getGUID();
  1428. $md_name2 = 'test_metadata_name_' . rand();
  1429. $md_value2 = 'test_metadata_value_' . rand();
  1430. $valid2 = new ElggObject();
  1431. $valid2->subtype = $subtype;
  1432. $valid2->$md_name2 = $md_value2;
  1433. $valid2->save();
  1434. $guids[] = $valid->getGUID();
  1435. $valid_guids[] = $valid2->getGUID();
  1436. // make some bad ones
  1437. $invalid_md_name = 'test_metadata_name_' . rand();
  1438. $e = new ElggObject();
  1439. $e->subtype = $subtype;
  1440. $e->$md_name = $invalid_md_value;
  1441. $e->save();
  1442. $guids[] = $e->getGUID();
  1443. $invalid_md_value = 'test_metadata_value_' . rand();
  1444. $e = new ElggObject();
  1445. $e->subtype = $subtype;
  1446. $e->$md_name = $invalid_md_value;
  1447. $e->save();
  1448. $guids[] = $e->getGUID();
  1449. $md_valid_values = array($md_value, $md_value2);
  1450. $options = array(
  1451. 'type' => 'object',
  1452. 'subtype' => $subtype,
  1453. 'metadata_name_value_pairs' => array(
  1454. array(
  1455. 'name' => $md_name,
  1456. 'value' => $md_valid_values,
  1457. 'operand' => 'IN'
  1458. ),
  1459. array(
  1460. 'name' => $md_name2,
  1461. 'value' => $md_valid_values,
  1462. 'operand' => 'IN'
  1463. ),
  1464. ),
  1465. 'metadata_name_value_pairs_operator' => 'OR'
  1466. );
  1467. $entities = elgg_get_entities_from_metadata($options);
  1468. $this->assertIsa($entities, 'array');
  1469. $this->assertEqual(count($entities), 2);
  1470. foreach ($entities as $entity) {
  1471. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1472. $entity->delete();
  1473. }
  1474. foreach ($guids as $guid) {
  1475. if ($e = get_entity($guid)) {
  1476. $e->delete();
  1477. }
  1478. }
  1479. }
  1480. function testElggApiGettersEntityMetadataNVPOrderByMDText() {
  1481. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1482. $subtype = $subtypes[0];
  1483. $md_name = 'test_metadata_name_' . rand();
  1484. $md_value = 2;
  1485. $guids = array();
  1486. $valid_guids = array();
  1487. // our targets
  1488. $valid = new ElggObject();
  1489. $valid->subtype = $subtype;
  1490. $valid->$md_name = $md_value;
  1491. $valid->save();
  1492. $guids[] = $valid->getGUID();
  1493. $valid_guids[] = $valid->getGUID();
  1494. $valid2 = new ElggObject();
  1495. $valid2->subtype = $subtype;
  1496. $valid2->$md_name = 3;
  1497. $valid2->save();
  1498. $guids[] = $valid->getGUID();
  1499. $valid_guids[] = $valid2->getGUID();
  1500. $valid3 = new ElggObject();
  1501. $valid3->subtype = $subtype;
  1502. $valid3->$md_name = 1;
  1503. $valid3->save();
  1504. $guids[] = $valid->getGUID();
  1505. $valid_guids[] = $valid3->getGUID();
  1506. $md_valid_values = array($md_value, $md_value2);
  1507. $options = array(
  1508. 'type' => 'object',
  1509. 'subtype' => $subtype,
  1510. //'metadata_name' => $md_name,
  1511. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  1512. );
  1513. $entities = elgg_get_entities_from_metadata($options);
  1514. $this->assertIsa($entities, 'array');
  1515. $this->assertEqual(count($entities), 3);
  1516. $i = 1;
  1517. foreach ($entities as $entity) {
  1518. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1519. $this->assertEqual($entity->$md_name, $i);
  1520. $i++;
  1521. $entity->delete();
  1522. }
  1523. foreach ($guids as $guid) {
  1524. if ($e = get_entity($guid)) {
  1525. $e->delete();
  1526. }
  1527. }
  1528. }
  1529. function testElggApiGettersEntityMetadataNVPOrderByMDString() {
  1530. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1531. $subtype = $subtypes[0];
  1532. $md_name = 'test_metadata_name_' . rand();
  1533. $md_value = 'b';
  1534. $guids = array();
  1535. $valid_guids = array();
  1536. // our targets
  1537. $valid = new ElggObject();
  1538. $valid->subtype = $subtype;
  1539. $valid->$md_name = $md_value;
  1540. $valid->save();
  1541. $guids[] = $valid->getGUID();
  1542. $valid_guids[] = $valid->getGUID();
  1543. $valid2 = new ElggObject();
  1544. $valid2->subtype = $subtype;
  1545. $valid2->$md_name = 'c';
  1546. $valid2->save();
  1547. $guids[] = $valid->getGUID();
  1548. $valid_guids[] = $valid2->getGUID();
  1549. $valid3 = new ElggObject();
  1550. $valid3->subtype = $subtype;
  1551. $valid3->$md_name = 'a';
  1552. $valid3->save();
  1553. $guids[] = $valid->getGUID();
  1554. $valid_guids[] = $valid3->getGUID();
  1555. $md_valid_values = array($md_value, $md_value2);
  1556. $options = array(
  1557. 'type' => 'object',
  1558. 'subtype' => $subtype,
  1559. 'metadata_name' => $md_name,
  1560. 'order_by_metadata' => array('name' => $md_name, 'as' => 'text')
  1561. );
  1562. $entities = elgg_get_entities_from_metadata($options);
  1563. $this->assertIsa($entities, 'array');
  1564. $this->assertEqual(count($entities), 3);
  1565. $alpha = array('a', 'b', 'c');
  1566. $i = 0;
  1567. foreach ($entities as $entity) {
  1568. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1569. $this->assertEqual($entity->$md_name, $alpha[$i]);
  1570. $i++;
  1571. $entity->delete();
  1572. }
  1573. foreach ($guids as $guid) {
  1574. if ($e = get_entity($guid)) {
  1575. $e->delete();
  1576. }
  1577. }
  1578. }
  1579. /**
  1580. * Annotations
  1581. */
  1582. public function testElggApiGettersEntitiesFromAnnotation() {
  1583. // grab a few different users to annotation
  1584. // there will always be at least 2 here because of the construct.
  1585. $users = elgg_get_entities(array('type' => 'user', 'limit' => 2));
  1586. // create some test annotations
  1587. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1588. $subtype = $subtypes[0];
  1589. $annotation_name = 'test_annotation_name_' . rand();
  1590. $annotation_value = rand(1000, 9999);
  1591. $annotation_name2 = 'test_annotation_name_' . rand();
  1592. $annotation_value2 = rand(1000, 9999);
  1593. $guids = array();
  1594. // our targets
  1595. $valid = new ElggObject();
  1596. $valid->subtype = $subtype;
  1597. $valid->save();
  1598. $guids[] = $valid->getGUID();
  1599. create_annotation($valid->getGUID(), $annotation_name, $annotation_value, 'integer', $users[0]->getGUID());
  1600. $valid2 = new ElggObject();
  1601. $valid2->subtype = $subtype;
  1602. $valid2->save();
  1603. $guids[] = $valid2->getGUID();
  1604. create_annotation($valid2->getGUID(), $annotation_name2, $annotation_value2, 'integer', $users[1]->getGUID());
  1605. $options = array(
  1606. 'annotation_owner_guid' => $users[0]->getGUID(),
  1607. 'annotation_name' => $annotation_name
  1608. );
  1609. $entities = elgg_get_entities_from_annotations($options);
  1610. foreach ($entities as $entity) {
  1611. $this->assertTrue(in_array($entity->getGUID(), $guids));
  1612. $annotations = $entity->getAnnotations($annotation_name);
  1613. $this->assertEqual(count($annotations), 1);
  1614. $this->assertEqual($annotations[0]->name, $annotation_name);
  1615. $this->assertEqual($annotations[0]->value, $annotation_value);
  1616. $this->assertEqual($annotations[0]->owner_guid, $users[0]->getGUID());
  1617. }
  1618. foreach ($guids as $guid) {
  1619. if ($e = get_entity($guid)) {
  1620. $e->delete();
  1621. }
  1622. }
  1623. }
  1624. }