create test for multivalued metadata in csv importer

This commit is contained in:
eduardohumberto 2018-07-03 22:47:41 -03:00
parent c2d4912877
commit ffda7b4f53
2 changed files with 134 additions and 5 deletions

View File

@ -14,7 +14,9 @@ class CSV extends Importer {
$this->set_default_options([
'delimiter' => ',',
'multivalued_delimiter' => '||'
'multivalued_delimiter' => '||',
'encode' => 'UTF-8',
'cell_encapsulate' => '"'
]);
}
@ -52,8 +54,14 @@ class CSV extends Importer {
return false;
}
foreach ($headers as $index => $header) {
$processedItem[ $header ] = $values[ $index ];
$cont = 0;
foreach ( $collection_definition['mapping'] as $metadatum_id => $header) {
$metadatum = new \Tainacan\Entities\Metadatum($metadatum_id);
$processedItem[ $header ] = ( $metadatum->get_multiple() ) ?
explode( $this->get_option('multivalued_delimiter'), $values[ $cont ]) : $values[ $cont ];
$cont++;
}
return $processedItem;
@ -81,6 +89,12 @@ class CSV extends Importer {
$form .= '<label class="label">' . __('Multivalued metadata delimiter', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="multivalued_delimiter" value="' . $this->get_option('multivalued_delimiter') . '" />';
$form .= '<label class="label">' . __('Encoding', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="encode" value="' . $this->get_option('encode') . '" />';
$form .= '<label class="label">' . __('Cell Encapsulate', 'tainacan') . '</label>';
$form .= '<input type="text" class="input" name="cell_encapsulate" value="' . $this->get_option('cell_encapsulate') . '" />';
return $form;
}

View File

@ -242,4 +242,119 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
}
*/
/**
* @group importer
*/
public function test_file_csv_multiple () {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$file_name = 'demosaved.csv';
$csv_importer = new Importer\CSV();
$id = $csv_importer->get_id();
// open the file "demosaved.csv" for writing
$file = fopen($file_name, 'w');
// save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
// Sample data
$data = array(
array('Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15'),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
array('Data 31', 'Data 32', 'Data 33||ROUPA', 'Data 34', 'Data 35'),
array('Data 41', 'Data 42', 'Data 43||limbbo', 'Data 44', 'Data 45'),
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
);
// save each row of the data
foreach ($data as $row){
fputcsv($file, $row);
}
// Close the file
fclose($file);
$_SESSION['tainacan_importer'][$id]->set_tmp_file( $file_name );
// file isset on importer
$this->assertTrue( !empty( $_SESSION['tainacan_importer'][$id]->get_tmp_file() ) );
// count size of csv
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_source_number_of_items() );
// get metadata to mapping
$headers = $_SESSION['tainacan_importer'][$id]->get_source_metadata();
$this->assertEquals( $headers[4], 'Column 5' );
// inserting the collection
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Other',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Data multiplo',
'description' => 'Descreve o dado do campo data.',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'multiple' => 'yes'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Texto simples',
'description' => 'Descreve o dado do campo data.',
'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'multiple' => 'no'
),
true
);
$collection_definition = [
'id' => $collection->get_id(),
'total_items' => $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(),
];
// get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ;
//create a random mapping
$map = [];
foreach ( $metadata as $index => $metadatum ){
$map[$metadatum->get_id()] = $headers[$index];
}
$collection_definition['mapping'] = $map;
// add the collection
$_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition );
//execute the process
$this->assertEquals(1, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 1 item');
$this->assertEquals(2, $_SESSION['tainacan_importer'][$id]->run(), 'second step should import 2 items');
$this->assertEquals(3, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 3 items');
$this->assertEquals(4, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 4 items');
$this->assertEquals(false, $_SESSION['tainacan_importer'][$id]->run(), '5 items and return false because its finished');
$this->assertEquals(false, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) );
}
}