echo import exceptions in csv hook test
[tine20] / tests / tine20 / Inventory / Import / CsvTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Inventory
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Michael Spahn <m.spahn@metaways.de>
9  */
10
11 /**
12  * Test helper
13  */
14 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
15
16 /**
17  * Test class for Inventory
18  */
19 class Inventory_Import_CsvTest extends PHPUnit_Framework_TestCase
20 {
21     /**
22      * @var Inventory_Import_Csv instance
23      */
24     protected $_instance = NULL;
25     
26     /**
27      * @var string $_filename
28      */
29     protected $_filename = NULL;
30     
31     /**
32      * @var boolean
33      */
34     protected $_deleteImportFile = TRUE;
35     
36     protected $_deletePersonalInventoryItems = FALSE;
37     
38     /**
39      * Runs the test methods of this class.
40      *
41      * @access public
42      * @static
43      */
44     public static function main()
45     {
46         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Inventory Csv Import Tests');
47         PHPUnit_TextUI_TestRunner::run($suite);
48     }
49
50     /**
51      * Sets up the fixture.
52      * This method is called before a test is executed.
53      *
54      * @access protected
55      */
56     protected function setUp()
57     {
58         Inventory_Controller_InventoryItem::getInstance()->resolveCustomfields(TRUE);
59     }
60
61     /**
62      * Tears down the fixture
63      * This method is called after a test is executed.
64      *
65      * @access protected
66      */
67     protected function tearDown()
68     {
69         // cleanup
70         if (file_exists($this->_filename) && $this->_deleteImportFile) {
71             unlink($this->_filename);
72         }
73         
74         if ($this->_deletePersonalInventoryItems) {
75             Inventory_Controller_InventoryItem::getInstance()->deleteByFilter(new Inventory_Model_InventoryItemFilter(array(array(
76                 'field' => 'container_id', 'operator' => 'equals', 'value' => Inventory_Controller_InventoryItem::getInstance()->getDefaultInventory()->getId()
77             ))));
78         }
79     }
80     
81     /**
82      * test import of a csv
83      */
84     public function testImportOfCSVWithHook ()
85     {
86         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv.xml';
87         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
88         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
89         
90         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import.csv';
91         $this->_deleteImportFile = FALSE;
92         
93         $result = $this->_doImport(array(), $definition);
94         $this->_deletePersonalInventoryItems = TRUE;
95         
96         // There are two test entries, so check for 3 imported entries because one is scripted in the postMappingHook :)
97         $this->assertEquals(3, $result['totalcount'], 'import exceptions: ' . print_r($result['exceptions']->toArray(), true));
98         
99         $translation = Tinebase_Translation::getTranslation('Tinebase');
100         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
101         
102         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
103         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2013-01-11 00:00:00');
104         $this->assertEquals($result['results'][0]['inventory_id'], '12345');
105         $this->assertContains($translatedString, $result['results'][0]['description']);
106         
107         $this->assertEquals($result['results'][1]['name'], 'Tine 2.0 für Tolle Leute - second mapping set');
108         $this->assertEquals($result['results'][1]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
109         $this->assertEquals($result['results'][1]['inventory_id'], '1333431646');
110         
111         $this->assertEquals($result['results'][2]['name'], 'Tine 2.0 für Profis');
112         $this->assertEquals($result['results'][2]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
113         $this->assertEquals($result['results'][2]['inventory_id'], '1333431666');
114         $this->assertContains($translatedString, $result['results'][2]['description']);
115     }
116     
117     /**
118      * Tests if import works without the _postMappingHook
119      */
120     public function testImportOfCSVWithoutHook ()
121     {
122         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
123         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
124         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
125         
126         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import.csv';
127         $this->_deleteImportFile = FALSE;
128         
129         $result = $this->_doImport(array(), $definition);
130         $this->_deletePersonalInventoryItems = TRUE;
131         
132         // There are two test entries, so check for 2 imported entries
133         $this->assertEquals(2, $result['totalcount']);
134         
135         $translation = Tinebase_Translation::getTranslation('Tinebase');
136         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
137         
138         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
139         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2013-01-11 00:00:00');
140         $this->assertNotEquals($result['results'][0]['inventory_id'], '');
141         $this->assertContains($translatedString, $result['results'][0]['description']);
142         
143         $this->assertEquals($result['results'][1]['name'], 'Tine 2.0 für Profis');
144         $this->assertEquals($result['results'][1]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
145         $this->assertEquals($result['results'][1]['inventory_id'], '1333431666');
146     }
147     
148      /**
149      * Test if different Datetime formats are correctly imported, if datetime_pattern is set
150      */
151     public function testImportOfDatetimeFormats ()
152     {
153         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
154         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
155         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
156         
157         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import_datetimes.csv';
158         $this->_deleteImportFile = FALSE;
159         
160         $result = $this->_doImport(array(), $definition);
161         $this->_deletePersonalInventoryItems = TRUE;
162         
163         $this->assertEquals('2013-12-31 00:00:00', $result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), 'Datetime and datetime_pattern should match');
164         $this->assertEquals('2013-12-31 00:00:00', $result['results'][0]['removed_date']->setTimezone('Europe/Berlin')->toString(), 'Datetime and datetime_pattern should match');
165         $this->assertNull($result['results'][1]['added_date'], 'Datetime and datetime_pattern do not match,  therefore should return null');
166         $this->assertNull( $result['results'][1]['added_date'], 'Datetime and datetime_pattern do not match,  therefore should return null');
167     }
168     
169     /**
170      * Tests if import of the example file works
171      */
172     public function testImportOfExampleFile ()
173     {
174         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
175         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
176         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
177         
178         $this->_filename = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/tine20/Inventory/Import/examples/inv_tine_import.csv'; 
179         $this->_deleteImportFile = FALSE;
180         
181         $result = $this->_doImport(array(), $definition);
182         $this->_deletePersonalInventoryItems = TRUE;
183         
184         $translation = Tinebase_Translation::getTranslation('Tinebase');
185         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
186         
187         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
188         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2014-08-27 00:00:00');
189         $this->assertEquals($result['results'][0]['inventory_id'], '133331666');
190         $this->assertContains($translatedString, $result['results'][0]['description']);
191     }
192     
193     /**
194      * import helper
195      *
196      * @param array $_options
197      * @param string|Tinebase_Model_ImportExportDefinition $_definition
198      * @param Inventory_Model_InventoryItemFilter $_exportFilter
199      * @return array
200      */
201     protected function _doImport(array $_options, $_definition, Inventory_Model_InventoryItemFilter $_exportFilter = NULL)
202     {
203         $definition = ($_definition instanceof Tinebase_Model_ImportExportDefinition) ? $_definition : Tinebase_ImportExportDefinition::getInstance()->getByName($_definition);
204         $this->_instance = Inventory_Import_Csv::createFromDefinition($definition, $_options);
205         
206         // export first
207         if ($_exportFilter !== NULL) {
208             $exporter = new Inventory_Export_Csv($_exportFilter, Inventory_Controller_InventoryItem::getInstance());
209             $this->_filename = $exporter->generate();
210         }
211         
212         // then import
213         $result = $this->_instance->importFile($this->_filename);
214         
215         return $result;
216     }
217 }
218