From ddd7872bdfb68369d0e0e1b0401a8640b08bfc7c Mon Sep 17 00:00:00 2001 From: cash Date: Wed, 23 Nov 2011 11:09:42 -0500 Subject: Refs #4143 adds a unit test for type/subtype where sql generation --- engine/lib/river.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'engine/lib/river.php') diff --git a/engine/lib/river.php b/engine/lib/river.php index 466eca253..309aeb30f 100644 --- a/engine/lib/river.php +++ b/engine/lib/river.php @@ -610,6 +610,16 @@ function elgg_river_page_handler($page) { return true; } +/** + * Register river unit tests + * @access private + */ +function elgg_river_test($hook, $type, $value) { + global $CONFIG; + $value[] = $CONFIG->path . 'engine/tests/api/river.php'; + return $value; +} + /** * Initialize river library * @access private @@ -620,6 +630,8 @@ function elgg_river_init() { elgg_register_menu_item('site', $item); elgg_register_widget_type('river_widget', elgg_echo('river:widget:title'), elgg_echo('river:widget:description')); + + elgg_register_plugin_hook_handler('unit_test', 'system', 'elgg_river_test'); } elgg_register_event_handler('init', 'system', 'elgg_river_init'); -- cgit v1.2.3 From 583e52700ca5c7ed1d0dd58054dbd08208c0c158 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Thu, 17 Nov 2011 14:17:24 +0100 Subject: Refs #3496 improving type/subtype handling for the river --- engine/lib/river.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'engine/lib/river.php') diff --git a/engine/lib/river.php b/engine/lib/river.php index 309aeb30f..02d52dea1 100644 --- a/engine/lib/river.php +++ b/engine/lib/river.php @@ -447,7 +447,8 @@ function elgg_get_river_type_subtype_where_sql($table, $types, $subtypes, $pairs return ''; } - $wheres = array(); + $types_wheres = array(); + $subtypes_wheres = array(); // if no pairs, use types and subtypes if (!is_array($pairs)) { @@ -457,7 +458,7 @@ function elgg_get_river_type_subtype_where_sql($table, $types, $subtypes, $pairs } foreach ($types as $type) { $type = sanitise_string($type); - $wheres[] = "({$table}.type = '$type')"; + $types_wheres[] = "({$table}.type = '$type')"; } } @@ -467,13 +468,20 @@ function elgg_get_river_type_subtype_where_sql($table, $types, $subtypes, $pairs } foreach ($subtypes as $subtype) { $subtype = sanitise_string($subtype); - $wheres[] = "({$table}.subtype = '$subtype')"; + $subtypes_wheres[] = "({$table}.subtype = '$subtype')"; } } - if (is_array($wheres) && count($wheres)) { - $wheres = array(implode(' OR ', $wheres)); + if (is_array($types_wheres) && count($types_wheres)) { + $types_wheres = array(implode(' OR ', $types_wheres)); } + + if (is_array($subtypes_wheres) && count($subtypes_wheres)) { + $subtypes_wheres = array(implode(' OR ', $subtypes_wheres)); + } + + $wheres = array(implode(' AND ', array_merge($types_wheres, $subtypes_wheres))); + } else { // using type/subtype pairs foreach ($pairs as $paired_type => $paired_subtypes) { -- cgit v1.2.3 From 6cc08f83225237d0cd57e0a5c0ebec0a8327aa30 Mon Sep 17 00:00:00 2001 From: cash Date: Mon, 28 Nov 2011 21:24:42 -0500 Subject: Fixes #3496 finished up this ticket by updating unit test and using more parantheses to make it clear how the OR/AND gets applied --- engine/lib/river.php | 5 +++-- engine/tests/api/river.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'engine/lib/river.php') diff --git a/engine/lib/river.php b/engine/lib/river.php index 02d52dea1..421813441 100644 --- a/engine/lib/river.php +++ b/engine/lib/river.php @@ -207,6 +207,8 @@ function elgg_delete_river(array $options = array()) { /** * Get river items * + * @note If using types and subtypes in a query, they are joined with an AND. + * * @param array $options * ids => INT|ARR River item id(s) * subject_guids => INT|ARR Subject guid(s) @@ -430,7 +432,6 @@ function elgg_river_get_access_sql() { * * @internal This is a simplified version of elgg_get_entity_type_subtype_where_sql() * which could be used for all queries once the subtypes have been denormalized. - * FYI: It allows types and subtypes to not be paired. * * @param string $table 'rv' * @param NULL|array $types Array of types or NULL if none. @@ -477,7 +478,7 @@ function elgg_get_river_type_subtype_where_sql($table, $types, $subtypes, $pairs } if (is_array($subtypes_wheres) && count($subtypes_wheres)) { - $subtypes_wheres = array(implode(' OR ', $subtypes_wheres)); + $subtypes_wheres = array('(' . implode(' OR ', $subtypes_wheres) . ')'); } $wheres = array(implode(' AND ', array_merge($types_wheres, $subtypes_wheres))); diff --git a/engine/tests/api/river.php b/engine/tests/api/river.php index 55ddbfeec..6931b9f41 100644 --- a/engine/tests/api/river.php +++ b/engine/tests/api/river.php @@ -11,7 +11,7 @@ class ElggCoreRiverAPITest extends ElggCoreUnitTest { $types = array('object'); $subtypes = array('blog'); $result = elgg_get_river_type_subtype_where_sql('rv', $types, $subtypes, null); - $this->assertIdentical($result, "((rv.type = 'object') AND (rv.subtype = 'blog'))"); + $this->assertIdentical($result, "((rv.type = 'object') AND ((rv.subtype = 'blog')))"); $types = array('object'); $subtypes = array('blog', 'file'); -- cgit v1.2.3