Clear tests #261

This commit is contained in:
Leo Germani 2019-09-13 19:25:42 -03:00
parent c3f97cf61b
commit 5dc058a787
8 changed files with 96 additions and 320 deletions

View File

@ -45,7 +45,7 @@ class Text extends Metadata_Type {
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value(); $value = $item_metadata->get_value();
$return = ''; $return = '';
if ( $item_metadata->is_multiple() ) { if ( is_array($value) && $item_metadata->is_multiple() ) {
$total = sizeof($value); $total = sizeof($value);
$count = 0; $count = 0;
$prefix = $item_metadata->get_multivalue_prefix(); $prefix = $item_metadata->get_multivalue_prefix();

View File

@ -386,11 +386,16 @@ class Logs extends Repository {
$creating = true; $creating = true;
$old = null;
if ( is_numeric( $unsaved->get_id() ) ) {
if ( $unsaved instanceof Entities\Term ) { if ( $unsaved instanceof Entities\Term ) {
$old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() );
} else { } else {
$old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); $old = $unsaved->get_repository()->fetch( $unsaved->get_id() );
} }
}
if ( $old instanceof Entities\Entity ) { if ( $old instanceof Entities\Entity ) {
@ -732,28 +737,4 @@ class Logs extends Repository {
} }
/**
*
* @param Entities\Log $log
*
* @return Entities\Entity|boolean return insert/update valeu or false
* @throws \Exception
*/
public function approve( $log ) {
$log = self::get_entity_by_post( $log );
if ( $log->get_status() == 'pending' ) {
/** @var Entity $value * */
$value = $log->get_value();
$value->set_status( 'publish' ); // TODO check if publish the entity on approve
$repository = self::get_repository( $value );
if ( $value->validate() ) {
return $repository->insert( $value );
}
}
return false;
}
} }

View File

@ -0,0 +1,59 @@
<?php
namespace Tainacan\Tests;
/**
* @group api_item_meta
*/
class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
protected $item;
protected $collection;
protected $metadatum;
protected function create_meta_requirements() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeItemMetadata',
'description' => 'No description',
),
true,
true
);
$type = $this->tainacan_metadatum_factory->create_metadatum('text');
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste_metadado',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => $type,
'accept_suggestion' => true
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_metadado',
'description' => 'adasdasdsa',
'collection' => $collection
),
true,
true
);
$this->collection = $collection;
$this->item = $item;
$this->metadatum = $metadatum;
return ['collection' => $collection, 'item' => $item, 'metadatum' => $metadatum];
}
}
?>

21
tests/_test-logs.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Entities\Collection;
use Tainacan\Entities\Log;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class Logs extends TAINACAN_UnitTestCase {
}

View File

@ -1,188 +0,0 @@
<?php
namespace Tainacan\Tests;
/**
* @group api_item_meta
*/
class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
protected $item;
protected $collection;
protected $metadatum;
protected function create_meta_requirements() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeItemMetadata',
'description' => 'No description',
),
true,
true
);
$type = $this->tainacan_metadatum_factory->create_metadatum('text');
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste_metadado',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => $type,
'accept_suggestion' => true
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_metadado',
'description' => 'adasdasdsa',
'collection' => $collection
),
true,
true
);
$this->collection = $collection;
$this->item = $item;
$this->metadatum = $metadatum;
return ['collection' => $collection, 'item' => $item, 'metadatum' => $metadatum];
}
public function test_create_suggestion_item_metadata_in_a_collection(){
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($item->get_id() , $data['item']['id']);
$this->assertEquals('TestValues_metadado', $data['value']);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'TestValues_metadado', $data['value'] );
// Test Suggestion
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
wp_set_current_user($new_user);
$item__metadata_json = json_encode([
'values' => 'TestValuesSuggestion_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'pending', $data['status'] );
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
$query = $Tainacan_Logs->fetch(['post_status' => 'pending']);
$log = false;
while ($query->have_posts()) {
$query->the_post();
$post = get_post();
$log = $Tainacan_Logs->get_entity_by_post($post);
}
$pending = $log->get_value();
$this->assertEquals('TestValuesSuggestion_metadado', $pending->get_value());
wp_set_current_user($this->user_id);
$request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'TestValuesSuggestion_metadado', $data['value'] );
}
function test_create_anonymous_suggestion_item_metadata_in_a_collection() {
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
// Test Anonymous Suggestion
wp_logout();
wp_set_current_user(0);
$this->assertEquals(0, get_current_user_id());
$item__metadata_json = json_encode([
'values' => 'TestValuesAnonymousSuggestion_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'pending', $data['status'] );
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
$query = $Tainacan_Logs->fetch(['post_status' => 'pending']);
$log = false;
while ($query->have_posts()) {
$query->the_post();
$post = get_post();
$log = $Tainacan_Logs->get_entity_by_post($post);
}
$pending = $log->get_value();
$this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->get_value());
wp_set_current_user($this->user_id);
$request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'TestValuesAnonymousSuggestion_metadado', $data['value'] );
}
}
?>

