From f3a413a2898eeb978a1548b0ca6f9f61cf4796be Mon Sep 17 00:00:00 2001 From: andre2ar Date: Tue, 24 Apr 2018 10:21:09 -0300 Subject: [PATCH] Importer fix --- .../repositories/class-tainacan-items.php | 3 +- src/importer/class-tainacan-importer.php | 13 +++--- tests/test-core-field-types.php | 45 +++++++++++++++++++ tests/test-importer.php | 8 ++-- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 5b03d9e74..9ac179af7 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -220,10 +220,9 @@ class Items extends Repository { $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 * public items and private items for users withe the correct permission. diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index fe6cee47a..64cea57e9 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -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 - $this->processed_items[ $index ] = $item->get_id(); - - if($item->validate()) { - $Tainacan_Items->update( $item ); + $this->processed_items[ $index ] = $insertedItem->get_id(); + if($insertedItem->validate()) { + $Tainacan_Items->update( $insertedItem ); } 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 $item; + return $insertedItem; } else { $this->add_log( 'error', 'Collection not set'); return false; diff --git a/tests/test-core-field-types.php b/tests/test-core-field-types.php index 2697261a5..8b29f73af 100644 --- a/tests/test-core-field-types.php +++ b/tests/test-core-field-types.php @@ -92,5 +92,50 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase { $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'); + + } } \ No newline at end of file diff --git a/tests/test-importer.php b/tests/test-importer.php index ac8f323d7..cdeca4e88 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -29,7 +29,7 @@ class ImporterTests extends TAINACAN_UnitTestCase { $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_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(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(), 'if call run again after finish, do nothing'); $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); $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(){ $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 ) ); + $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' ); + $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); } } \ No newline at end of file