tainacan/tests/test-items.php

168 lines
5.9 KiB
PHP
Raw Normal View History

2017-11-12 23:14:47 +00:00
<?php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests;
2017-11-12 23:14:47 +00:00
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
2017-11-29 18:06:22 +00:00
use Tainacan\Entities\Entity;
2017-11-12 23:14:47 +00:00
/**
* Sample test case.
*/
2017-11-15 18:50:11 +00:00
class Items extends \WP_UnitTestCase {
2017-11-12 23:14:47 +00:00
function teste_query(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
2017-11-12 23:14:47 +00:00
2017-11-15 18:50:11 +00:00
$collection = new \Tainacan\Entities\Collection();
$collection2 = new \Tainacan\Entities\Collection();
2017-11-12 23:14:47 +00:00
$collection->set_name('teste');
$collection->validate();
$collection = $Tainacan_Collections->insert($collection);
2017-11-12 23:14:47 +00:00
$collection2->set_name('teste2');
$collection2->validate();
$collection2 = $Tainacan_Collections->insert($collection2);
2017-11-12 23:14:47 +00:00
2017-11-15 18:50:11 +00:00
$metadata = new \Tainacan\Entities\Metadata();
$metadata2 = new \Tainacan\Entities\Metadata();
$metadata3 = new \Tainacan\Entities\Metadata();
2017-11-12 23:14:47 +00:00
$metadata->set_name('metadado');
$metadata->set_collection( $collection );
$metadata->validate();
2017-11-12 23:14:47 +00:00
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata2->set_name('metadado2');
$metadata2->set_collection( $collection2 );
$metadata2->validate();
2017-11-12 23:14:47 +00:00
$metadata2 = $Tainacan_Metadatas->insert($metadata2);
$metadata3->set_name('metadado3');
$metadata3->set_collection( $collection2 );
$metadata3->validate();
2017-11-12 23:14:47 +00:00
$metadata3 = $Tainacan_Metadatas->insert($metadata3);
global $Tainacan_Items;
2017-11-15 18:50:11 +00:00
$i = new \Tainacan\Entities\Item();
2017-11-12 23:14:47 +00:00
$i->set_title('orange');
$i->set_collection($collection);
$i->add_metadata($metadata, 'value_1');
$i->validate();
$item = $Tainacan_Items->insert($i);
2017-11-12 23:14:47 +00:00
2017-11-20 17:13:37 +00:00
$item = $Tainacan_Items->fetch($item->get_id());
2017-11-12 23:14:47 +00:00
$meta_test = $item->get_metadata();
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );
2017-11-15 18:50:11 +00:00
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof \Tainacan\Entities\Item_Metadata_Entity );
2017-11-24 18:54:52 +00:00
$this->assertEquals( 'value_1', $meta_test[$metadata->get_id()]->get_value());
2017-11-12 23:14:47 +00:00
2017-11-15 18:50:11 +00:00
$i = new \Tainacan\Entities\Item();
2017-11-12 23:14:47 +00:00
$i->set_title('apple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_2');
$i->add_metadata($metadata3, 'value_2');
$i->validate();
$item = $Tainacan_Items->insert($i);
2017-11-12 23:14:47 +00:00
2017-11-15 18:50:11 +00:00
$i = new \Tainacan\Entities\Item();
2017-11-12 23:14:47 +00:00
$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');
$i->validate();
$item = $Tainacan_Items->insert($i);
2017-11-12 23:14:47 +00:00
2017-11-15 18:50:11 +00:00
$i = new \Tainacan\Entities\Item();
2017-11-12 23:14:47 +00:00
$i->set_title('pinapple');
$i->set_collection($collection2);
$i->add_metadata($metadata2, 'value_3');
$i->add_metadata($metadata3, 'value_6');
$i->validate();
$item = $Tainacan_Items->insert($i);
2017-11-12 23:14:47 +00:00
// should return all 4 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([]);
2017-11-29 18:06:22 +00:00
$this->assertEquals(4, $test_query->post_count );
2017-11-12 23:14:47 +00:00
// should also return all 4 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], [$collection, $collection2]);
2017-11-29 18:06:22 +00:00
$this->assertEquals(4, $test_query->post_count);
2017-11-12 23:14:47 +00:00
2017-11-30 16:42:11 +00:00
// should return 2 items
$test_query = $Tainacan_Items->fetch(['posts_per_page' => 2], [$collection, $collection2]);
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return only the first item
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], $collection);
2017-11-29 18:06:22 +00:00
$this->assertEquals(1,$test_query->post_count);
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals('orange', $item->get_title() );
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch(['title' => 'orange']);
2017-11-29 18:06:22 +00:00
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('orange', $item->get_title());
2017-11-12 23:14:47 +00:00
// should return the other 3 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(3,$test_query->post_count);
2017-11-12 23:14:47 +00:00
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch(['title' => 'apple']);
2017-11-29 18:06:22 +00:00
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('apple', $item->get_title());
$apple_meta = $item->get_metadata();
$this->assertEquals(2, sizeof( $apple_meta ));
2017-11-12 23:14:47 +00:00
$apple_meta_values = [];
foreach ($apple_meta as $am) {
$this->assertEquals('value_2', $am->get_value());
}
// should return 1 item
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $metadata2->get_id(),
'value' => 'value_2'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(1, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return 2 items
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $metadata2->get_id(),
'value' => 'value_3'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
// should return 2 item
2017-11-30 16:42:11 +00:00
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
2017-11-12 23:14:47 +00:00
[
'key' => $metadata3->get_id(),
'value' => 'value_2',
'compare' => '>'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(2, $test_query->post_count);
2017-11-12 23:14:47 +00:00
}
}