From 8b0af53de693fbd8a5bff2c089bf48fafc695f10 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Thu, 2 May 2019 11:58:43 -0300 Subject: [PATCH] adds tests for facets with hierarchical taxonomies --- .../repositories/class-tainacan-metadata.php | 11 ++- tests/test-facets.php | 94 ++++++++++++++++--- 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index c20214707..35cab99b1 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -1066,15 +1066,18 @@ class Metadata extends Repository { } } else { + $parent_q = $wpdb->prepare("AND tt.parent = %d", $args['parent_id']); + if ($search_q) { + $parent_q = ''; + } $base_query = $wpdb->prepare("FROM $wpdb->term_taxonomy tt INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id - WHERE - tt.parent = %d AND - tt.taxonomy = %s + WHERE 1=1 + $parent_q + AND tt.taxonomy = %s $search_q ORDER BY t.name ASC ", - $args['parent_id'], $taxonomy_slug ); diff --git a/tests/test-facets.php b/tests/test-facets.php index 111088cf7..0607be115 100644 --- a/tests/test-facets.php +++ b/tests/test-facets.php @@ -712,20 +712,21 @@ class Facets extends TAINACAN_UnitApiTestCase { $values = $this->repository->fetch_all_metadatum_values( $this->meta_2_tax->get_id(), [ 'count_items' => true, - 'collection_id' => $this->collection2->get_id(), 'search' => 'child', - 'items_filter' => false + 'items_filter' => false, ] ); $values = $this->get_values($values); - $this->assertEquals( 2, sizeof($values) ); + $this->assertEquals( 3, sizeof($values) ); $valuesParsed = array_map(function($el) { - $this->assertEquals( 10, $el['total_items'] ); + //$this->assertEquals( 10, $el['total_items'] ); + $this->assertContains($el['total_items'], [10,20]); return $el['label']; }, $values); - $this->assertContains( 'Term for collection 1', $valuesParsed); - $this->assertContains( 'Term for collection 2', $valuesParsed); + $this->assertContains( 'Term for collection 2 child', $valuesParsed); + $this->assertContains( 'Term for collection 1 child', $valuesParsed); + $this->assertContains( 'Term for all child', $valuesParsed); // test search relationship without filter $values = $this->repository->fetch_all_metadatum_values( $this->meta_relationship->get_id(), [ @@ -809,7 +810,7 @@ class Facets extends TAINACAN_UnitApiTestCase { 'search' => 'Children', ]); $values = $this->get_values($values); - $this->assertEquals(5, sizeof($values)); + $this->assertEquals(9, sizeof($values)); $values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ 'items_filter' => false, @@ -819,15 +820,84 @@ class Facets extends TAINACAN_UnitApiTestCase { $this->assertEquals(1, sizeof($values)); // test search taxonomy with filter - // test search taxonomy without filter + $values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'count_items' => true, + 'search' => 'GGC', + 'items_filter' => [ + 'meta_query' => [ + [ + 'key' => $this->metadatum_text->get_id(), + 'value' => ['even'] + ] + ] + ] + ] ); + $values = $this->get_values($values); + $this->assertEquals( 1, sizeof($values) ); + $this->assertEquals( 5, $values[0]['total_items']); + // test offset taxonomy + $values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'items_filter' => false, + 'search' => 'Children', + 'number' => 9, + 'offset' => 0 + ]); + $values = $this->get_values($values); + $this->assertEquals(9, sizeof($values)); + + $values_p1 = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'items_filter' => false, + 'search' => 'Children', + 'number' => 3, + 'offset' => 0 + ]); + $values_p1 = $this->get_values($values_p1); + $this->assertEquals(3, sizeof($values_p1)); + + $values_p2 = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'items_filter' => false, + 'search' => 'Children', + 'number' => 3, + 'offset' => 3 + ]); + $values_p2 = $this->get_values($values_p2); + $this->assertEquals(3, sizeof($values_p2)); + + $values_p3 = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'items_filter' => false, + 'search' => 'Children', + 'number' => 3, + 'offset' => 6 + ]); + $values_p3 = $this->get_values($values_p3); + $this->assertEquals(3, sizeof($values_p3)); + + $this->assertEquals($values[0]['label'], $values_p1[0]['label']); + $this->assertEquals($values[3]['label'], $values_p2[0]['label']); + $this->assertEquals($values[6]['label'], $values_p3[0]['label']); + // test include taxonomy + $values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [ + 'count_items' => true, + 'include' => ['18','16'], + 'search' => 'GGC', + 'items_filter' => [ + 'meta_query' => [ + [ + 'key' => $this->metadatum_text->get_id(), + 'value' => ['even'] + ] + ] + ] + ] ); + $values = $this->get_values($values); + $this->assertEquals(3, sizeof($values)); + + + // test search taxonomy without filter // test count items taxonomy - // - - - }