2018-02-20 13:35:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
use Tainacan\Importer;
|
|
|
|
/**
|
|
|
|
* Class Importer
|
|
|
|
*
|
2018-06-01 21:37:02 +00:00
|
|
|
* @group importer
|
2018-02-20 13:35:43 +00:00
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ImporterTests extends TAINACAN_UnitTestCase {
|
|
|
|
|
2018-06-06 22:38:13 +00:00
|
|
|
/*public function test_intance_old_tainacan()
|
2018-04-03 18:41:07 +00:00
|
|
|
{
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'OtherOldTainacan',
|
|
|
|
'description' => 'Collection from old tainacan',
|
|
|
|
'default_order' => 'DESC',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$old_tainacan_importer = new Importer\Old_Tainacan();
|
|
|
|
$id = $old_tainacan_importer->get_id();
|
|
|
|
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
|
2018-06-06 22:38:13 +00:00
|
|
|
this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() );
|
2018-06-07 01:16:03 +00:00
|
|
|
}*/
|
2018-04-11 14:18:55 +00:00
|
|
|
|
2018-06-06 22:38:13 +00:00
|
|
|
public function test_automapping_old_tainacan()
|
2018-04-11 14:18:55 +00:00
|
|
|
{
|
2018-04-26 18:51:49 +00:00
|
|
|
//$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
2018-06-11 15:10:07 +00:00
|
|
|
//$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-11 14:18:55 +00:00
|
|
|
|
2018-04-26 18:51:49 +00:00
|
|
|
$old_tainacan = new Importer\Old_Tainacan();
|
2018-04-11 14:18:55 +00:00
|
|
|
$id = $old_tainacan->get_id();
|
|
|
|
|
2018-05-03 17:37:20 +00:00
|
|
|
//if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt'))
|
|
|
|
//{
|
|
|
|
//return false;
|
|
|
|
//}
|
2018-04-03 18:41:07 +00:00
|
|
|
|
2018-04-26 18:51:49 +00:00
|
|
|
//$_SESSION['tainacan_importer'][$id]->set_file( './tests/attachment/json_old_tainacan.txt' );
|
2018-06-07 01:16:03 +00:00
|
|
|
$url_repository = 'http://localhost/wordpress_tainacan/';
|
|
|
|
$url_repository = '';
|
|
|
|
if( $url_repository !== '' ){
|
2018-06-08 01:52:46 +00:00
|
|
|
$_SESSION['tainacan_importer'][$id]->set_url($url_repository);
|
2018-06-07 01:16:03 +00:00
|
|
|
|
|
|
|
while (!$_SESSION['tainacan_importer'][$id]->is_finished())
|
|
|
|
{
|
|
|
|
$_SESSION['tainacan_importer'][$id]->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
|
|
|
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
|
|
|
|
$this->assertEquals(3, $collections, 'total collection in this url does not match');
|
|
|
|
|
|
|
|
$this->assertTrue(true);
|
2018-05-04 16:26:18 +00:00
|
|
|
}
|
2018-06-07 01:16:03 +00:00
|
|
|
|
|
|
|
}
|
2018-04-11 14:18:55 +00:00
|
|
|
|
2018-04-23 17:43:41 +00:00
|
|
|
/*public function test_file_old_tainacan () {
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
2018-06-11 15:10:07 +00:00
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-03 18:41:07 +00:00
|
|
|
|
|
|
|
$old_tainacan = new Importer\Old_Tainacan();
|
|
|
|
$id = $old_tainacan->get_id();
|
|
|
|
|
|
|
|
$_SESSION['tainacan_importer'][$id]->set_items_per_step(2);
|
|
|
|
|
|
|
|
if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt'))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-17 17:34:02 +00:00
|
|
|
//$_SESSION['tainacan_importer'][$id]->set_file( './tests/attachment/json_old_tainacan.txt' );
|
2018-04-03 18:41:07 +00:00
|
|
|
|
|
|
|
//$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wp-json/tainacan/v1/collections/970/items' );
|
2018-04-17 17:34:02 +00:00
|
|
|
$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/colecao/colecao-to-import/' );
|
2018-04-03 18:41:07 +00:00
|
|
|
|
|
|
|
// file isset on importer
|
|
|
|
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
|
|
|
|
|
2018-04-17 17:34:02 +00:00
|
|
|
$_SESSION['tainacan_importer'][$id]->run();
|
2018-04-23 17:43:41 +00:00
|
|
|
|
2018-04-03 18:41:07 +00:00
|
|
|
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() );
|
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
// get metadata to mapping
|
|
|
|
$headers = $_SESSION['tainacan_importer'][$id]->get_metadata();
|
2018-04-03 18:41:07 +00:00
|
|
|
$this->assertEquals( $headers[5], 'post_title' );
|
|
|
|
|
|
|
|
// inserting the collection
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'OtherOldTainacan',
|
|
|
|
'description' => 'Collection from old tainacan',
|
|
|
|
'default_order' => 'DESC',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// set the importer
|
|
|
|
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
|
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
// get collection metadata to map
|
|
|
|
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ;
|
2018-04-03 18:41:07 +00:00
|
|
|
|
|
|
|
//create a random mapping
|
|
|
|
$map = [];
|
2018-06-11 15:10:07 +00:00
|
|
|
foreach ( $metadata as $index => $metadatum ){
|
|
|
|
$map[$metadatum->get_id()] = $headers[$index];
|
2018-04-03 18:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the mapping
|
|
|
|
$_SESSION['tainacan_importer'][$id]->set_mapping( $map );
|
|
|
|
|
|
|
|
// check is equal
|
|
|
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map );
|
|
|
|
|
|
|
|
//execute the process
|
|
|
|
|
|
|
|
$this->assertEquals(2, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 2 items');
|
|
|
|
$this->assertEquals(4, $_SESSION['tainacan_importer'][$id]->run(), 'second step should import 2 items');
|
|
|
|
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 1 item');
|
|
|
|
|
|
|
|
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
|
|
|
|
|
|
|
|
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
|
|
|
|
|
2018-04-23 17:43:41 +00:00
|
|
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );
|
|
|
|
}*/
|
2018-06-06 22:38:13 +00:00
|
|
|
|
2018-02-20 18:11:10 +00:00
|
|
|
/**
|
|
|
|
* @group importer
|
|
|
|
*/
|
2018-02-22 15:13:19 +00:00
|
|
|
public function test_file_csv () {
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
2018-06-11 15:10:07 +00:00
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-23 17:43:41 +00:00
|
|
|
$file_name = 'demosaved.csv';
|
2018-02-20 18:11:10 +00:00
|
|
|
$csv_importer = new Importer\CSV();
|
2018-02-21 12:41:52 +00:00
|
|
|
$id = $csv_importer->get_id();
|
2018-06-01 21:37:02 +00:00
|
|
|
|
2018-02-20 18:11:10 +00:00
|
|
|
// open the file "demosaved.csv" for writing
|
2018-04-23 17:43:41 +00:00
|
|
|
$file = fopen($file_name, 'w');
|
2018-02-20 18:11:10 +00:00
|
|
|
|
|
|
|
// save the column headers
|
|
|
|
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
|
|
|
|
|
2018-02-21 12:41:52 +00:00
|
|
|
// Sample data
|
2018-02-20 18:11:10 +00:00
|
|
|
$data = array(
|
|
|
|
array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
|
|
|
|
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
|
|
|
|
array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
|
|
|
|
array('Data 41', 'Data 42', 'Data 43', '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);
|
|
|
|
|
2018-06-06 22:38:13 +00:00
|
|
|
$_SESSION['tainacan_importer'][$id]->set_tmp_file( $file_name );
|
|
|
|
|
2018-02-22 15:13:19 +00:00
|
|
|
// file isset on importer
|
2018-06-06 22:38:13 +00:00
|
|
|
$this->assertTrue( !empty( $_SESSION['tainacan_importer'][$id]->get_tmp_file() ) );
|
2018-02-22 15:13:19 +00:00
|
|
|
|
|
|
|
// count size of csv
|
2018-06-06 22:38:13 +00:00
|
|
|
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_progress_total_from_source() );
|
2018-02-22 15:13:19 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
// get metadata to mapping
|
|
|
|
$headers = $_SESSION['tainacan_importer'][$id]->get_source_metadata();
|
2018-02-22 15:13:19 +00:00
|
|
|
$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
|
|
|
|
);
|
2018-06-06 22:38:13 +00:00
|
|
|
|
|
|
|
$collection_definition = [
|
|
|
|
'id' => $collection->get_id(),
|
|
|
|
'total_items' => $_SESSION['tainacan_importer'][$id]->get_progress_total_from_source(),
|
|
|
|
];
|
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
// get collection metadata to map
|
|
|
|
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ;
|
2018-02-22 15:13:19 +00:00
|
|
|
|
|
|
|
//create a random mapping
|
|
|
|
$map = [];
|
2018-06-11 15:10:07 +00:00
|
|
|
foreach ( $metadata as $index => $metadatum ){
|
|
|
|
$map[$metadatum->get_id()] = $headers[$index];
|
2018-02-22 15:13:19 +00:00
|
|
|
}
|
2018-06-06 22:38:13 +00:00
|
|
|
|
|
|
|
$collection_definition['map'] = $map;
|
2018-02-22 15:13:19 +00:00
|
|
|
|
2018-06-06 22:38:13 +00:00
|
|
|
// add the collection
|
|
|
|
$_SESSION['tainacan_importer'][$id]->add_collection( $collection_definition );
|
2018-02-22 15:13:19 +00:00
|
|
|
|
2018-02-23 19:20:16 +00:00
|
|
|
//execute the process
|
2018-06-06 22:38:13 +00:00
|
|
|
$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');
|
2018-02-23 19:20:16 +00:00
|
|
|
|
|
|
|
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
|
|
|
|
|
2018-06-06 22:38:13 +00:00
|
|
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_progress_total_from_source(), count( $items ) );
|
2018-02-20 18:11:10 +00:00
|
|
|
}
|
2018-02-21 16:38:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group importer
|
|
|
|
*/
|
2018-06-06 22:38:13 +00:00
|
|
|
/*
|
|
|
|
public function test_fetch_file(){
|
2018-02-21 16:38:45 +00:00
|
|
|
$csv_importer = new Importer\CSV();
|
|
|
|
$id = $csv_importer->get_id();
|
2018-05-03 13:53:07 +00:00
|
|
|
$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' );
|
2018-06-06 12:20:21 +00:00
|
|
|
|
|
|
|
if(!isset( $_SESSION['tainacan_importer'][$id]->tmp_file )){
|
|
|
|
#TODO: Remove dependence of web server (see fetch_from_remote)
|
|
|
|
$this->markTestSkipped('This test need a apache installation available.');
|
|
|
|
}
|
|
|
|
|
2018-05-03 13:53:07 +00:00
|
|
|
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
|
2018-02-21 16:38:45 +00:00
|
|
|
}
|
2018-06-06 22:38:13 +00:00
|
|
|
*/
|
2018-06-01 21:37:02 +00:00
|
|
|
}
|