Merge branch '2013.03'
[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         $this->markTestSkipped('@see 0008240: Inventory_Import_CsvTest::testImportOfCSVWithHook fails sometimes');
87         
88         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv.xml';
89         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
90         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
91         
92         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import.csv';
93         $this->_deleteImportFile = FALSE;
94         
95         $result = $this->_doImport(array(), $definition);
96         $this->_deletePersonalInventoryItems = TRUE;
97         
98         // There are two test entries, so check for 3 imported entries because one is scripted in the postMappingHook :)
99         $this->assertEquals(3, $result['totalcount']);
100         
101         $translation = Tinebase_Translation::getTranslation('Tinebase');
102         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
103         
104         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
105         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2013-01-11 00:00:00');
106         $this->assertEquals($result['results'][0]['inventory_id'], '12345');
107         $this->assertContains($translatedString, $result['results'][0]['description']);
108         
109         $this->assertEquals($result['results'][1]['name'], 'Tine 2.0 für Tolle Leute - second mapping set');
110         $this->assertEquals($result['results'][1]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
111         $this->assertEquals($result['results'][1]['inventory_id'], '1333431646');
112         
113         $this->assertEquals($result['results'][2]['name'], 'Tine 2.0 für Profis');
114         $this->assertEquals($result['results'][2]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
115         $this->assertEquals($result['results'][2]['inventory_id'], '1333431666');
116         $this->assertContains($translatedString, $result['results'][2]['description']);
117         
118     }
119     
120     /**
121      * Tests if import works without the _postMappingHook
122      */
123     public function testImportOfCSVWithoutHook ()
124     {
125         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
126         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
127         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
128         
129         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import.csv';
130         $this->_deleteImportFile = FALSE;
131         
132         $result = $this->_doImport(array(), $definition);
133         $this->_deletePersonalInventoryItems = TRUE;
134         
135         // There are two test entries, so check for 2 imported entries
136         $this->assertEquals(2, $result['totalcount']);
137         
138         $translation = Tinebase_Translation::getTranslation('Tinebase');
139         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
140         
141         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
142         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2013-01-11 00:00:00');
143         $this->assertEquals($result['results'][0]['inventory_id'], '');
144         $this->assertContains($translatedString, $result['results'][0]['description']);
145         
146         $this->assertEquals($result['results'][1]['name'], 'Tine 2.0 für Profis');
147         $this->assertEquals($result['results'][1]['added_date']->setTimezone('Europe/Berlin')->toString(), '2012-01-11 00:00:00');
148         $this->assertEquals($result['results'][1]['inventory_id'], '1333431666');
149     }
150     
151      /**
152      * Test if different Datetime formats are correctly imported, if datetime_pattern is set
153      */
154     public function testImportOfDatetimeFormats ()
155     {
156         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
157         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
158         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
159         
160         $this->_filename = dirname(__FILE__) . '/files/inv_tine_import_datetimes.csv';
161         $this->_deleteImportFile = FALSE;
162         
163         $result = $this->_doImport(array(), $definition);
164         $this->_deletePersonalInventoryItems = TRUE;
165         
166         $this->assertEquals('2013-12-31 00:00:00', $result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), 'Datetime and datetime_pattern should match');
167         $this->assertEquals('2013-12-31 00:00:00', $result['results'][0]['removed_date']->setTimezone('Europe/Berlin')->toString(), 'Datetime and datetime_pattern should match');
168         $this->assertNull($result['results'][1]['added_date'], 'Datetime and datetime_pattern do not match,  therefore should return null');
169         $this->assertNull( $result['results'][1]['added_date'], 'Datetime and datetime_pattern do not match,  therefore should return null');
170     }
171     
172     /**
173      * Tests if import of the example file works
174      */
175     public function testImportOfExampleFile ()
176     {
177         $filename = dirname(__FILE__) . '/files/inv_tine_import_csv_nohook.xml';
178         $applicationId = Tinebase_Application::getInstance()->getApplicationByName('Inventory')->getId();
179         $definition = Tinebase_ImportExportDefinition::getInstance()->getFromFile($filename, $applicationId);
180         
181         $this->_filename = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/tine20/Inventory/Import/examples/inv_tine_import.csv'; 
182         $this->_deleteImportFile = FALSE;
183         
184         $result = $this->_doImport(array(), $definition);
185         $this->_deletePersonalInventoryItems = TRUE;
186         
187         $translation = Tinebase_Translation::getTranslation('Tinebase');
188         $translatedString = sprintf($translation->_("The following fields weren't imported: %s"), "\n");
189         
190         $this->assertEquals($result['results'][0]['name'], 'Tine 2.0 für Einsteiger');
191         $this->assertEquals($result['results'][0]['added_date']->setTimezone('Europe/Berlin')->toString(), '2013-12-06 00:00:00');
192         $this->assertEquals($result['results'][0]['inventory_id'], '133331666');
193         $this->assertContains($translatedString, $result['results'][0]['description']);
194     }
195     
196     /**
197      * import helper
198      *
199      * @param array $_options
200      * @param string|Tinebase_Model_ImportExportDefinition $_definition
201      * @param Inventory_Model_InventoryItemFilter $_exportFilter
202      * @return array
203      */
204     protected function _doImport(array $_options, $_definition, Inventory_Model_InventoryItemFilter $_exportFilter = NULL)
205     {
206         $definition = ($_definition instanceof Tinebase_Model_ImportExportDefinition) ? $_definition : Tinebase_ImportExportDefinition::getInstance()->getByName($_definition);
207         $this->_instance = Inventory_Import_Csv::createFromDefinition($definition, $_options);
208         
209         // export first
210         if ($_exportFilter !== NULL) {
211             $exporter = new Inventory_Export_Csv($_exportFilter, Inventory_Controller_InventoryItem::getInstance());
212             $this->_filename = $exporter->generate();
213         }
214         
215         // then import
216         $result = $this->_instance->importFile($this->_filename);
217         
218         return $result;
219     }
220 }
221