create fetch form remote in importer

This commit is contained in:
Eduardo humberto 2018-02-21 13:38:45 -03:00
parent fe215d7116
commit 8dd729092c
2 changed files with 20 additions and 7 deletions

View File

@ -70,16 +70,19 @@ abstract class Importer {
}
/**
* get the content form url and creates a file
*
* @param $url
* @return array
*/
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 );
$tmp = wp_remote_get( $url );
if( isset( $tmp['body'] ) ){
$file = fopen( $this->get_id().'.txt', 'w' );
fwrite( $file, $tmp['body'] );
fclose( $file );
return $this->set_file( $this->get_id().'.txt' );
}
}
/**

View File

@ -68,4 +68,14 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// here the session is init already
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
}
/**
* @group importer
*/
public function test_fetch_file(){
$csv_importer = new Importer\CSV();
$id = $csv_importer->get_id();
$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' );
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
}
}