Reformat code and create a test update core field status

This commit is contained in:
weryques 2018-03-20 13:59:10 -03:00
parent 833c590a9c
commit 868c917c90
4 changed files with 780 additions and 708 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,77 +1,78 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Item_Metadata extends Repository {
public $entities_type = '\Tainacan\Entities\Item_Metadata_Entity';
public function insert($item_metadata) {
$unique = !$item_metadata->is_multiple();
public function insert( $item_metadata ) {
$unique = ! $item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
$this->save_core_field_value($item_metadata);
} elseif ($field_type->get_primitive_type() == 'term') {
$this->save_terms_field_value($item_metadata);
if ( $field_type->get_core() ) {
$this->save_core_field_value( $item_metadata );
} elseif ( $field_type->get_primitive_type() == 'term' ) {
$this->save_terms_field_value( $item_metadata );
} else {
if ($unique) {
update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
} else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id());
if (is_array($item_metadata->get_value())){
$values = $item_metadata->get_value();
if ( $unique ) {
update_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
} else {
delete_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id() );
foreach ($values as $value){
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value ));
}
}
}
if ( is_array( $item_metadata->get_value() ) ) {
$values = $item_metadata->get_value();
foreach ( $values as $value ) {
add_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value ) );
}
}
}
}
do_action('tainacan-insert', $item_metadata);
do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata);
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());
}
do_action( 'tainacan-insert', $item_metadata );
do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata );
return new Entities\Item_Metadata_Entity( $item_metadata->get_item(), $item_metadata->get_field() );
}
/**
* @param $item_metadata
*
* @return mixed|void
*/
public function delete($item_metadata){}
public function delete( $item_metadata ) {
}
public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
$item = $item_metadata->get_item();
$set_method = 'set_' . $field_type->get_related_mapped_prop();
$value = $item_metadata->get_value();
$item->$set_method( is_array( $value ) ? $value[0] : $value );
if ($item->validate_core_fields()) {
global $Tainacan_Items;
$Tainacan_Items->insert($item);
} else {
throw new \Exception('Item metadata should be validated beforehand');
}
}
}
public function save_terms_field_value($item_metadata) {
public function save_core_field_value( \Tainacan\Entities\Item_Metadata_Entity $item_metadata ) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_primitive_type() == 'term') {
if ( $field_type->get_core() ) {
$item = $item_metadata->get_item();
$set_method = 'set_' . $field_type->get_related_mapped_prop();
$value = $item_metadata->get_value();
$item->$set_method( is_array( $value ) ? $value[0] : $value );
if ( $item->validate_core_fields() ) {
global $Tainacan_Items;
$Tainacan_Items->update( $item );
} else {
throw new \Exception( 'Item metadata should be validated beforehand' );
}
}
}
public function save_terms_field_value( $item_metadata ) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ( $field_type->get_primitive_type() == 'term' ) {
$new_terms = $item_metadata->get_value();
$taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') );
if( $taxonomy ){
wp_set_object_terms($item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() );
}
$taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) );
if ( $taxonomy ) {
wp_set_object_terms( $item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() );
}
}
@ -85,78 +86,87 @@ class Item_Metadata extends Repository {
* @return array
* @throws \Exception
*/
public function fetch($object, $output = null ){
if($object instanceof Entities\Item){
global $Tainacan_Items, $Tainacan_Fields;
$collection = $object->get_collection();
if (!$collection instanceof Entities\Collection){
return [];
}
$meta_list = $Tainacan_Fields->fetch_by_collection($collection, [], 'OBJECT' );
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return[] = new Entities\Item_Metadata_Entity($object, $meta);
}
}
return $return;
}else{
return [];
}
}
public function fetch( $object, $output = null ) {
if ( $object instanceof Entities\Item ) {
global $Tainacan_Items, $Tainacan_Fields;
/**
* Get the value for a Item field.
*
* @param Entities\Item_Metadata_Entity $item_metadata
* @return mixed
*/
public function get_value(Entities\Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
$item = $item_metadata->get_item();
$get_method = 'get_' . $field_type->get_related_mapped_prop();
return $item->$get_method();
} elseif ($field_type->get_primitive_type() == 'term') {
$collection = $object->get_collection();
if( is_numeric( $field_type->get_option('taxonomy_id') ) ){
$taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') );
if( $taxonomy ){
$taxonomy_slug = $taxonomy->get_db_identifier();
} else {
return null;
}
} else {
return null;
}
if ( ! $collection instanceof Entities\Collection ) {
return [];
}
$meta_list = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' );
$return = [];
if ( is_array( $meta_list ) ) {
foreach ( $meta_list as $meta ) {
$return[] = new Entities\Item_Metadata_Entity( $object, $meta );
}
}
return $return;
} else {
return [];
}
}
/**
* Get the value for a Item field.
*
* @param Entities\Item_Metadata_Entity $item_metadata
*
* @return mixed
*/
public function get_value( Entities\Item_Metadata_Entity $item_metadata ) {
$unique = ! $item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
if ( $field_type->get_core() ) {
$item = $item_metadata->get_item();
$get_method = 'get_' . $field_type->get_related_mapped_prop();
return $item->$get_method();
} elseif ( $field_type->get_primitive_type() == 'term' ) {
if ( is_numeric( $field_type->get_option( 'taxonomy_id' ) ) ) {
$taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) );
if ( $taxonomy ) {
$taxonomy_slug = $taxonomy->get_db_identifier();
} else {
return null;
}
} else {
return null;
}
$terms = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy_slug );
if ( $unique ) {
$terms = reset( $terms );
}
$terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug );
if ($unique)
$terms = reset($terms);
return $terms;
} else {
return get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique);
}
}
public function register_post_type() { }
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
} else {
return get_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique );
}
}
public function register_post_type() {
}
public function get_map() {
return [];
}
public function get_default_properties( $map ) {
return [];
}
/**
* @param $object
@ -164,15 +174,17 @@ class Item_Metadata extends Repository {
* @return mixed
*/
public function update( $object, $new_values = null ) {
return $this->insert($object);
return $this->insert( $object );
}
/**
* Suggest a value to be inserted as a item Field value, return a pending log
* @param Entities\Item_Metadata_Entity $item_metadata
* @return Entities\Log
*/
public function suggest($item_metadata) {
return Entities\Log::create(false, '', $item_metadata, null, 'pending');
/**
* Suggest a value to be inserted as a item Field value, return a pending log
*
* @param Entities\Item_Metadata_Entity $item_metadata
*
* @return Entities\Log
*/
public function suggest( $item_metadata ) {
return Entities\Log::create( false, '', $item_metadata, null, 'pending' );
}
}

