criando e testando criação de metadado simples
This commit is contained in:
parent
332acfb3ab
commit
0f57f52a06
|
@ -2,4 +2,5 @@ build-config.cfg
|
|||
src/style.css
|
||||
src/style.css.map
|
||||
src/scss/.sass-cache
|
||||
tests/bootstrap.php
|
||||
.idea
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Tainacan_Metadata extends Entity {
|
||||
|
||||
function __construct( $which = 0 ) {
|
||||
|
||||
$this->repository = 'Tainacan_Metadatas';
|
||||
|
||||
if ( is_numeric( $which ) && $which > 0) {
|
||||
$post = get_post( $which );
|
||||
if ( $post instanceof WP_Post) {
|
||||
$this->WP_Post = get_post( $which );
|
||||
}
|
||||
|
||||
} elseif ( $which instanceof WP_Post ) {
|
||||
$this->WP_Post = $which;
|
||||
} else {
|
||||
$this->WP_Post = new StdClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @return
|
||||
*/
|
||||
function getter( $attribute ){
|
||||
return $this->get_mapped_property( $attribute );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
*/
|
||||
function setter( $attribute, $value ){
|
||||
return $this->set_mapped_property( $attribute, $value );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Tainacan_Metadatas
|
||||
*/
|
||||
class Tainacan_Metadatas {
|
||||
|
||||
const POST_TYPE = 'tainacan-metadata';
|
||||
const DB_IDENTIFIER_META = '_db_identifier';
|
||||
|
||||
var $map = [
|
||||
'ID' => 'ID',
|
||||
'name' => 'post_title',
|
||||
'order' => 'menu_order',
|
||||
'parent' => 'parent',
|
||||
'description' => 'post_content',
|
||||
'type' => 'meta',
|
||||
'required' => 'meta',
|
||||
'collection' => 'meta',
|
||||
];
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'register_post_type'));
|
||||
}
|
||||
|
||||
function register_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'Metadata',
|
||||
'singular_name' => 'Metadata',
|
||||
'add_new' => 'Adicionar Metadata',
|
||||
'add_new_item' =>'Adicionar Metadata',
|
||||
'edit_item' => 'Editar',
|
||||
'new_item' => 'Novo Metadata',
|
||||
'view_item' => 'Visualizar',
|
||||
'search_items' => 'Pesquisar',
|
||||
'not_found' => 'Nenhum ticket encontrado',
|
||||
'not_found_in_trash' => 'Nenhum Collections encontrado na lixeira',
|
||||
'parent_item_colon' => 'Metadata acima:',
|
||||
'menu_name' => 'Metadata'
|
||||
);
|
||||
|
||||
$args = array(
|
||||
'labels' => $labels,
|
||||
'hierarchical' => true,
|
||||
//'supports' => array('title'),
|
||||
//'taxonomies' => array(self::TAXONOMY),
|
||||
'public' => true,
|
||||
'show_ui' => tnc_enable_dev_wp_interface(),
|
||||
'show_in_menu' => tnc_enable_dev_wp_interface(),
|
||||
//'menu_position' => 5,
|
||||
//'show_in_nav_menus' => false,
|
||||
'publicly_queryable' => true,
|
||||
'exclude_from_search' => true,
|
||||
'has_archive' => true,
|
||||
'query_var' => true,
|
||||
'can_export' => true,
|
||||
'rewrite' => true,
|
||||
'capability_type' => 'post',
|
||||
);
|
||||
register_post_type(self::POST_TYPE, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tainacan_Metadata $metadata
|
||||
* @return int O id do metadado criado
|
||||
*/
|
||||
function insert( Tainacan_Metadata $metadata ) {
|
||||
// First iterate through the native post properties
|
||||
$map = $this->map;
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped != 'meta') {
|
||||
$metadata->WP_Post->$mapped = $metadata->get_mapped_property($prop);
|
||||
}
|
||||
}
|
||||
|
||||
// save post and get its ID
|
||||
$metadata->WP_Post->post_type = self::POST_TYPE;
|
||||
$metadata->WP_Post->post_status = 'publish';
|
||||
$id = wp_insert_post($metadata->WP_Post);
|
||||
|
||||
// Now run through properties stored as postmeta
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped == 'meta') {
|
||||
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
|
||||
}
|
||||
}
|
||||
|
||||
// DB Identifier
|
||||
// the first time a collection is saved, save the slug as a db Identifier,
|
||||
// This will be the slug of the post type that will be created for this collection
|
||||
// Later, if the slug is changed (and thus the URL of this collection) the db Identifier
|
||||
// does not change and we dont lose all the items
|
||||
if (!get_post_meta($id, self::DB_IDENTIFIER_META, true)) {
|
||||
$p = get_post($id);
|
||||
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $collection_id
|
||||
* @param array $args
|
||||
* @return array
|
||||
*/
|
||||
function get_collection_metadata($collection_id,$args = array()) {
|
||||
|
||||
$args = array_merge([
|
||||
'post_type' => self::POST_TYPE,
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => 'collection',
|
||||
'meta_value' => $collection_id
|
||||
], $args);
|
||||
|
||||
$posts = get_posts($args);
|
||||
|
||||
$return = [];
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$return[] = new Tainacan_Metadata($post);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
function get_metadata_db_identifier($id) {
|
||||
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
|
||||
|
||||
if (!$meta) {
|
||||
$p = get_post($id);
|
||||
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
|
||||
return $p->post_name;
|
||||
}
|
||||
|
||||
return $meta;
|
||||
}
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Tainacan_Metadata
|
||||
*/
|
||||
function get_metadata_by_id($id) {
|
||||
return new Tainacan_Metadata($id);
|
||||
}
|
||||
}
|
||||
|
||||
global $Tainacan_Metadatas;
|
||||
$Tainacan_Metadatas = new Tainacan_Metadatas();
|
|
@ -9,8 +9,10 @@ Version: 10.9.8.7.6.5.4
|
|||
|
||||
include('classes/Repositories/Collections.php');
|
||||
include('classes/Repositories/Items.php');
|
||||
include('classes/Repositories/Metatadas.php');
|
||||
include('classes/Entity.php');
|
||||
include('classes/Entities/Collection.php');
|
||||
include('classes/Entities/Metadata.php');
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
|
@ -4,16 +4,12 @@
|
|||
*
|
||||
* @package Test_Tainacan
|
||||
*/
|
||||
|
||||
$_tests_dir = getenv( 'WP_TESTS_DIR' );
|
||||
$_tests_dir = '/home/leo/devel/rhs/tests/wordpress-tests-lib';
|
||||
if ( ! $_tests_dir ) {
|
||||
$_tests_dir = '/tmp/wordpress-tests-lib';
|
||||
}
|
||||
|
||||
// Give access to tests_add_filter() function.
|
||||
require_once $_tests_dir . '/includes/functions.php';
|
||||
|
||||
/**
|
||||
* Manually load the plugin being tested.
|
||||
*/
|
||||
|
@ -21,6 +17,5 @@ function _manually_load_plugin() {
|
|||
require dirname( dirname( __FILE__ ) ) . '/src/test-tainacan.php';
|
||||
}
|
||||
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
|
||||
|
||||
// Start up the WP testing environment.
|
||||
require $_tests_dir . '/includes/bootstrap.php';
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Class TestCollections
|
||||
*
|
||||
* @package Test_Tainacan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sample test case.
|
||||
*/
|
||||
class TestMetadata extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Teste da insercao de um metadado simples sem o tipo
|
||||
*/
|
||||
function test_add() {
|
||||
global $TainacanCollections, $Tainacan_Metadatas;
|
||||
|
||||
$collection = new TainacanCollection();
|
||||
$metadata = new Tainacan_Metadata();
|
||||
|
||||
$collection->set_name('teste');
|
||||
$id = $TainacanCollections->insert($collection);
|
||||
|
||||
//setando os valores na classe do metadado
|
||||
$metadata->setter('name','metadado');
|
||||
$metadata->setter('description','descricao');
|
||||
$metadata->setter('collection',$id);
|
||||
|
||||
//inserindo o metadado
|
||||
$metadata_id = $Tainacan_Metadatas->insert($metadata);
|
||||
|
||||
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata_id);
|
||||
|
||||
$this->assertEquals($test->getter('name'), 'metadado');
|
||||
$this->assertEquals($test->getter('description'), 'descricao');
|
||||
$this->assertEquals($test->getter('collection'), $id);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue