From d15b5098d14bb2a3f8f887b5e68f174928f0d0a4 Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 24 Feb 2010 02:20:15 +0000 Subject: Fixes #1542: Pulled in Kevin's patch to add ability to sort by metadata. Added unit tests for this. git-svn-id: http://code.elgg.org/elgg/trunk@3970 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/tests/api/entity_getter_functions.php | 120 +++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) (limited to 'engine/tests') diff --git a/engine/tests/api/entity_getter_functions.php b/engine/tests/api/entity_getter_functions.php index 04fa3e4e5..49fd4ec0d 100644 --- a/engine/tests/api/entity_getter_functions.php +++ b/engine/tests/api/entity_getter_functions.php @@ -1610,4 +1610,124 @@ class ElggCoreEntityGetterFunctionsTest extends ElggCoreUnitTest { } } } + + function testElggApiGettersEntityMetadataNVPOrderByMDText() { + $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->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid->getGUID(); + + $valid2 = new ElggObject(); + $valid2->subtype = $subtype; + $valid2->$md_name = 3; + $valid2->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid2->getGUID(); + + $valid3 = new ElggObject(); + $valid3->subtype = $subtype; + $valid3->$md_name = 1; + $valid3->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid3->getGUID(); + + $md_valid_values = array($md_value, $md_value2); + + $options = array( + 'type' => 'object', + 'subtype' => $subtype, + //'metadata_name' => $md_name, + 'order_by_metadata' => array('name' => $md_name, 'as' => 'integer') + ); + + $entities = elgg_get_entities_from_metadata($options); + + $this->assertIsa($entities, 'array'); + $this->assertEqual(count($entities), 3); + + $i = 1; + foreach ($entities as $entity) { + $this->assertTrue(in_array($entity->getGUID(), $valid_guids)); + $this->assertEqual($entity->$md_name, $i); + $i++; + $entity->delete(); + } + + foreach ($guids as $guid) { + if ($e = get_entity($guid)) { + $e->delete(); + } + } + } + + function testElggApiGettersEntityMetadataNVPOrderByMDString() { + $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->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid->getGUID(); + + $valid2 = new ElggObject(); + $valid2->subtype = $subtype; + $valid2->$md_name = 'c'; + $valid2->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid2->getGUID(); + + $valid3 = new ElggObject(); + $valid3->subtype = $subtype; + $valid3->$md_name = 'a'; + $valid3->save(); + $guids[] = $valid->getGUID(); + $valid_guids[] = $valid3->getGUID(); + + $md_valid_values = array($md_value, $md_value2); + + $options = array( + 'type' => 'object', + 'subtype' => $subtype, + 'metadata_name' => $md_name, + 'order_by_metadata' => array('name' => $md_name, 'as' => 'text') + ); + + $entities = elgg_get_entities_from_metadata($options); + + $this->assertIsa($entities, 'array'); + $this->assertEqual(count($entities), 3); + + $alpha = array('a', 'b', 'c'); + + $i = 0; + foreach ($entities as $entity) { + $this->assertTrue(in_array($entity->getGUID(), $valid_guids)); + $this->assertEqual($entity->$md_name, $alpha[$i]); + $i++; + $entity->delete(); + } + + foreach ($guids as $guid) { + if ($e = get_entity($guid)) { + $e->delete(); + } + } + } } -- cgit v1.2.3