diff --git a/src/classes/entities/class-tainacan-field.php b/src/classes/entities/class-tainacan-field.php index 194bda3a3..3c81a0532 100644 --- a/src/classes/entities/class-tainacan-field.php +++ b/src/classes/entities/class-tainacan-field.php @@ -32,6 +32,15 @@ class Field extends Entity { function get_name() { return $this->get_mapped_property('name'); } + + /** + * Get field slug + * + * @return string + */ + function get_slug() { + return $this->get_mapped_property('slug'); + } /** * Return the field order type @@ -164,6 +173,22 @@ class Field extends Entity { $this->set_mapped_property('name', $value); } + /** + * Set the field slug + * + * If you dont set the field slug, it will be set automatically based on the name and + * following WordPress default behavior of creating slugs for posts. + * + * If you set the slug for an existing one, WordPress will append a number at the end of in order + * to make it unique (e.g slug-1, slug-2) + * + * @param [string] $value + * @return void + */ + function set_slug($value) { + $this->set_mapped_property('slug', $value); + } + /** * Set manually the order of the field * diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index 72f35774b..aa3a76490 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -39,6 +39,13 @@ class Fields extends Repository { 'on_error' => __('The name should be a text value and not empty', 'tainacan'), 'validation' => v::stringType()->notEmpty(), ], + 'slug' => [ + 'map' => 'post_name', + 'title' => __('Slug', 'tainacan'), + 'type' => 'string', + 'description'=> __('A unique and santized string representation of the field', 'tainacan'), + //'validation' => v::stringType(), + ], 'order' => [ 'map' => 'menu_order', 'title' => __('Order', 'tainacan'), diff --git a/tests/test-fields.php b/tests/test-fields.php index 788b691d7..3e65db3e2 100644 --- a/tests/test-fields.php +++ b/tests/test-fields.php @@ -27,15 +27,13 @@ class Fields extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'metadado', 'description' => 'descricao', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -61,15 +59,13 @@ class Fields extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'metadado', 'description' => 'descricao', 'collection_id' => $collection->get_id(), - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -79,7 +75,7 @@ class Fields extends TAINACAN_UnitTestCase { $this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_collection_id(), $collection->get_id()); $this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type()); - $this->assertEquals($test->get_field_type_object(), $type); + $this->assertEquals($test->get_field_type(), 'Tainacan\Field_Types\Text'); } /** @@ -88,14 +84,12 @@ class Fields extends TAINACAN_UnitTestCase { function test_hierarchy_metadata(){ global $Tainacan_Fields; - $type = $this->tainacan_field_factory->create_field('text'); - $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'field default', 'collection_id' => $Tainacan_Fields->get_default_metadata_attribute(), - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -114,7 +108,7 @@ class Fields extends TAINACAN_UnitTestCase { array( 'name' => 'field grandfather', 'collection_id' => $collection_grandfather->get_id(), - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -134,7 +128,7 @@ class Fields extends TAINACAN_UnitTestCase { array( 'name' => 'field father', 'collection_id' => $collection_father->get_id(), - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -157,7 +151,7 @@ class Fields extends TAINACAN_UnitTestCase { array( 'name' => 'field son', 'collection_id' => $collection_son->get_id(), - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -223,15 +217,13 @@ class Fields extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field1 = $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'field1', 'description' => 'descricao', 'collection' => $collection, - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -243,7 +235,7 @@ class Fields extends TAINACAN_UnitTestCase { 'name' => 'field2', 'description' => 'field2', 'collection' => $collection, - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -256,7 +248,7 @@ class Fields extends TAINACAN_UnitTestCase { 'name' => 'field3', 'description' => 'field3', 'collection' => $collection, - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'status' => 'publish' ), true @@ -277,6 +269,73 @@ class Fields extends TAINACAN_UnitTestCase { $fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' ); $this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() ); } + + /** + * @group blabla + */ + function test_unique_slugs() { + $x = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'teste', + 'description' => 'adasdasdsa', + 'slug' => 'duplicated_slug', + 'status' => 'publish', + 'field_type' => 'Tainacan\Field_Types\Text', + ), + true + ); + + $y = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'teste', + 'description' => 'adasdasdsa', + 'slug' => 'duplicated_slug', + 'status' => 'publish', + 'field_type' => 'Tainacan\Field_Types\Text', + ), + true + ); + + $this->assertNotEquals($x->get_slug(), $y->get_slug()); + + // Create as draft and publish later + $x = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'teste', + 'description' => 'adasdasdsa', + 'slug' => 'duplicated_slug', + 'field_type' => 'Tainacan\Field_Types\Text', + ), + true + ); + + $y = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'teste', + 'description' => 'adasdasdsa', + 'slug' => 'duplicated_slug', + 'field_type' => 'Tainacan\Field_Types\Text', + ), + true + ); + + $this->assertEquals($x->get_slug(), $y->get_slug()); + + global $Tainacan_Fields; + $x->set_status('publish'); + $x->validate(); + $x = $Tainacan_Fields->insert($x); + $y->set_status('private'); // or publish shoud behave the same + $y->validate(); + $y = $Tainacan_Fields->insert($y); + + $this->assertNotEquals($x->get_slug(), $y->get_slug()); + + } } /** diff --git a/tests/test-filters.php b/tests/test-filters.php index ede09123b..35cb91aa2 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -54,14 +54,12 @@ class Filters extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'metadado', 'collection_id' => $collection->get_id(), - 'field_type' => $type, + 'field_type' => 'Tainacan\Field_Types\Text', 'description' => 'descricao', ), true diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index 9e506c28d..9473ff056 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -29,15 +29,13 @@ class Item_Metadata extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( 'name' => 'metadado', 'description' => 'descricao', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -81,8 +79,6 @@ class Item_Metadata extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( @@ -90,7 +86,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'required' => 'yes', - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -136,8 +132,6 @@ class Item_Metadata extends TAINACAN_UnitTestCase { true ); - $type = $this->tainacan_field_factory->create_field('text'); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( @@ -145,7 +139,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'collection_key' => 'yes', - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -196,8 +190,6 @@ class Item_Metadata extends TAINACAN_UnitTestCase { ); - $type = $this->tainacan_field_factory->create_field('text'); - $this->tainacan_entity_factory->create_entity( 'field', array( @@ -205,7 +197,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'status' => 'publish', - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); diff --git a/tests/test-items.php b/tests/test-items.php index 3425c10fa..4f5c1cc03 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -74,8 +74,6 @@ class Items extends TAINACAN_UnitTestCase { } function teste_query(){ - $type = $this->tainacan_field_factory->create_field('text'); - $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -100,7 +98,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -111,7 +109,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado2', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -122,7 +120,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado3', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); diff --git a/tests/test-objects.php b/tests/test-objects.php index e22c3b472..4d8458877 100644 --- a/tests/test-objects.php +++ b/tests/test-objects.php @@ -27,8 +27,6 @@ class Objects extends TAINACAN_UnitTestCase { $entity = Repository::get_entity_by_post($test); $this->assertEquals($x->get_db_identifier(), $entity->get_db_identifier()); - $type = $this->tainacan_field_factory->create_field('text'); - $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -53,7 +51,7 @@ class Objects extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -64,7 +62,7 @@ class Objects extends TAINACAN_UnitTestCase { 'name' => 'metadado2', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -75,7 +73,7 @@ class Objects extends TAINACAN_UnitTestCase { 'name' => 'metadado3', 'status' => 'publish', 'collection' => $collection, - 'field_type' => $type + 'field_type' => 'Tainacan\Field_Types\Text', ), true );