Fix bug in facets search by parent and better document tests
This commit is contained in:
parent
d10a1e0bc2
commit
27c4a66dfc
|
@ -1255,46 +1255,71 @@ class Metadata extends Repository {
|
|||
*/
|
||||
public function _process_terms_tree($tree, $search_value, $search_type='parent') {
|
||||
|
||||
$h_map = [];
|
||||
$results = [];
|
||||
$h_map = []; // all terms will mapped to this array
|
||||
$results = []; // terms that match search criteria will be copied to this array
|
||||
foreach ( $tree as $h ) {
|
||||
|
||||
// if current term comes with have_items = 1 from the database
|
||||
// or, if it was temporarily added by its child that had have_items = 1:
|
||||
if ( $h->have_items > 0 || ( isset($h_map[$h->term_id]) && $h_map[$h->term_id]->have_items > 0 ) ) {
|
||||
|
||||
// in the case of a parent that was temporarily added by a child, mark it as having items as well
|
||||
$h->have_items = 1;
|
||||
$h_map[$h->term_id] = $h;
|
||||
$h_map[$h->term_id] = $h; // send it to the map array, overriding temporary item if existed
|
||||
|
||||
// if current term matches seach criteria, add to results array
|
||||
if(($search_type == 'parent' && $h->parent == $search_value) ||
|
||||
($search_type == 'name' && $h->have_items > 0 && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||
($search_type == 'name' && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||
$results[$h->term_id] = $h;
|
||||
}
|
||||
|
||||
// Now that we know this ter have_items. Lets climb the tree all the way up
|
||||
// marking all parents with have_items = 1
|
||||
$_parent = $h->parent;
|
||||
|
||||
// If parent was not added to the map array yet
|
||||
// Lets add a temporary entry
|
||||
if ( $h->parent > 0 && !isset($h_map[$_parent]) ) {
|
||||
$h_map[$_parent] = (object)['have_items' => 1];
|
||||
}
|
||||
|
||||
// Now lets loop thorough the map array until I check all my parents
|
||||
while( isset($h_map[$_parent]) && $h_map[$_parent]->have_items != 1 ) {
|
||||
|
||||
// If my parent was added before, but marked with have_items = 0
|
||||
// Lets set it to 1
|
||||
|
||||
$h_map[$_parent]->have_items = 1;
|
||||
|
||||
// If my parent is a whole object, and not a temporary one
|
||||
if ( isset($h_map[$_parent]->parent) ) {
|
||||
if(($search_type == 'parent' && $h->parent == $search_value) ||
|
||||
($search_type == 'name' && $h->have_items > 0 && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||
// if parent matches search criteira, add to results
|
||||
if(($search_type == 'parent' && $h_map[$_parent]->parent == $search_value) ||
|
||||
($search_type == 'name' && strpos(strtolower($h_map[$_parent]->name), strtolower($search_value)) !== false)) {
|
||||
$results[$h_map[$_parent]->term_id] = $h_map[$_parent];
|
||||
}
|
||||
// increment _parent to next Loop interation
|
||||
$_parent = $h_map[$_parent]->parent;
|
||||
} else {
|
||||
// Quit loop. We have reached as high as we could in the tree
|
||||
$_parent = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// if current term have_items = 0
|
||||
|
||||
// add it to the map
|
||||
$h_map[$h->term_id] = $h;
|
||||
|
||||
// if parent was not mapped yet, create a temporary entry for him
|
||||
if ( $h->parent > 0 && !isset($h_map[$h->parent]) ) {
|
||||
$h_map[$h->parent] = (object)['have_items' => $h->have_items];
|
||||
}
|
||||
|
||||
// if item matches search criteria, add it to the results
|
||||
if(($search_type == 'parent' && $h->parent == $search_value) ||
|
||||
($search_type == 'name' && $h->have_items > 0 && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||
$results[$h->term_id] = $h;
|
||||
|
@ -1303,7 +1328,7 @@ class Metadata extends Repository {
|
|||
|
||||
}
|
||||
|
||||
// Results have all terms with wanted parent. Now we unset those who dont have items
|
||||
// Results have all terms that matches search criteria. Now we unset those who dont have items
|
||||
// and set it back to incremental keys]
|
||||
// we could have sent to $results only those with items, but doing that we would not preserve their order
|
||||
$results = array_reduce($results, function ($return, $el) {
|
||||
|
|
|
@ -16,7 +16,69 @@ use Tainacan\Entities;
|
|||
class Facets extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public $items_ids = [];
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* This tests use sample data in this format
|
||||
*
|
||||
* Taxonomy 1
|
||||
* + Term for collection 1
|
||||
* +-- Term 1 Child
|
||||
* + Term for collection 2
|
||||
* +-- Term 2 Child
|
||||
* + Term for All
|
||||
* +-- Term for All child
|
||||
* + Term for nobody
|
||||
*
|
||||
* Taxonomy 2
|
||||
* + Root
|
||||
* +-- Children (with items)
|
||||
* +-- Children2
|
||||
* +---- GChildren
|
||||
* +---- GChildren2
|
||||
* + Root2
|
||||
* +-- Children (with items)
|
||||
* +-- Children2
|
||||
* +---- GChildren
|
||||
* +---- GChildren2 (with items)
|
||||
* +------ GGChildren (with items)
|
||||
*
|
||||
* Text Metadata in repository level (inherited by two collections)
|
||||
* *** Evey item as has a value of "Value %d" where %d is the item number (1 to 80)
|
||||
*
|
||||
* Collection 1
|
||||
* * 40 items
|
||||
* * Taxonomy metadata (Taxonomy 1)
|
||||
* ** items 1 - 10 => Term for collection 1
|
||||
* ** items 11 - 20 => Term 1 Child
|
||||
* ** items 21 - 30 => Term for All
|
||||
* ** items 31 - 40 => Term for All child
|
||||
*
|
||||
* * Taxonomy metadata (Taxonomy 2)
|
||||
* ** items 1 - 10 => Root Children
|
||||
* ** items 11 - 20 => Root2 Children
|
||||
* ** items 21 - 30 => Root2 GChildren2
|
||||
* ** items 31 - 40 => Root2 GGChildren
|
||||
*
|
||||
* * Text Metadata
|
||||
* ** items 1 - 40 => even/odd
|
||||
*
|
||||
*
|
||||
* Collection 2
|
||||
* * 40 items
|
||||
* * Taxonomy metadata (Taxonomy 1)
|
||||
* ** items 41 - 50 => Term for All
|
||||
* ** items 51 - 60 => Term for All child
|
||||
* ** items 61 - 70 => Term for collection 2
|
||||
* ** items 71 - 80 => Term 2 Child
|
||||
*
|
||||
* * Relationship Metadata (pointing to collection 1)
|
||||
* ** items 41 - 80 => related to items 1 - 40 in sequence
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$collection1 = $this->tainacan_entity_factory->create_entity(
|
||||
|
@ -134,6 +196,7 @@ class Facets extends TAINACAN_UnitApiTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
$this->term2_root = $term2_root;
|
||||
$term2_root2 = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -142,6 +205,7 @@ class Facets extends TAINACAN_UnitApiTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
$this->term2_root2 = $term2_root2;
|
||||
$term2_root_c1 = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -268,7 +332,7 @@ class Facets extends TAINACAN_UnitApiTestCase {
|
|||
array(
|
||||
'name' => 'test taxonomy',
|
||||
'status' => 'publish',
|
||||
'collection' => $collection2,
|
||||
'collection' => $collection1,
|
||||
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
|
||||
'metadata_type_options' => [
|
||||
'allow_new_terms' => true,
|
||||
|
@ -895,7 +959,25 @@ class Facets extends TAINACAN_UnitApiTestCase {
|
|||
] );
|
||||
$values = $this->get_values($values);
|
||||
$this->assertEquals(3, sizeof($values));
|
||||
|
||||
|
||||
// test parent
|
||||
$values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [
|
||||
'count_items' => true,
|
||||
'parent_id' => $this->term2_root2_c2->get_id()
|
||||
] );
|
||||
$values = $this->get_values($values);
|
||||
$this->assertEquals(1, sizeof($values));
|
||||
$this->assertEquals($values[0]['value'], $this->term2_root2_gc2->get_id());
|
||||
|
||||
$values = $this->repository->fetch_all_metadatum_values( $this->meta_3_tax->get_id(), [
|
||||
'count_items' => true,
|
||||
'items_filter' => false,
|
||||
'parent' => $this->term2_root2_c2->get_id()
|
||||
] );
|
||||
$values = $this->get_values($values);
|
||||
$this->assertEquals(2, sizeof($values));
|
||||
|
||||
|
||||
|
||||
// test search taxonomy without filter
|
||||
// test count items taxonomy
|
||||
|
|
Loading…
Reference in New Issue