item & item_metadata

This commit is contained in:
Leo Germani 2017-11-12 21:14:47 -02:00
parent 5f8e2b1a8e
commit 21e71a8997
18 changed files with 773 additions and 105 deletions

View File

@ -104,6 +104,11 @@ class TainacanCollection extends Entity {
return $this->get_id() ? 'tnc_col_' . $this->get_id() : false;
}
// metadata
function get_metadata() {
global $Tainacan_Metadatas;
return $Tainacan_Metadatas->get_metadata_by_collection($this);
}
// Setters
//
@ -127,20 +132,4 @@ class TainacanCollection extends Entity {
return $this->set_mapped_property('itens_per_page', $value);
}
// Relation methods
function get_metadata( $collection ){
global $Tainacan_Metadatas;
return $Tainacan_Metadatas->get_collection_metadata( $collection );
}
}

View File

@ -48,15 +48,7 @@ class TainacanItem extends Entity {
}
function get_metadata(){
global $TainacanItems;
if(isset($this->metadata)) {
return $this->metadata;
}
return $TainacanItems->get_metadata( $this );
}
// Setters
//
@ -75,14 +67,37 @@ class TainacanItem extends Entity {
}
// sepecial Setters
// Metadata
function get_metadata() {
if (isset($this->metadata))
return $this->metadata;
$collection = $this->get_collection();
$return = [];
if ($collection) {
$metaList = $collection->get_metadata();
function set_metadata( $metadata ){
foreach ($metaList as $meta) {
$return[$meta->get_id()] = new Tainacan_Item_Metadata_Entity($this, $meta);
}
}
return $return;
}
function add_metadata(Tainacan_Metadata $new_metadata, $value) {
//TODO Multiple metadata must receive an array as value
$item_metadata = new Tainacan_Item_Metadata_Entity($this, $new_metadata);
$item_metadata->set_value($value);
$current_meta = $this->get_metadata();
$current_meta[$new_metadata->get_id()] = $item_metadata;
$this->set_metadata($current_meta);
}
function set_metadata(Array $metadata) {
$this->metadata = $metadata;
}
}

View File

@ -0,0 +1,158 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Item_Metadata_Entity extends Entity {
function __construct(TainacanItem $item, Tainacan_Metadata $metadata) {
$this->repository = 'Tainacan_Item_Metadata';
$this->set_item($item);
$this->set_metadata($metadata);
}
function set_item(TainacanItem $item) {
$this->item = $item;
}
function set_value($value) {
$this->value = $value;
}
function set_metadata(Tainacan_Metadata $metadata) {
$this->metadata = $metadata;
}
function get_item() {
return $this->item;
}
function get_metadata() {
return $this->metadata;
}
function get_value() {
if (isset($this->value))
return $this->value;
global $Tainacan_Item_Metadata;
return $Tainacan_Item_Metadata->get_item_metadata_value($this);
}
//////////////////
function is_multiple() {
return $this->get_metadata()->is_multiple();
}
function is_collection_key() {
return $this->get_metadata()->is_collection_key();
}
function is_required() {
return $this->get_metadata()->is_required();
}
function validate() {
$value = $this->get_value();
$metadata = $this->get_metadata();
$item = $this->get_item();
if (empty($value) && $this->is_required()) {
$this->add_error('required', $metadata->get_name() . ' is required');
return false;
}
if ($this->is_multiple()) {
if (is_array($value)) {
// if its required, at least one must be filled
$one_filled = false;
$valid = true;
foreach($value as $val) {
if (!empty($val))
$one_filled = true;
// TODO: call fieldtype validation
// if (invalid) $valid = false;
}
if ($this->is_required() && !$one_filled) {
$this->add_error('required', $metadata->get_name() . ' is required');
return false;
}
if (!$valid) {
$this->add_error('invalid', $metadata->get_name() . ' is invalid');
return false;
}
$this->reset_errors();
return true;
} else {
$this->add_error('invalid', $metadata->get_name() . ' is invalid');
return false;
}
} else {
if ($this->is_collection_key()) {
global $TainacanItems;
$test = $TainacanItems->query([
'collections' => $item->get_collection(),
'metadata' => [
[
'key' => $this->metadata->get_id(),
'value' => $value
]
]
]);
if (!empty($test)) {
$this->add_error('key_exists', $metadata->get_name() . ' is a collection key and there is another item with the same value');
return false;
}
}
// TODO: call fieldType validation
//
$this->reset_errors();
return true;
}
}
protected function validate_one_multiple_value($value) {
$metadata = $this->get_metadata();
if (empty($value) && $this->is_required())
return false;
// This method is only called for multiple Metadata
// therefore it can not be collection_key
// TODO: call fieldType validation
return true;
}
}