View File

@ -145,12 +145,12 @@ class Items extends Repository {
// iterate through the native post properties
foreach ( $map as $prop => $mapped ) {
if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' && $mapped['map'] != 'thumbnail_id' ) {
if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' ) {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property( $prop );
}
}
// save post and geet its ID
// save post and get its ID
$item->WP_Post->post_type = $cpt;
//$item->WP_Post->post_status = 'publish';

View File

@ -8,88 +8,132 @@ namespace Tainacan\Tests;
* @package Test_Tainacan
*/
use Tainacan\Entities;
class CoreFieldTypes extends TAINACAN_UnitApiTestCase {
/**
* Sample test case.
*/
class CoreFieldTypes extends TAINACAN_UnitTestCase {
function test_core_field_types() {
global $Tainacan_Item_Metadata, $Tainacan_Items;
$collection = $this->tainacan_entity_factory->create_entity(
function test_core_field_types() {
global $Tainacan_Item_Metadata, $Tainacan_Items;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
'name' => 'test',
),
true
);
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'metadado',
'description' => 'title',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Core_Title'
),
true
);
$fieldDescription = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'metadado_desc',
'description' => 'description',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Core_Description'
),
true
);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $field);
$item_metadata->set_value('changed title');
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
$checkItem = $Tainacan_Items->fetch($i->get_id());
$this->assertEquals('changed title', $checkItem->get_title());
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field);
$this->assertEquals('changed title', $check_item_metadata->get_value());
// description
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $fieldDescription);
$item_metadata->set_value('changed description');
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
$checkItem = $Tainacan_Items->fetch($i->get_id());
$this->assertEquals('changed description', $checkItem->get_description());
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $fieldDescription);
$this->assertEquals('changed description', $check_item_metadata->get_value());
}
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'metadado',
'description' => 'title',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Core_Title'
),
true
);
$fieldDescription = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'metadado_desc',
'description' => 'description',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Core_Description'
),
true
);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'collection' => $collection
),
true
);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $field );
$item_metadata->set_value( 'changed title' );
$item_metadata->validate();
$Tainacan_Item_Metadata->insert( $item_metadata );
$checkItem = $Tainacan_Items->fetch( $i->get_id() );
$this->assertEquals( 'changed title', $checkItem->get_title() );
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $field );
$this->assertEquals( 'changed title', $check_item_metadata->get_value() );
// description
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $fieldDescription );
$item_metadata->set_value( 'changed description' );
$item_metadata->validate();
$Tainacan_Item_Metadata->insert( $item_metadata );
$checkItem = $Tainacan_Items->fetch( $i->get_id() );
$this->assertEquals( 'changed description', $checkItem->get_description() );
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $fieldDescription );
$this->assertEquals( 'changed description', $check_item_metadata->get_value() );
}
protected function get_fields( $collection ) {
$request_fields = new \WP_REST_Request( 'GET', $this->namespace . '/collection/' . $collection->get_id() . '/fields' );
$response = $this->server->dispatch( $request_fields );
$data = $response->get_data();
return $data;
}
public function test_update_core_fields_status() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
// GET
$data1 = self::get_fields( $collection );
// PATCH
$field_id = $data1[0]['id'];
$body = json_encode( array(
'status' => 'private'
) );
$request = new \WP_REST_Request( 'PATCH', $this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field_id );
$request->set_body( $body );
$response = $this->server->dispatch( $request );
$data2 = $response->get_data();
$this->assertEquals('private', $data2['status']);
// GET
$data3 = self::get_fields( $collection );
$this->assertCount(2, $data3);
$statuses = [$data3[0]['status'], $data3[1]['status']];
$this->assertContains('private', $statuses);
$this->assertContains('publish', $statuses);
}
}