From 91095aade2b1ef2e346a27837243b742b8404575 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Wed, 21 Feb 2018 09:41:52 -0300 Subject: [PATCH] add unique ID for each importer created --- src/importer/class-tainacan-importer.php | 27 +++++++++++++++++++++--- tests/test-importer.php | 12 ++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index d8681ff23..82ddf0517 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -4,6 +4,7 @@ use Tainacan; abstract class Importer { + private $id; public $collection; public $mapping; public $tmp_file; @@ -14,6 +15,16 @@ abstract class Importer { if (!session_id()) { @session_start(); } + + $this->id = uniqid(); + $_SESSION['tainacan_importer'][$this->id] = $this; + } + + /** + * @return string + */ + public function get_id(){ + return $this->id; } /** @@ -21,7 +32,6 @@ abstract class Importer { */ public function set_collection( Tainacan\Entities\Collection $collection ){ $this->collection = $collection; - $_SESSION['tainacan_importer'] = $this; } /** @@ -29,7 +39,6 @@ abstract class Importer { */ public function set_mapping( $mapping ){ $this->mapping = $mapping; - $_SESSION['tainacan_importer'] = $this; } /** @@ -41,7 +50,6 @@ abstract class Importer { if ( is_numeric( $new_file ) ) { $attach = get_post($new_file); $this->tmp_file = $attach->guid; - $_SESSION['tainacan_importer'] = $this; } else { return false; } @@ -61,6 +69,19 @@ abstract class Importer { return media_handle_sideload( $file_array, 0 ); } + /** + * @param $url + */ + public function fetch_from_remote( $url ){ + $tmp = download_url( $url ); + $path_parts = pathinfo( $url ); + + $file_array['name'] = $path_parts['basename']; + $file_array['tmp_name'] = $tmp; + $file_array['size'] = filesize( $tmp ); + return media_handle_sideload( $file_array, 0 ); + } + /** * get the fields of file/url to allow mapping * should returns an array diff --git a/tests/test-importer.php b/tests/test-importer.php index c28c2ecdb..56ef04771 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -26,10 +26,11 @@ class ImporterTests extends TAINACAN_UnitTestCase { ); $csv_importer = new Importer\CSV(); - $csv_importer->set_collection( $collection ); + $id = $csv_importer->get_id(); + $_SESSION['tainacan_importer'][$id]->set_collection( $collection ); // here the session is init already - $this->assertEquals( $_SESSION['tainacan_importer'], $csv_importer ); + $this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() ); } /** @@ -37,6 +38,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { */ public function test_file_import_csv () { $csv_importer = new Importer\CSV(); + $id = $csv_importer->get_id(); // open the file "demosaved.csv" for writing $file = fopen('demosaved.csv', 'w'); @@ -44,7 +46,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { // save the column headers fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); - // Sample data. This can be fetched from mysql too + // Sample data $data = array( array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'), array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'), @@ -61,9 +63,9 @@ class ImporterTests extends TAINACAN_UnitTestCase { // Close the file fclose($file); - $csv_importer->set_file( 'demosaved.csv' ); + $_SESSION['tainacan_importer'][$id]->set_file( 'demosaved.csv' ); // here the session is init already - $this->assertEquals( $_SESSION['tainacan_importer']->tmp_file , $csv_importer->tmp_file ); + $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); } } \ No newline at end of file