Add slug for fields and fix fields tests

This commit is contained in:
Leo Germani 2018-02-21 14:04:25 -03:00
parent 4825cc3469
commit b23378ceff
7 changed files with 120 additions and 43 deletions

View File

@ -32,6 +32,15 @@ class Field extends Entity {
function get_name() { function get_name() {
return $this->get_mapped_property('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 * Return the field order type
@ -164,6 +173,22 @@ class Field extends Entity {
$this->set_mapped_property('name', $value); $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 * Set manually the order of the field
* *

View File

@ -39,6 +39,13 @@ class Fields extends Repository {
'on_error' => __('The name should be a text value and not empty', 'tainacan'), 'on_error' => __('The name should be a text value and not empty', 'tainacan'),
'validation' => v::stringType()->notEmpty(), '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' => [ 'order' => [
'map' => 'menu_order', 'map' => 'menu_order',
'title' => __('Order', 'tainacan'), 'title' => __('Order', 'tainacan'),

View File

@ -27,15 +27,13 @@ class Fields extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado', 'name' => 'metadado',
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -61,15 +59,13 @@ class Fields extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado', 'name' => 'metadado',
'description' => 'descricao', 'description' => 'descricao',
'collection_id' => $collection->get_id(), 'collection_id' => $collection->get_id(),
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -79,7 +75,7 @@ class Fields extends TAINACAN_UnitTestCase {
$this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_collection_id(), $collection->get_id()); $this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type()); $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(){ function test_hierarchy_metadata(){
global $Tainacan_Fields; global $Tainacan_Fields;
$type = $this->tainacan_field_factory->create_field('text');
$this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'field default', 'name' => 'field default',
'collection_id' => $Tainacan_Fields->get_default_metadata_attribute(), 'collection_id' => $Tainacan_Fields->get_default_metadata_attribute(),
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -114,7 +108,7 @@ class Fields extends TAINACAN_UnitTestCase {
array( array(
'name' => 'field grandfather', 'name' => 'field grandfather',
'collection_id' => $collection_grandfather->get_id(), 'collection_id' => $collection_grandfather->get_id(),
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -134,7 +128,7 @@ class Fields extends TAINACAN_UnitTestCase {
array( array(
'name' => 'field father', 'name' => 'field father',
'collection_id' => $collection_father->get_id(), 'collection_id' => $collection_father->get_id(),
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -157,7 +151,7 @@ class Fields extends TAINACAN_UnitTestCase {
array( array(
'name' => 'field son', 'name' => 'field son',
'collection_id' => $collection_son->get_id(), 'collection_id' => $collection_son->get_id(),
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -223,15 +217,13 @@ class Fields extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field1 = $this->tainacan_entity_factory->create_entity( $field1 = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'field1', 'name' => 'field1',
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -243,7 +235,7 @@ class Fields extends TAINACAN_UnitTestCase {
'name' => 'field2', 'name' => 'field2',
'description' => 'field2', 'description' => 'field2',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -256,7 +248,7 @@ class Fields extends TAINACAN_UnitTestCase {
'name' => 'field3', 'name' => 'field3',
'description' => 'field3', 'description' => 'field3',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -277,6 +269,73 @@ class Fields extends TAINACAN_UnitTestCase {
$fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' ); $fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' );
$this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() ); $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());
}
} }
/** /**

View File

@ -54,14 +54,12 @@ class Filters extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado', 'name' => 'metadado',
'collection_id' => $collection->get_id(), 'collection_id' => $collection->get_id(),
'field_type' => $type, 'field_type' => 'Tainacan\Field_Types\Text',
'description' => 'descricao', 'description' => 'descricao',
), ),
true true

View File

@ -29,15 +29,13 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado', 'name' => 'metadado',
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -81,8 +79,6 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
@ -90,7 +86,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'required' => 'yes', 'required' => 'yes',
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -136,8 +132,6 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
true true
); );
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
@ -145,7 +139,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'collection_key' => 'yes', 'collection_key' => 'yes',
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true 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( $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
@ -205,7 +197,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );

View File

@ -74,8 +74,6 @@ class Items extends TAINACAN_UnitTestCase {
} }
function teste_query(){ function teste_query(){
$type = $this->tainacan_field_factory->create_field('text');
$collection = $this->tainacan_entity_factory->create_entity( $collection = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -100,7 +98,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado', 'name' => 'metadado',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -111,7 +109,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado2', 'name' => 'metadado2',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -122,7 +120,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado3', 'name' => 'metadado3',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );

View File

@ -27,8 +27,6 @@ class Objects extends TAINACAN_UnitTestCase {
$entity = Repository::get_entity_by_post($test); $entity = Repository::get_entity_by_post($test);
$this->assertEquals($x->get_db_identifier(), $entity->get_db_identifier()); $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 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -53,7 +51,7 @@ class Objects extends TAINACAN_UnitTestCase {
'name' => 'metadado', 'name' => 'metadado',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -64,7 +62,7 @@ class Objects extends TAINACAN_UnitTestCase {
'name' => 'metadado2', 'name' => 'metadado2',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );
@ -75,7 +73,7 @@ class Objects extends TAINACAN_UnitTestCase {
'name' => 'metadado3', 'name' => 'metadado3',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection, 'collection' => $collection,
'field_type' => $type 'field_type' => 'Tainacan\Field_Types\Text',
), ),
true true
); );