Merge branch 'master' of github.com:tainacan/tainacan

This commit is contained in:
Jacson Passold 2017-12-12 11:46:50 -02:00
commit f04fb3d81c
26 changed files with 1699 additions and 67 deletions

View File

@ -1,5 +1,6 @@
{
"presets": [
["es2015", { "modules": false }]
["es2015", { "modules": false }],
["stage-2"]
]
}

View File

@ -15,6 +15,7 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
1.1.1 GET (Fetch a collection)
1.1.2 DELETE (Delete or Trash a collection and all your dependencies)
```
To delete pass in body of a requisition the parameter is_permanently as true.
To only trash pass false.
@ -44,7 +45,46 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
2.1.1 GET (Fetch a item)
2.1.2 DELETE (Delete or Trash a item and all your dependencies)
```
To delete pass in body of a requisition the parameter is_permanently as true.
To only trash pass false.
```
#### Metadata
1. Route `wp-json/tainacan/v2/metadata/collection/(?P<collection_id>[\d]+)`
1.1. Endpoints supported:
1.1.1 POST (Create a metadata in collection and all your items)
```
In body of requisition pass a JSON with the attributes of metadata like:
```
```javascript
{
name: '',
description: '',
field_type: ''
}
```
2. Route `wp-json/tainacan/v2/metadata/item/(?P<item_id>[\d]+)`
2.1. Endpoints supported:
2.1.1 POST (Set a value of item metadata)
```
In body of requisition pass a JSON with value e and id of metadata like:
```
```javascript
{
metadata_id: '',
values: ''
}
```

1149
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,18 +8,21 @@
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"axios": "^0.17.1",
"bootstrap": "^3.3.7",
"vue": "^2.0.1"
"vue": "^2.0.1",
"vuex": "^3.0.1"
},
"devDependencies": {
"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-preset-es2015": "^6.0.0",
"babel-preset-stage-2": "^6.24.1",
"cross-env": "^3.0.0",
"css-loader": "^0.25.0",
"file-loader": "^0.9.0",
"vue-loader": "^9.7.0",
"vue-custom-element": "^1.0.13",
"vue-loader": "^9.7.0",
"webpack": "2.1.0-beta.25",
"webpack-dev-server": "2.1.0-beta.0"
}

View File

@ -34,11 +34,12 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_item_schema()
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
//'permission_callback' => array($this, 'create_item_permissions_check'),
'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
),
));
@ -88,7 +89,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
$collection_id = $request['collection_id'];
$collection = $this->collections_repository->fetch($collection_id);
$response = $this->prepare_item_for_response($collection, $request);
$response = $this->prepare_item_for_response( $collection, $request );
return new WP_REST_Response($response, 200);
}
@ -118,9 +119,11 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return json_encode($collections_as_json);
}
else {
elseif(!empty($item)){
return $item->__toJSON();
}
return $item;
}
/**
@ -152,15 +155,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return array|WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
$request = json_decode($request->get_body(), true);
$request = json_decode($request->get_body(), true);
if(empty($request)) {
return new WP_Error('rest_empty', __('Empty request.'), array('status' => 204));
} elseif (!empty($request['collection_id'])){
return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
}
$prepared_post = $this->prepare_item_for_database($request);
try {
$prepared_post = $this->prepare_item_for_database( $request );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
if($prepared_post->validate()) {
$collection = $this->collections_repository->insert( $prepared_post );
@ -179,11 +180,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return bool|WP_Error
*/
public function create_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
return true;
}
/**
@ -194,8 +191,11 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return object|Entities\Collection|WP_Error
*/
public function prepare_item_for_database( $request ) {
$this->collection->set_name($request['name']);
$this->collection->set_description($request['description']);
foreach ($request as $key => $value){
$set_ = 'set_' . $key;
$this->collection->$set_($value);
}
return $this->collection;
}
@ -251,11 +251,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
return true;
}
public function get_collection_params() {
@ -274,6 +270,12 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
}
public function get_item_schema() {
$args = $this->collections_repository->get_map();
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
}
}
?>

View File

@ -11,6 +11,7 @@ use Tainacan\Entities;
class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
private $items_repository;
private $item;
private $item_metadata;
/**
* TAINACAN_REST_Items_Controller constructor.
@ -21,6 +22,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
$this->rest_base = 'items';
$this->items_repository = new Repositories\Items();
$this->item = new Entities\Item();
$this->item_metadata = new Repositories\Item_Metadata();
add_action('rest_api_init', array($this, 'register_routes'));
}
@ -139,15 +141,35 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @return object|Entities\Item|WP_Error
*/
public function prepare_item_for_database( $request ) {
$this->item->set_title($request[0]['title']);
$this->item->set_description($request[0]['description']);
$collection_wp_post = get_post($request[1]);
$collection = new Entities\Collection($collection_wp_post);
$item_as_array = $request[0];
foreach ($item_as_array as $key => $value){
$set_ = 'set_' . $key;
$this->item->$set_($value);
}
$collection = new Entities\Collection($request[1]);
$this->item->set_collection($collection);
return $this->item;
$metadata = get_post_meta($collection->get_id());
if(!empty($metadata)) {
foreach ($metadata as $key => $value){
$new_metadata = new Entities\Metadata();
try {
$set_ = 'set_' . $key;
$new_metadata->$set_( $value );
} catch (\Error $exception){
//echo $exception->getMessage();
}
}
}
return $new_metadata;
}
/**
@ -157,17 +179,24 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
*/
public function create_item( $request ) {
$collection_id = $request['collection_id'];
$item = json_decode($request->get_body(), true);
$item = json_decode($request->get_body(), true);
$prepared_item = $this->prepare_item_for_database([$item, $collection_id]);
if($prepared_item->validate()){
$item = $this->items_repository->insert($prepared_item);
return new WP_REST_Response($item->__toJSON(), 201);
try {
$metadata = $this->prepare_item_for_database( [ $item, $collection_id ] );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
return new WP_REST_Response($prepared_item->get_errors(), 400);
if($this->item->validate()) {
$item = $this->items_repository->insert($this->item );
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadata );
$metadata_added = $this->item_metadata->insert( $item_metadata );
return new WP_REST_Response($metadata_added->get_item()->__toJSON(), 201 );
}
return new WP_REST_Response($item->get_errors(), 400);
}
/**

View File

@ -0,0 +1,173 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
private $metadata;
private $metadata_repository;
private $item_metadata_repository;
private $item_repository;
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'metadata';
$this->metadata = new Entities\Metadata();
$this->metadata_repository = new Repositories\Metadatas();
$this->item_metadata_repository = new Repositories\Item_Metadata();
$this->item_repository = new Repositories\Items();
add_action('rest_api_init', array($this, 'register_routes'));
}
/**
* If POST on metadata/collection/<collection_id>, then
* a metadata will be created in matched collection and all your item will receive this metadata
*
* If POST on metadata/item/<item_id>, then a value will be added in a field and metadata passed
* id body of requisition
*
* Both of GETs return the metadata of matched objects
*/
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base . '/collection/(?P<collection_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
),
)
);
register_rest_route($this->namespace, '/' . $this->rest_base . '/item/(?P<item_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
)
)
);
}
/**
* @param WP_REST_Request $request
*
* @return object|void|WP_Error
*/
public function prepare_item_for_database( $request ) {
$meta = json_decode($request[0]->get_body(), true);
foreach ($meta as $key => $value){
$set_ = 'set_' . $key;
$this->metadata->$set_($value);
}
$collection = new Entities\Collection($request[1]);
$this->metadata->set_collection($collection);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if(!empty($request['collection_id'])){
$collection_id = $request['collection_id'];
try {
$this->prepare_item_for_database( [ $request, $collection_id ] );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
if($this->metadata->validate()) {
$this->metadata_repository->insert( $this->metadata );
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
$metadata_added = '';
if($items->have_posts()){
while ($items->have_posts()){
$items->the_post();
$item = new Entities\Item($items->post);
$item_meta = new Entities\Item_Metadata_Entity($item, $this->metadata);
$metadata_added = $this->item_metadata_repository->insert($item_meta);
}
return new WP_REST_Response($metadata_added->get_metadata()->__toJSON(), 201);
}
else {
return new WP_REST_Response($this->metadata->__toJSON(), 201);
}
} else {
return new WP_REST_Response($this->metadata->get_errors(), 200);
}
} elseif (!empty($request['item_id']) && !empty($request->get_body())){
$body = json_decode($request->get_body(), true);
$item_id = $request['item_id'];
$metadata_id = $body['metadata_id'];
$value = $body['values'];
$item = $this->item_repository->fetch($item_id);
$metadata = $this->metadata_repository->fetch($metadata_id);
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value($value);
if($item_metadata->validate()) {
$metadata_updated = $this->item_metadata_repository->insert( $item_metadata );
return new WP_REST_Response( $metadata_updated->get_metadata()->__toJSON(), 201 );
} else {
return new WP_REST_Response( $item_metadata->get_errors(), 200);
}
} else {
return new WP_REST_Response($request->get_body(), 400);
}
}
public function create_item_permissions_check( $request ) {
return true;
}
public function prepare_item_for_response( $item, $request ) {
}
public function get_items( $request ) {
$id = !empty($request['collection_id']) ? $request['collection_id'] : (!empty($request['item_id']) ? $request['item_id'] : '');
$this->metadata_repository;
}
public function get_item_permissions_check( $request ) {
return true;
}
public function get_collection_params() {
return parent::get_collection_params(); // TODO: Change the autogenerated stub
}
}
?>

View File

@ -2,6 +2,7 @@
$rest_collections_controller = new TAINACAN_REST_Collections_Controller();
$rest_items_controller = new TAINACAN_REST_Items_Controller();
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
// Add here other endpoints imports
?>

View File

@ -16,8 +16,7 @@ class Log extends Entity {
*/
protected $repository = 'Tainacan_Logs';
public function __construct($which=0)
{
public function __construct($which=0) {
parent::__construct($which);
if( is_int($which) && $which == 0) {

View File

@ -1,14 +1,39 @@
<template>
<div class="component">
<p>{{ name }}</p>
<input type="text">
<input type="text" v-model.lazy="value" >
</div>
</template>
<script>
import store from '../../../js/store/store'
import { mapGetters } from 'vuex';
export default {
store,
props: {
name: { type: String }
name: { type: String },
item: { type: Number },
metadata: { type: Number },
},
computed:{
value : {
get(){
let metadata = this.$store.getters['item/getMetadata'].find(metadata => metadata.metadata_id === this.metadata );
if( metadata ){
return metadata.values;
}
},
set( value ){
let metadata = this.$store.getters['item/getMetadata'].find(metadata => metadata.metadata_id === this.metadata );
if( ! metadata ){
this.$store.dispatch('item/addMetadata', { item_id: this.item, metadata_id: this.metadata, values: value });
}else{
this.$store.dispatch('item/updateMetadata', { item_id: this.item, metadata_id: this.metadata, values: value });
}
}
}
}
}
</script>

View File

@ -21,7 +21,9 @@ class Text extends Field_Type {
*/
public function render( $metadata ){
return '<tainacan-text name="'.$metadata->get_name().'"></tainacan-text>';
return '<tainacan-text metadata="'.$metadata->get_id().'"
item="'.get_the_ID().'"
name="'.$metadata->get_name().'"></tainacan-text>';
}
/**

View File

@ -162,8 +162,13 @@ class Collections extends Repository {
}
public function delete($args){
if($args[1]['is_permanently'] === true){
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*
* @return mixed|Collection
*/
public function delete($args){
if(!empty($args[1]) && $args[1]['is_permanently'] === true){
return new Entities\Collection(wp_delete_post($args[0], $args[1]['is_permanently']));
}

View File

@ -200,8 +200,13 @@ class Items extends Repository {
}
public function delete($args){
if($args[1]['is_permanently'] === true){
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*
* @return mixed|Entities\Item
*/
public function delete($args){
if(!empty($args[1]) && $args[1]['is_permanently'] === true){
return new Entities\Item(wp_delete_post($args[0], $args[1]['is_permanently']));
}

View File

@ -15,6 +15,8 @@ class DevInterface {
add_action('save_post', array(&$this, 'save_post'), 10, 2);
add_action('admin_enqueue_scripts', array(&$this, 'add_admin_js'));
add_filter('post_type_link', array(&$this, 'permalink_filter'), 10, 3);
global $Tainacan_Collections, $Tainacan_Filters, $Tainacan_Logs, $Tainacan_Metadatas, $Tainacan_Taxonomies;
$repositories = [$Tainacan_Collections, $Tainacan_Filters, $Tainacan_Logs, $Tainacan_Metadatas, $Tainacan_Taxonomies];
@ -28,9 +30,40 @@ class DevInterface {
function add_admin_js() {
global $TAINACAN_BASE_URL;
wp_enqueue_script('wp-settings',$TAINACAN_BASE_URL . '/js/wp-settings.js');
wp_localize_script( 'wp-settings', 'wpApiSettings', array( 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ) ) );
wp_enqueue_script('tainacan-dev-admin', $TAINACAN_BASE_URL . '/assets/web-components.js');
}
/**
* Filters the permalink for posts to:
*
* * Replace Collectino single permalink with the link to the post type archive for items of that collection
*
* @return string new permalink
*/
function permalink_filter($permalink, $post, $leavename) {
$collection_post_type = \Tainacan\Entities\Collection::get_post_type();
if (!is_admin() && $post->post_type == $collection_post_type) {
$collection = new \Tainacan\Entities\Collection($post);
$items_post_type = $collection->get_db_identifier();
$post_type_object = get_post_type_object($items_post_type);
if (isset($post_type_object->rewrite) && is_array($post_type_object->rewrite) && isset($post_type_object->rewrite['slug']))
return site_url($post_type_object->rewrite['slug']);
}
return $permalink;
}
/**
* Run through all post types attributes and add metaboxes for them.
*

9
src/js/axios/axios.js Normal file
View File

@ -0,0 +1,9 @@
import axios from 'axios';
const instance = axios.create({
baseURL: wpApiSettings.api_root
});
//instance.defaults.headers.common['something'] = 'none';
export default instance;

View File

@ -1,4 +1,6 @@
import Vue from 'vue'
import store from './store/store'
// include vue-custom-element plugin to Vue
import VueCustomElement from 'vue-custom-element';

View File

@ -0,0 +1,76 @@
import axios from '../../axios/axios';
const state = {
item: null,
metadata:[],
isInit:false,
};
const mutations = {
setItem( state, item){
state.item = item;
},
setMetadata( state, metadata){
state.metadata = metadata;
},
addMetadata( state, metadata){
state.metadata.push( metadata );
},
updateMetadata( state, metadata){
var index = state.metadata.findIndex(itemMetadata => itemMetadata.metadata_id === metadata.metadata_id);
state.metadata[index] = metadata;
},
setInit( state, init){
state.isInit = init;
},
};
const actions = {
initItem: ( { commit }, item) => {
commit('setItem', { id: 1, title: 'item' });
},
initMetadata: ( { commit }, item) => {
commit('setMetadata', [
{ item_id: 1, metadata_id: 1, values: [ 'valor' ] }
]);
},
addMetadata: ( { commit }, { item_id, metadata_id, values }) => {
// axios.post('/metadata/item/'+item, {
// metadata_id: metadata,
// values: values
// })
// .then( res => {
// commit('setMetadata', res);
// })
// .catch(error => console.log( error ));
commit('addMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
},
updateMetadata: ( { commit }, { item_id, metadata_id, values }) => {
// axios.post('/metadata/item/'+item, {
// metadata_id: metadata,
// values: values
// })
// .then( res => {
// commit('setMetadata', res);
// })
// .catch(error => console.log( error ));
commit('updateMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
}
};
const getters = {
getItem: state => {
return state.item;
},
getMetadata: state => {
return state.metadata;
}
};
export default {
namespaced: true,
state,
mutations,
actions,
getters
}

12
src/js/store/store.js Normal file
View File

@ -0,0 +1,12 @@
import Vue from 'vue';
import Vuex from 'vuex';
import item from './modules/item';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
item
}
})

0
src/js/wp-settings.js Normal file
View File

View File

@ -5,6 +5,7 @@ Plugin Name: Tainacan
Plugin URI: https://github.com/tainacan/tainacan
Description: Transforme seu site Wordpress em um repositório digital
Author: Media Lab / UFG
Author URI: https://www.medialab.ufg.br
Version: 1.0
*/

View File

@ -34,7 +34,7 @@ class Entity_Factory {
try {
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
@ -75,7 +75,7 @@ class Entity_Factory {
if ($this->entity->validate()) {
$this->entity = $this->repository->insert($this->entity);
} else {
throw new \ErrorException( __( 'The entity wasn\'t validated.' ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.', 'tainacan' ) );
}
} elseif (!empty($args) && !$is_validated_and_in_db){
@ -92,19 +92,19 @@ class Entity_Factory {
} elseif (empty($args) && !$is_validated_and_in_db) {
try {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_name( "$type " . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Error $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_title( "$type " . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
} elseif (empty($args) && $is_validated_and_in_db) {
try {
$this->entity->set_name( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_name( "$type " . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Error $exception){
$this->entity->set_title( "$type" . random_int( 0, 10000 ) . " for test" );
$this->entity->set_title( "$type " . random_int( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
@ -112,7 +112,8 @@ class Entity_Factory {
if ($this->entity->validate()) {
$this->entity = $this->repository->insert( $this->entity );
} else {
throw new \ErrorException( __( 'The entity wasn\'t validated.'.print_r($this->entity->get_errors(), true) ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.', 'tainacan').print_r( $this->entity->get_errors(), true ) ); // TODO show this in a better way
throw new \ErrorException( __( 'The entity wasn\'t validated.', 'tainacan' ) );
}
} else {

View File

@ -8,7 +8,7 @@ class Field_Factory {
public function create_field($type, $primitive_type = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {

View File

@ -8,7 +8,7 @@ class Filter_Factory {
public function create_filter($type, $supported_types = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {

View File

@ -12,7 +12,7 @@ class TAINACAN_UnitTestCase extends \WP_UnitTestCase {
public function setUp(){
parent::setUp();
$this->tainacan_entity_factory = new Factories\Entity_Factory();
$this->tainacan_field_factory = new Factories\Field_Factory();
$this->tainacan_field_factory = new Factories\Field_Factory();
$this->tainacan_filter_factory = new Factories\Filter_Factory();
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Repositories;
class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
public function test_insert_metadata() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name',
'description' => 'No description',
'collection' => $collection
),
true
);
$field = $this->tainacan_field_factory->create_field('text', '', true);
$metadata = json_encode(
array(
'name' => 'Moeda',
'description' => 'Descreve campo moeda.',
'field_type' => $field->get_primitive_type(),
)
);
$request = new \WP_REST_Request(
'POST',
$this->namespaced_route . '/metadata/collection/' . $collection->get_id()
);
$request->set_body($metadata);
$response = $this->server->dispatch($request);
$metadata_added = json_decode($response->get_data(), true);
$this->assertEquals('Moeda', $metadata_added['name']);
####################
$meta_values = json_encode(
array(
'metadata_id' => $metadata_added['id'],
'values' => 'Valorado'
)
);
$request = new \WP_REST_Request(
'POST',
$this->namespaced_route . '/metadata/item/' . $item->get_id()
);
$request->set_body($meta_values);
$response = $this->server->dispatch($request);
$metadata_updated = json_decode($response->get_data(), true);
$this->assertEquals($metadata_added['id'], $metadata_updated['id']);
$metav = get_post_meta($item->get_id(), $metadata_updated['id'], true);
$this->assertEquals('Valorado', $metav);
}
}
?>

View File

@ -13,8 +13,7 @@ use Tainacan\Repositories\Repository;
* @group architecture
*/
class Objects extends TAINACAN_UnitTestCase {
function test_object_transformation()
{
function test_object_transformation() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(