Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ExportItemToCsvFileUtilTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
37  Yii::import('ext.csvparser.CsvParser');
38 
43  {
44  public static function setUpBeforeClass()
45  {
46  parent::setUpBeforeClass();
47  $super = SecurityTestHelper::createSuperAdmin();
48  }
49 
50  public static function getDependentTestModelClassNames()
51  {
52  return array('ExportTestModelItem', 'ExportTestModelItem2', 'ExportTestModelItem3', 'ExportTestModelItem4');
53  }
54 
55  public function testGetDataWithHasOneRelatedModel()
56  {
57  $super = User::getByUsername('super');
58  Yii::app()->user->userModel = $super;
59 
60  $multiSelectValues = array(
61  'Multi 1',
62  'Multi 2',
63  'Multi 3',
64  );
65  $customFieldData = CustomFieldData::getByName('ExportTestMultiDropDown');
66  $customFieldData->serializedData = serialize($multiSelectValues);
67  $save = $customFieldData->save();
68  assert('$save'); // Not Coding Standard
69 
70  $tagCloudValues = array(
71  'Cloud 1',
72  'Cloud 2',
73  'Cloud 3',
74  );
75  $customFieldData = CustomFieldData::getByName('ExportTestTagCloud');
76  $customFieldData->serializedData = serialize($tagCloudValues);
77  $save = $customFieldData->save();
78  assert('$save'); // Not Coding Standard
79 
80  $currencies = Currency::getAll();
81  $currencyValue = new CurrencyValue();
82  $currencyValue->value = 100;
83  $currencyValue->currency = $currencies[0];
84  $this->assertEquals('USD', $currencyValue->currency->code);
85 
86  $testItem2 = new ExportTestModelItem2();
87  $testItem2->name = 'John';
88  $this->assertTrue($testItem2->save());
89 
90  $testItem4 = new ExportTestModelItem4();
91  $testItem4->name = 'John';
92  $this->assertTrue($testItem4->save());
93 
94  //HAS_MANY and MANY_MANY relationships should be ignored.
95  $testItem3_1 = new ExportTestModelItem3();
96  $testItem3_1->name = 'Kevin';
97  $this->assertTrue($testItem3_1->save());
98 
99  $testItem3_2 = new ExportTestModelItem3();
100  $testItem3_2->name = 'Jim';
101  $this->assertTrue($testItem3_2->save());
102 
103  $testItem = new ExportTestModelItem();
104  $testItem->firstName = 'Bob3';
105  $testItem->lastName = 'Bob3';
106  $testItem->boolean = true;
107  $testItem->date = '2002-04-03';
108  $testItem->dateTime = '2002-04-03 02:00:43';
109  $testItem->float = 54.22;
110  $testItem->integer = 10;
111  $testItem->phone = '21313213';
112  $testItem->string = 'aString';
113  $testItem->textArea = 'Some Text Area';
114  $testItem->url = 'http://www.asite.com';
115  $testItem->email = 'a@a.com';
116  $testItem->owner = $super;
117  $testItem->currencyValue = $currencyValue;
118  $testItem->hasOne = $testItem2;
119  $testItem->hasMany->add($testItem3_1);
120  $testItem->hasMany->add($testItem3_2);
121  $testItem->hasOneAlso = $testItem4;
122 
123  $customFieldValue = new CustomFieldValue();
124  $customFieldValue->value = 'Multi 1';
125  $testItem->multiDropDown->values->add($customFieldValue);
126 
127  $customFieldValue = new CustomFieldValue();
128  $customFieldValue->value = 'Multi 3';
129  $testItem->multiDropDown->values->add($customFieldValue);
130 
131  $customFieldValue = new CustomFieldValue();
132  $customFieldValue->value = 'Cloud 2';
133  $testItem->tagCloud->values->add($customFieldValue);
134 
135  $customFieldValue = new CustomFieldValue();
136  $customFieldValue->value = 'Cloud 3';
137  $testItem->tagCloud->values->add($customFieldValue);
138 
139  $createStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
140  $this->assertTrue($testItem->save());
141  $id = $testItem->id;
142  $testItem->forget();
143  unset($testItem);
144 
145  $data = array();
146  $testItem = ExportTestModelItem::getById($id);
147  $adapter = new ModelToExportAdapter($testItem);
148  $data[] = $adapter->getData();
149  $headerData = $adapter->getHeaderData();
150  // Export data to csv, and then revert csv back to array, so we compare data
151  $csvData = ExportItemToCsvFileUtil::export($data, $headerData, '', false);
152  $revertedData = CsvParser::parseFromString($csvData);
153 
154  // We are testing ModelToExportAdapter in details in another test
155  // so in this test we suppose that ModelToExportAdapter::getData
156  // return correct results
157  $adapter = new ModelToExportAdapter($testItem);
158  $compareData = $adapter->getData();
159  $compareHeaderData = $adapter->getHeaderData();
160 
161  $this->assertEquals($compareHeaderData, array_keys($revertedData[0]));
162  $this->assertEquals($compareData, array_values($revertedData[0]));
163  }
164 
165  public function testExportItemToCsvWorksWithNormalData()
166  {
167  $this->assertTrue($this->isValidCsvConversion('Data without linebreaks or commas'));
168  }
169 
170  public function testExportItemToCsvWorksWithDataContainingComma()
171  {
172  $this->assertTrue($this->isValidCsvConversion('Data, with, multiple, comma, occurances'));
173  }
174 
175  public function testExportItemToCsvWorksWithDataContainingLineBreaks()
176  {
177  $this->assertTrue($this->isValidCsvConversion("Data \n with \n linebreaks \n"));
178  }
179 
180  public function testExportItemToCsvWorksWithDataContainingCommaAndLineBreaks()
181  {
182  $this->assertTrue($this->isValidCsvConversion("Data, \n with, \n ,linebreaks, \n")); // Not Coding Standard
183  }
184 
185  public function testExportItemToCsvWorksWithShouldTrim()
186  {
187  $super = User::getByUsername('super');
188  Yii::app()->user->userModel = $super;
189 
190  $testItem = new ExportTestModelItem();
191  $testItem->firstName = 'Bob3 ';
192  $testItem->lastName = ' Bob3';
193  $testItem->boolean = true;
194  $testItem->date = '2002-04-03';
195  $testItem->dateTime = '2002-04-03 02:00:43';
196  $testItem->float = 54.22;
197  $testItem->integer = 10;
198  $testItem->phone = '21313213';
199  $testItem->string = ' aString ';
200  $testItem->textArea = ' aText area \n new line ';
201  $testItem->url = 'http://www.asite.com';
202  $testItem->email = 'a@a.com';
203 
204  $testItem->save();
205  $id = $testItem->id;
206  $testItem->forget();
207  unset($testItem);
208 
209  $testItem = ExportTestModelItem::getById($id);
210  $adapter = new ModelToExportAdapter($testItem);
211  $data = array($adapter->getData());
212  $headerData = $adapter->getHeaderData();
213 
214  // Export data to csv, and then revert csv back to array, so we compare data
215  $csvData = ExportItemToCsvFileUtil::export($data, $headerData, 'exports.csv', false, false, true);
216  $revertedData = CsvParser::parseFromString($csvData);
217 
218  $this->assertContains(trim($testItem->firstName), array_values($revertedData[0]));
219  $this->assertContains(trim($testItem->lastName), array_values($revertedData[0]));
220  $this->assertContains(trim($testItem->string), array_values($revertedData[0]));
221  $this->assertContains(trim($testItem->textArea), array_values($revertedData[0]));
222  $this->assertNotContains($testItem->firstName, array_values($revertedData[0]));
223  $this->assertNotContains($testItem->lastName, array_values($revertedData[0]));
224  $this->assertNotContains($testItem->string, array_values($revertedData[0]));
225  $this->assertNotContains($testItem->textArea, array_values($revertedData[0]));
226  }
227 
228  protected function isValidCsvConversion($textAreaContent)
229  {
230  $super = User::getByUsername('super');
231  Yii::app()->user->userModel = $super;
232 
233  $testItem = new ExportTestModelItem();
234  $testItem->firstName = 'Bob3';
235  $testItem->lastName = 'Bob3';
236  $testItem->boolean = true;
237  $testItem->date = '2002-04-03';
238  $testItem->dateTime = '2002-04-03 02:00:43';
239  $testItem->float = 54.22;
240  $testItem->integer = 10;
241  $testItem->phone = '21313213';
242  $testItem->string = 'aString';
243  $testItem->textArea = $textAreaContent;
244  $testItem->url = 'http://www.asite.com';
245  $testItem->email = 'a@a.com';
246 
247  $testItem->save();
248  $id = $testItem->id;
249  $testItem->forget();
250  unset($testItem);
251 
252  $data = array();
253  $testItem = ExportTestModelItem::getById($id);
254  $adapter = new ModelToExportAdapter($testItem);
255  $data[] = $adapter->getData();
256  $headerData = $adapter->getHeaderData();
257 
258  // Export data to csv, and then revert csv back to array, so we compare data
259  $csvData = ExportItemToCsvFileUtil::export($data, $headerData, 'exports.csv', false);
260  $revertedData = CsvParser::parseFromString($csvData);
261 
262  // We are testing ModelToExportAdapter in details in another test
263  // so in this test we suppose that ModelToExportAdapter::getData
264  // return correct results
265  $adapter = new ModelToExportAdapter($testItem);
266  $compareData = $adapter->getData();
267  $compareHeaderData = $adapter->getHeaderData();
268 
269  // Using === here would fail as we are not setting all keys part of getData()'s return array
270  if ($compareData == array_values($revertedData[0]) && $compareHeaderData == array_keys($revertedData[0]))
271  {
272  return true;
273  }
274  return false;
275  }
276  }
277 ?>
static getByUsername($username)
Definition: User.php:49
static getByName($name, $shouldCache=true)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
static export($data, $headerData=array(), $exportFileName= 'exports.csv', $download=false, $allowHeaderOnlyConversion=false, $shouldTrim=false)
static getById($id, $modelClassName=null)
Generated on Wed Jul 8 2020 07:10:34
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.