View File

@ -53,10 +53,22 @@ class Tainacan_Metadata extends Entity {
}
function get_required(){
return $this->get_mapped_property('required');
}
function get_multiple(){
return $this->get_mapped_property('multiple');
}
function get_cardinality(){
return $this->get_mapped_property('cardinality');
}
function get_collection_key(){
return $this->get_mapped_property('collection_key');
}
function get_mask(){
return $this->get_mapped_property('mask');
}
@ -100,10 +112,22 @@ class Tainacan_Metadata extends Entity {
}
function set_required( $value ){
return $this->set_mapped_property('required', $value);
}
function set_multiple( $value ){
return $this->set_mapped_property('multiple', $value);
}
function set_cardinality( $value ){
return $this->set_mapped_property('cardinality', $value);
}
function set_collection_key( $value ){
return $this->set_mapped_property('collection_key', $value);
}
function set_mask( $value ){
return $this->set_mapped_property('mask', $value);
}
@ -129,4 +153,18 @@ class Tainacan_Metadata extends Entity {
}
// helpers
function is_multiple() {
return $this->get_multiple() === 'yes';
}
function is_collection_key() {
return $this->get_collection_key() === 'yes';
}
function is_required() {
return $this->get_required() === 'yes';
}
}

View File

@ -4,10 +4,10 @@
class Entity {
var $repository;
var $errors = [];
function get_mapped_property($prop) {
global ${$this->repository};
$map = ${$this->repository}->map;
@ -21,17 +21,21 @@ class Entity {
$mapped = $map[$prop]['map'];
if ( $mapped == 'meta') {
return get_post_meta($this->WP_Post->ID, $prop, true);
$return = get_post_meta($this->WP_Post->ID, $prop, true);
}elseif ( $mapped == 'meta_multi') {
return get_post_meta($this->WP_Post->ID, $prop, false);
$return = get_post_meta($this->WP_Post->ID, $prop, false);
}elseif ( $mapped == 'termmeta' ){
return get_term_meta($this->WP_Term->term_id, $prop, true);
$return = get_term_meta($this->WP_Term->term_id, $prop, true);
}elseif ( isset( $this->WP_Post )) {
return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null;
$return = isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null;
} elseif ( isset( $this->WP_Term )) {
return isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null;
$return = isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null;
}
if (empty($return) && isset($map[$prop]['default']) && !empty($map[$prop]['default']))
$return = $map[$prop]['default'];
return $return;
}
function set_mapped_property($prop, $value) {
@ -53,12 +57,20 @@ class Entity {
}
function validate($value) {
function validate() {
return true;
}
function get_validation_errors() {
return [];
function get_errors() {
return $this->errors;
}
function add_error($type, $message) {
$this->errors[] = [$type => $message];
}
function reset_errors() {
$this->errors = [];
}
}

View File

@ -6,7 +6,15 @@ if ( ! defined( 'ABSPATH' ) ) {
/**
* Class TainacanFieldType
*/
abstract class Tainacan_Field_Type extends Entity {
abstract class Tainacan_Field_Type {
abstract function render( $metadata );
function validate($value) {
return true;
}
function get_validation_errors() {
return [];
}
}

View File

@ -84,7 +84,7 @@ class TainacanCollections {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta') {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
}
}
@ -114,7 +114,8 @@ class TainacanCollections {
$collection->register_post_type();
return $id;
// return a brand new object
return new TainacanCollection($collection->WP_Post);
}
function get_collections($args = array()) {

View File

@ -0,0 +1,61 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Item_Metadata {
function get_item_metadata_by_item(TainacanItem $item) {
global $TainacanItems, $Tainacan_Metadatas;
$collection = $item->get_collection();
if (!$collection instanceof TainacanCollection)
return [];
$meta_list = $Tainacan_Metadatas->get_metadata_by_collection($collection);
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return = new Tainacan_Item_Metadata_Entity($item, $meta);
}
}
return $return;
}
function insert(Tainacan_Item_Metadata_Entity $item_metadata) {
$unique = $item_metadata->get_metadata()->get_cardinality() > 1 ? false : true;
if ($unique) {
update_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $item_metadata->get_value());
} else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id());
if (is_array($item_metadata->get_value()))
foreach ($item_metadata->get_value() as $value)
add_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $value);
}
// return a brand new object
return new Tainacan_Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadata());
}
function get_item_metadata_value(Tainacan_Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
return get_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $unique);
}
}
global $Tainacan_Item_Metadata;
$Tainacan_Item_Metadata = new Tainacan_Item_Metadata();

View File

@ -79,13 +79,14 @@ class TainacanItems {
// iterate through the native post properties
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta') {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
}
}
// save post and geet its ID
$item->WP_Post->post_type = $cpt;
$item->WP_Post->post_status = 'publish';
$id = wp_insert_post($item->WP_Post);
$item->WP_Post = get_post($id);
@ -102,45 +103,118 @@ class TainacanItems {
}
}
// TODO - save item medatada
// get collection Metadata
// foreach metadata...
return $id;
// save metadata
$metadata = $item->get_metadata();
global $Tainacan_Item_Metadata;
foreach ($metadata as $meta) {
$Tainacan_Item_Metadata->insert($meta);
}
// return a brand new object
return new TainacanItem($item->WP_Post);
}
// collections id or array of ids; collection object or array of objects
function get_items($args = array(), $collections = []) {
global $TainacanCollections;
if (empty($collections)) {
$collections = $TainacanCollections->get_collections();
}
if (is_numeric($collections))
$collections = $TainacanCollections->get_collection_by_id($collection);
if ($collections instanceof TainacanCollection) {
$cpt = $collections->get_db_identifier();
} elseif (is_array($collections)) {
$cpt = [];
foreach ($collections as $collection) {
if (is_numeric($collection))
$collection = $TainacanCollections->get_collection_by_id($collection);
if ($collection instanceof TainacanCollection)
$cpt[] = $collection->get_db_identifier();
}
} else {
return [];
}
if (empty($cpt))
return [];
$args = array_merge([
'post_type' => $cpt,
'posts_per_page' => -1,
'post_status' => 'publish',
], $args);
$posts = get_posts($args);
$return = [];
foreach ($posts as $post) {
$return[] = new TainacanItem($post);
}
return $return;
}
// same as WP_Query with few paramaters more:
// collections ID or array of IDs, object or array of objects
// metadata - array of metadata in meta_query format
// other item properties, present in the "map"
function query($args) {
$map = $this->map;
$wp_query_exceptions = [
'ID' => 'p',
'post_title' => 'title'
];
$meta_query = [];
foreach ($map as $prop => $mapped) {
if (array_key_exists($prop, $args)) {
$prop_value = $args[$prop];
unset($args[$prop]);
if ( $mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi' ) {
$meta_query[] = [
'key' => $prop,
'value' => $prop_value
];
} else {
$prop_search_name = array_key_exists($mapped['map'], $wp_query_exceptions) ? $wp_query_exceptions[$mapped['map']] : $mapped['map'];
$args[$prop_search_name] = $prop_value;
}
}
}
if ( isset($args['metadata']) && is_array($args['metadata']) && !empty($args['metadata'])) {
$meta_query = $args['metadata'];
}
$args['meta_query'] = $meta_query;
unset($args['metadata']);
$collections = !empty($args['collections']) ? $args['collections'] : [];
unset($args['collections']);
return $this->get_items($args, $collections);
### TODO I think its better if we return a WP_Query object. easier for loop and debugging
}
function get_item_by_id($id) {
return new TainacanItem($id);
}
function get_metadata( TainacanItem $item ){
global $TainacanCollections;
$values = [];
$collection_metadata = $TainacanCollections->get_metadata( $item->get_collection() );
foreach ($collection_metadata as $metadata) {
$values[] = [
'metadata_id' => $metadata->get_id(),
'value' => get_post_meta( $item->get_id(), $metadata->get_id()),
];
}
return $values;
}
function set_metadata( TainacanItem $item, $values){
global $TainacanCollections;
$collection_metadata = $TainacanCollections->get_metadata( $item->get_collection() );
foreach ($collection_metadata as $metadata) {
}
}
}
global $TainacanItems;

