From f8b64edd6fefce35190c35697c31ba6efd699002 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Fri, 1 Dec 2017 14:06:35 -0200 Subject: [PATCH] insert validation methods in repositories mapping --- .../entities/class-tainacan-taxonomy.php | 2 +- .../repositories/class-tainacan-filters.php | 33 +++++++++++++++++-- .../repositories/class-tainacan-items.php | 16 ++++++++- .../repositories/class-tainacan-metadatas.php | 8 +++-- .../class-tainacan-taxonomies.php | 30 +++++++++++++++-- .../repositories/class-tainacan-terms.php | 28 ++++++++++++++-- tests/test-taxonomies.php | 5 +-- 7 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src/classes/entities/class-tainacan-taxonomy.php b/src/classes/entities/class-tainacan-taxonomy.php index 18ba971d3..f6ce5e381 100644 --- a/src/classes/entities/class-tainacan-taxonomy.php +++ b/src/classes/entities/class-tainacan-taxonomy.php @@ -91,7 +91,7 @@ class Taxonomy extends Entity { } function get_allow_insert() { - return ( boolean ) $this->get_mapped_property('allow_insert'); + return $this->get_mapped_property('allow_insert'); } function get_slug() { diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 89cc4b868..1f48a7fe4 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -5,6 +5,7 @@ use Tainacan\Entities; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); +use \Respect\Validation\Validator as v; class Filters extends Repository { protected $entities_type = '\Tainacan\Entities\Filter'; @@ -12,38 +13,66 @@ class Filters extends Repository { return apply_filters('tainacan-get-map', [ 'id' => [ 'map' => 'ID', + 'title' => __('ID', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Unique identifier', 'tainacan'), //'validation' => '' ], 'name' => [ 'map' => 'post_title', - 'validation' => '' + 'title' => __('Name', 'tainacan'), + 'type' => 'string', + 'description'=> __('Name of the filter', 'tainacan'), + 'on_error' => __('The filter name should be a text value and not empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty(), ], 'order' => [ 'map' => 'menu_order', + 'title' => __('Order', 'tainacan'), + 'type' => 'string', + 'description'=> __('Filter order. Field used if filters are manually ordered', 'tainacan'), 'validation' => '' ], 'description' => [ 'map' => 'post_content', - 'validation' => '' + 'title' => __('Description', 'tainacan'), + 'type' => 'string', + 'description'=> __('The filter description', 'tainacan'), + 'validation' => '' ], 'filter_type_object' => [ 'map' => 'meta', + 'title' => __('Type', 'tainacan'), + 'type' => 'string', + 'description'=> __('The filter type object', 'tainacan'), 'validation' => '' ], 'filter_type' => [ 'map' => 'meta', + 'title' => __('Type', 'tainacan'), + 'type' => 'string', + 'description'=> __('The filter type', 'tainacan'), 'validation' => '' ], 'collection_id' => [ 'map' => 'meta', + 'title' => __('Collection', 'tainacan'), + 'type' => 'integer', + 'description'=> __('The collection ID', 'tainacan'), 'validation' => '' ], 'color' => [ 'map' => 'meta', + 'title' => __('Color', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Filter color', 'tainacan'), 'validation' => '' ], 'metadata' => [ 'map' => 'meta', + 'title' => __('Metadata', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Filter metadata', 'tainacan'), 'validation' => '' ], ]); diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index a8a5017e0..45bab2f32 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -2,6 +2,7 @@ namespace Tainacan\Repositories; use Tainacan\Entities; +use \Respect\Validation\Validator as v; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); @@ -11,18 +12,31 @@ class Items extends Repository { return apply_filters('tainacan-get-map', [ 'id' => [ 'map' => 'ID', + 'title' => __('ID', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Unique identifier', 'tainacan'), //'validation' => '' ], 'title' => [ 'map' => 'post_title', - 'validation' => '' + 'title' => __('Title', 'tainacan'), + 'type' => 'string', + 'description'=> __('Title of the item', 'tainacan'), + 'on_error' => __('The title should be a text value and not empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty() ], 'description' => [ 'map' => 'post_content', + 'title' => __('Description', 'tainacan'), + 'type' => 'string', + 'description'=> __('The item description', 'tainacan'), 'validation' => '' ], 'collection_id' => [ 'map' => 'meta', + 'title' => __('Collection', 'tainacan'), + 'type' => 'integer', + 'description'=> __('The collection ID', 'tainacan'), 'validation' => '' ], //'collection' => 'relation...', diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index cc4043474..2c837adc5 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -124,13 +124,17 @@ class Metadatas extends Repository { ], 'field_type_object' => [ // not showed in form 'map' => 'meta', + 'title' => __('Type', 'tainacan'), 'type' => 'string', - 'validation' => '' + 'description'=> __('The object type', 'tainacan'), + // 'validation' => '' ], 'collection_id' => [ // not showed in form 'map' => 'meta', + 'title' => __('Collection', 'tainacan'), 'type' => 'integer', - 'validation' => '' + 'description'=> __('The collection ID', 'tainacan'), + //'validation' => '' ], ]); } diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index d744ef131..86c9e1e7e 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -5,6 +5,8 @@ use Tainacan\Entities; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); + +use \Respect\Validation\Validator as v; /** * Class Tainacan_Taxonomies */ @@ -15,30 +17,54 @@ class Taxonomies extends Repository { return apply_filters('tainacan-get-map', [ 'id' => [ 'map' => 'ID', + 'title' => __('ID', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Unique identifier', 'tainacan'), //'validation' => '' ], 'name' => [ 'map' => 'post_title', - 'validation' => '' + 'title' => __('Name', 'tainacan'), + 'type' => 'string', + 'description'=> __('Name of the taxonomy', 'tainacan'), + 'on_error' => __('The taxonomy should be a text value and not empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty(), ], 'parent' => [ 'map' => 'parent', + 'title' => __('Parent', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Parent taxonomy', 'tainacan'), 'validation' => '' ], 'description' => [ 'map' => 'post_content', + 'title' => __('Description', 'tainacan'), + 'type' => 'string', + 'description'=> __('The taxonomy description', 'tainacan'), 'validation' => '' ], 'slug' => [ 'map' => 'post_name', + 'title' => __('Slug', 'tainacan'), + 'type' => 'string', + 'description'=> __('The taxonomy slug', 'tainacan'), 'validation' => '' ], 'allow_insert' => [ 'map' => 'meta', - 'validation' => '' + 'title' => __('Allow insert', 'tainacan'), + 'type' => 'string', + 'description'=> __('Allow/Deny the creation of new terms in the taxonomy', 'tainacan'), + 'on_error' => __('Allow insert is invalid, allowed values ( yes/no )', 'tainacan'), + 'validation' => v::stringType()->in(['yes', 'no']), // yes or no + 'default' => 'yes' ], 'collections_ids' => [ 'map' => 'meta_multi', + 'title' => __('Collections', 'tainacan'), + 'type' => 'string', + 'description'=> __('The IDs of collection where the taxonomy is used', 'tainacan'), 'validation' => '' ], ]); diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index a334ade9a..a0d90f373 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -5,6 +5,7 @@ use Tainacan\Entities; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); +use \Respect\Validation\Validator as v; /** * Class Tainacan_Terms */ @@ -15,27 +16,48 @@ class Terms extends Repository { return apply_filters('tainacan-get-map', [ 'term_id' => [ 'map' => 'term_id', + 'title' => __('ID', 'tainacan'), + 'type' => 'integer', + 'description'=> __('Unique identifier', 'tainacan'), //'validation' => '' ], 'name' => [ 'map' => 'name', - 'validation' => '' + 'title' => __('Name', 'tainacan'), + 'type' => 'string', + 'description'=> __('Name of the term', 'tainacan'), + 'on_error' => __('The name is empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty(), ], 'parent' => [ 'map' => 'parent', + 'title' => __('Parent', 'tainacan'), + 'type' => 'integer', + 'description'=> __('The parent of the term', 'tainacan'), 'validation' => '' ], 'description' => [ 'map' => 'description', + 'title' => __('Description', 'tainacan'), + 'type' => 'string', + 'description'=> __('The term description', 'tainacan'), 'validation' => '' ], 'taxonomy' => [ 'map' => 'taxonomy', - 'validation' => '' + 'title' => __('Taxonomy', 'tainacan'), + 'type' => 'string', + 'description'=> __('The term taxonomy', 'tainacan'), + 'on_error' => __('The taxonomy is empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty(), ], 'user' => [ 'map' => 'termmeta', - 'validation' => '' + 'title' => __('User', 'tainacan'), + 'type' => 'integer', + 'description'=> __('The term creator', 'tainacan'), + 'on_error' => __('The user is empty or invalid', 'tainacan'), + 'validation' => v::numeric()->positive(), ], ]); } diff --git a/tests/test-taxonomies.php b/tests/test-taxonomies.php index f4ed83835..3926238f3 100644 --- a/tests/test-taxonomies.php +++ b/tests/test-taxonomies.php @@ -25,10 +25,11 @@ class Taxonomies extends \WP_UnitTestCase { //setando os valores na classe do tainacan $taxonomy->set_name('genero'); $taxonomy->set_description('tipos de musica'); - $taxonomy->set_allow_insert(true); + $taxonomy->set_allow_insert('yes'); //inserindo $taxonomy->validate(); + $taxonomy = $Tainacan_Taxonomies->insert($taxonomy); //retorna a taxonomia @@ -36,7 +37,7 @@ class Taxonomies extends \WP_UnitTestCase { $this->assertEquals( $test->get_name(), 'genero' ); $this->assertEquals( $test->get_description(), 'tipos de musica' ); - $this->assertEquals( $test->get_allow_insert(), true ); + $this->assertEquals( $test->get_allow_insert(), 'yes' ); $this->assertEquals( taxonomy_exists( $test->get_db_identifier() ) , true ); }