diff options
Diffstat (limited to 'engine/tests/api/entity_getter_functions.php')
| -rw-r--r-- | engine/tests/api/entity_getter_functions.php | 590 |
1 files changed, 481 insertions, 109 deletions
diff --git a/engine/tests/api/entity_getter_functions.php b/engine/tests/api/entity_getter_functions.php index 7a2943c0e..fef9dc0c5 100644 --- a/engine/tests/api/entity_getter_functions.php +++ b/engine/tests/api/entity_getter_functions.php @@ -10,7 +10,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { * Called before each test object. */ public function __construct() { - elgg_set_ignore_access(TRUE); + elgg_set_ignore_access(TRUE); $this->entities = array(); $this->subtypes = array( 'object' => array(), @@ -175,9 +175,10 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } /** + * Get a mix of valid and invalid types * - * @param unknown_type $num - * @return unknown_type + * @param int $num + * @return array */ public function getRandomMixedTypes($num = 2) { $have_valid = $have_invalid = false; @@ -196,8 +197,8 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { * Get random mix of valid and invalid subtypes for types given. * * @param array $types - * @param unknown_type $num - * @return unknown_type + * @param int $num + * @return array */ public function getRandomMixedSubtypes(array $types, $num = 2) { $types_c = count($types); @@ -227,6 +228,24 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { return $r; } + /** + * Creates random annotations on $entity + * + * @param ElggEntity $entity + * @param int $max + */ + public function createRandomAnnotations($entity, $max = 1) { + $annotations = array(); + for ($i=0; $i<$max; $i++) { + $name = 'test_annotation_name_' . rand(); + $value = rand(); + $id = create_annotation($entity->getGUID(), $name, $value, 'integer', $entity->getGUID()); + $annotations[] = elgg_get_annotation_from_id($id); + } + + return $annotations; + } + /*********************************** * TYPE TESTS @@ -545,7 +564,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { * TYPE_SUBTYPE_PAIRS ***************************/ - + /** + * Valid type, valid subtype pairs + */ public function testElggAPIGettersTSPValidTypeValidSubtype() { $type_num = 1; $subtype_num = 1; @@ -568,6 +589,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } + /** + * Valid type, multiple valid subtypes + */ public function testElggAPIGettersTSPValidTypeValidPluralSubtype() { $type_num = 1; $subtype_num = 3; @@ -590,6 +614,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } + /** + * Valid type, both valid and invalid subtypes + */ public function testElggAPIGettersTSPValidTypeMixedPluralSubtype() { $type_num = 1; $valid_subtype_num = 2; @@ -617,9 +644,6 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } - - - /**************************** * FALSE-RETURNING TESTS **************************** @@ -634,8 +658,8 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { */ - /* - * Test invalid types. + /** + * Test invalid types with singular 'type'. */ public function testElggApiGettersInvalidTypeUsingType() { $type_arr = $this->getRandomInvalids(); @@ -649,7 +673,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $this->assertFalse($es); } - + /** + * Test invalid types with plural 'types'. + */ public function testElggApiGettersInvalidTypeUsingTypesAsString() { $type_arr = $this->getRandomInvalids(); $type = $type_arr[0]; @@ -662,8 +688,11 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $this->assertFalse($es); } + /** + * Test invalid types with plural 'types' and an array of a single type + */ public function testElggApiGettersInvalidTypeUsingTypesAsArray() { - $type_arr = $this->getRandomInvalids(); + $type_arr = $this->getRandomInvalids(1); $options = array( 'types' => $type_arr @@ -673,6 +702,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $this->assertFalse($es); } + /** + * Test invalid types with plural 'types' and an array of a two types + */ public function testElggApiGettersInvalidTypes() { $type_arr = $this->getRandomInvalids(2); @@ -837,7 +869,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { public function testElggApiGettersEntityNoSubtype() { // create an entity we can later delete. - // order by time created and limit by 1 should == this entity. + // order by guid and limit by 1 should == this entity. $e = new ElggObject(); $e->save(); @@ -1035,7 +1067,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); $e->delete(); } @@ -1063,7 +1095,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); $e->delete(); } @@ -1196,7 +1228,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } - function testElggApiGettersEntityMetadatavalueInvalidSingle() { + function testElggApiGettersEntityMetadataValueInvalidSingle() { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $md_name = 'test_metadata_name_' . rand(); @@ -1217,7 +1249,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); $e->delete(); } @@ -1245,7 +1277,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); $e->delete(); } @@ -1323,7 +1355,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; + $e->$invalid_md_name = $md_value; $e->save(); $guids[] = $e->getGUID(); @@ -1388,11 +1420,13 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { // make some bad ones $invalid_md_name = 'test_metadata_name_' . rand(); + $invalid_md_name2 = 'test_metadata_name_' . rand(); + $invalid_md_name3 = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; - $e->$md_name2 = $invalid_md_value; - $e->$md_name3 = $invalid_md_value; + $e->$invalid_md_name = $md_value; + $e->$invalid_md_name2 = $md_value2; + $e->$invalid_md_name3 = $md_value3; $e->save(); $guids[] = $e->getGUID(); @@ -1465,10 +1499,11 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { // make some bad ones $invalid_md_name = 'test_metadata_name_' . rand(); + $invalid_md_name2 = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; - $e->$md_name2 = $invalid_md_value; + $e->$invalid_md_name = $md_value; + $e->$invalid_md_name2 = $md_value2; $e->save(); $guids[] = $e->getGUID(); @@ -1515,7 +1550,8 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } - function testElggApiGettersEntityMetadataNVPValidNValidVEqualsStupid() { + // this keeps locking up my database... + function xtestElggApiGettersEntityMetadataNVPValidNValidVEqualsStupid() { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $md_name = 'test_metadata_name_' . rand(); @@ -1553,11 +1589,11 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; - $e->$md_name2 = $invalid_md_value; - $e->$md_name3 = $invalid_md_value; - $e->$md_name4 = $invalid_md_value; - $e->$md_name5 = $invalid_md_value; + $e->$invalid_md_name = $md_value; + $e->$md_name2 = $md_value2; + $e->$md_name3 = $md_value3; + $e->$md_name4 = $md_value4; + $e->$md_name5 = $md_value5; $e->save(); $guids[] = $e->getGUID(); @@ -1619,6 +1655,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } + /** + * Name value pair with valid name and invalid value + */ function testElggApiGettersEntityMetadataNVPValidNInvalidV() { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; @@ -1630,7 +1669,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; + $e->$invalid_md_name = $md_value; $e->save(); $guids[] = $e->getGUID(); @@ -1654,7 +1693,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); foreach ($guids as $guid) { if ($e = get_entity($guid)) { @@ -1663,7 +1702,9 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } - + /** + * Name value pair with invalid name and valid value + */ function testElggApiGettersEntityMetadataNVPInvalidNValidV() { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; @@ -1675,7 +1716,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; + $e->$invalid_md_name = $md_value; $e->save(); $guids[] = $e->getGUID(); @@ -1699,7 +1740,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $entities = elgg_get_entities_from_metadata($options); - $this->assertFalse($entities); + $this->assertIdentical(array(), $entities); foreach ($guids as $guid) { if ($e = get_entity($guid)) { @@ -1739,7 +1780,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; + $e->$invalid_md_name = $md_value; $e->save(); $guids[] = $e->getGUID(); @@ -1817,7 +1858,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $invalid_md_name = 'test_metadata_name_' . rand(); $e = new ElggObject(); $e->subtype = $subtype; - $e->$md_name = $invalid_md_value; + $e->$invalid_md_name = $md_value; $e->save(); $guids[] = $e->getGUID(); @@ -1869,33 +1910,32 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $md_name = 'test_metadata_name_' . rand(); - $md_value = 2; $guids = array(); $valid_guids = array(); // our targets $valid = new ElggObject(); $valid->subtype = $subtype; - $valid->$md_name = $md_value; + $valid->$md_name = 1; $valid->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid->getGUID(); $valid2 = new ElggObject(); $valid2->subtype = $subtype; - $valid2->$md_name = 3; + $valid2->$md_name = 2; $valid2->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid2->getGUID(); $valid3 = new ElggObject(); $valid3->subtype = $subtype; - $valid3->$md_name = 1; + $valid3->$md_name = 3; $valid3->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid3->getGUID(); - $md_valid_values = array($md_value, $md_value2); + $md_valid_values = array(1, 2, 3); $options = array( 'type' => 'object', @@ -1928,33 +1968,32 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $md_name = 'test_metadata_name_' . rand(); - $md_value = 'b'; $guids = array(); $valid_guids = array(); // our targets $valid = new ElggObject(); $valid->subtype = $subtype; - $valid->$md_name = $md_value; + $valid->$md_name = 'a'; $valid->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid->getGUID(); $valid2 = new ElggObject(); $valid2->subtype = $subtype; - $valid2->$md_name = 'c'; + $valid2->$md_name = 'b'; $valid2->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid2->getGUID(); $valid3 = new ElggObject(); $valid3->subtype = $subtype; - $valid3->$md_name = 'a'; + $valid3->$md_name = 'c'; $valid3->save(); $guids[] = $valid->getGUID(); $valid_guids[] = $valid3->getGUID(); - $md_valid_values = array($md_value, $md_value2); + $md_valid_values = array('a', 'b', 'c'); $options = array( 'type' => 'object', @@ -2039,11 +2078,11 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } } - + // Make sure metadata doesn't affect getting entities by relationship. See #2274 public function testElggApiGettersEntityRelationshipWithMetadata() { $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; $obj1->save(); @@ -2055,29 +2094,29 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $guids[] = $obj2->guid; add_entity_relationship($guids[0], 'test', $guids[1]); - + $options = array( 'relationship' => 'test', 'relationship_guid' => $guids[0] ); - + $es = elgg_get_entities_from_relationship($options); $this->assertTrue(is_array($es)); - $this->assertTrue(count($es), 1); - + $this->assertIdentical(count($es), 1); + foreach ($es as $e) { $this->assertEqual($guids[1], $e->guid); } - + foreach ($guids as $guid) { $e = get_entity($guid); $e->delete(); } } - + public function testElggApiGettersEntityRelationshipWithOutMetadata() { $guids = array(); - + $obj1 = new ElggObject(); $obj1->save(); $guids[] = $obj1->guid; @@ -2087,29 +2126,29 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $guids[] = $obj2->guid; add_entity_relationship($guids[0], 'test', $guids[1]); - + $options = array( 'relationship' => 'test', 'relationship_guid' => $guids[0] ); - + $es = elgg_get_entities_from_relationship($options); $this->assertTrue(is_array($es)); - $this->assertTrue(count($es), 1); - + $this->assertIdentical(count($es), 1); + foreach ($es as $e) { $this->assertEqual($guids[1], $e->guid); } - + foreach ($guids as $guid) { $e = get_entity($guid); $e->delete(); } } - + public function testElggApiGettersEntityRelationshipWithMetadataIncludingRealMetadata() { $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; $obj1->save(); @@ -2121,31 +2160,31 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $guids[] = $obj2->guid; add_entity_relationship($guids[0], 'test', $guids[1]); - + $options = array( 'relationship' => 'test', 'relationship_guid' => $guids[0], 'metadata_name' => 'test_md', 'metadata_value' => 'test', ); - + $es = elgg_get_entities_from_relationship($options); $this->assertTrue(is_array($es)); - $this->assertTrue(count($es), 1); - + $this->assertIdentical(count($es), 1); + foreach ($es as $e) { $this->assertEqual($guids[1], $e->guid); } - + foreach ($guids as $guid) { $e = get_entity($guid); $e->delete(); } } - + public function testElggApiGettersEntityRelationshipWithMetadataIncludingFakeMetadata() { $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; $obj1->save(); @@ -2157,14 +2196,14 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $guids[] = $obj2->guid; add_entity_relationship($guids[0], 'test', $guids[1]); - + $options = array( 'relationship' => 'test', 'relationship_guid' => $guids[0], 'metadata_name' => 'test_md', 'metadata_value' => 'invalid', ); - + $es = elgg_get_entities_from_relationship($options); $this->assertTrue(empty($es)); @@ -2174,12 +2213,12 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $e->delete(); } } - + public function testElggApiGettersEntitySiteSingular() { global $CONFIG; - + $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; // luckily this is never checked. @@ -2193,28 +2232,28 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $obj2->site_guid = $CONFIG->site->guid; $obj2->save(); $guids[] = $obj2->guid; - + $options = array( 'metadata_name' => 'test_md', 'metadata_value' => 'test', 'site_guid' => 2 ); - + $es = elgg_get_entities_from_metadata($options); $this->assertTrue(is_array($es)); $this->assertEqual(1, count($es)); $this->assertEqual($right_guid, $es[0]->guid); - + foreach ($guids as $guid) { get_entity($guid)->delete(); } } - + public function testElggApiGettersEntitySiteSingularAny() { global $CONFIG; - + $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; // luckily this is never checked. @@ -2227,7 +2266,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $obj2->site_guid = $CONFIG->site->guid; $obj2->save(); $guids[] = $obj2->guid; - + $options = array( 'metadata_name' => 'test_md', 'metadata_value' => 'test', @@ -2235,25 +2274,25 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { 'limit' => 2, 'order_by' => 'e.guid DESC' ); - + $es = elgg_get_entities_from_metadata($options); $this->assertTrue(is_array($es)); $this->assertEqual(2, count($es)); - + foreach ($es as $e) { $this->assertTrue(in_array($e->guid, $guids)); } - + foreach ($guids as $guid) { get_entity($guid)->delete(); } } - + public function testElggApiGettersEntitySitePlural() { global $CONFIG; - + $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; // luckily this is never checked. @@ -2266,7 +2305,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $obj2->site_guid = $CONFIG->site->guid; $obj2->save(); $guids[] = $obj2->guid; - + $options = array( 'metadata_name' => 'test_md', 'metadata_value' => 'test', @@ -2274,25 +2313,25 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { 'limit' => 2, 'order_by' => 'e.guid DESC' ); - + $es = elgg_get_entities_from_metadata($options); $this->assertTrue(is_array($es)); $this->assertEqual(2, count($es)); - + foreach ($es as $e) { $this->assertTrue(in_array($e->guid, $guids)); } - + foreach ($guids as $guid) { get_entity($guid)->delete(); } } - + public function testElggApiGettersEntitySitePluralSomeInvalid() { global $CONFIG; - + $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; // luckily this is never checked. @@ -2305,7 +2344,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $obj2->save(); $guids[] = $obj2->guid; $right_guid = $obj2->guid; - + $options = array( 'metadata_name' => 'test_md', 'metadata_value' => 'test', @@ -2314,23 +2353,23 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { 'limit' => 2, 'order_by' => 'e.guid DESC' ); - + $es = elgg_get_entities_from_metadata($options); - + $this->assertTrue(is_array($es)); $this->assertEqual(1, count($es)); $this->assertEqual($es[0]->guid, $right_guid); - + foreach ($guids as $guid) { get_entity($guid)->delete(); } } - + public function testElggApiGettersEntitySitePluralAllInvalid() { global $CONFIG; - + $guids = array(); - + $obj1 = new ElggObject(); $obj1->test_md = 'test'; // luckily this is never checked. @@ -2343,7 +2382,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $obj2->save(); $guids[] = $obj2->guid; $right_guid = $obj2->guid; - + $options = array( 'metadata_name' => 'test_md', 'metadata_value' => 'test', @@ -2352,11 +2391,11 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { 'limit' => 2, 'order_by' => 'e.guid DESC' ); - + $es = elgg_get_entities_from_metadata($options); - + $this->assertTrue(empty($es)); - + foreach ($guids as $guid) { get_entity($guid)->delete(); } @@ -2372,7 +2411,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { $setting_value = rand(1000, 9999); $setting_name2 = 'test_setting_name_' . rand(); $setting_value2 = rand(1000, 9999); - + $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $guids = array(); @@ -2433,7 +2472,7 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { ) ) ); - + $entities = elgg_get_entities_from_private_settings($options); $this->assertEqual(2, count($entities)); foreach ($entities as $entity) { @@ -2509,4 +2548,337 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } } + + + public function testElggGetEntitiesFromRelationshipCount() { + $entities = $this->entities; + $relationships = array(); + $count = count($entities); + $max = $count - 1; + $relationship_name = 'test_relationship_' . rand(0, 1000); + + for ($i = 0; $i < $count; $i++) { + do { + $popular_entity = $entities[array_rand($entities)]; + } while (array_key_exists($popular_entity->guid, $relationships)); + + $relationships[$popular_entity->guid] = array(); + + for ($c = 0; $c < $max; $c++) { + do { + $fan_entity = $entities[array_rand($entities)]; + } while ($fan_entity->guid == $popular_entity->guid || in_array($fan_entity->guid, $relationships[$popular_entity->guid])); + + $relationships[$popular_entity->guid][] = $fan_entity->guid; + add_entity_relationship($fan_entity->guid, $relationship_name, $popular_entity->guid); + } + + $max--; + } + + $options = array( + 'relationship' => $relationship_name, + 'limit' => $count + ); + + $entities = elgg_get_entities_from_relationship_count($options); + + foreach ($entities as $e) { + $options = array( + 'relationship' => $relationship_name, + 'limit' => 100, + 'relationship_guid' => $e->guid, + 'inverse_relationship' => true + ); + + $fan_entities = elgg_get_entities_from_relationship($options); + + $this->assertEqual(count($fan_entities), count($relationships[$e->guid])); + + foreach ($fan_entities as $fan_entity) { + $this->assertTrue(in_array($fan_entity->guid, $relationships[$e->guid])); + $this->assertNotIdentical(false, check_entity_relationship($fan_entity->guid, $relationship_name, $e->guid)); + } + } + } + + public function testElggGetEntitiesByGuidSingular() { + foreach ($this->entities as $e) { + $options = array( + 'guid' => $e->guid + ); + $es = elgg_get_entities($options); + + $this->assertEqual(count($es), 1); + $this->assertEqual($es[0]->guid, $e->guid); + } + } + + public function testElggGetEntitiesByGuidPlural() { + $guids = array(); + + foreach ($this->entities as $e) { + $guids[] = $e->guid; + } + + $options = array( + 'guids' => $guids, + 'limit' => 100 + ); + + $es = elgg_get_entities($options); + + $this->assertEqual(count($es), count($this->entities)); + + foreach ($es as $e) { + $this->assertTrue(in_array($e->guid, $guids)); + } + } + + public function testElggGetEntitiesFromAnnotationsCalculateX() { + $types = array( + 'sum', + 'avg', + 'min', + 'max' + ); + + foreach ($types as $type) { + $subtypes = $this->getRandomValidSubtypes(array('object'), 5); + $name = 'test_annotation_' . rand(0, 9999); + $values = array(); + $options = array( + 'type' => 'object', + 'subtypes' => $subtypes, + 'limit' => 5 + ); + + $es = elgg_get_entities($options); + + foreach ($es as $e) { + $value = rand(0,9999); + $e->annotate($name, $value); + + $value2 = rand(0,9999); + $e->annotate($name, $value2); + + switch ($type) { + case 'sum': + $calc_value = $value + $value2; + break; + + case 'avg': + $calc_value = ($value + $value2) / 2; + break; + + case 'min': + $calc_value = min(array($value, $value2)); + break; + + case 'max': + $calc_value = max(array($value, $value2)); + break; + } + + $values[$e->guid] = $calc_value; + } + + arsort($values); + $order = array_keys($values); + + $options = array( + 'type' => 'object', + 'subtypes' => $subtypes, + 'limit' => 5, + 'annotation_name' => $name, + 'calculation' => $type + ); + + $es = elgg_get_entities_from_annotation_calculation($options); + + foreach ($es as $i => $e) { + $value = 0; + $as = $e->getAnnotations($name); + // should only ever be 2 + $this->assertEqual(2, count($as)); + + $value = $as[0]->value; + $value2 = $as[1]->value; + + switch ($type) { + case 'sum': + $calc_value = $value + $value2; + break; + + case 'avg': + $calc_value = ($value + $value2) / 2; + break; + + case 'min': + $calc_value = min(array($value, $value2)); + break; + + case 'max': + $calc_value = max(array($value, $value2)); + break; + } + + $this->assertEqual($e->guid, $order[$i]); + $this->assertEqual($values[$e->guid], $calc_value); + } + } + } + + public function testElggGetEntitiesFromAnnotationCalculationCount() { + // add two annotations with a unique name to an entity + // then count the number of entities with that annotation name + + $subtypes = $this->getRandomValidSubtypes(array('object'), 1); + $name = 'test_annotation_' . rand(0, 9999); + $values = array(); + $options = array( + 'type' => 'object', + 'subtypes' => $subtypes, + 'limit' => 1 + ); + $es = elgg_get_entities($options); + $entity = $es[0]; + $value = rand(0, 9999); + $entity->annotate($name, $value); + $value = rand(0, 9999); + $entity->annotate($name, $value); + + $options = array( + 'type' => 'object', + 'subtypes' => $subtypes, + 'annotation_name' => $name, + 'calculation' => 'count', + 'count' => true, + ); + $count = elgg_get_entities_from_annotation_calculation($options); + $this->assertEqual(1, $count); + } + + public function testElggGetAnnotationsAnnotationNames() { + $options = array('annotation_names' => array()); + $a_e_map = array(); + + // create test annotations on a few entities. + for ($i=0; $i<3; $i++) { + do { + $e = $this->entities[array_rand($this->entities)]; + } while(in_array($e->guid, $a_e_map)); + $annotations = $this->createRandomAnnotations($e); + + foreach($annotations as $a) { + $options['annotation_names'][] = $a->name; + $a_e_map[$a->id] = $e->guid; + } + } + + $as = elgg_get_annotations($options); + + $this->assertEqual(count($a_e_map), count($as)); + + foreach ($as as $a) { + $this->assertEqual($a_e_map[$a->id], $a->entity_guid); + } + } + + public function testElggGetAnnotationsAnnotationValues() { + $options = array('annotation_values' => array()); + $a_e_map = array(); + + // create test annotations on a few entities. + for ($i=0; $i<3; $i++) { + do { + $e = $this->entities[array_rand($this->entities)]; + } while(in_array($e->guid, $a_e_map)); + $annotations = $this->createRandomAnnotations($e); + + foreach($annotations as $a) { + $options['annotation_values'][] = $a->value; + $a_e_map[$a->id] = $e->guid; + } + } + + $as = elgg_get_annotations($options); + + $this->assertEqual(count($a_e_map), count($as)); + + foreach ($as as $a) { + $this->assertEqual($a_e_map[$a->id], $a->entity_guid); + } + } + + public function testElggGetAnnotationsAnnotationOwnerGuids() { + $options = array('annotation_owner_guids' => array()); + $a_e_map = array(); + + // create test annotations on a single entity + for ($i=0; $i<3; $i++) { + do { + $e = $this->entities[array_rand($this->entities)]; + } while(in_array($e->guid, $a_e_map)); + + // remove annotations left over from previous tests. + elgg_delete_annotations(array('annotation_owner_guid' => $e->guid)); + $annotations = $this->createRandomAnnotations($e); + + foreach($annotations as $a) { + $options['annotation_owner_guids'][] = $e->guid; + $a_e_map[$a->id] = $e->guid; + } + } + + $as = elgg_get_annotations($options); + $this->assertEqual(count($a_e_map), count($as)); + + foreach ($as as $a) { + $this->assertEqual($a_e_map[$a->id], $a->owner_guid); + } + } + + public function testElggGetEntitiesBadWheres() { + $options = array( + 'container_guid' => 'abc' + ); + + $entities = elgg_get_entities($options); + $this->assertFalse($entities); + } + + public function testEGEEmptySubtypePlurality() { + $options = array( + 'type' => 'user', + 'subtypes' => '' + ); + + $entities = elgg_get_entities($options); + $this->assertTrue(is_array($entities)); + + $options = array( + 'type' => 'user', + 'subtype' => '' + ); + + $entities = elgg_get_entities($options); + $this->assertTrue(is_array($entities)); + + $options = array( + 'type' => 'user', + 'subtype' => array('') + ); + + $entities = elgg_get_entities($options); + $this->assertTrue(is_array($entities)); + + $options = array( + 'type' => 'user', + 'subtypes' => array('') + ); + + $entities = elgg_get_entities($options); + $this->assertTrue(is_array($entities)); + } } |
