PageRenderTime 77ms CodeModel.GetById 17ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/tests/ElggCoreEntityGetterFunctionsTest.php

https://github.com/fragilbert/Elgg
PHP | 2884 lines | 2052 code | 585 blank | 247 comment | 44 complexity | ceb9e706e740237dbb48b3ff474e692b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file