PageRenderTime 71ms CodeModel.GetById 2ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 1ms

/engine/tests/api/entity_getter_functions.php

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