2022-03-30 13:35:12 +00:00
< ? php
namespace Tainacan\Tests ;
/**
* @ group api
*/
2022-04-13 18:38:17 +00:00
class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCase {
2022-03-30 13:35:12 +00:00
2022-04-13 18:38:17 +00:00
public function test_create_empty_metadata_section () {
2022-03-30 13:35:12 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
$metadatum = json_encode (
array (
'name' => 'Dados Pessoais' ,
2022-03-31 19:15:50 +00:00
'description' => 'Informações e detalhes.' ,
'collection_id' => $collection -> get_id ()
2022-03-30 13:35:12 +00:00
)
);
$request = new \WP_REST_Request (
'POST' ,
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections'
);
$request -> set_body ( $metadatum );
$response = $this -> server -> dispatch ( $request );
2022-04-13 18:38:17 +00:00
$metadata_section_added = $response -> get_data ();
$this -> assertTrue ( is_array ( $metadata_section_added ) && array_key_exists ( 'name' , $metadata_section_added ), sprintf ( 'cannot create metadata section, response: %s' , print_r ( $metadata_section_added , true )));
$this -> assertEquals ( 'Dados Pessoais' , $metadata_section_added [ 'name' ]);
2022-05-12 04:31:17 +00:00
$this -> assertTrue ( empty ( $metadata_section_added [ 'metadata_object_list' ]));
2022-03-30 13:35:12 +00:00
}
2022-04-13 18:38:17 +00:00
public function test_create_fill_metadata_section () {
2022-03-30 13:35:12 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
$metadatum_1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-1' ,
'description' => 'description-1' ,
'collection' => $collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'status' => 'publish' ,
2022-03-30 13:35:12 +00:00
),
true
);
$metadatum_2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-2' ,
'description' => 'description-2' ,
'collection' => $collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'status' => 'publish' ,
2022-03-30 13:35:12 +00:00
),
true
);
2022-04-13 18:38:17 +00:00
$metadata_section = json_encode (
2022-03-30 13:35:12 +00:00
array (
'name' => 'Dados Pessoais' ,
'description' => 'Informações e detalhes.' ,
2022-03-31 19:15:50 +00:00
'collection_id' => $collection -> get_id (),
2022-03-30 13:35:12 +00:00
)
);
$request = new \WP_REST_Request (
'POST' ,
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections'
);
2022-04-13 18:38:17 +00:00
$request -> set_body ( $metadata_section );
2022-03-30 13:35:12 +00:00
$response = $this -> server -> dispatch ( $request );
2022-04-13 18:38:17 +00:00
$metadata_section_added = $response -> get_data ();
$this -> assertTrue ( is_array ( $metadata_section_added ) && array_key_exists ( 'name' , $metadata_section_added ), sprintf ( 'cannot create metadata section, response: %s' , print_r ( $metadata_section_added , true )));
$this -> assertEquals ( 'Dados Pessoais' , $metadata_section_added [ 'name' ]);
2022-05-12 04:31:17 +00:00
$metadata_list = json_encode (
array (
'metadata_list' => [ $metadatum_1 -> get_id (), $metadatum_2 -> get_id (), $metadatum_2 -> get_id ()]
)
);
$request = new \WP_REST_Request (
'POST' ,
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections/' . $metadata_section_added [ 'id' ] . '/metadata'
);
$request -> set_body ( $metadata_list );
$response = $this -> server -> dispatch ( $request );
$metadata_section = $response -> get_data ();
$this -> assertEquals ( 2 , count ( $metadata_section [ 'metadata_object_list' ]));
2022-03-30 13:35:12 +00:00
}
2022-04-13 18:38:17 +00:00
public function test_add_metadata_metadata_section () {
2022-03-30 13:35:12 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
2022-05-12 04:31:17 +00:00
$metadata_section = $this -> tainacan_entity_factory -> create_entity (
'Metadata_Section' ,
array (
'name' => 'Section' ,
'description' => 'Section Description' ,
'collection' => $collection ,
'status' => 'publish' ,
),
true
);
2022-03-30 13:35:12 +00:00
$metadatum_1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-1' ,
'description' => 'description-1' ,
'collection' => $collection ,
2022-04-11 12:42:27 +00:00
'status' => 'publish' ,
2022-03-30 13:35:12 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
$metadatum_2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-2' ,
'description' => 'description-2' ,
'collection' => $collection ,
2022-04-11 12:42:27 +00:00
'status' => 'publish' ,
2022-03-30 13:35:12 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
$metadatum_3 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-3' ,
'description' => 'description-3' ,
'collection' => $collection ,
2022-04-11 12:42:27 +00:00
'status' => 'publish' ,
2022-03-30 13:35:12 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
2022-04-13 18:38:17 +00:00
$metadata_list = json_encode (
2022-03-30 13:35:12 +00:00
array (
2022-04-13 18:38:17 +00:00
'metadata_list' => [ $metadatum_2 -> get_id (), $metadatum_3 -> get_id ()]
2022-03-30 13:35:12 +00:00
)
);
$request = new \WP_REST_Request (
'POST' ,
2022-04-13 18:38:17 +00:00
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections/' . $metadata_section -> get_id () . '/metadata'
2022-03-30 13:35:12 +00:00
);
2022-04-13 18:38:17 +00:00
$request -> set_body ( $metadata_list );
2022-03-30 13:35:12 +00:00
$response = $this -> server -> dispatch ( $request );
2022-04-13 18:38:17 +00:00
$metadata_section_added = $response -> get_data ();
2022-03-30 13:35:12 +00:00
2022-04-13 18:38:17 +00:00
$this -> assertTrue ( is_array ( $metadata_section_added ) && array_key_exists ( 'name' , $metadata_section_added ), sprintf ( 'cannot create metadata section, response: %s' , print_r ( $metadata_section_added , true )));
$this -> assertEquals ( 'Section' , $metadata_section_added [ 'name' ]);
2022-05-12 04:31:17 +00:00
$this -> assertEquals ( 3 , count ( $metadata_section_added [ 'metadata_object_list' ]));
$this -> assertContains ( $metadatum_1 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
$this -> assertContains ( $metadatum_2 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
$this -> assertContains ( $metadatum_3 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
2022-04-11 12:42:27 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Data' ,
'description' => 'Descreve o dado do campo data.' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
'multiple' => 'yes'
),
true
);
$values = json_encode ([
'name' => 'Dia/Mês/Ano' ,
'description' => 'Continua descrevendo o dado do campo.' ,
2022-04-13 18:38:17 +00:00
'metadata_section_id' => $metadata_section -> get_id ()
2022-04-11 12:42:27 +00:00
]);
$request = new \WP_REST_Request (
'PATCH' ,
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata/' . $metadatum -> get_id ()
);
$request -> set_body ( $values );
$response = $this -> server -> dispatch ( $request );
$data = $response -> get_data ();
$this -> assertEquals ( $metadatum -> get_id (), $data [ 'id' ]);
$this -> assertEquals ( 'Dia/Mês/Ano' , $data [ 'name' ]);
2022-04-13 18:38:17 +00:00
$this -> assertEquals ( $metadata_section -> get_id (), $data [ 'metadata_section_id' ]);
2022-04-11 12:42:27 +00:00
$request = new \WP_REST_Request (
'GET' ,
2022-04-13 18:38:17 +00:00
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections/' . $metadata_section -> get_id () . '/metadata'
2022-04-11 12:42:27 +00:00
);
$response = $this -> server -> dispatch ( $request );
$metadata_list = $response -> get_data ();
$this -> assertEquals ( 4 , count ( $metadata_list ));
$this -> assertNotNull ( array_search ( $metadatum_1 -> get_id (), array_column ( $metadata_list , " id " )));
$this -> assertNotNull ( array_search ( $metadatum_2 -> get_id (), array_column ( $metadata_list , " id " )));
$this -> assertNotNull ( array_search ( $metadatum_3 -> get_id (), array_column ( $metadata_list , " id " )));
$this -> assertNotNull ( array_search ( $metadatum -> get_id (), array_column ( $metadata_list , " id " )));
2022-03-30 13:35:12 +00:00
}
2022-04-13 18:38:17 +00:00
public function test_delete_metadata_metadata_section () {
2022-03-30 13:35:12 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
2022-05-12 04:31:17 +00:00
$metadata_section = $this -> tainacan_entity_factory -> create_entity (
'Metadata_Section' ,
array (
'name' => 'Section' ,
'description' => 'Section Description' ,
'collection' => $collection ,
),
true
);
2022-03-30 13:35:12 +00:00
$metadatum_1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-1' ,
'description' => 'description-1' ,
'collection' => $collection ,
2022-05-12 04:31:17 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
'status' => 'publish' ,
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
$metadatum_2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-2' ,
'description' => 'description-2' ,
'collection' => $collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'status' => 'publish' ,
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
$metadatum_3 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-3' ,
'description' => 'description-3' ,
'collection' => $collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'status' => 'publish' ,
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
2022-04-13 18:38:17 +00:00
$metadata_list = json_encode (
2022-03-30 13:35:12 +00:00
array (
2022-04-13 18:38:17 +00:00
'metadata_list' => [ $metadatum_1 -> get_id (), $metadatum_3 -> get_id ()]
2022-03-30 13:35:12 +00:00
)
);
$request = new \WP_REST_Request (
'DELETE' ,
2022-04-13 18:38:17 +00:00
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections/' . $metadata_section -> get_id () . '/metadata'
2022-03-30 13:35:12 +00:00
);
2022-04-13 18:38:17 +00:00
$request -> set_body ( $metadata_list );
2022-03-30 13:35:12 +00:00
$response = $this -> server -> dispatch ( $request );
2022-04-13 18:38:17 +00:00
$metadata_section_added = $response -> get_data ();
2022-03-30 13:35:12 +00:00
2022-04-13 18:38:17 +00:00
$this -> assertTrue ( is_array ( $metadata_section_added ) && array_key_exists ( 'name' , $metadata_section_added ), sprintf ( 'cannot create metadata section, response: %s' , print_r ( $metadata_section_added , true )));
$this -> assertEquals ( 'Section' , $metadata_section_added [ 'name' ]);
2022-05-12 04:31:17 +00:00
$this -> assertEquals ( 1 , count ( $metadata_section_added [ 'metadata_object_list' ]));
$this -> assertNotContains ( $metadatum_1 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
$this -> assertContains ( $metadatum_2 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
$this -> assertNotContains ( $metadatum_3 -> get_id (), array_column ( $metadata_section_added [ 'metadata_object_list' ], 'id' ));
2022-03-30 13:35:12 +00:00
}
2022-04-13 18:38:17 +00:00
public function test_get_metadata_metadata_section () {
2022-03-30 13:35:12 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
2022-05-12 04:31:17 +00:00
$metadata_section = $this -> tainacan_entity_factory -> create_entity (
'Metadata_Section' ,
array (
'name' => 'Section' ,
'description' => 'Section Description' ,
'collection' => $collection
),
true
);
$this -> tainacan_entity_factory -> create_entity (
2022-03-30 13:35:12 +00:00
'metadatum' ,
array (
'name' => 'name-1' ,
'description' => 'description-1' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
2022-05-12 04:31:17 +00:00
$this -> tainacan_entity_factory -> create_entity (
2022-03-30 13:35:12 +00:00
'metadatum' ,
array (
'name' => 'name-2' ,
'description' => 'description-2' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
2022-05-12 04:31:17 +00:00
$this -> tainacan_entity_factory -> create_entity (
2022-03-30 13:35:12 +00:00
'metadatum' ,
array (
'name' => 'name-3' ,
'description' => 'description-3' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $metadata_section -> get_id ()
2022-03-30 13:35:12 +00:00
),
true
);
$request = new \WP_REST_Request (
'GET' ,
2022-04-13 18:38:17 +00:00
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections/' . $metadata_section -> get_id () . '/metadata'
2022-03-30 13:35:12 +00:00
);
$response = $this -> server -> dispatch ( $request );
$metadata_list = $response -> get_data ();
$this -> assertEquals ( 3 , count ( $metadata_list ));
}
2022-04-13 18:38:17 +00:00
public function test_get_metadata_section () {
2022-03-31 19:15:50 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity ( 'collection' , '' , true );
2022-03-30 13:35:12 +00:00
2022-05-12 04:31:17 +00:00
$section1 = $this -> tainacan_entity_factory -> create_entity (
'Metadata_Section' ,
array (
'name' => 'Section' ,
'description' => 'Section Description' ,
'collection' => $collection ,
// 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id()]
),
true ,
true
);
$section2 = $this -> tainacan_entity_factory -> create_entity (
'Metadata_Section' ,
array (
'name' => 'Section' ,
'description' => 'Section Description' ,
'collection' => $collection ,
// 'metadata_list' => [$metadatum_3->get_id(), $metadatum_4->get_id()]
),
true ,
true
);
2022-03-31 19:15:50 +00:00
$metadatum_1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-1' ,
'description' => 'description-1' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $section1 -> get_id ()
2022-03-31 19:15:50 +00:00
),
true
);
2022-03-30 13:35:12 +00:00
2022-03-31 19:15:50 +00:00
$metadatum_2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-2' ,
'description' => 'description-2' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $section1 -> get_id ()
2022-03-31 19:15:50 +00:00
),
true
);
2022-03-30 13:35:12 +00:00
2022-03-31 19:15:50 +00:00
$metadatum_3 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-3' ,
'description' => 'description-3' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $section2 -> get_id ()
2022-03-31 19:15:50 +00:00
),
true
);
2022-03-30 13:35:12 +00:00
2022-03-31 19:15:50 +00:00
$metadatum_4 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'name-4' ,
'description' => 'description-4' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2022-05-12 04:31:17 +00:00
'metadata_section_id' => $section2 -> get_id ()
2022-03-31 19:15:50 +00:00
),
true
);
2022-03-30 13:35:12 +00:00
2022-03-31 19:15:50 +00:00
$request = new \WP_REST_Request (
'GET' ,
$this -> namespace . '/collection/' . $collection -> get_id () . '/metadata-sections'
);
$response = $this -> server -> dispatch ( $request );
$response_data = $response -> get_data ();
2022-03-30 13:35:12 +00:00
2022-05-12 19:36:21 +00:00
$this -> assertEquals ( 3 , count ( $response_data ));
2022-05-12 04:31:17 +00:00
$metadata_list_1 = $response_data [ 0 ][ 'metadata_object_list' ];
$metadata_list_2 = $response_data [ 1 ][ 'metadata_object_list' ];
2022-05-12 19:27:05 +00:00
$this -> assertContains ( $metadatum_1 -> get_id (), array_column ( $metadata_list_1 , 'id' ), sprintf ( 'Id: %s not found in array: %s' , $metadatum_1 -> get_id (), print_r ( array_column ( $metadata_list_1 , 'id' ), true )));
$this -> assertContains ( $metadatum_2 -> get_id (), array_column ( $metadata_list_1 , 'id' ), sprintf ( 'Id: %s not found in array: %s' , $metadatum_2 -> get_id (), print_r ( array_column ( $metadata_list_1 , 'id' ), true )));
$this -> assertContains ( $metadatum_3 -> get_id (), array_column ( $metadata_list_2 , 'id' ), sprintf ( 'Id: %s not found in array: %s' , $metadatum_3 -> get_id (), print_r ( array_column ( $metadata_list_2 , 'id' ), true )));
$this -> assertContains ( $metadatum_4 -> get_id (), array_column ( $metadata_list_2 , 'id' ), sprintf ( 'Id: %s not found in array: %s' , $metadatum_4 -> get_id (), print_r ( array_column ( $metadata_list_2 , 'id' ), true )));
2022-03-31 19:15:50 +00:00
}
2022-03-30 13:35:12 +00:00
}