From fc2898f701a6436f8fbbd2b982816fe26b37313e Mon Sep 17 00:00:00 2001 From: leogermani Date: Fri, 25 Oct 2019 17:28:05 -0300 Subject: [PATCH] set taxmeta as private when tax turns private #274 --- .../repositories/class-tainacan-metadata.php | 389 ++++++++++-------- tests/test-category-metadatum-types.php | 268 +++++++----- 2 files changed, 371 insertions(+), 286 deletions(-) diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index 61668dfe0..bbc0a8a59 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -40,6 +40,8 @@ class Metadata extends Repository { add_filter( 'pre_trash_post', array( &$this, 'disable_delete_core_metadata' ), 10, 2 ); add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 ); + add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']); + } /** @@ -373,57 +375,57 @@ class Metadata extends Repository { //get parent collections $parents = get_post_ancestors( $collection_id ); - + //insert the actual collection if ( is_numeric($collection_id) ) { $parents[] = $collection_id; } - + //search for default metadatum $parents[] = $this->get_default_metadata_attribute(); - + $results = []; - + $args = array_merge( [ 'parent' => 0 ], $args ); - + $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : []; - + /** - * Since we introduced roles & capabalities management, we can not rely + * Since we introduced roles & capabalities management, we can not rely * on WordPress behavior when handling default post status values. - * WordPress checks if the current user can read_priva_posts, but this is - * not enough for us. We have to handle this ourselves to mimic WordPress behavior - * considering how tainacan manages metadata capabilities + * WordPress checks if the current user can read_priva_posts, but this is + * not enough for us. We have to handle this ourselves to mimic WordPress behavior + * considering how tainacan manages metadata capabilities */ if ( ! isset($args['post_status']) ) { - + foreach ( $parents as $parent_id ) { - + // Add public states. $statuses = get_post_stati( array( 'public' => true ) ); - + $read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata'; if ( current_user_can($read_private_cap) ) { $statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) ); } - + $args['post_status'] = $statuses; $meta_query = array( 'key' => 'collection_id', 'value' => $parent_id, ); - + $args['meta_query'] = $original_meta_q; $args['meta_query'][] = $meta_query; - + //var_dump($args); $results = array_merge($results, $this->fetch( $args, 'OBJECT' )); - + } - + } else { $meta_query = array( 'key' => 'collection_id', @@ -434,15 +436,15 @@ class Metadata extends Repository { $args = array_merge( [ 'parent' => 0 ], $args ); - + $args['meta_query'] = $original_meta_q; $args['meta_query'][] = $meta_query; - + $results = $this->fetch( $args, 'OBJECT' ); } - - - + + + return $this->order_result( $results, $collection, @@ -476,51 +478,51 @@ class Metadata extends Repository { if ( is_numeric($collection_id) ) { $parents[] = $collection_id; } - + //search for default metadatum $parents[] = $this->get_default_metadata_attribute(); - + $results = []; - + $args = array_merge( [ 'parent' => 0 ], $args ); - + $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : []; - + /** - * Since we introduced roles & capabalities management, we can not rely + * Since we introduced roles & capabalities management, we can not rely * on WordPress behavior when handling default post status values. - * WordPress checks if the current user can read_priva_posts, but this is - * not enough for us. We have to handle this ourselves to mimic WordPress behavior - * considering how tainacan manages metadata capabilities + * WordPress checks if the current user can read_priva_posts, but this is + * not enough for us. We have to handle this ourselves to mimic WordPress behavior + * considering how tainacan manages metadata capabilities */ if ( ! isset($args['post_status']) ) { - + foreach ( $parents as $parent_id ) { - + // Add public states. $statuses = get_post_stati( array( 'public' => true ) ); - + $read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata'; if ( current_user_can($read_private_cap) ) { $statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) ); } - + $args['post_status'] = $statuses; $meta_query = array( 'key' => 'collection_id', 'value' => $parent_id, ); - + $args['meta_query'] = $original_meta_q; $args['meta_query'][] = $meta_query; - + $results = array_merge($results, $this->fetch_ids( $args )); - + } - + } else { $meta_query = array( 'key' => 'collection_id', @@ -531,10 +533,10 @@ class Metadata extends Repository { $args = array_merge( [ 'parent' => 0 ], $args ); - + $args['meta_query'] = $original_meta_q; $args['meta_query'][] = $meta_query; - + $results = $this->fetch_ids( $args ); } @@ -550,7 +552,7 @@ class Metadata extends Repository { * @param Entities\Collection $collection * @param bool $include_disabled Wether to include disabled metadata in the results or not * - * @return array + * @return array */ public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) { $order = $collection->get_metadata_order(); @@ -585,7 +587,7 @@ class Metadata extends Repository { $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); return $result_ordinate; - } + } } @@ -678,8 +680,8 @@ class Metadata extends Repository { "UPDATE $wpdb->postmeta SET meta_key = %s WHERE meta_key = %s AND post_id IN ( - SELECT ID - FROM $wpdb->posts + SELECT ID + FROM $wpdb->posts WHERE post_type = %s )", $new_title_metadatum->get_id(), $old_title_metadatum->get_id(), $item_post_type ); @@ -690,8 +692,8 @@ class Metadata extends Repository { "UPDATE $wpdb->postmeta SET meta_key = %s WHERE meta_key = %s AND post_id IN ( - SELECT ID - FROM $wpdb->posts + SELECT ID + FROM $wpdb->posts WHERE post_type = %s )", $new_description_metadatum->get_id(), $old_description_metadatum->get_id(), $item_post_type ); @@ -708,7 +710,7 @@ class Metadata extends Repository { * @return array */ private function get_data_core_metadata( Entities\Collection $collection ) { - + return $data_core_metadata = [ 'core_description' => [ 'name' => 'Description', @@ -775,11 +777,11 @@ class Metadata extends Repository { * @throws \Exception */ public function disable_delete_core_metadata( $before, $post ) { - + if ( Entities\Metadatum::get_post_type() != $post->post_type ) { return null; } - + $metadatum = $this->fetch( $post->ID ); if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) { @@ -799,11 +801,11 @@ class Metadata extends Repository { * @internal param The $post_id post ID which is deleting */ public function force_delete_core_metadata( $before, $post, $force_delete ) { - + if ( Entities\Metadatum::get_post_type() != $post->post_type ) { return null; } - + $metadatum = $this->fetch( $post->ID ); if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) { @@ -915,7 +917,7 @@ class Metadata extends Repository { /** - * Return all possible values for a metadatum + * Return all possible values for a metadatum * * Each metadata is a label with the metadatum name and the value. * @@ -925,32 +927,32 @@ class Metadata extends Repository { * @param int $metadatum_id The ID of the metadata to fetch values from * @param array|string $args { * Optional. Array or string of arguments. - * + * * @type mixed $collection_id The collection ID you want to consider or null for all collections. If a collectoin is set * then only values applied to items in this collection will be returned - * + * * @type int $number The number of values to return (for pagination). Default empty (unlimited) - * + * * @type int $offset The offset (for pagination). Default 0 - * + * * @type array|bool $items_filter Array in the same format used in @see \Tainacan\Repositories\Items::fetch(). It will filter the results to only return values used in the items inside this criteria. If false, it will return all values, even unused ones. Defatul [] (all items) - * + * * @type array $include Array if ids to be included in the result. Default [] (nothing) * * @type array $search String to search. It will only return values that has this string. Default '' (nothing) * - * @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0 + * @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0 * * @type bool $count_items Include the count of items that can be found in each value (uses $items_filter as well). Default false * * @type string $last_term The last term returned when using a elasticsearch for calculates the facet. - * + * * } - * + * * @return array Array with the total number of values found. The total number of pages with the current number and the results with id and label for each value. Terms also include parent, taxonomy and number of children. */ public function fetch_all_metadatum_values( $metadatum_id, $args = [] ) { - + $defaults = array( 'collection_id' => null, 'search' => '', @@ -963,34 +965,34 @@ class Metadata extends Repository { 'last_term' => '' ); $args = wp_parse_args($args, $defaults); - + global $wpdb; $itemsRepo = Items::get_instance(); $metadataRepo = Metadata::get_instance(); - + $metadatum = $metadataRepo->fetch($metadatum_id); $metadatum_type = $metadatum->get_metadata_type(); $metadatum_options = $metadatum->get_metadata_type_options(); - + if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) { $taxonomy_id = $metadatum_options['taxonomy_id']; $taxonomy_slug = Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id); } - - - + + + $items_query = false; if ( false !== $args['items_filter'] && is_array($args['items_filter']) ) { - + $args['items_filter']['fields'] = 'ids'; unset($args['items_filter']['paged']); unset($args['items_filter']['offset']); unset($args['items_filter']['perpage']); $args['items_filter']['nopaging'] = 1; - - // When filtering the items, we should consider only other metadata, and ignore current metadatum - // This is because the relation between values from the same metadatum when filtering item is OR, + + // When filtering the items, we should consider only other metadata, and ignore current metadatum + // This is because the relation between values from the same metadatum when filtering item is OR, // so when you filter items by one value of a metadatum you dont want to exclude all the other possibilities for that meta. // Only values of all other filters (facets) are reduced. if ( $metadatum_type == 'Tainacan\Metadata_Types\Taxonomy' && isset($args['items_filter']['tax_query']) && is_array($args['items_filter']['tax_query']) ) { @@ -1004,12 +1006,12 @@ class Metadata extends Repository { } } - + $filter = apply_filters('tainacan-fetch-all-metadatum-values', null, $metadatum, $args); if ($filter !== null) { return $filter; } - + ////////////////////////////////////////// // Get the query for current items // this avoids wp_query to run the query. We just want to build the query @@ -1023,12 +1025,12 @@ class Metadata extends Repository { //////////////////////////////////////////// //////////////////////////////////////////// - + $pagination = ''; if ( $args['offset'] >= 0 && $args['number'] >= 1 ) { $pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $args['offset'], (int) $args['number'] ); } - + $search_q = ''; $search = trim($args['search']); if (!empty($search)) { @@ -1039,24 +1041,24 @@ class Metadata extends Repository { } else { $search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%'); } - - + + } if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) { - + if ($items_query) { - + $check_hierarchy_q = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent > 0 LIMIT 1", $taxonomy_slug); $has_hierarchy = ! is_null($wpdb->get_var($check_hierarchy_q)); - + if ( ! $has_hierarchy ) { - $base_query = $wpdb->prepare("FROM $wpdb->term_relationships tr + $base_query = $wpdb->prepare("FROM $wpdb->term_relationships tr INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id - INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id - WHERE + INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id + WHERE tt.parent = %d AND - tr.object_id IN ($items_query) AND + tr.object_id IN ($items_query) AND tt.taxonomy = %s $search_q ORDER BY t.name ASC @@ -1064,46 +1066,46 @@ class Metadata extends Repository { $args['parent_id'], $taxonomy_slug ); - + $query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination"; - + $total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query"; $total = $wpdb->get_var($total_query); - + $results = $wpdb->get_results($query); - + } else { - + $base_query = $wpdb->prepare(" SELECT DISTINCT t.term_id, t.name, tt.parent, coalesce(tr.term_taxonomy_id, 0) as have_items - FROM - $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id + FROM + $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id LEFT JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id IN ($items_query) WHERE tt.taxonomy = %s ORDER BY t.name ASC", $taxonomy_slug ); - + $all_hierarchy = $wpdb->get_results($base_query); - + if (empty($search)) { $results = $this->_process_terms_tree($all_hierarchy, $args['parent_id'], 'parent'); } else { $results = $this->_process_terms_tree($all_hierarchy, $search, 'name'); } - + $total = count($results); - + if ( $args['offset'] >= 0 && $args['number'] >= 1 ) { $results = array_slice($results, (int) $args['offset'], (int) $args['number']); } } } 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 + $base_query = $wpdb->prepare("FROM $wpdb->term_taxonomy tt + INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id WHERE 1=1 $parent_q AND tt.taxonomy = %s @@ -1112,59 +1114,59 @@ class Metadata extends Repository { ", $taxonomy_slug ); - + $query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination"; - + $total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query"; $total = $wpdb->get_var($total_query); - + $results = $wpdb->get_results($query); - + } - - - - + + + + // add selected to the result if ( !empty($args['include']) ) { if ( is_array($args['include']) && !empty($args['include']) ) { - + // protect sql $args['include'] = array_map(function($t) { return (int) $t; }, $args['include']); - + $include_ids = implode(',', $args['include']); - $query_to_include = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent FROM $wpdb->term_taxonomy tt - INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id - WHERE + $query_to_include = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent FROM $wpdb->term_taxonomy tt + INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id + WHERE t.term_id IN ($include_ids)"; - + $to_include = $wpdb->get_results($query_to_include); - + // remove terms that will be included at the begining $results = array_filter($results, function($t) use($args) { return !in_array($t->term_id, $args['include']); }); - + $results = array_merge($to_include, $results); - + } } - - + + $number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total; if( $number < 1){ $pages = 1; } else { $pages = ceil( $total / $number ); } - + $values = []; foreach ($results as $r) { - + $count_query = $wpdb->prepare("SELECT COUNT(term_id) FROM $wpdb->term_taxonomy WHERE parent = %d", $r->term_id); $total_children = $wpdb->get_var($count_query); - + $label = wp_specialchars_decode($r->name); $total_items = null; - + if ( $args['count_items'] ) { $count_items_query = $args['items_filter']; $count_items_query['posts_per_page'] = 1; @@ -1177,11 +1179,11 @@ class Metadata extends Repository { ]; $count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']); $total_items = $count_items_results->found_posts; - + //$label .= " ($total_items)"; - + } - + $values[] = [ 'value' => $r->term_id, 'label' => $label, @@ -1192,22 +1194,22 @@ class Metadata extends Repository { 'total_items' => $total_items, 'type' => 'Taxonomy' ]; - + } - - - + + + } else { - + $items_query_clause = ''; if ($items_query) { $items_query_clause = "AND post_id IN($items_query)"; } $base_query = $wpdb->prepare( "FROM $wpdb->postmeta WHERE meta_key = %s $search_q $items_query_clause ORDER BY meta_value", $metadatum_id ); - + $total_query = "SELECT COUNT(DISTINCT meta_value) $base_query"; $query = "SELECT DISTINCT meta_value $base_query $pagination"; - + $results = $wpdb->get_col($query); $total = $wpdb->get_var($total_query); $number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total; @@ -1216,17 +1218,17 @@ class Metadata extends Repository { } else { $pages = ceil( $total / $number ); } - + // add selected to the result if ( !empty($args['include']) ) { if ( is_array($args['include']) ) { $results = array_unique( array_merge($args['include'], $results) ); } } - + $values = []; foreach ($results as $r) { - + $label = $r; if ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) { @@ -1236,9 +1238,9 @@ class Metadata extends Repository { } $label = $_post->post_title; } - + $total_items = null; - + if ( $args['count_items'] ) { $count_items_query = $args['items_filter']; $count_items_query['posts_per_page'] = 1; @@ -1251,50 +1253,50 @@ class Metadata extends Repository { ]; $count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']); $total_items = $count_items_results->found_posts; - + //$label .= " ($total_items)"; - + } - + $values[] = [ 'label' => $label, 'value' => $r, 'total_items' => $total_items, 'type' => 'Text' ]; - + } } - + return [ 'total' => $total, 'pages' => $pages, 'values' => $values, 'last_term' => $args['last_term'] ]; - + } - + /** * This method processes the result of the query for all terms in a taxonomy done in get_all_metadatum_values() - * It efficiently runs through all the terms and checks what terms with a given $parent have items in itself or any of + * It efficiently runs through all the terms and checks what terms with a given $parent have items in itself or any of * its descendants, keeping the order they originally came. - * - * It returns an array with the term objects with the given $parent that have items considering items in its descendants. The objects are - * in the same format they came, as expected by the rest of the method. + * + * It returns an array with the term objects with the given $parent that have items considering items in its descendants. The objects are + * in the same format they came, as expected by the rest of the method. * * This method is public only for tests purposes, it should not be used anywhere else */ public function _process_terms_tree($tree, $search_value, $search_type='parent') { - - $h_map = []; // all terms will mapped to this array + + $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 + + // 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; // send it to the map array, overriding temporary item if existed @@ -1304,25 +1306,25 @@ class Metadata extends Repository { ($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 + + // 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 parent matches search criteira, add to results @@ -1336,21 +1338,21 @@ class Metadata extends Repository { // 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)) { @@ -1359,7 +1361,7 @@ class Metadata extends Repository { } } - + // 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 @@ -1369,9 +1371,9 @@ class Metadata extends Repository { } return $return; }, []); - + return $results; - + } /** @@ -1421,10 +1423,10 @@ class Metadata extends Repository { } } - + /** * Creates an index with the exploded values of metadata_type_options array. Each option is prefixed with '_option_' - * This is useful to allow metadata to be queried based on a specific value of a metadata type option. + * This is useful to allow metadata to be queried based on a specific value of a metadata type option. * For example, fetch all taxonomy metadata which the taxonomy_id metadata type option is equal to 4 * * $metadata_repository->fetch([ @@ -1435,11 +1437,11 @@ class Metadata extends Repository { * ] * ] * ]) - * + * * @var Entities\Metadatum $metadatum */ private function update_metadata_type_index( Entities\Metadatum $metadatum ) { - + $options = $this->get_mapped_property($metadatum, 'metadata_type_options'); if (!is_array($options)) { return; @@ -1447,9 +1449,9 @@ class Metadata extends Repository { foreach ($options as $option => $value) { update_post_meta($metadatum->get_id(), '_option_' . $option, $value); } - + } - + /** * @inheritDoc */ @@ -1471,4 +1473,39 @@ class Metadata extends Repository { } } } + + /** + * When a private taxonomy is saved, check if there are any public metadata and set them to private + * + * @param \Tainacan\Entities\Taxonomy $taxonomy + * @return void + */ + public function hook_taxonomies_saved_as_private($taxonomy) { + + if ( $taxonomy instanceof Entities\Taxonomy ) { + + $status_obj = get_post_status_object( $taxonomy->get_status() ); + + if ( is_object($status_obj) && ! $status_obj->public ) { + + $stati = get_post_stati(['public' => true]); + + $taxonomy_id = $taxonomy->get_id(); + + $metadata = $this->fetch(['metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', '_option_taxonomy_id' => $taxonomy_id, 'post_status' => $stati], 'OBJECT'); + + foreach ($metadata as $meta) { + $meta->set_status( $taxonomy->get_status() ); + if ( $meta->validate() ) { + $this->insert($meta); + } + + } + + } + + + } + + } } diff --git a/tests/test-category-metadatum-types.php b/tests/test-category-metadatum-types.php index c3fc81c32..526686c9c 100644 --- a/tests/test-category-metadatum-types.php +++ b/tests/test-category-metadatum-types.php @@ -15,13 +15,13 @@ use Tainacan\Entities; */ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { - + function test_taxonomy_metadata_types() { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -29,7 +29,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -39,7 +39,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -55,7 +55,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $i = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -66,7 +66,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum2 = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -78,11 +78,11 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - - - - - + + + + + $term = $this->tainacan_entity_factory->create_entity( 'term', array( @@ -122,23 +122,23 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $metadatum); $this->assertEquals('Tainacan\Entities\Term', get_class($check_item_metadata->get_value())); - + // test 2 metadata with same taxonomy $metadatum2->set_metadata_type_options([ 'taxonomy_id' => $tax->get_id(), ]); $metadatum2->set_status('publish'); - + $this->assertFalse($metadatum2->validate(), 'Taxonomy Metadatum should not validate when using a taxonomy in use by another metadatum in the same collection'); $errors = $metadatum2->get_errors(); $this->assertInternalType('array', $errors); $this->assertArrayHasKey('taxonomy_id', $errors[0]['metadata_type_options']); } - + function test_relate_taxonomy() { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -147,7 +147,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -156,7 +156,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax2 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -165,7 +165,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax3 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -174,9 +174,9 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $this->assertNotContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -192,34 +192,34 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $this->assertContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); - - + + $checkTax = $Tainacan_Taxonomies->fetch($tax->get_id()); $this->assertContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is created'); - - + + $metadatum->set_metadata_type_options([ 'taxonomy_id' => $tax2->get_id(), 'allow_new_terms' => 'no' ]); - + $metadatum->validate(); $metadatum = $Tainacan_Metadata->insert($metadatum); - + $checkTax = $Tainacan_Taxonomies->fetch($tax->get_id()); $checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id()); $this->assertContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is updated'); $this->assertNotContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is updated'); - + $metadatum = $Tainacan_Metadata->trash($metadatum); - + $checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id()); - + $this->assertNotContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is deleted'); - - + + $metadatum_repo = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -235,18 +235,18 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $this->assertContains($tax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Taxonommy used by repository level metadatum must be assigned to all collections post types'); - - - - + + + + } - + function test_relate_taxonomy_match() { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -255,7 +255,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $collection2 = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -264,7 +264,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -273,7 +273,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax2 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -282,7 +282,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax3 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -291,7 +291,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -307,7 +307,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum2 = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -323,7 +323,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum3 = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -339,7 +339,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum4 = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -355,34 +355,34 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $checkTax = $Tainacan_Taxonomies->fetch($tax->get_id()); $checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id()); $checkTax3 = $Tainacan_Taxonomies->fetch($tax3->get_id()); - + $this->assertEquals( sizeof($checkTax->get_collections_ids()), sizeof($checkTax->get_collections()) ); $this->assertEquals( sizeof($checkTax2->get_collections_ids()), sizeof($checkTax2->get_collections()) ); $this->assertEquals( sizeof($checkTax3->get_collections_ids()), sizeof($checkTax3->get_collections()) ); - + $this->assertNotContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); $this->assertContains($checkTax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); $this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); - + $this->assertNotContains($checkTax->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); $this->assertContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); $this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created'); - + $this->assertEquals(2, sizeof( get_object_taxonomies($collection2->get_db_identifier()) )); $this->assertEquals(2, sizeof( get_object_taxonomies($collection->get_db_identifier()) )); } - + /** * @group fetch_by_collection */ function test_fetch_by_collection() { - + $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -391,7 +391,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $collection2 = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -400,7 +400,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $collection2_c = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -410,7 +410,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $collection2_gc = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -420,7 +420,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -429,7 +429,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax2 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -438,7 +438,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax3 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -447,7 +447,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax4 = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -456,8 +456,8 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - - + + // metadata 1 in repo level for every one $metadatum_repo = $this->tainacan_entity_factory->create_entity( 'metadatum', @@ -474,7 +474,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + // meta 2 in collection 1 just for it $metadatum2 = $this->tainacan_entity_factory->create_entity( 'metadatum', @@ -491,7 +491,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - // meta 3 in collection 2 for it and chidlren and grand children + // meta 3 in collection 2 for it and chidlren and grand children $metadatum3 = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -507,7 +507,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + // meta 4 in collection 2c only for children and grand children $metadatum4 = $this->tainacan_entity_factory->create_entity( 'metadatum', @@ -524,27 +524,27 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $taxonomies_1 = $Tainacan_Taxonomies->fetch_by_collection($collection); $this->assertEquals(2, sizeof($taxonomies_1)); - + $taxonomies_2 = $Tainacan_Taxonomies->fetch_by_collection($collection2); $this->assertEquals(2, sizeof($taxonomies_2)); - + $taxonomies_3 = $Tainacan_Taxonomies->fetch_by_collection($collection2_c); $this->assertEquals(3, sizeof($taxonomies_3)); - + $taxonomies_4 = $Tainacan_Taxonomies->fetch_by_collection($collection2_gc); $this->assertEquals(3, sizeof($taxonomies_4)); - - + + } - + function test_values_and_html() { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -552,7 +552,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -561,7 +561,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $item = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -571,7 +571,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -587,32 +587,32 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - - - + + + $meta = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum); - + $meta->set_value('new_term'); - + $meta->validate(); - + $meta = $Tainacan_ItemMetadata->insert($meta); - + $this->assertInternalType( 'string', $meta->get_value_as_html() ); $this->assertInternalType( 'string', $meta->get_value_as_string() ); - + $this->assertInternalType( 'integer', strpos($meta->get_value_as_html(), 'assertFalse( strpos($meta->get_value_as_string(), 'tainacan_entity_factory->create_entity( 'collection', array( @@ -620,7 +620,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -629,9 +629,9 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $meta = new \Tainacan\Entities\Metadatum(); - + $meta->set_name('test meta'); $meta->set_status('publish'); $meta->set_metadata_type('Tainacan\Metadata_Types\Taxonomy'); @@ -640,23 +640,71 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { 'taxonomy_id' => $tax->get_id(), 'allow_new_terms' => 'yes' ]); - + $this->assertFalse($meta->validate(), 'Metadatum should not validate because taxonomy is private'); - + $meta->set_status('private'); - + $this->assertTrue($meta->validate(), 'Metadatum should validate because it is private now'); - - - - - + + } - + + function test_set_metadata_to_private_after_taxonomy_is_set_to_private() { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); + $Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'test', + 'status' => 'publish' + ), + true + ); + + $tax = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'tax_test', + 'status' => 'publish' + ), + true + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'meta', + 'description' => 'description', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'status' => 'publish', + 'metadata_type_options' => [ + 'taxonomy_id' => $tax->get_id(), + 'allow_new_terms' => 'yes' + ] + ), + true + ); + + $tax->set_status('private'); + $tax->validate(); + $Tainacan_Taxonomies->insert($tax); + + $checkMeta = $Tainacan_Metadata->fetch( $metadatum->get_id() ); + + $this->assertEquals('private', $checkMeta->get_status()); + + + + } + function test_get_taxonomy_method() { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -664,7 +712,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -674,7 +722,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -690,14 +738,14 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $object = $metadatum->get_metadata_type_object(); - + $taxCheck = $object->get_taxonomy(); - + $this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy ); $this->assertEquals($tax->get_id(), $taxCheck->get_id()); } - -} \ No newline at end of file + +}