View File

@ -72,7 +72,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items']; $data = $response->get_data()['items'];
$items_titles = [$data[0]['title'], $data[1]['title']]; $items_titles = [$data[0]['title'], $data[1]['title']];
$this->assertContains($item1->get_title(), $items_titles); $this->assertContains($item1->get_title(), $items_titles);
@ -155,7 +154,11 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
} }
public function test_update_item(){ public function test_update_item(){
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', array(
'name' => 'Agile',
'description' => 'Agile methods',
'status' => 'publish'
), true);
$item = $this->tainacan_entity_factory->create_entity( $item = $this->tainacan_entity_factory->create_entity(
'item', 'item',
@ -163,6 +166,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
'title' => 'SCRUM e PMBOK', 'title' => 'SCRUM e PMBOK',
'description' => 'Unidos no Gerenciamento de Projetos', 'description' => 'Unidos no Gerenciamento de Projetos',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish'
), ),
true true
); );
@ -181,7 +185,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$data = $response->get_data(); $data = $response->get_data();
$this->assertNotEquals($item->get_title(), $data['title']); $this->assertNotEquals($item->get_title(), $data['title']);
$this->assertEquals('SCRUM e XP', $data['title']); $this->assertEquals('SCRUM e XP', $data['title']);
} }

View File

@ -318,34 +318,6 @@ class Collections extends TAINACAN_UnitTestCase {
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!'); $this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
} }
/**
* @group diff
*/
function test_diff() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeDiff',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'moderators_ids' => [1,2,3]
),
true
);
$x->set_name('OtherValue');
$x->set_description('testeDiff2');
$x->set_moderators_ids([3,4,5]);
$diff = $x->diff();
$this->assertEquals(3, count($diff));
$this->assertEquals($diff['name']['new'][0], 'OtherValue');
$this->assertEquals($diff['name']['old'], 'testeDiff');
$this->assertEquals($diff['description']['new'][0], 'testeDiff2');
$this->assertEquals($diff['description']['old'], 'adasdasdsa');
$this->assertEquals([1 => 4, 2 => 5, 0 => 3], $diff['moderators_ids']['diff_with_index']);
}
function test_create_child_collection() { function test_create_child_collection() {

View File

@ -1,72 +0,0 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Entities\Collection;
use Tainacan\Entities\Log;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class Logs extends TAINACAN_UnitTestCase {
/**
* Teste da insercao de um log simples apenas se criar o dado bruto
*/
function test_add() {
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
$log = $this->tainacan_entity_factory->create_entity(
'log',
array(
'title' => 'blame someone',
'description' => 'someone did that'
),
true
);
$user_id = get_current_user_id();
$blog_id = get_current_blog_id();
//retorna a taxonomia
$test = $Tainacan_Logs->fetch( $log->get_id() );
$this->assertEquals( 'blame someone', $test->get_title() );
$this->assertEquals( 'someone did that', $test->get_description() );
$this->assertEquals( $user_id, $test->get_user_id() );
$this->assertEquals( $blog_id, $test->get_blog_id() );
}
public function test_log_diff() {
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance();
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'No name',
),
true
);
// Modify filter name
$filter->set_name( 'With name' );
$Tainacan_Filters->update( $filter );
$log = $Tainacan_Logs->fetch_last();
$diff = $log->get_log_diffs();
$this->assertEquals( 'With name', "{$diff['name']['new'][0]} {$diff['name']['new'][1]}" );
$this->assertEquals( 'No name', $diff['name']['old'] );
$this->assertEquals( 'With', $diff['name']['diff_with_index'][0] );
}
}