From 58dc05317f20fcc59b28a0f8f51711e12636ae02 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 3 Jun 2019 11:41:44 -0300 Subject: [PATCH] refactor filter metadatum_id getters and setters --- ...class-tainacan-rest-filters-controller.php | 10 +- ...lass-tainacan-rest-metadata-controller.php | 7 +- .../entities/class-tainacan-filter.php | 50 ++++++++-- .../checkbox/class-tainacan-checkbox.php | 3 +- .../numeric/class-tainacan-numeric.php | 2 +- .../selectbox/class-tainacan-selectbox.php | 3 +- .../taginput/class-tainacan-taginput.php | 3 +- .../class-tainacan-taxonomycheckbox.php | 3 +- .../class-tainacan-taxonomytaginput.php | 3 +- .../repositories/class-tainacan-filters.php | 4 +- src/migrations.php | 10 ++ tests/test-api-filters.php | 8 +- tests/test-filters.php | 99 ++++++++++++++++--- 13 files changed, 156 insertions(+), 49 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index 702879823..ff46d15ea 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -136,25 +136,25 @@ class REST_Filters_Controller extends REST_Controller { $metadatum_id = $request['metadatum_id']; $filter_obj->set_collection_id( $collection_id ); - $filter_obj->set_metadatum( $metadatum_id ); + $filter_obj->set_metadatum_id( $metadatum_id ); } elseif (isset($request['collection_id'])){ $collection_id = $request['collection_id']; $filter_obj->set_collection_id( $collection_id ); - if(!isset($body['metadatum'])){ + if(!isset($body['metadatum_id'])){ throw new \InvalidArgumentException('You need provide a metadatum id'); } - $filter_obj->set_metadatum($body['metadatum']); + $filter_obj->set_metadatum_id($body['metadatum_id']); } else { $filter_obj->set_collection_id( 'filter_in_repository' ); - if(!isset($body['metadatum'])){ + if(!isset($body['metadatum_id'])){ throw new \InvalidArgumentException('You need provide a metadatum id'); } - $filter_obj->set_metadatum($body['metadatum']); + $filter_obj->set_metadatum_id($body['metadatum_id']); } $filter_obj->set_filter_type($filter_type); diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php index e26bfdfe8..45a3569e7 100644 --- a/src/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -288,9 +288,10 @@ class REST_Metadata_Controller extends REST_Controller { $item_arr['metadata_type_object'] = $item->get_metadata_type_object()->_toArray(); if(isset($item_arr['metadata_type_options']) && isset($item_arr['metadata_type_options']['taxonomy_id'])){ - $taxonomy = new Entities\Taxonomy($item_arr['metadata_type_options']['taxonomy_id']); - - $item_arr['metadata_type_options']['taxonomy'] = $taxonomy->get_db_identifier(); + $taxonomy = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id( $item_arr['metadata_type_options']['taxonomy_id'] ); + //$taxonomy = new Entities\Taxonomy($item_arr['metadata_type_options']['taxonomy_id']); + //$item_arr['metadata_type_options']['taxonomy'] = $taxonomy->get_db_identifier(); + $item_arr['metadata_type_options']['taxonomy'] = $taxonomy; } if($request['context'] === 'edit'){ diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index fd6a8f0c0..75015a5dd 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -16,6 +16,7 @@ class Filter extends Entity { $order, $color, $metadatum, + $metadatum_id, $max_options, $filter_type, $filter_type_options; @@ -40,7 +41,7 @@ class Filter extends Entity { */ public function _toArray(){ $filter_array = parent::_toArray(); - $metadatum_id = $filter_array['metadatum']; + $metadatum_id = $filter_array['metadatum_id']; $metadatum = $this->get_metadatum(); $filter_array['metadatum'] = []; @@ -106,14 +107,33 @@ class Filter extends Entity { } /** - * Return the metadatum + * Return the metadatum ID * - * @return Metadatum + * @return integer Metadatum ID + */ + function get_metadatum_id() { + return $this->get_mapped_property('metadatum_id'); + } + + /** + * Return the metadatum object + * + * @return Metadatum | null * @throws \Exception */ function get_metadatum() { - $id = $this->get_mapped_property('metadatum'); - return new Metadatum( $id ); + if (isset($this->metadatum)) { + return $this->metadatum; + } + $id = $this->get_metadatum_id(); + return new Metadatum($id); + $metadatum = \Tainacan\Repositories\Metadata::get_instance()->fetch((int) $id); + if ($metadatum instanceof Metadatum) { + $this->metadatum = $metadatum; + return $metadatum; + } else { + return null; + } } /** @@ -192,15 +212,27 @@ class Filter extends Entity { } /** - * Define the filter metadatum + * Define the filter metadatum passing an object * * @param \Tainacan\Entities\Metadatum * @return void */ - function set_metadatum( $value ){ - $id = ( $value instanceof Metadatum ) ? $value->get_id() : $value; + function set_metadatum( \Tainacan\Entities\Metadatum $value ){ + $id = $value->get_id(); - $this->set_mapped_property('metadatum', $id); + $this->set_metadatum_id($id); + $this->metadatum = $value; + } + + /** + * Define the filter metadatum passing an ID + * + * @param int $value the metadatum ID + * @return void + */ + function set_metadatum_id( $value ){ + unset($this->metadatum); + $this->set_mapped_property('metadatum_id', $value); } /** diff --git a/src/classes/filter-types/checkbox/class-tainacan-checkbox.php b/src/classes/filter-types/checkbox/class-tainacan-checkbox.php index 1e2befc3d..1354eff94 100644 --- a/src/classes/filter-types/checkbox/class-tainacan-checkbox.php +++ b/src/classes/filter-types/checkbox/class-tainacan-checkbox.php @@ -64,8 +64,7 @@ class Checkbox extends Filter_Type { public function render( $filter ){ return ''; + metadatum_id="'.$filter->get_metadatum_id().'">'; } } \ No newline at end of file diff --git a/src/classes/filter-types/numeric/class-tainacan-numeric.php b/src/classes/filter-types/numeric/class-tainacan-numeric.php index 9d266b3e4..abd2ae8a8 100644 --- a/src/classes/filter-types/numeric/class-tainacan-numeric.php +++ b/src/classes/filter-types/numeric/class-tainacan-numeric.php @@ -89,7 +89,7 @@ class Numeric extends Filter_Type { step="' . $this->get_option('step') . '" name="'.$filter->get_name().'" collection_id="'.$filter->get_collection_id().'" - metadatum_id="'.$filter->get_metadatum()->get_id().'">'; + metadatum_id="'.$filter->get_metadatum_id().'">'; } diff --git a/src/classes/filter-types/selectbox/class-tainacan-selectbox.php b/src/classes/filter-types/selectbox/class-tainacan-selectbox.php index e83fdcad0..a9cc6bbc3 100644 --- a/src/classes/filter-types/selectbox/class-tainacan-selectbox.php +++ b/src/classes/filter-types/selectbox/class-tainacan-selectbox.php @@ -32,8 +32,7 @@ class Selectbox extends Filter_Type { public function render( $filter ){ return ''; + metadatum_id="'.$filter->get_metadatum_id().'">'; } } \ No newline at end of file diff --git a/src/classes/filter-types/taginput/class-tainacan-taginput.php b/src/classes/filter-types/taginput/class-tainacan-taginput.php index e9ba673d2..518c5dd03 100644 --- a/src/classes/filter-types/taginput/class-tainacan-taginput.php +++ b/src/classes/filter-types/taginput/class-tainacan-taginput.php @@ -62,8 +62,7 @@ class Taginput extends Filter_Type { public function render( $filter ){ return ''; + metadatum_id="'.$filter->get_metadatum_id().'">'; } } \ No newline at end of file diff --git a/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php b/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php index 40691f32a..9070a8c42 100644 --- a/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php +++ b/src/classes/filter-types/taxonomy/class-tainacan-taxonomycheckbox.php @@ -64,8 +64,7 @@ class TaxonomyCheckbox extends Filter_Type { public function render( $filter ){ return ''; + metadatum_id="'.$filter->get_metadatum_id().'">'; } } \ No newline at end of file diff --git a/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php b/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php index e63ea0097..158751856 100644 --- a/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php +++ b/src/classes/filter-types/taxonomy/class-tainacan-taxonomytaginput.php @@ -62,8 +62,7 @@ class TaxonomyTaginput extends Filter_Type { public function render( $filter ){ return ''; + metadatum_id="'.$filter->get_metadatum_id().'">'; } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index ae803ebce..53a9c3268 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -80,10 +80,10 @@ class Filters extends Repository { 'description' => __( 'Filter color', 'tainacan' ), 'validation' => '' ], - 'metadatum' => [ + 'metadatum_id' => [ 'map' => 'meta', 'title' => __( 'Metadata', 'tainacan' ), - 'type' => 'array/object/string', + 'type' => 'integer', 'description' => __( 'Filter metadata', 'tainacan' ), 'validation' => '' ], diff --git a/src/migrations.php b/src/migrations.php index b00a0bef9..3aab96a54 100644 --- a/src/migrations.php +++ b/src/migrations.php @@ -338,6 +338,16 @@ class Migrations { flush_rewrite_rules(false); } + static function update_filters_definition() { + global $wpdb; + + $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = 'metadatum_id' WHERE + meta_key = 'metadatum' AND post_id IN ( + SELECT ID FROM $wpdb->posts WHERE post_type = 'tainacan-filter' + )"); + + } + } diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 87f0bcd1e..4d494dc18 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -70,8 +70,6 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { ) ); - $filter_type = $this->tainacan_filter_factory->create_filter('custom_interval'); - $filter = $this->tainacan_entity_factory->create_entity( 'filter', array( @@ -79,7 +77,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { 'collection' => $collection, 'description' => 'descricao', 'metadatum' => $metadatum, - 'filter_type' => $filter_type, + 'filter_type' => 'Tainacan\Filter_Types\Custom_Interval', ), true ); @@ -296,7 +294,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { 'description' => 'Description of 2x Filter', 'status' => 'publish' ], - 'metadatum' => $metadatum2->get_id() + 'metadatum_id' => $metadatum2->get_id() ]); $filter_attr2 = json_encode([ @@ -306,7 +304,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { 'description' => 'Description of 4x Filter', 'status' => 'publish' ], - 'metadatum' => $metadatum->get_id() + 'metadatum_id' => $metadatum->get_id() ]); #### CREATE A FILTER IN REPOSITORY #### diff --git a/tests/test-filters.php b/tests/test-filters.php index e638ec929..395c4ecb0 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -65,7 +65,6 @@ class Filters extends TAINACAN_UnitTestCase { true ); - $filter_list_type = $this->tainacan_filter_factory->create_filter('selectbox'); $filter = $this->tainacan_entity_factory->create_entity( 'filter', @@ -74,7 +73,7 @@ class Filters extends TAINACAN_UnitTestCase { 'collection' => $collection, 'description' => 'descricao', 'metadatum' => $metadatum, - 'filter_type' => $filter_list_type + 'filter_type' => 'Tainacan\Filter_Types\Selectbox' ), true ); @@ -89,9 +88,8 @@ class Filters extends TAINACAN_UnitTestCase { $this->assertEquals( 'filtro', $test->get_name() ); $this->assertEquals( $collection->get_id(), $test->get_collection_id() ); $this->assertEquals( $metadatum->get_id(), $test->get_metadatum()->get_id() ); - $objClass = get_class( $filter_list_type ); $storedObjClass = get_class( $test->get_filter_type_object() ); - $this->assertEquals($objClass , $storedObjClass ); + $this->assertEquals('Tainacan\Filter_Types\Selectbox' , $storedObjClass ); } @@ -131,8 +129,6 @@ class Filters extends TAINACAN_UnitTestCase { true ); - $autocomplete = $this->tainacan_filter_factory->create_filter('autocomplete'); - $filter = $this->tainacan_entity_factory->create_entity( 'filter', array( @@ -140,7 +136,7 @@ class Filters extends TAINACAN_UnitTestCase { 'collection' => $collection, 'description' => 'descricao', 'metadatum' => $metadatum2, - 'filter_type' => $autocomplete + 'filter_type' => 'Tainacan\Filter_Types\Autocomplete' ), true ); @@ -149,14 +145,12 @@ class Filters extends TAINACAN_UnitTestCase { $this->assertEquals( 'Tainacan\Filter_Types\Autocomplete', $test->get_filter_type()); - $custom_interval = $this->tainacan_filter_factory->create_filter('custom_interval'); - $filter2 = new \Tainacan\Entities\Filter(); $filter2->set_name('filter 2'); $filter2->set_collection($collection); $filter2->set_description('description'); $filter2->set_metadatum($metadatum2); - $filter2->set_filter_type($custom_interval); + $filter2->set_filter_type('Tainacan\Filter_Types\Custom_Interval'); $this->assertFalse($filter2->validate(), 'filter with a metadatum with unsupported primitive type should not validate'); @@ -186,7 +180,7 @@ class Filters extends TAINACAN_UnitTestCase { 'name' => 'filter default', 'collection_id' => 'filter_in_repository', 'filter_type' => 'Tainacan\Filter_Types\Selectbox', - 'metadatum' => $meta_repo->get_id(), + 'metadatum_id' => $meta_repo->get_id(), 'status' => 'publish' ), true @@ -216,7 +210,7 @@ class Filters extends TAINACAN_UnitTestCase { 'name' => 'filter grandfather', 'collection_id' => $collection_grandfather->get_id(), 'filter_type' => 'Tainacan\Filter_Types\Selectbox', - 'metadatum' => $meta_grand->get_id(), + 'metadatum_id' => $meta_grand->get_id(), 'status' => 'publish' ), true @@ -247,7 +241,7 @@ class Filters extends TAINACAN_UnitTestCase { 'name' => 'filter father', 'collection_id' => $collection_father->get_id(), 'filter_type' => 'Tainacan\Filter_Types\Selectbox', - 'metadatum' => $meta_father->get_id(), + 'metadatum_id' => $meta_father->get_id(), 'status' => 'publish' ), true @@ -281,7 +275,7 @@ class Filters extends TAINACAN_UnitTestCase { 'name' => 'filter son', 'collection_id' => $collection_son->get_id(), 'filter_type' => 'Tainacan\Filter_Types\Selectbox', - 'metadatum' => $meta_son->get_id(), + 'metadatum_id' => $meta_son->get_id(), 'status' => 'publish' ), true @@ -295,5 +289,82 @@ class Filters extends TAINACAN_UnitTestCase { $this->assertEquals( 4, sizeof( $retrieve_filters ) ); $this->assertEquals( 4, sizeof( $retrieve_filters_ids ) ); } + + function test_metadatum_getter_setter(){ + $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Collection filtered', + 'description' => 'Is filtered', + ), + true + ); + + $metadatum2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'collection_id' => $collection->get_id() + ), + true + ); + + $filter2 = new \Tainacan\Entities\Filter(); + $filter2->set_name('filter 2'); + $filter2->set_collection($collection); + $filter2->set_description('description'); + $filter2->set_metadatum($metadatum2); + $filter2->set_filter_type('Tainacan\Filter_Types\Selectbox'); + + $filter2->validate(); + + $filter = $Tainacan_Filters->insert($filter2); + + $this->assertEquals($metadatum2->get_id(), $filter->get_metadatum_id()); + $this->assertEquals($metadatum2->get_name(), $filter->get_metadatum()->get_name()); + + + $filter3 = new \Tainacan\Entities\Filter(); + $filter3->set_name('filter 3'); + $filter3->set_collection($collection); + $filter3->set_description('description'); + $filter3->set_metadatum_id($metadatum2->get_id()); + $filter3->set_filter_type('Tainacan\Filter_Types\Selectbox'); + + $filter3->validate(); + + $filter = $Tainacan_Filters->insert($filter3); + + $this->assertEquals($metadatum2->get_id(), $filter->get_metadatum_id()); + $this->assertEquals($metadatum2->get_name(), $filter->get_metadatum()->get_name()); + + $metadatum3 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'collection_id' => $collection->get_id() + ), + true + ); + + $filter4 = new \Tainacan\Entities\Filter(); + $filter4->set_name('filter 4'); + + $filter4->set_metadatum_id($metadatum3->get_id()); + $this->assertEquals($metadatum3->get_id(), $filter4->get_metadatum_id()); + $this->assertEquals($metadatum3->get_name(), $filter4->get_metadatum()->get_name()); + + $filter4->set_metadatum($metadatum2); + $this->assertEquals($metadatum2->get_id(), $filter4->get_metadatum_id()); + $this->assertEquals($metadatum2->get_name(), $filter4->get_metadatum()->get_name()); + + + } } \ No newline at end of file