View File

@ -37,11 +37,23 @@ class Tainacan_Metadatas {
],
'required' => [
'map' => 'meta',
'validation' => ''
'validation' => '', // yes or no
'default' => 'no'
],
'collection_key' => [
'map' => 'meta',
'validation' => '', // yes or no. it cant be key if its multiple
'default' => 'no'
],
'multiple' => [
'map' => 'meta',
'validation' => '', // yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
'map' => 'meta',
'validation' => ''
'validation' => '',
'default' => 1
],
'privacy' => [
'map' => 'meta',
@ -114,7 +126,7 @@ class Tainacan_Metadatas {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta') {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);
}
}
@ -138,7 +150,8 @@ class Tainacan_Metadatas {
}
}
return $id;
// return a brand new object
return new Tainacan_Metadata($metadata->WP_Post);
}
/**

View File

@ -112,7 +112,7 @@ class Tainacan_Taxonomies {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta') {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop);
}
}
@ -138,7 +138,8 @@ class Tainacan_Taxonomies {
$taxonomy->register_taxonomy();
return $id;
// return a brand new object
return new Tainacan_Taxonomy($taxonomy->WP_Post);
}
function registerTainacanTaxonomy( $taxonomy_name ){

View File

@ -24,8 +24,9 @@ trait EntityCollectionRelation {
}
function set_collection_id($value) {
return $this->set_mapped_property('collection_id', $value);
$this->collection = null;
return $this->set_mapped_property('collection_id', $value);
}
function set_collection(TainacanCollection $collection) {

View File

@ -14,12 +14,14 @@ include('classes/Repositories/Items.php');
include('classes/Repositories/Metatadas.php');
include('classes/Repositories/Taxonomies.php');
include('classes/Repositories/Terms.php');
include('classes/Repositories/ItemMetadata.php');
include('classes/Entity.php');
include('classes/Entities/Collection.php');
include('classes/Entities/Item.php');
include('classes/Entities/Metadata.php');
include('classes/Entities/Taxonomy.php');
include('classes/Entities/Term.php');
include('classes/Entities/Item_Metadata_Entity.php');
include('classes/FieldTypes/FieldType.php');
include('classes/FieldTypes/TextFieldType.php');

View File

@ -0,0 +1,151 @@
<?php
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class TestItemMetadata extends WP_UnitTestCase {
/**
* Teste da insercao de um metadado simples sem o tipo
*/
function test_add() {
global $TainacanCollections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new TainacanCollection();
$metadata = new Tainacan_Metadata();
$collection->set_name('teste');
$collection = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
//inserindo o metadado
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$i = new TainacanItem();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
global $TainacanItems;
$item = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($item->get_id());
$item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value('teste_value');
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$this->assertEquals('teste_value', $item_metadata->get_value());
}
/**
* Teste da insercao de um metadado simples com o tipo
*/
function teste_required(){
global $TainacanCollections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new TainacanCollection();
$metadata = new Tainacan_Metadata();
$collection->set_name('teste');
$collection = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_required( 'yes' );
//inserindo o metadado
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$i = new TainacanItem();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
global $TainacanItems;
$item = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($item->get_id());
$item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
// false because its required
$this->assertFalse($item_metadata->validate());
$item_metadata->set_value('teste_value');
$this->assertTrue($item_metadata->validate());
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$this->assertEquals('teste_value', $item_metadata->get_value());
}
function teste_collection_key(){
global $TainacanCollections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new TainacanCollection();
$metadata = new Tainacan_Metadata();
$collection->set_name('teste');
$collection = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_collection_key( 'yes' );
//inserindo o metadado
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$i = new TainacanItem();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
global $TainacanItems;
$item = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($item->get_id());
$value = 'teste_val';
$item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value($value);
$this->assertTrue($item_metadata->validate());
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$n_item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
$n_item_metadata->set_value($value);
$this->assertFalse($n_item_metadata->validate());
}
}

143
tests/test-items.php Normal file
View File

@ -0,0 +1,143 @@
<?php
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class TestItems extends WP_UnitTestCase {
function teste_query(){
global $TainacanCollections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$collection = new TainacanCollection();
$collection2 = new TainacanCollection();
$collection->set_name('teste');
$collection = $TainacanCollections->insert($collection);
$collection2->set_name('teste2');
$collection2 = $TainacanCollections->insert($collection2);
$metadata = new Tainacan_Metadata();
$metadata2 = new Tainacan_Metadata();
$metadata3 = new Tainacan_Metadata();
$metadata->set_name('metadado');
$metadata->set_collection( $collection );
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata2->set_name('metadado2');
$metadata2->set_collection( $collection2 );
$metadata2 = $Tainacan_Metadatas->insert($metadata2);
$metadata3->set_name('metadado3');
$metadata3->set_collection( $collection2 );
$metadata3 = $Tainacan_Metadatas->insert($metadata3);
global $TainacanItems;
$i = new TainacanItem();
$i->set_title('orange');
$i->set_collection($collection);
$i->add_metadata($metadata, 'value_1');
$item = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($item->get_id());
$meta_test = $item->get_metadata();
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof Tainacan_Item_Metadata_Entity );
$this->assertEquals( $meta_test[$metadata->get_id()]->get_value(), 'value_1');
$i = new TainacanItem();
$i->set_title('apple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_2');
$i->add_metadata($metadata3, 'value_2');
$item = $TainacanItems->insert($i);
$i = new TainacanItem();
$i->set_title('lemon');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_2');
$i->add_metadata($metadata2, 'value_3'); // if we set twice, value is overridden
$i->add_metadata($metadata3, 'value_3');
$item = $TainacanItems->insert($i);
$i = new TainacanItem();
$i->set_title('pinapple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_3');
$i->add_metadata($metadata3, 'value_6');
$item = $TainacanItems->insert($i);
// should return all 4 items
$test_query = $TainacanItems->query([]);
$this->assertEquals(4, sizeof($test_query));
// should also return all 4 items
$test_query = $TainacanItems->query(['collections' => [$collection, $collection2]]);
$this->assertEquals(4, sizeof($test_query));
// should return only the first item
$test_query = $TainacanItems->query(['collections' => $collection]);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('orange', $test_query[0]->get_title());
$test_query = $TainacanItems->query(['title' => 'orange']);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('orange', $test_query[0]->get_title());
// should return the other 3 items
$test_query = $TainacanItems->query(['collections' => $collection2]);
$this->assertEquals(3, sizeof($test_query));
$test_query = $TainacanItems->query(['title' => 'apple']);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('apple', $test_query[0]->get_title());
$apple_meta = $test_query[0]->get_metadata();
$this->assertEquals(2, sizeof($apple_meta));
$apple_meta_values = [];
foreach ($apple_meta as $am) {
$this->assertEquals('value_2', $am->get_value());
}
// should return 1 item
$test_query = $TainacanItems->query([
'collections' => $collection2,
'metadata' => [
[
'key' => $metadata2->get_id(),
'value' => 'value_2'
]
]
]);
$this->assertEquals(1, sizeof($test_query));
// should return 2 items
$test_query = $TainacanItems->query([
'collections' => $collection2,
'metadata' => [
[
'key' => $metadata2->get_id(),
'value' => 'value_3'
]
]
]);
$this->assertEquals(2, sizeof($test_query));
// should return 2 item
$test_query = $TainacanItems->query([
'collections' => $collection2,
'metadata' => [
[
'key' => $metadata3->get_id(),
'value' => 'value_2',
'compare' => '>'
]
]
]);
$this->assertEquals(2, sizeof($test_query));
}
}

View File

@ -20,21 +20,21 @@ class TestMetadata extends WP_UnitTestCase {
$metadata = new Tainacan_Metadata();
$collection->set_name('teste');
$id = $TainacanCollections->insert($collection);
$collection = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection_id( $id );
$metadata->set_collection( $collection );
//inserindo o metadado
$metadata_id = $Tainacan_Metadatas->insert($metadata);
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata_id);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
$this->assertEquals($test->get_collection()->get_id(), $id);
$this->assertEquals($test->get_collection_id(), $collection->get_id());
}
@ -49,20 +49,21 @@ class TestMetadata extends WP_UnitTestCase {
$type = new Tainacan_Text_Field_Type();
$collection->set_name('teste');
$id = $TainacanCollections->insert($collection);
$collection = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_collection_id( $id );
$metadata->set_collection_id( $collection->get_id() );
$metadata->set_type( $type );
//inserindo o metadado
$metadata_id = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata_id);
//inserindo o metadado
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_collection()->get_id(), $id);
$this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertEquals($test->get_type('name'), 'Tainacan_Text_Field_Type');
$this->assertEquals($test->get_type(), $type);
}

View File

@ -22,11 +22,11 @@ class TestCollections extends WP_UnitTestCase {
$x->set_itens_per_page(23);
global $TainacanCollections;
$id = $TainacanCollections->insert($x);
$col = $TainacanCollections->insert($x);
//
$test = $TainacanCollections->get_collection_by_id($id);
$test = $TainacanCollections->get_collection_by_id($col->get_id());
$this->assertEquals($test->get_name(), 'teste');
@ -46,9 +46,9 @@ class TestCollections extends WP_UnitTestCase {
$x->set_itens_per_page(23);
global $TainacanCollections;
$cid = $TainacanCollections->insert($x);
$col = $TainacanCollections->insert($x);
$collection = $TainacanCollections->get_collection_by_id($cid);
$collection = $TainacanCollections->get_collection_by_id($col->get_id());
@ -59,13 +59,13 @@ class TestCollections extends WP_UnitTestCase {
$i->set_collection($collection);
global $TainacanItems;
$id = $TainacanItems->insert($i);
$item = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($id);
$item = $TainacanItems->get_item_by_id($item->get_id());
$this->assertEquals($item->get_title(), 'item teste');
$this->assertEquals($item->get_description(), 'adasdasdsa');
$this->assertEquals($item->get_collection_id(), $cid);
$this->assertEquals($item->get_collection_id(), $collection->get_id());
}
}

View File

@ -25,10 +25,10 @@ class TestTaxonomies extends WP_UnitTestCase {
$taxonomy->set_allow_insert(true);
//inserindo
$taxonomy_id = $Tainacan_Taxonomies->insert($taxonomy);
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
//retorna a taxonomia
$test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
$test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy->get_id());
$this->assertEquals( $test->get_name(), 'genero' );
$this->assertEquals( $test->get_description(), 'tipos de musica' );
@ -45,10 +45,10 @@ class TestTaxonomies extends WP_UnitTestCase {
$taxonomy->set_name('genero');
//insere a taxonomia
$taxonomy_id = $Tainacan_Taxonomies->insert($taxonomy);
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
//retorna a taxonomia
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy->get_id());
//insere um termo na taxonomia
$term->set_taxonomy( $taxonomy_test->get_db_identifier() );