feat: add test metada section tests #184

This commit is contained in:
vnmedeiros 2022-05-03 15:46:23 -03:00
parent 69261bfe85
commit d837f1eec8
3 changed files with 252 additions and 15 deletions

View File

@ -382,7 +382,7 @@ class REST_Collections_Controller extends REST_Controller {
* @return bool|\WP_Error
* @throws \Exception
*/
public function get_item_permissions_check($request){
public function get_item_permissions_check($request){
$collection = $this->collections_repository->fetch($request['collection_id']);
if(($collection instanceof Entities\Collection)) {
@ -641,19 +641,25 @@ class REST_Collections_Controller extends REST_Controller {
if( $collection instanceof Entities\Collection) {
$metadata_section_order = $collection->get_metadata_section_order();
foreach($metadata_section_order['metadata_section_order'] as $section) {
if ($section['id'] == $metadata_section_id) {
$section['metadata_order'] = $body['metadata_order'];
break;
}
if( !isset( $metadata_section_order ) || !is_array($metadata_section_order) ) {
$metadata_section_order = array();
}
$section_order_index = array_search( $metadata_section_id, array_column( $metadata_section_order, 'id' ) );
if ( $section_order_index !== false ) {
$metadata_section_order[$section_order_index]['metadata_order'] = $body['metadata_order'];
} else {
$metadata_section_order[] = array(
'id' => $metadata_section_id,
'metadata_order' => $body['metadata_order'],
'enabled' => true
);
}
$collection->set_metadata_section_order( $metadata_section_order );
if ( $collection->validate() ) {
$updated_collection = $this->collections_repository->update( $collection );
$response = $this->prepare_item_for_response($updated_collection, $request);
return new \WP_REST_Response( $response, 200 );
}

View File

@ -739,14 +739,11 @@ class Collection extends Entity {
* @return void
*/
function set_metadata_section_order( $value ) {
if ( isset($value['metadata_section_order']) ) {
$metadata_section_order = $value['metadata_section_order'];
$metadata_order = array('metadata_order' => array( ) );
foreach($metadata_section_order as $section) {
$metadata_order['metadata_order'] = $section['metadata_order'];
}
$this->set_metadata_order($metadata_order);
$metadata_order = array( );
foreach($value as $section) {
$metadata_order = array_merge($metadata_order, $section['metadata_order']);
}
$this->set_metadata_order($metadata_order);
$this->set_mapped_property( 'metadata_section_order', $value );
}

View File

@ -0,0 +1,234 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Metadata_Types;
/**
* Class Metadatum
*
* @package Test_Tainacan
*/
/**
* Metadatum test case.
* @group metadata
*/
class MetadataSection extends TAINACAN_UnitTestCase {
/**
* Test insert a regular metadatum with type
*/
function test_add() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Metadata_Section = \Tainacan\Repositories\Metadata_Sections::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'accept_suggestion' => true,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section->get_id(),
),
true
);
$test = $Tainacan_Metadata->fetch($metadatum->get_id());
$section = $Tainacan_Metadata_Section->fetch($metadata_section->get_id());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
$this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertEquals($test->get_metadata_section_id(), $metadata_section->get_id());
$metadata_list = $metadata_section->get_metadata_list();
$this->assertEquals(count($metadata_list), 1);
$this->assertEquals($test->get_id(), $metadata_list[0]);
$this->assertTrue((bool) $test->get_accept_suggestion());
}
// function test_remove() {
// return;
// }
// function test_change_section() {
// return;
// }
/**
*
*/
function test_ordenation_metadata(){
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum1',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum2',
'description' => 'metadatum2',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum3',
'description' => 'metadatum3',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_a = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_a',
'description' => 'descricao_a',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_b = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_b',
'description' => 'metadatum_b',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_c = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_c',
'description' => 'metadatum_c',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadata_section_1 = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section 1',
'description' => 'Section 1 Description',
'collection' => $collection,
'status' => 'publish',
'metadata_list' => [$metadatum1->get_id(), $metadatum3->get_id(), $metadatum2->get_id()]
),
true
);
$metadata_section_a = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section A',
'description' => 'Section A Description',
'collection' => $collection,
'status' => 'publish',
'metadata_list' => [$metadatum_a->get_id(), $metadatum_b->get_id(), $metadatum_c->get_id()]
),
true
);
$collection->set_metadata_order(
[
array( 'id' => $metadatum3->get_id(), 'enabled' => false ),
array( 'id' => $metadatum2->get_id(), 'enabled' => true ),
array( 'id' => $metadatum1->get_id(), 'enabled' => true )
]
);
$update_collection = $Tainacan_Collections->update( $collection );
$metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() );
$this->assertFalse($metadata_ordinate_enabled[0]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[1]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection());
$collection->set_metadata_section_order(
[
array( 'id' => $metadata_section_1->get_id(), 'enabled' => true, 'metadata_order' => [
array( 'id' => $metadatum2->get_id(), 'enabled' => true ),
array( 'id' => $metadatum3->get_id(), 'enabled' => false ),
array( 'id' => $metadatum1->get_id(), 'enabled' => true )
] ),
array( 'id' => $metadata_section_a->get_id(), 'enabled' => false, 'metadata_order' => [
array( 'id' => $metadatum_c->get_id(), 'enabled' => false ),
array( 'id' => $metadatum_b->get_id(), 'enabled' => true ),
array( 'id' => $metadatum_a->get_id(), 'enabled' => true )
])
]
);
$update_collection = $Tainacan_Collections->update( $collection );
$metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[1]->get_name() );
$this->assertTrue($metadata_ordinate_enabled[0]->get_enabled_for_collection());
$this->assertFalse($metadata_ordinate_enabled[1]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection());
}
}