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

View File

@ -1,12 +1,14 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
use Tainacan\Field_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Fields
*/
@ -20,6 +22,7 @@ class Fields extends Repository {
'Tainacan\Field_Types\Core_Title',
'Tainacan\Field_Types\Core_Description'
];
/**
* Register specific hooks for field repository
*/
@ -104,7 +107,8 @@ class Fields extends Repository {
'type' => 'string',
'description' => __( 'Allow multiple fields for the field', 'tainacan' ),
'on_error' => __( 'Multiple fields is invalid', 'tainacan' ),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
@ -122,7 +126,8 @@ class Fields extends Repository {
'type' => 'string',
'description' => __( 'The field should be omitted in item view', 'tainacan' ),
'on_error' => __( 'Privacy is invalid', 'tainacan' ),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'mask' => [
@ -168,7 +173,8 @@ class Fields extends Repository {
'type' => 'string',
'description' => __( 'The field can be deleted', 'tainacan' ),
'on_error' => __( 'Can delete is invalid', 'tainacan' ),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'yes'
],
] );
@ -176,6 +182,7 @@ class Fields extends Repository {
/**
* Get the labels for the custom post type of this repository
*
* @return array Labels in the format expected by register_post_type()
*/
public function get_cpt_labels() {
@ -299,6 +306,7 @@ class Fields extends Repository {
$args['post_type'] = Entities\Field::get_post_type();
$wp_query = new \WP_Query( $args );
return $this->fetch_output( $wp_query, $output );
}
}
@ -352,6 +360,7 @@ class Fields extends Repository {
* @param $result Response from method fetch
* @param Entities\Collection $collection
* @param bool $include_disabled Wether to include disabled fields in the results or not
*
* @return array or WP_Query ordinate
*/
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
@ -387,8 +396,7 @@ class Fields extends Repository {
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
return $result_ordinate;
}
// if the result is a wp query object
} // if the result is a wp query object
else {
$posts = $result->posts;
$result_ordinate = [];
@ -412,11 +420,13 @@ class Fields extends Repository {
return $result;
}
}
return $result;
}
/**
* @param \Tainacan\Entities\Field $field
*
* @return \Tainacan\Entities\Field
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
@ -428,6 +438,7 @@ class Fields extends Repository {
$new_field = parent::insert( $field );
$this->update_category_field( $new_field );
return $new_field;
}
@ -444,6 +455,7 @@ class Fields extends Repository {
public function delete( $field_id ) {
$this->delete_category_field( $field_id );
return new Entities\Field( wp_trash_post( $field_id ) );
}
@ -455,6 +467,7 @@ class Fields extends Repository {
* NAME - return an Array of the names of field types registered
*
* @param $output string CLASS | NAME
*
* @return array of Entities\Field_Types\Field_Type classes path name
*/
public function fetch_field_types( $output = 'CLASS' ) {
@ -575,7 +588,7 @@ class Fields extends Repository {
array(
'key' => 'collection_id',
'value' => $collection->get_id(),
'compare' => 'IN',
'compare' => '=',
),
array(
'key' => 'field_type',
@ -607,6 +620,7 @@ class Fields extends Repository {
if ( $field->validate() ) {
$field = $this->insert( $field );
return $field->get_id();
} else {
throw new \ErrorException( 'The entity wasn\'t validated.' . print_r( $field->get_errors(), true ) );
@ -723,6 +737,7 @@ class Fields extends Repository {
* a field type category is inserted or removed
*
* @param [type] $field [description]
*
* @return [type] [description]
*/
private function update_category_field( $field ) {
@ -749,8 +764,9 @@ class Fields extends Repository {
$field_type = $field->get_field_type_object();
if ( $field_type->get_primitive_type() == 'term' ) {
$removed_tax = $field_type->get_option( 'taxonomy_id' );
if (!empty($removed_tax))
if ( ! empty( $removed_tax ) ) {
do_action( 'tainacan-taxonomy-removed-from-collection', $removed_tax, $field->get_collection() );
}
}
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
@ -34,7 +35,6 @@ class Item_Metadata extends Repository {
}
do_action( 'tainacan-insert', $item_metadata );
do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata );
@ -46,7 +46,8 @@ class Item_Metadata extends Repository {
*
* @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();
@ -57,7 +58,7 @@ class Item_Metadata extends Repository {
$item->$set_method( is_array( $value ) ? $value[0] : $value );
if ( $item->validate_core_fields() ) {
global $Tainacan_Items;
$Tainacan_Items->insert($item);
$Tainacan_Items->update( $item );
} else {
throw new \Exception( 'Item metadata should be validated beforehand' );
}
@ -115,6 +116,7 @@ class Item_Metadata extends Repository {
* 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 ) {
@ -125,6 +127,7 @@ class Item_Metadata extends Repository {
$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' ) {
@ -142,8 +145,9 @@ class Item_Metadata extends Repository {
$terms = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy_slug );
if ($unique)
if ( $unique ) {
$terms = reset( $terms );
}
return $terms;
@ -153,10 +157,16 @@ class Item_Metadata extends Repository {
}
public function register_post_type() { }
public function register_post_type() {
}
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
public function get_map() {
return [];
}
public function get_default_properties( $map ) {
return [];
}
/**
* @param $object
@ -169,7 +179,9 @@ class Item_Metadata extends Repository {
/**
* 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 ) {

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,12 +8,7 @@ namespace Tainacan\Tests;
* @package Test_Tainacan
*/
use Tainacan\Entities;
/**
* Sample test case.
*/
class CoreFieldTypes extends TAINACAN_UnitTestCase {
class CoreFieldTypes extends TAINACAN_UnitApiTestCase {
function test_core_field_types() {
@ -92,4 +87,53 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
}
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);
}
}