Importer fix

This commit is contained in:
andre2ar 2018-04-24 10:21:09 -03:00
parent 8d1e5fa7d9
commit f3a413a289
4 changed files with 55 additions and 14 deletions

View File

@ -220,10 +220,9 @@ class Items extends Repository {
$collections_objects[] = $col; $collections_objects[] = $col;
} }
} }
} }
foreach ( $collections_objects as $collection ) {
foreach ( $collections_objects as $collection ) {
/** /**
* If no specific status is defined in the query, WordPress will fetch * If no specific status is defined in the query, WordPress will fetch
* public items and private items for users withe the correct permission. * public items and private items for users withe the correct permission.

View File

@ -401,19 +401,18 @@ abstract class Importer {
} }
} }
$item->set_status('publish' ); $insertedItem->set_status('publish' );
// inserted the id on processed item with its index as array index // inserted the id on processed item with its index as array index
$this->processed_items[ $index ] = $item->get_id(); $this->processed_items[ $index ] = $insertedItem->get_id();
if($insertedItem->validate()) {
if($item->validate()) { $Tainacan_Items->update( $insertedItem );
$Tainacan_Items->update( $item );
} else { } else {
$this->add_log( 'error', 'Item ' . $index . ': ' ); // TODO add the $item->get_errors() array $this->add_log( 'error', 'Item ' . $index . ': ' . $insertedItem->get_errors()[0] ); // TODO add the $item->get_errors() array
return false; return false;
} }
return $item; return $insertedItem;
} else { } else {
$this->add_log( 'error', 'Collection not set'); $this->add_log( 'error', 'Collection not set');
return false; return false;

View File

@ -92,5 +92,50 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
$this->assertEquals('changed description', $check_item_metadata->get_value()); $this->assertEquals('changed description', $check_item_metadata->get_value());
} }
function test_validate_required_title() {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'description' => 'adasdasdsa',
'collection' => $collection,
'status' => 'draft'
),
true
);
$fields = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ) ;
foreach ( $fields as $index => $field ){
if ( $field->get_field_type_object()->get_core() && $field->get_field_type_object()->get_related_mapped_prop() == 'title') {
$core_title = $field;
}
}
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $core_title);
$item_metadata->set_value('title');
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
$i->set_status('publish' );
$this->assertTrue($i->validate(), 'Item with empy title should validate because core title field has value');
}
} }

View File

@ -29,7 +29,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() ); $this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() );
} }
/*public function test_automapping_old_tainacan() /* public function test_automapping_old_tainacan()
{ {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance(); $Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
@ -228,13 +228,11 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertEquals(2, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 2 items'); $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(4, $_SESSION['tainacan_importer'][$id]->run(), 'second step should import 2 items');
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 3 items'); $this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 3 items');
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing'); $this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) ); $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );
} }
/** /**
@ -243,7 +241,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
public function test_fetch_file(){ public function test_fetch_file(){
$csv_importer = new Importer\CSV(); $csv_importer = new Importer\CSV();
$id = $csv_importer->get_id(); $id = $csv_importer->get_id();
//$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' ); $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' );
//$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
} }
} }