ElggCoreGetEntitiesFromMetadataTest.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. <?php
  2. /**
  3. * Test elgg_get_entities_from_metadata()
  4. */
  5. class ElggCoreGetEntitiesFromMetadataTest extends \ElggCoreGetEntitiesBaseTest {
  6. //names
  7. function testElggApiGettersEntityMetadataNameValidSingle() {
  8. // create a new entity with a subtype we know
  9. // use an existing type so it will clean up automatically
  10. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  11. $subtype = $subtypes[0];
  12. $md_name = 'test_metadata_name_' . rand();
  13. $md_value = 'test_metadata_value_' . rand();
  14. $e = new \ElggObject();
  15. $e->subtype = $subtype;
  16. $e->$md_name = $md_value;
  17. $e->save();
  18. $options = array(
  19. 'type' => 'object',
  20. 'subtype' => $subtype,
  21. 'metadata_name' => $md_name
  22. );
  23. $entities = elgg_get_entities_from_metadata($options);
  24. $this->assertIsA($entities, 'array');
  25. $this->assertEqual(count($entities), 1);
  26. foreach ($entities as $entity) {
  27. $this->assertEqual($entity->getGUID(), $e->getGUID());
  28. $this->assertEqual($entity->$md_name, $md_value);
  29. }
  30. $e->delete();
  31. }
  32. function testElggApiGettersEntityMetadataNameValidMultiple() {
  33. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  34. $subtype = $subtypes[0];
  35. $md_names = array();
  36. $md_name = 'test_metadata_name_' . rand();
  37. $md_value = 'test_metadata_value_' . rand();
  38. $md_names[] = $md_name;
  39. $e_guids = array();
  40. $e = new \ElggObject();
  41. $e->subtype = $subtype;
  42. $e->$md_name = $md_value;
  43. $e->save();
  44. $e_guids[] = $e->getGUID();
  45. $md_name = 'test_metadata_name_' . rand();
  46. $md_value = 'test_metadata_value_' . rand();
  47. $md_names[] = $md_name;
  48. $e = new \ElggObject();
  49. $e->subtype = $subtype;
  50. $e->$md_name = $md_value;
  51. $e->save();
  52. $e_guids[] = $e->getGUID();
  53. $options = array(
  54. 'type' => 'object',
  55. 'subtype' => $subtype,
  56. 'metadata_names' => $md_names
  57. );
  58. $entities = elgg_get_entities_from_metadata($options);
  59. $this->assertIsA($entities, 'array');
  60. $this->assertEqual(count($entities), 2);
  61. foreach ($entities as $entity) {
  62. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  63. $entity->delete();
  64. }
  65. }
  66. function testElggApiGettersEntityMetadataNameInvalidSingle() {
  67. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  68. $subtype = $subtypes[0];
  69. $md_name = 'test_metadata_name_' . rand();
  70. $md_value = 'test_metadata_value_' . rand();
  71. $e = new \ElggObject();
  72. $e->subtype = $subtype;
  73. $e->$md_name = $md_value;
  74. $e->save();
  75. $md_invalid_name = 'test_metadata_name_' . rand();
  76. $options = array(
  77. 'type' => 'object',
  78. 'subtype' => $subtype,
  79. 'metadata_name' => $md_invalid_name
  80. );
  81. $entities = elgg_get_entities_from_metadata($options);
  82. $this->assertIdentical(array(), $entities);
  83. $e->delete();
  84. }
  85. function testElggApiGettersEntityMetadataNameInvalidMultiple() {
  86. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  87. $subtype = $subtypes[0];
  88. $md_name = 'test_metadata_name_' . rand();
  89. $md_value = 'test_metadata_value_' . rand();
  90. $e = new \ElggObject();
  91. $e->subtype = $subtype;
  92. $e->$md_name = $md_value;
  93. $e->save();
  94. $md_invalid_names = array();
  95. $md_invalid_names[] = 'test_metadata_name_' . rand();
  96. $md_invalid_names[] = 'test_metadata_name_' . rand();
  97. $options = array(
  98. 'type' => 'object',
  99. 'subtype' => $subtype,
  100. 'metadata_names' => $md_invalid_names
  101. );
  102. $entities = elgg_get_entities_from_metadata($options);
  103. $this->assertIdentical(array(), $entities);
  104. $e->delete();
  105. }
  106. function testElggApiGettersEntityMetadataNameMixedMultiple() {
  107. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  108. $subtype = $subtypes[0];
  109. $md_names = array();
  110. $md_name = 'test_metadata_name_' . rand();
  111. $md_value = 'test_metadata_value_' . rand();
  112. $md_names[] = $md_name;
  113. $e_guids = array();
  114. $valid = new \ElggObject();
  115. $valid->subtype = $subtype;
  116. $valid->$md_name = $md_value;
  117. $valid->save();
  118. $e_guids[] = $valid->getGUID();
  119. $md_name = 'test_metadata_name_' . rand();
  120. $md_value = 'test_metadata_value_' . rand();
  121. // add a random invalid name.
  122. $md_names[] = 'test_metadata_name_' . rand();
  123. $e = new \ElggObject();
  124. $e->subtype = $subtype;
  125. $e->$md_name = $md_value;
  126. $e->save();
  127. $e_guids[] = $e->getGUID();
  128. $options = array(
  129. 'type' => 'object',
  130. 'subtype' => $subtype,
  131. 'metadata_names' => $md_names
  132. );
  133. $entities = elgg_get_entities_from_metadata($options);
  134. $this->assertIsA($entities, 'array');
  135. $this->assertEqual(count($entities), 1);
  136. foreach ($entities as $entity) {
  137. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  138. }
  139. foreach ($e_guids as $guid) {
  140. if ($e = get_entity($guid)) {
  141. $e->delete();
  142. }
  143. }
  144. }
  145. // values
  146. function testElggApiGettersEntityMetadataValueValidSingle() {
  147. // create a new entity with a subtype we know
  148. // use an existing type so it will clean up automatically
  149. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  150. $subtype = $subtypes[0];
  151. $md_name = 'test_metadata_name_' . rand();
  152. $md_value = 'test_metadata_value_' . rand();
  153. $e = new \ElggObject();
  154. $e->subtype = $subtype;
  155. $e->$md_name = $md_value;
  156. $e->save();
  157. $options = array(
  158. 'type' => 'object',
  159. 'subtype' => $subtype,
  160. 'metadata_value' => $md_value
  161. );
  162. $entities = elgg_get_entities_from_metadata($options);
  163. $this->assertIsA($entities, 'array');
  164. $this->assertEqual(count($entities), 1);
  165. foreach ($entities as $entity) {
  166. $this->assertEqual($entity->getGUID(), $e->getGUID());
  167. $this->assertEqual($entity->$md_name, $md_value);
  168. }
  169. $e->delete();
  170. }
  171. function testElggApiGettersEntityMetadataValueValidMultiple() {
  172. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  173. $subtype = $subtypes[0];
  174. $md_values = array();
  175. $md_name = 'test_metadata_name_' . rand();
  176. $md_value = 'test_metadata_value_' . rand();
  177. $md_values[] = $md_value;
  178. $e_guids = array();
  179. $e = new \ElggObject();
  180. $e->subtype = $subtype;
  181. $e->$md_name = $md_value;
  182. $e->save();
  183. $e_guids[] = $e->getGUID();
  184. $md_name = 'test_metadata_name_' . rand();
  185. $md_value = 'test_metadata_value_' . rand();
  186. $md_values[] = $md_value;
  187. $e = new \ElggObject();
  188. $e->subtype = $subtype;
  189. $e->$md_name = $md_value;
  190. $e->save();
  191. $e_guids[] = $e->getGUID();
  192. $options = array(
  193. 'type' => 'object',
  194. 'subtype' => $subtype,
  195. 'metadata_values' => $md_values
  196. );
  197. $entities = elgg_get_entities_from_metadata($options);
  198. $this->assertIsA($entities, 'array');
  199. $this->assertEqual(count($entities), 2);
  200. foreach ($entities as $entity) {
  201. $this->assertTrue(in_array($entity->getGUID(), $e_guids));
  202. $entity->delete();
  203. }
  204. }
  205. function testElggApiGettersEntityMetadataValueInvalidSingle() {
  206. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  207. $subtype = $subtypes[0];
  208. $md_name = 'test_metadata_name_' . rand();
  209. $md_value = 'test_metadata_value_' . rand();
  210. $e = new \ElggObject();
  211. $e->subtype = $subtype;
  212. $e->$md_name = $md_value;
  213. $e->save();
  214. $md_invalid_value = 'test_metadata_value_' . rand();
  215. $options = array(
  216. 'type' => 'object',
  217. 'subtype' => $subtype,
  218. 'metadata_value' => $md_invalid_value
  219. );
  220. $entities = elgg_get_entities_from_metadata($options);
  221. $this->assertIdentical(array(), $entities);
  222. $e->delete();
  223. }
  224. function testElggApiGettersEntityMetadataValueInvalidMultiple() {
  225. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  226. $subtype = $subtypes[0];
  227. $md_name = 'test_metadata_name_' . rand();
  228. $md_value = 'test_metadata_value_' . rand();
  229. $e = new \ElggObject();
  230. $e->subtype = $subtype;
  231. $e->$md_name = $md_value;
  232. $e->save();
  233. $md_invalid_values = array();
  234. $md_invalid_values[] = 'test_metadata_value_' . rand();
  235. $md_invalid_values[] = 'test_metadata_value_' . rand();
  236. $options = array(
  237. 'type' => 'object',
  238. 'subtype' => $subtype,
  239. 'metadata_values' => $md_invalid_values
  240. );
  241. $entities = elgg_get_entities_from_metadata($options);
  242. $this->assertIdentical(array(), $entities);
  243. $e->delete();
  244. }
  245. function testElggApiGettersEntityMetadataValueMixedMultiple() {
  246. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  247. $subtype = $subtypes[0];
  248. $md_values = array();
  249. $md_name = 'test_metadata_name_' . rand();
  250. $md_value = 'test_metadata_value_' . rand();
  251. $md_values[] = $md_value;
  252. $e_guids = array();
  253. $valid = new \ElggObject();
  254. $valid->subtype = $subtype;
  255. $valid->$md_name = $md_value;
  256. $valid->save();
  257. $e_guids[] = $valid->getGUID();
  258. $md_name = 'test_metadata_name_' . rand();
  259. $md_value = 'test_metadata_value_' . rand();
  260. // add a random invalid value.
  261. $md_values[] = 'test_metadata_value_' . rand();
  262. $e = new \ElggObject();
  263. $e->subtype = $subtype;
  264. $e->$md_name = $md_value;
  265. $e->save();
  266. $e_guids[] = $e->getGUID();
  267. $options = array(
  268. 'type' => 'object',
  269. 'subtype' => $subtype,
  270. 'metadata_values' => $md_values
  271. );
  272. $entities = elgg_get_entities_from_metadata($options);
  273. $this->assertIsA($entities, 'array');
  274. $this->assertEqual(count($entities), 1);
  275. foreach ($entities as $entity) {
  276. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  277. }
  278. foreach ($e_guids as $guid) {
  279. if ($e = get_entity($guid)) {
  280. $e->delete();
  281. }
  282. }
  283. }
  284. // name_value_pairs
  285. function testElggApiGettersEntityMetadataNVPValidNValidVEquals() {
  286. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  287. $subtype = $subtypes[0];
  288. $md_name = 'test_metadata_name_' . rand();
  289. $md_value = 'test_metadata_value_' . rand();
  290. $guids = array();
  291. // our target
  292. $valid = new \ElggObject();
  293. $valid->subtype = $subtype;
  294. $valid->$md_name = $md_value;
  295. $valid->save();
  296. $guids[] = $valid->getGUID();
  297. // make some bad ones
  298. $invalid_md_name = 'test_metadata_name_' . rand();
  299. $e = new \ElggObject();
  300. $e->subtype = $subtype;
  301. $e->$invalid_md_name = $md_value;
  302. $e->save();
  303. $guids[] = $e->getGUID();
  304. $invalid_md_value = 'test_metadata_value_' . rand();
  305. $e = new \ElggObject();
  306. $e->subtype = $subtype;
  307. $e->$md_name = $invalid_md_value;
  308. $e->save();
  309. $guids[] = $e->getGUID();
  310. $md_invalid_names = array();
  311. $options = array(
  312. 'type' => 'object',
  313. 'subtype' => $subtype,
  314. 'metadata_name_value_pairs' => array(array(
  315. 'name' => $md_name,
  316. 'value' => $md_value
  317. ))
  318. );
  319. $entities = elgg_get_entities_from_metadata($options);
  320. $this->assertIsA($entities, 'array');
  321. $this->assertEqual(count($entities), 1);
  322. foreach ($entities as $entity) {
  323. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  324. $this->assertEqual($entity->$md_name, $md_value);
  325. $entity->delete();
  326. }
  327. foreach ($guids as $guid) {
  328. if ($e = get_entity($guid)) {
  329. $e->delete();
  330. }
  331. }
  332. }
  333. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsTriple() {
  334. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  335. $subtype = $subtypes[0];
  336. $md_name = 'test_metadata_name_' . rand();
  337. $md_value = 'test_metadata_value_' . rand();
  338. $md_name2 = 'test_metadata_name_' . rand();
  339. $md_value2 = 'test_metadata_value_' . rand();
  340. $md_name3 = 'test_metadata_name_' . rand();
  341. $md_value3 = 'test_metadata_value_' . rand();
  342. $guids = array();
  343. // our target
  344. $valid = new \ElggObject();
  345. $valid->subtype = $subtype;
  346. $valid->$md_name = $md_value;
  347. $valid->$md_name2 = $md_value2;
  348. $valid->$md_name3 = $md_value3;
  349. $valid->save();
  350. $guids[] = $valid->getGUID();
  351. // make some bad ones
  352. $invalid_md_name = 'test_metadata_name_' . rand();
  353. $invalid_md_name2 = 'test_metadata_name_' . rand();
  354. $invalid_md_name3 = 'test_metadata_name_' . rand();
  355. $e = new \ElggObject();
  356. $e->subtype = $subtype;
  357. $e->$invalid_md_name = $md_value;
  358. $e->$invalid_md_name2 = $md_value2;
  359. $e->$invalid_md_name3 = $md_value3;
  360. $e->save();
  361. $guids[] = $e->getGUID();
  362. $invalid_md_value = 'test_metadata_value_' . rand();
  363. $e = new \ElggObject();
  364. $e->subtype = $subtype;
  365. $e->$md_name = $invalid_md_value;
  366. $e->$md_name2 = $invalid_md_value;
  367. $e->$md_name3 = $invalid_md_value;
  368. $e->save();
  369. $guids[] = $e->getGUID();
  370. $md_invalid_names = array();
  371. $options = array(
  372. 'type' => 'object',
  373. 'subtype' => $subtype,
  374. 'metadata_name_value_pairs' => array(
  375. array(
  376. 'name' => $md_name,
  377. 'value' => $md_value
  378. ),
  379. array(
  380. 'name' => $md_name2,
  381. 'value' => $md_value2
  382. ),
  383. array(
  384. 'name' => $md_name3,
  385. 'value' => $md_value3
  386. )
  387. )
  388. );
  389. $entities = elgg_get_entities_from_metadata($options);
  390. $this->assertIsA($entities, 'array');
  391. $this->assertEqual(count($entities), 1);
  392. foreach ($entities as $entity) {
  393. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  394. $this->assertEqual($entity->$md_name, $md_value);
  395. $entity->delete();
  396. }
  397. foreach ($guids as $guid) {
  398. if ($e = get_entity($guid)) {
  399. $e->delete();
  400. }
  401. }
  402. }
  403. function testElggApiGettersEntityMetadataNVPValidNValidVEqualsDouble() {
  404. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  405. $subtype = $subtypes[0];
  406. $md_name = 'test_metadata_name_' . rand();
  407. $md_value = 'test_metadata_value_' . rand();
  408. $md_name2 = 'test_metadata_name_' . rand();
  409. $md_value2 = 'test_metadata_value_' . rand();
  410. $guids = array();
  411. // our target
  412. $valid = new \ElggObject();
  413. $valid->subtype = $subtype;
  414. $valid->$md_name = $md_value;
  415. $valid->$md_name2 = $md_value2;
  416. $valid->save();
  417. $guids[] = $valid->getGUID();
  418. // make some bad ones
  419. $invalid_md_name = 'test_metadata_name_' . rand();
  420. $invalid_md_name2 = 'test_metadata_name_' . rand();
  421. $e = new \ElggObject();
  422. $e->subtype = $subtype;
  423. $e->$invalid_md_name = $md_value;
  424. $e->$invalid_md_name2 = $md_value2;
  425. $e->save();
  426. $guids[] = $e->getGUID();
  427. $invalid_md_value = 'test_metadata_value_' . rand();
  428. $e = new \ElggObject();
  429. $e->subtype = $subtype;
  430. $e->$md_name = $invalid_md_value;
  431. $e->$md_name2 = $invalid_md_value;
  432. $e->save();
  433. $guids[] = $e->getGUID();
  434. $md_invalid_names = array();
  435. $options = array(
  436. 'type' => 'object',
  437. 'subtype' => $subtype,
  438. 'metadata_name_value_pairs' => array(
  439. array(
  440. 'name' => $md_name,
  441. 'value' => $md_value
  442. ),
  443. array(
  444. 'name' => $md_name2,
  445. 'value' => $md_value2
  446. )
  447. )
  448. );
  449. $entities = elgg_get_entities_from_metadata($options);
  450. $this->assertIsA($entities, 'array');
  451. $this->assertEqual(count($entities), 1);
  452. foreach ($entities as $entity) {
  453. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  454. $this->assertEqual($entity->$md_name, $md_value);
  455. $entity->delete();
  456. }
  457. foreach ($guids as $guid) {
  458. if ($e = get_entity($guid)) {
  459. $e->delete();
  460. }
  461. }
  462. }
  463. // this keeps locking up my database...
  464. function xtestElggApiGettersEntityMetadataNVPValidNValidVEqualsStupid() {
  465. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  466. $subtype = $subtypes[0];
  467. $md_name = 'test_metadata_name_' . rand();
  468. $md_value = 'test_metadata_value_' . rand();
  469. $md_name2 = 'test_metadata_name_' . rand();
  470. $md_value2 = 'test_metadata_value_' . rand();
  471. $md_name3 = 'test_metadata_name_' . rand();
  472. $md_value3 = 'test_metadata_value_' . rand();
  473. $md_name3 = 'test_metadata_name_' . rand();
  474. $md_value3 = 'test_metadata_value_' . rand();
  475. $md_name4 = 'test_metadata_name_' . rand();
  476. $md_value4 = 'test_metadata_value_' . rand();
  477. $md_name5 = 'test_metadata_name_' . rand();
  478. $md_value5 = 'test_metadata_value_' . rand();
  479. $guids = array();
  480. // our target
  481. $valid = new \ElggObject();
  482. $valid->subtype = $subtype;
  483. $valid->$md_name = $md_value;
  484. $valid->$md_name2 = $md_value2;
  485. $valid->$md_name3 = $md_value3;
  486. $valid->$md_name4 = $md_value4;
  487. $valid->$md_name5 = $md_value5;
  488. $valid->save();
  489. $guids[] = $valid->getGUID();
  490. // make some bad ones
  491. $invalid_md_name = 'test_metadata_name_' . rand();
  492. $e = new \ElggObject();
  493. $e->subtype = $subtype;
  494. $e->$invalid_md_name = $md_value;
  495. $e->$md_name2 = $md_value2;
  496. $e->$md_name3 = $md_value3;
  497. $e->$md_name4 = $md_value4;
  498. $e->$md_name5 = $md_value5;
  499. $e->save();
  500. $guids[] = $e->getGUID();
  501. $invalid_md_value = 'test_metadata_value_' . rand();
  502. $e = new \ElggObject();
  503. $e->subtype = $subtype;
  504. $e->$md_name = $invalid_md_value;
  505. $e->$md_name2 = $invalid_md_value;
  506. $e->$md_name3 = $invalid_md_value;
  507. $e->$md_name4 = $invalid_md_value;
  508. $e->$md_name5 = $invalid_md_value;
  509. $e->save();
  510. $guids[] = $e->getGUID();
  511. $md_invalid_names = array();
  512. $options = array(
  513. 'type' => 'object',
  514. 'subtype' => $subtype,
  515. 'metadata_name_value_pairs' => array(
  516. array(
  517. 'name' => $md_name,
  518. 'value' => $md_value
  519. ),
  520. array(
  521. 'name' => $md_name2,
  522. 'value' => $md_value2
  523. ),
  524. array(
  525. 'name' => $md_name3,
  526. 'value' => $md_value3
  527. ),
  528. array(
  529. 'name' => $md_name4,
  530. 'value' => $md_value4
  531. ),
  532. array(
  533. 'name' => $md_name5,
  534. 'value' => $md_value5
  535. ),
  536. )
  537. );
  538. $entities = elgg_get_entities_from_metadata($options);
  539. $this->assertIsA($entities, 'array');
  540. $this->assertEqual(count($entities), 1);
  541. foreach ($entities as $entity) {
  542. $this->assertEqual($entity->getGUID(), $valid->getGUID());
  543. $this->assertEqual($entity->$md_name, $md_value);
  544. $entity->delete();
  545. }
  546. foreach ($guids as $guid) {
  547. if ($e = get_entity($guid)) {
  548. $e->delete();
  549. }
  550. }
  551. }
  552. /**
  553. * Name value pair with valid name and invalid value
  554. */
  555. function testElggApiGettersEntityMetadataNVPValidNInvalidV() {
  556. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  557. $subtype = $subtypes[0];
  558. $md_name = 'test_metadata_name_' . rand();
  559. $md_value = 'test_metadata_value_' . rand();
  560. $guids = array();
  561. // make some bad ones
  562. $invalid_md_name = 'test_metadata_name_' . rand();
  563. $e = new \ElggObject();
  564. $e->subtype = $subtype;
  565. $e->$invalid_md_name = $md_value;
  566. $e->save();
  567. $guids[] = $e->getGUID();
  568. $invalid_md_value = 'test_metadata_value_' . rand();
  569. $e = new \ElggObject();
  570. $e->subtype = $subtype;
  571. $e->$md_name = $invalid_md_value;
  572. $e->save();
  573. $guids[] = $e->getGUID();
  574. $md_invalid_names = array();
  575. $options = array(
  576. 'type' => 'object',
  577. 'subtype' => $subtype,
  578. 'metadata_name_value_pairs' => array(array(
  579. 'name' => $md_name,
  580. 'value' => 'test_metadata_value_' . rand()
  581. ))
  582. );
  583. $entities = elgg_get_entities_from_metadata($options);
  584. $this->assertIdentical(array(), $entities);
  585. foreach ($guids as $guid) {
  586. if ($e = get_entity($guid)) {
  587. $e->delete();
  588. }
  589. }
  590. }
  591. /**
  592. * Name value pair with invalid name and valid value
  593. */
  594. function testElggApiGettersEntityMetadataNVPInvalidNValidV() {
  595. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  596. $subtype = $subtypes[0];
  597. $md_name = 'test_metadata_name_' . rand();
  598. $md_value = 'test_metadata_value_' . rand();
  599. $guids = array();
  600. // make some bad ones
  601. $invalid_md_name = 'test_metadata_name_' . rand();
  602. $e = new \ElggObject();
  603. $e->subtype = $subtype;
  604. $e->$invalid_md_name = $md_value;
  605. $e->save();
  606. $guids[] = $e->getGUID();
  607. $invalid_md_value = 'test_metadata_value_' . rand();
  608. $e = new \ElggObject();
  609. $e->subtype = $subtype;
  610. $e->$md_name = $invalid_md_value;
  611. $e->save();
  612. $guids[] = $e->getGUID();
  613. $md_invalid_names = array();
  614. $options = array(
  615. 'type' => 'object',
  616. 'subtype' => $subtype,
  617. 'metadata_name_value_pairs' => array(array(
  618. 'name' => 'test_metadata_name_' . rand(),
  619. 'value' => $md_value
  620. ))
  621. );
  622. $entities = elgg_get_entities_from_metadata($options);
  623. $this->assertIdentical(array(), $entities);
  624. foreach ($guids as $guid) {
  625. if ($e = get_entity($guid)) {
  626. $e->delete();
  627. }
  628. }
  629. }
  630. function testElggApiGettersEntityMetadataNVPValidNValidVOperandIn() {
  631. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  632. $subtype = $subtypes[0];
  633. $md_name = 'test_metadata_name_' . rand();
  634. $md_value = 'test_metadata_value_' . rand();
  635. $guids = array();
  636. $valid_guids = array();
  637. // our targets
  638. $valid = new \ElggObject();
  639. $valid->subtype = $subtype;
  640. $valid->$md_name = $md_value;
  641. $valid->save();
  642. $guids[] = $valid->getGUID();
  643. $valid_guids[] = $valid->getGUID();
  644. $md_name2 = 'test_metadata_name_' . rand();
  645. $md_value2 = 'test_metadata_value_' . rand();
  646. $valid2 = new \ElggObject();
  647. $valid2->subtype = $subtype;
  648. $valid2->$md_name2 = $md_value2;
  649. $valid2->save();
  650. $guids[] = $valid->getGUID();
  651. $valid_guids[] = $valid2->getGUID();
  652. // make some bad ones
  653. $invalid_md_name = 'test_metadata_name_' . rand();
  654. $e = new \ElggObject();
  655. $e->subtype = $subtype;
  656. $e->$invalid_md_name = $md_value;
  657. $e->save();
  658. $guids[] = $e->getGUID();
  659. $invalid_md_value = 'test_metadata_value_' . rand();
  660. $e = new \ElggObject();
  661. $e->subtype = $subtype;
  662. $e->$md_name = $invalid_md_value;
  663. $e->save();
  664. $guids[] = $e->getGUID();
  665. $md_valid_values = "'$md_value', '$md_value2'";
  666. $options = array(
  667. 'type' => 'object',
  668. 'subtype' => $subtype,
  669. 'metadata_name_value_pairs' => array(
  670. array(
  671. 'name' => $md_name,
  672. 'value' => $md_valid_values,
  673. 'operand' => 'IN'
  674. ),
  675. array(
  676. 'name' => $md_name2,
  677. 'value' => $md_valid_values,
  678. 'operand' => 'IN'
  679. ),
  680. ),
  681. 'metadata_name_value_pairs_operator' => 'OR'
  682. );
  683. $entities = elgg_get_entities_from_metadata($options);
  684. $this->assertIsA($entities, 'array');
  685. $this->assertEqual(count($entities), 2);
  686. foreach ($entities as $entity) {
  687. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  688. $entity->delete();
  689. }
  690. foreach ($guids as $guid) {
  691. if ($e = get_entity($guid)) {
  692. $e->delete();
  693. }
  694. }
  695. }
  696. function testElggApiGettersEntityMetadataNVPValidNValidVPlural() {
  697. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  698. $subtype = $subtypes[0];
  699. $md_name = 'test_metadata_name_' . rand();
  700. $md_value = 'test_metadata_value_' . rand();
  701. $guids = array();
  702. $valid_guids = array();
  703. // our targets
  704. $valid = new \ElggObject();
  705. $valid->subtype = $subtype;
  706. $valid->$md_name = $md_value;
  707. $valid->save();
  708. $guids[] = $valid->getGUID();
  709. $valid_guids[] = $valid->getGUID();
  710. $md_name2 = 'test_metadata_name_' . rand();
  711. $md_value2 = 'test_metadata_value_' . rand();
  712. $valid2 = new \ElggObject();
  713. $valid2->subtype = $subtype;
  714. $valid2->$md_name2 = $md_value2;
  715. $valid2->save();
  716. $guids[] = $valid->getGUID();
  717. $valid_guids[] = $valid2->getGUID();
  718. // make some bad ones
  719. $invalid_md_name = 'test_metadata_name_' . rand();
  720. $e = new \ElggObject();
  721. $e->subtype = $subtype;
  722. $e->$invalid_md_name = $md_value;
  723. $e->save();
  724. $guids[] = $e->getGUID();
  725. $invalid_md_value = 'test_metadata_value_' . rand();
  726. $e = new \ElggObject();
  727. $e->subtype = $subtype;
  728. $e->$md_name = $invalid_md_value;
  729. $e->save();
  730. $guids[] = $e->getGUID();
  731. $md_valid_values = array($md_value, $md_value2);
  732. $options = array(
  733. 'type' => 'object',
  734. 'subtype' => $subtype,
  735. 'metadata_name_value_pairs' => array(
  736. array(
  737. 'name' => $md_name,
  738. 'value' => $md_valid_values,
  739. 'operand' => 'IN'
  740. ),
  741. array(
  742. 'name' => $md_name2,
  743. 'value' => $md_valid_values,
  744. 'operand' => 'IN'
  745. ),
  746. ),
  747. 'metadata_name_value_pairs_operator' => 'OR'
  748. );
  749. $entities = elgg_get_entities_from_metadata($options);
  750. $this->assertIsA($entities, 'array');
  751. $this->assertEqual(count($entities), 2);
  752. foreach ($entities as $entity) {
  753. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  754. $entity->delete();
  755. }
  756. foreach ($guids as $guid) {
  757. if ($e = get_entity($guid)) {
  758. $e->delete();
  759. }
  760. }
  761. }
  762. function testElggApiGettersEntityMetadataNVPOrderByMDText() {
  763. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  764. $subtype = $subtypes[0];
  765. $md_name = 'test_metadata_name_' . rand();
  766. $guids = array();
  767. $valid_guids = array();
  768. // our targets
  769. $valid = new \ElggObject();
  770. $valid->subtype = $subtype;
  771. $valid->$md_name = 1;
  772. $valid->save();
  773. $guids[] = $valid->getGUID();
  774. $valid_guids[] = $valid->getGUID();
  775. $valid2 = new \ElggObject();
  776. $valid2->subtype = $subtype;
  777. $valid2->$md_name = 2;
  778. $valid2->save();
  779. $guids[] = $valid->getGUID();
  780. $valid_guids[] = $valid2->getGUID();
  781. $valid3 = new \ElggObject();
  782. $valid3->subtype = $subtype;
  783. $valid3->$md_name = 3;
  784. $valid3->save();
  785. $guids[] = $valid->getGUID();
  786. $valid_guids[] = $valid3->getGUID();
  787. $md_valid_values = array(1, 2, 3);
  788. $options = array(
  789. 'type' => 'object',
  790. 'subtype' => $subtype,
  791. //'metadata_name' => $md_name,
  792. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  793. );
  794. $entities = elgg_get_entities_from_metadata($options);
  795. $this->assertIsA($entities, 'array');
  796. $this->assertEqual(count($entities), 3);
  797. $i = 1;
  798. foreach ($entities as $entity) {
  799. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  800. $this->assertEqual($entity->$md_name, $i);
  801. $i++;
  802. $entity->delete();
  803. }
  804. foreach ($guids as $guid) {
  805. if ($e = get_entity($guid)) {
  806. $e->delete();
  807. }
  808. }
  809. }
  810. function testElggApiGettersEntityMetadataNVPOrderByMDString() {
  811. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  812. $subtype = $subtypes[0];
  813. $md_name = 'test_metadata_name_' . rand();
  814. $guids = array();
  815. $valid_guids = array();
  816. // our targets
  817. $valid = new \ElggObject();
  818. $valid->subtype = $subtype;
  819. $valid->$md_name = 'a';
  820. $valid->save();
  821. $guids[] = $valid->getGUID();
  822. $valid_guids[] = $valid->getGUID();
  823. $valid2 = new \ElggObject();
  824. $valid2->subtype = $subtype;
  825. $valid2->$md_name = 'b';
  826. $valid2->save();
  827. $guids[] = $valid->getGUID();
  828. $valid_guids[] = $valid2->getGUID();
  829. $valid3 = new \ElggObject();
  830. $valid3->subtype = $subtype;
  831. $valid3->$md_name = 'c';
  832. $valid3->save();
  833. $guids[] = $valid->getGUID();
  834. $valid_guids[] = $valid3->getGUID();
  835. $md_valid_values = array('a', 'b', 'c');
  836. $options = array(
  837. 'type' => 'object',
  838. 'subtype' => $subtype,
  839. 'metadata_name' => $md_name,
  840. 'order_by_metadata' => array('name' => $md_name, 'as' => 'text')
  841. );
  842. $entities = elgg_get_entities_from_metadata($options);
  843. $this->assertIsA($entities, 'array');
  844. $this->assertEqual(count($entities), 3);
  845. $alpha = array('a', 'b', 'c');
  846. $i = 0;
  847. foreach ($entities as $entity) {
  848. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  849. $this->assertEqual($entity->$md_name, $alpha[$i]);
  850. $i++;
  851. $entity->delete();
  852. }
  853. foreach ($guids as $guid) {
  854. if ($e = get_entity($guid)) {
  855. $e->delete();
  856. }
  857. }
  858. }
  859. // test getting by name sorting by value as integer
  860. function testElggApiGettersEntityMetadataNOrderByMDInt() {
  861. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  862. $subtype = $subtypes[0];
  863. $md_name = 'test_metadata_name_' . rand();
  864. $guids = array();
  865. $valid_guids = array();
  866. // our targets
  867. $valid = new ElggObject();
  868. $valid->subtype = $subtype;
  869. $valid->$md_name = 5;
  870. $valid->save();
  871. $guids[] = $valid->getGUID();
  872. $valid_guids[] = $valid->getGUID();
  873. $valid2 = new ElggObject();
  874. $valid2->subtype = $subtype;
  875. $valid2->$md_name = 1;
  876. $valid2->save();
  877. $guids[] = $valid->getGUID();
  878. $valid_guids[] = $valid2->getGUID();
  879. $valid3 = new ElggObject();
  880. $valid3->subtype = $subtype;
  881. $valid3->$md_name = 15;
  882. $valid3->save();
  883. $guids[] = $valid->getGUID();
  884. $valid_guids[] = $valid3->getGUID();
  885. $options = array(
  886. 'type' => 'object',
  887. 'subtype' => $subtype,
  888. 'metadata_name' => $md_name,
  889. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  890. );
  891. $entities = elgg_get_entities_from_metadata($options);
  892. $this->assertIsA($entities, 'array');
  893. $this->assertEqual(count($entities), 3);
  894. $num = array(1, 5, 15);
  895. $i = 0;
  896. foreach ($entities as $entity) {
  897. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  898. $this->assertEqual($entity->$md_name, $num[$i]);
  899. $i++;
  900. $entity->delete();
  901. }
  902. foreach ($guids as $guid) {
  903. if ($e = get_entity($guid)) {
  904. $e->delete();
  905. }
  906. }
  907. }
  908. // test getting by name sorting by value as integer with defined values
  909. function testElggApiGettersEntityMetadataNOrderByMDIntDefinedVals() {
  910. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  911. $subtype = $subtypes[0];
  912. $md_name = 'test_metadata_name_' . rand();
  913. $guids = array();
  914. $valid_guids = array();
  915. // our targets
  916. $valid = new ElggObject();
  917. $valid->subtype = $subtype;
  918. $valid->$md_name = 5;
  919. $valid->save();
  920. $guids[] = $valid->getGUID();
  921. $valid_guids[] = $valid->getGUID();
  922. $valid2 = new ElggObject();
  923. $valid2->subtype = $subtype;
  924. $valid2->$md_name = 1;
  925. $valid2->save();
  926. $guids[] = $valid->getGUID();
  927. $valid_guids[] = $valid2->getGUID();
  928. $valid3 = new ElggObject();
  929. $valid3->subtype = $subtype;
  930. $valid3->$md_name = 15;
  931. $valid3->save();
  932. $guids[] = $valid->getGUID();
  933. $valid_guids[] = $valid3->getGUID();
  934. $num = array(1, 5, 15);
  935. $options = array(
  936. 'type' => 'object',
  937. 'subtype' => $subtype,
  938. 'metadata_name' => $md_name,
  939. 'metadata_values' => $num,
  940. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  941. );
  942. $entities = elgg_get_entities_from_metadata($options);
  943. $this->assertIsA($entities, 'array');
  944. $this->assertEqual(count($entities), 3);
  945. $i = 0;
  946. foreach ($entities as $entity) {
  947. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  948. $this->assertEqual($entity->$md_name, $num[$i]);
  949. $i++;
  950. $entity->delete();
  951. }
  952. foreach ($guids as $guid) {
  953. if ($e = get_entity($guid)) {
  954. $e->delete();
  955. }
  956. }
  957. }
  958. // test getting by name_value_pairs sorting by value as integer
  959. // because string comparison '5' > '15'
  960. function testElggApiGettersEntityMetadataNVPOrderByMDInt() {
  961. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  962. $subtype = $subtypes[0];
  963. $md_name = 'test_metadata_name_' . rand();
  964. $guids = array();
  965. $valid_guids = array();
  966. // our targets
  967. $valid = new ElggObject();
  968. $valid->subtype = $subtype;
  969. $valid->$md_name = 5;
  970. $valid->save();
  971. $guids[] = $valid->getGUID();
  972. $valid_guids[] = $valid->getGUID();
  973. $valid2 = new ElggObject();
  974. $valid2->subtype = $subtype;
  975. $valid2->$md_name = 1;
  976. $valid2->save();
  977. $guids[] = $valid->getGUID();
  978. $valid_guids[] = $valid2->getGUID();
  979. $valid3 = new ElggObject();
  980. $valid3->subtype = $subtype;
  981. $valid3->$md_name = 15;
  982. $valid3->save();
  983. $guids[] = $valid->getGUID();
  984. $valid_guids[] = $valid3->getGUID();
  985. $num = array(1, 5, 15);
  986. $options = array(
  987. 'type' => 'object',
  988. 'subtype' => $subtype,
  989. 'metadata_name_value_pairs' => array(
  990. 'name' => $md_name,
  991. 'value' => $num
  992. ),
  993. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  994. );
  995. $entities = elgg_get_entities_from_metadata($options);
  996. $this->assertIsA($entities, 'array');
  997. $this->assertEqual(count($entities), 3);
  998. $i = 0;
  999. foreach ($entities as $entity) {
  1000. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1001. $this->assertEqual($entity->$md_name, $num[$i]);
  1002. $i++;
  1003. $entity->delete();
  1004. }
  1005. foreach ($guids as $guid) {
  1006. if ($e = get_entity($guid)) {
  1007. $e->delete();
  1008. }
  1009. }
  1010. }
  1011. // test getting by name sorting by value as integer with defined values
  1012. function testElggApiGettersEntityMetadataNVPGreaterThanInt() {
  1013. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1014. $subtype = $subtypes[0];
  1015. $md_name = 'test_metadata_name_' . rand();
  1016. $guids = array();
  1017. $valid_guids = array();
  1018. // our targets
  1019. $valid = new ElggObject();
  1020. $valid->subtype = $subtype;
  1021. $valid->$md_name = 5;
  1022. $valid->save();
  1023. $guids[] = $valid->getGUID();
  1024. $valid_guids[] = $valid->getGUID();
  1025. $valid2 = new ElggObject();
  1026. $valid2->subtype = $subtype;
  1027. $valid2->$md_name = 1;
  1028. $valid2->save();
  1029. $guids[] = $valid->getGUID();
  1030. $valid_guids[] = $valid2->getGUID();
  1031. $valid3 = new ElggObject();
  1032. $valid3->subtype = $subtype;
  1033. $valid3->$md_name = 15;
  1034. $valid3->save();
  1035. $guids[] = $valid->getGUID();
  1036. $valid_guids[] = $valid3->getGUID();
  1037. $options = array(
  1038. 'type' => 'object',
  1039. 'subtype' => $subtype,
  1040. 'metadata_name_value_pairs' => array(
  1041. 'name' => $md_name,
  1042. 'value' => 4,
  1043. 'operand' => '>'
  1044. ),
  1045. 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer')
  1046. );
  1047. $entities = elgg_get_entities_from_metadata($options);
  1048. $this->assertIsA($entities, 'array');
  1049. $this->assertEqual(count($entities), 2);
  1050. $num = array(5, 15);
  1051. $i = 0;
  1052. foreach ($entities as $entity) {
  1053. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1054. $this->assertEqual($entity->$md_name, $num[$i]);
  1055. $i++;
  1056. $entity->delete();
  1057. }
  1058. foreach ($guids as $guid) {
  1059. if ($e = get_entity($guid)) {
  1060. $e->delete();
  1061. }
  1062. }
  1063. }
  1064. // test getting from string value interpreted as numeric
  1065. // see https://github.com/Elgg/Elgg/issues/7009
  1066. function testElggApiGettersEntityMetadataNVPInvalidDouble() {
  1067. $subtypes = $this->getRandomValidSubtypes(array('object'), 1);
  1068. $subtype = $subtypes[0];
  1069. $md_name = 'test_metadata_name_' . rand();
  1070. $guids = array();
  1071. $valid_guids = array();
  1072. $value = '052e866869';
  1073. // our targets
  1074. $valid = new ElggObject();
  1075. $valid->subtype = $subtype;
  1076. $valid->$md_name = $value;
  1077. $valid->save();
  1078. $guids[] = $valid->getGUID();
  1079. $valid_guids[] = $valid->getGUID();
  1080. $options = array(
  1081. 'type' => 'object',
  1082. 'subtype' => $subtype,
  1083. 'metadata_name_value_pairs' => array(
  1084. 'name' => $md_name,
  1085. 'value' => $value
  1086. )
  1087. );
  1088. $entities = elgg_get_entities_from_metadata($options);
  1089. $this->assertIsA($entities, 'array');
  1090. $this->assertEqual(count($entities), 1);
  1091. foreach ($entities as $entity) {
  1092. $this->assertTrue(in_array($entity->getGUID(), $valid_guids));
  1093. $this->assertEqual($entity->$md_name, $value);
  1094. $entity->delete();
  1095. }
  1096. foreach ($guids as $guid) {
  1097. if ($e = get_entity($guid)) {
  1098. $e->delete();
  1099. }
  1100. }
  1101. }
  1102. }