From f48bd5099f033cc148f9c1e3f781f8c932bda891 Mon Sep 17 00:00:00 2001 From: cash Date: Sat, 26 Mar 2011 14:31:14 +0000 Subject: Closes #3202 'count' already supported, added unit tests git-svn-id: http://code.elgg.org/elgg/trunk@8840 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/annotations.php | 11 ++++ engine/lib/metadata.php | 2 +- engine/tests/api/annotations.php | 46 ++++++++++++++ engine/tests/api/metadata.php | 125 ++++++++++++++++++++++++++++++++++++++ engine/tests/objects/metadata.php | 106 -------------------------------- 5 files changed, 183 insertions(+), 107 deletions(-) create mode 100644 engine/tests/api/annotations.php create mode 100644 engine/tests/api/metadata.php delete mode 100644 engine/tests/objects/metadata.php diff --git a/engine/lib/annotations.php b/engine/lib/annotations.php index 130ab37ab..0e446c949 100644 --- a/engine/lib/annotations.php +++ b/engine/lib/annotations.php @@ -536,3 +536,14 @@ function elgg_register_annotation_url_handler($extender_name = "all", $function_ /** Register the hook */ elgg_register_plugin_hook_handler("export", "all", "export_annotation_plugin_hook", 2); + +elgg_register_plugin_hook_handler('unit_test', 'system', 'annotations_test'); + +/** + * Register annotation unit tests + */ +function annotations_test($hook, $type, $value, $params) { + global $CONFIG; + $value[] = $CONFIG->path . 'engine/tests/api/annotations.php'; + return $value; +} diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index 3182ed077..cc9212711 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -900,6 +900,6 @@ elgg_register_plugin_hook_handler('unit_test', 'system', 'metadata_test'); */ function metadata_test($hook, $type, $value, $params) { global $CONFIG; - $value[] = $CONFIG->path . 'engine/tests/objects/metadata.php'; + $value[] = $CONFIG->path . 'engine/tests/api/metadata.php'; return $value; } \ No newline at end of file diff --git a/engine/tests/api/annotations.php b/engine/tests/api/annotations.php new file mode 100644 index 000000000..d7551a0fa --- /dev/null +++ b/engine/tests/api/annotations.php @@ -0,0 +1,46 @@ +object = new ElggObject(); + } + + /** + * Called after each test method. + */ + public function tearDown() { + // do not allow SimpleTest to interpret Elgg notices as exceptions + $this->swallowErrors(); + + unset($this->object); + } + + public function testElggGetAnnotationsCount() { + $this->object->title = 'Annotation Unit Test'; + $this->object->save(); + + $guid = $this->object->getGUID(); + create_annotation($guid, 'tested', 'tested1', 'text', 0, ACCESS_PUBLIC); + create_annotation($guid, 'tested', 'tested2', 'text', 0, ACCESS_PUBLIC); + + $count = (int)elgg_get_annotations(array( + 'annotation_names' => array('tested'), + 'guid' => $guid, + 'count' => true, + )); + + $this->assertIdentical($count, 2); + + $this->object->delete(); + } +} diff --git a/engine/tests/api/metadata.php b/engine/tests/api/metadata.php new file mode 100644 index 000000000..d9113b68a --- /dev/null +++ b/engine/tests/api/metadata.php @@ -0,0 +1,125 @@ +metastrings = array(); + $this->object = new ElggObject(); + } + + /** + * Called after each test method. + */ + public function tearDown() { + // do not allow SimpleTest to interpret Elgg notices as exceptions + $this->swallowErrors(); + + unset($this->object); + } + + public function testGetMetastringById() { + foreach (array('metaUnitTest', 'metaunittest', 'METAUNITTEST') as $string) { + $this->create_metastring($string); + } + + // lookup metastring id + $cs_ids = get_metastring_id('metaUnitTest', TRUE); + $this->assertEqual($cs_ids, $this->metastrings['metaUnitTest']); + + // lookup all metastrings, ignoring case + $cs_ids = get_metastring_id('metaUnitTest', FALSE); + $this->assertEqual(count($cs_ids), 3); + $this->assertEqual(count($cs_ids), count($this->metastrings)); + foreach ($cs_ids as $string ) + { + $this->assertTrue(in_array($string, $this->metastrings)); + } + + // clean up + $this->delete_metastrings(); + } + + public function testElggGetEntitiesFromMetadata() { + global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; + $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); + + $this->object->title = 'Meta Unit Test'; + $this->object->save(); + $this->create_metastring('metaUnitTest'); + $this->create_metastring('tested'); + + // create_metadata returns id of metadata on success + $this->assertTrue(create_metadata($this->object->guid, 'metaUnitTest', 'tested')); + + // check value with improper case + $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => TRUE); + $this->assertFalse(elgg_get_entities_from_metadata($options)); + + // compare forced case with ignored case + $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'tested', 'limit' => 10, 'metadata_case_sensitive' => TRUE); + $case_true = elgg_get_entities_from_metadata($options); + $this->assertIsA($case_true, 'array'); + + $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => FALSE); + $case_false = elgg_get_entities_from_metadata($options); + $this->assertIsA($case_false, 'array'); + + $this->assertIdentical($case_true, $case_false); + + // check deprecated get_entities_from_metadata() function + $deprecated = get_entities_from_metadata('metaUnitTest', 'tested', '', '', 0, 10, 0, '', 0, FALSE, TRUE); + $this->assertIdentical($deprecated, $case_true); + + // check entity list + //$this->dump(list_entities_from_metadata('metaUnitTest', 'Tested', '', '', 0, 10, TRUE, TRUE, TRUE, FALSE)); + + // clean up + $this->delete_metastrings(); + $this->object->delete(); + } + + public function testElggGetMetadataCount() { + $this->object->title = 'Meta Unit Test'; + $this->object->save(); + + $guid = $this->object->getGUID(); + create_metadata($guid, 'tested', 'tested1', 'text', 0, ACCESS_PUBLIC, true); + create_metadata($guid, 'tested', 'tested2', 'text', 0, ACCESS_PUBLIC, true); + + $count = (int)elgg_get_metadata(array( + 'metadata_names' => array('tested'), + 'guid' => $guid, + 'count' => true, + )); + + $this->assertIdentical($count, 2); + + $this->object->delete(); + } + + + protected function create_metastring($string) { + global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; + $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); + + mysql_query("INSERT INTO {$CONFIG->dbprefix}metastrings (string) VALUES ('$string')"); + $this->metastrings[$string] = mysql_insert_id(); + } + + protected function delete_metastrings() { + global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; + $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); + + $strings = implode(', ', $this->metastrings); + mysql_query("DELETE FROM {$CONFIG->dbprefix}metastrings WHERE id IN ($strings)"); + } +} diff --git a/engine/tests/objects/metadata.php b/engine/tests/objects/metadata.php deleted file mode 100644 index b5b9aba02..000000000 --- a/engine/tests/objects/metadata.php +++ /dev/null @@ -1,106 +0,0 @@ -metastrings = array(); - $this->object = new ElggObject(); - } - - /** - * Called after each test method. - */ - public function tearDown() { - // do not allow SimpleTest to interpret Elgg notices as exceptions - $this->swallowErrors(); - - unset($this->object); - } - - public function testGetMetastringById() { - foreach (array('metaUnitTest', 'metaunittest', 'METAUNITTEST') as $string) { - $this->create_metastring($string); - } - - // lookup metastring id - $cs_ids = get_metastring_id('metaUnitTest', TRUE); - $this->assertEqual($cs_ids, $this->metastrings['metaUnitTest']); - - // lookup all metastrings, ignoring case - $cs_ids = get_metastring_id('metaUnitTest', FALSE); - $this->assertEqual(count($cs_ids), 3); - $this->assertEqual(count($cs_ids), count($this->metastrings)); - foreach ($cs_ids as $string ) - { - $this->assertTrue(in_array($string, $this->metastrings)); - } - - // clean up - $this->delete_metastrings(); - } - - public function testElggGetEntitiesFromMetadata() { - global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; - $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); - - $this->object->title = 'Meta Unit Test'; - $this->object->save(); - $this->create_metastring('metaUnitTest'); - $this->create_metastring('tested'); - - // create_metadata returns id of metadata on success - $this->assertTrue(create_metadata($this->object->guid, 'metaUnitTest', 'tested')); - - // check value with improper case - $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => TRUE); - $this->assertFalse(elgg_get_entities_from_metadata($options)); - - // compare forced case with ignored case - $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'tested', 'limit' => 10, 'metadata_case_sensitive' => TRUE); - $case_true = elgg_get_entities_from_metadata($options); - $this->assertIsA($case_true, 'array'); - - $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => FALSE); - $case_false = elgg_get_entities_from_metadata($options); - $this->assertIsA($case_false, 'array'); - - $this->assertIdentical($case_true, $case_false); - - // check deprecated get_entities_from_metadata() function - $deprecated = get_entities_from_metadata('metaUnitTest', 'tested', '', '', 0, 10, 0, '', 0, FALSE, TRUE); - $this->assertIdentical($deprecated, $case_true); - - // check entity list - //$this->dump(list_entities_from_metadata('metaUnitTest', 'Tested', '', '', 0, 10, TRUE, TRUE, TRUE, FALSE)); - - // clean up - $this->delete_metastrings(); - $this->object->delete(); - } - - - protected function create_metastring($string) { - global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; - $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); - - mysql_query("INSERT INTO {$CONFIG->dbprefix}metastrings (string) VALUES ('$string')"); - $this->metastrings[$string] = mysql_insert_id(); - } - - protected function delete_metastrings() { - global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; - $METASTRINGS_CACHE = $METASTRINGS_DEADNAME_CACHE = array(); - - $strings = implode(', ', $this->metastrings); - mysql_query("DELETE FROM {$CONFIG->dbprefix}metastrings WHERE id IN ($strings)"); - } -} -- cgit v1.2.3