create set mapping and its tests
This commit is contained in:
parent
b3a6a9222a
commit
a731fe21f8
|
@ -5,17 +5,31 @@ use Tainacan;
|
|||
|
||||
class CSV extends Importer {
|
||||
|
||||
public $delimiter;
|
||||
public $delimiter = ',';
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string $delimiter value that divides each column
|
||||
*/
|
||||
public function get_delimiter(){
|
||||
return $this->delimiter;
|
||||
}
|
||||
|
||||
|
||||
public function set_delimiter( $delimiter ){
|
||||
$this->delimiter = $delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function get_fields_source(){
|
||||
// TODO: Implement get_fields_source() method.
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek(0 );
|
||||
return $file->fgetcsv( $this->get_delimiter() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,4 +45,16 @@ class CSV extends Importer {
|
|||
public function get_options(){
|
||||
// TODO: Implement get_options() method.
|
||||
}
|
||||
|
||||
|
||||
public function get_total_items(){
|
||||
if( isset( $this->total_items ) ){
|
||||
return $this->total_items;
|
||||
} else {
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek(PHP_INT_MAX);
|
||||
// -1 removing header
|
||||
return $this->total_items = $file->key() - 1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,6 +27,14 @@ abstract class Importer {
|
|||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_mapping(){
|
||||
return $this->mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tainacan\Entities\Collection $collection
|
||||
*/
|
||||
|
@ -35,6 +43,8 @@ abstract class Importer {
|
|||
}
|
||||
|
||||
/**
|
||||
* save an associative array with tainacan field id and field from source
|
||||
*
|
||||
* @param array $mapping Mapping importer-fields
|
||||
*/
|
||||
public function set_mapping( $mapping ){
|
||||
|
@ -48,8 +58,7 @@ abstract class Importer {
|
|||
public function set_file( $file ){
|
||||
$new_file = $this->upload_file( $file );
|
||||
if ( is_numeric( $new_file ) ) {
|
||||
$attach = get_post($new_file);
|
||||
$this->tmp_file = $attach->guid;
|
||||
$this->tmp_file = get_attached_file( $new_file );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -85,9 +94,18 @@ abstract class Importer {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_collection_fields(){
|
||||
return $this->collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the fields of file/url to allow mapping
|
||||
* should returns an array
|
||||
*
|
||||
* @return array $fields_source the fields from the source
|
||||
*/
|
||||
abstract public function get_fields_source();
|
||||
|
||||
|
@ -101,6 +119,13 @@ abstract class Importer {
|
|||
*/
|
||||
abstract public function get_options();
|
||||
|
||||
/**
|
||||
* return the all items found
|
||||
*
|
||||
* @return int Total of items
|
||||
*/
|
||||
abstract public function get_total_items();
|
||||
|
||||
/**
|
||||
* @param $start
|
||||
* @param $end
|
||||
|
|
|
@ -36,7 +36,9 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
|||
/**
|
||||
* @group importer
|
||||
*/
|
||||
public function test_file_import_csv () {
|
||||
public function test_file_csv () {
|
||||
global $Tainacan_Fields;
|
||||
|
||||
$csv_importer = new Importer\CSV();
|
||||
$id = $csv_importer->get_id();
|
||||
|
||||
|
@ -65,8 +67,46 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
|||
|
||||
$_SESSION['tainacan_importer'][$id]->set_file( 'demosaved.csv' );
|
||||
|
||||
// here the session is init already
|
||||
// file isset on importer
|
||||
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
|
||||
|
||||
// count size of csv
|
||||
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() );
|
||||
|
||||
// get fields to mapping
|
||||
$headers = $_SESSION['tainacan_importer'][$id]->get_fields_source();
|
||||
$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
|
||||
);
|
||||
|
||||
// set the importer
|
||||
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
|
||||
|
||||
// get collection fields to map
|
||||
$fields = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ) ;
|
||||
|
||||
//create a random mapping
|
||||
$map = [];
|
||||
foreach ( $fields as $index => $field ){
|
||||
$map[$field->get_id()] = $headers[$index];
|
||||
}
|
||||
|
||||
// set the mapping
|
||||
$_SESSION['tainacan_importer'][$id]->set_mapping( $map );
|
||||
|
||||
// check is equal
|
||||
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue