Test Failed
Push — CI ( 0f01dd...c95a04 )
by Adam
55:13
created

file_utilsTest::testwrite_array_to_file()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 27
rs 8.8571
c 1
b 0
f 1
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
use org\bovigo\vfs\vfsStream,
4
org\bovigo\vfs\vfsStreamDirectory;
5
6
require_once 'include/utils/file_utils.php';
7
class file_utilsTest extends PHPUnit_Framework_TestCase
8
{
9
10
    public function setUp() {
11
        $this->rootFs = org\bovigo\vfs\vfsStream::setup('root');
12
        $this->rootFs->addChild(org\bovigo\vfs\vfsStream::newDirectory('testDir'));
13
        $this->rootFs->addChild(org\bovigo\vfs\vfsStream::newFile('test.txt')->withContent("Hello world!"));
14
    }
15
16
17
    public function testclean_path()
18
	{
19
		//execute the method and test if it returns expected values
20
		
21
		//invalid path
22
		$expected = "";
23
		$path = "";
24
		$actual = clean_path($path);
25
		$this->assertSame($expected,$actual);
26
		
27
28
		//a simple valid path
29
		$expected = "/SuiteCRM-develop/include/utils";
30
		$path = '\SuiteCRM-develop\include\utils';
31
		$actual = clean_path($path);
32
		$this->assertSame($expected,$actual);
33
34
		
35
		//valid network path 
36
		$expected = "//SuiteCRM-develop/include/utils";
37
		$path = "\\\\/SuiteCRM-develop/include/utils";
38
		$actual = clean_path($path);
39
		$this->assertSame($expected,$actual);
40
41
42
        $expected = "/SuiteCRM-develop/include/utils";
43
        $path = "/SuiteCRM-develop/./include/utils";
44
        $actual = clean_path($path);
45
        $this->assertSame($expected,$actual);
46
47
        $expected = "/SuiteCRM-develop/include/utils";
48
        $path = "/SuiteCRM-develop//include/utils";
49
        $actual = clean_path($path);
50
        $this->assertSame($expected,$actual);
51
52
53
		
54
	}
55
	
56
	public function testcreate_cache_directory()
57
	{
58
		//execute the method and test if it created file/dir exists
59
		
60
		$cache_dir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\');
61
		$file = "Test/";
62
		
63
		if ( $this->rootFs->hasChild($file)  == true)
64
			rmdir($cache_dir . "/" . $file);
65
		
66
		$actual = create_cache_directory($file);
67
		$this->assertFileExists($actual);
68
		
69
		if ( $this->rootFs->hasChild($file)  == true)
70
			rmdir($cache_dir . "/" . $file);
71
		
72
	}
73
	
74
	public function testget_module_dir_list()
75
	{
76
		//execute the method and test if it returns expected values
77
		
78
		$expected  = array (
79
				'Accounts' => 'Accounts',
80
				'ACL' => 'ACL',
81
				'ACLActions' => 'ACLActions',
82
				'ACLRoles' => 'ACLRoles',
83
				'Activities' => 'Activities',
84
				'Administration' => 'Administration',
85
				'Alerts' => 'Alerts',
86
				'AM_ProjectTemplates' => 'AM_ProjectTemplates',
87
				'AM_TaskTemplates' => 'AM_TaskTemplates',
88
				'AOD_Index' => 'AOD_Index',
89
				'AOD_IndexEvent' => 'AOD_IndexEvent',
90
				'AOK_KnowledgeBase' => 'AOK_KnowledgeBase',
91
				'AOK_Knowledge_Base_Categories' => 'AOK_Knowledge_Base_Categories',
92
				'AOP_Case_Events' => 'AOP_Case_Events',
93
				'AOP_Case_Updates' => 'AOP_Case_Updates',
94
				'AOR_Charts' => 'AOR_Charts',
95
				'AOR_Conditions' => 'AOR_Conditions',
96
				'AOR_Fields' => 'AOR_Fields',
97
				'AOR_Reports' => 'AOR_Reports',
98
				'AOR_Scheduled_Reports' => 'AOR_Scheduled_Reports',
99
				'AOS_Contracts' => 'AOS_Contracts',
100
				'AOS_Invoices' => 'AOS_Invoices',
101
				'AOS_Line_Item_Groups' => 'AOS_Line_Item_Groups',
102
				'AOS_PDF_Templates' => 'AOS_PDF_Templates',
103
				'AOS_Products' => 'AOS_Products',
104
				'AOS_Products_Quotes' => 'AOS_Products_Quotes',
105
				'AOS_Product_Categories' => 'AOS_Product_Categories',
106
				'AOS_Quotes' => 'AOS_Quotes',
107
				'AOW_Actions' => 'AOW_Actions',
108
				'AOW_Conditions' => 'AOW_Conditions',
109
				'AOW_Processed' => 'AOW_Processed',
110
				'AOW_WorkFlow' => 'AOW_WorkFlow',
111
				'Audit' => 'Audit',
112
				'Bugs' => 'Bugs',
113
				'Calendar' => 'Calendar',
114
				'Calls' => 'Calls',
115
				'Calls_Reschedule' => 'Calls_Reschedule',
116
				'CampaignLog' => 'CampaignLog',
117
				'Campaigns' => 'Campaigns',
118
				'CampaignTrackers' => 'CampaignTrackers',
119
				'Cases' => 'Cases',
120
				'Charts' => 'Charts',
121
				'Configurator' => 'Configurator',
122
				'Connectors' => 'Connectors',
123
				'Contacts' => 'Contacts',
124
				'Currencies' => 'Currencies',
125
				'Delegates' => 'Delegates',
126
				'DocumentRevisions' => 'DocumentRevisions',
127
				'Documents' => 'Documents',
128
				'DynamicFields' => 'DynamicFields',
129
				'EAPM' => 'EAPM',
130
				'EmailAddresses' => 'EmailAddresses',
131
				'EmailMan' => 'EmailMan',
132
				'EmailMarketing' => 'EmailMarketing',
133
				'Emails' => 'Emails',
134
				'EmailTemplates' => 'EmailTemplates',				
135
				'EmailText' => 'EmailText',
136
				'Employees' => 'Employees',
137
				'Favorites' => 'Favorites',
138
				'FP_events' => 'FP_events',
139
				'FP_Event_Locations' => 'FP_Event_Locations',
140
				'Groups' => 'Groups',
141
				'Help' => 'Help',
142
				'History' => 'History',
143
				'Home' => 'Home',
144
				'iCals' => 'iCals',
145
				'Import' => 'Import',
146
				'InboundEmail' => 'InboundEmail',
147
				'jjwg_Address_Cache' => 'jjwg_Address_Cache',
148
				'jjwg_Areas' => 'jjwg_Areas',
149
				'jjwg_Maps' => 'jjwg_Maps',
150
				'jjwg_Markers' => 'jjwg_Markers',
151
				'LabelEditor' => 'LabelEditor',
152
				'Leads' => 'Leads',
153
				'MailMerge' => 'MailMerge',
154
				'Meetings' => 'Meetings',
155
				'MergeRecords' => 'MergeRecords',
156
				'ModuleBuilder' => 'ModuleBuilder',
157
				'MySettings' => 'MySettings',
158
				'Notes' => 'Notes',
159
				'OAuthKeys' => 'OAuthKeys',
160
				'OAuthTokens' => 'OAuthTokens',
161
				'Opportunities' => 'Opportunities',
162
				'OptimisticLock' => 'OptimisticLock',
163
				'Project' => 'Project',
164
				'ProjectTask' => 'ProjectTask',
165
				'ProspectLists' => 'ProspectLists',
166
				'Prospects' => 'Prospects',
167
				'Relationships' => 'Relationships',
168
                'Reminders' => 'Reminders',
169
                'Reminders_Invitees' => 'Reminders_Invitees',
170
				'Releases' => 'Releases',
171
				'Roles' => 'Roles',
172
				'SavedSearch' => 'SavedSearch',
173
				'Schedulers' => 'Schedulers',
174
				'SchedulersJobs' => 'SchedulersJobs',
175
				'SecurityGroups' => 'SecurityGroups',
176
				'Studio' => 'Studio',
177
				'SugarFeed' => 'SugarFeed',
178
				'Tasks' => 'Tasks',
179
				'Trackers' => 'Trackers',
180
				'UpgradeWizard' => 'UpgradeWizard',
181
				'UserPreferences' => 'UserPreferences',
182
				'Users' => 'Users',
183
				'vCals' => 'vCals',
184
				'Versions' => 'Versions',
185
				);
186
		
187
		
188
		$actual = get_module_dir_list();
189
        sort($actual);
190
        sort($expected);
191
		$this->assertSame($expected,$actual);
192
		
193
	}
194
	
195
	public function testmk_temp_dir(  )
196
	{
197
		//execute the method and test if created dir/file exists
198
		
199
		//without prefix
200
		$actual = mk_temp_dir('vfs://root', '' );
201
		$this->assertFileExists($actual);
202
203
		//with prefix
204
		$actual = mk_temp_dir('vfs://root', 'pfx' );
205
		$this->assertFileExists($actual);
206
		
207
	}
208
	
209
	public function testremove_file_extension(  )
210
	{
211
		//execute the method and test if it returns expected values
212
		
213
		//no file extension
214
		$expected = "";
215
		$actual = remove_file_extension('fileNoExt');
216
		$this->assertSame($expected,$actual);
217
		
218
		
219
		//simple file extension
220
		$expected = "file1";
221
		$actual = remove_file_extension('file1.txt');
222
		$this->assertSame($expected,$actual);
223
		
224
		
225
		//complex filename
226
		$expected = "file2.ext1";
227
		$actual =  remove_file_extension('file2.ext1.ext2');
228
		$this->assertSame($expected,$actual);
229
		
230
	}
231
	
232
	public function testwrite_array_to_file()
233
	{
234
        $this->markTestSkipped('write_array_to_file cannot be tested with vfsStream');
235
		//execute the method and test if it returns true and verify contents
236
		
237
		$cache_dir = "vfs://root";
238
		$tempArray = Array("Key1" => Array( "Key2" => "value2" , "Key3" => "value3" ));
239
		
240
		
241
		//without header
242
		$expected = "<?php\n// created: " . date('Y-m-d H:i:s') . "\n\$tempArray = array (\n  'Key1' => \n  array (\n    'Key2' => 'value2',\n    'Key3' => 'value3',\n  ),\n);" ;
243
		$actual = write_array_to_file('tempArray', $tempArray, $cache_dir . '\tempArray.txt');
244
		$this->assertTrue($actual);
245
		$actual_contents = file_get_contents($cache_dir . '\tempArray.txt');
246
		$this->assertSame($expected,$actual_contents);
247
		unlink($cache_dir . '\tempArray.txt');
248
		
249
		//with header
250
		$expected = "test header \$tempArray = array (\n  'Key1' => \n  array (\n    'Key2' => 'value2',\n    'Key3' => 'value3',\n  ),\n);" ;
251
		$actual = write_array_to_file('tempArray', $tempArray, $cache_dir . '\tempArray.txt','w' ,'test header ');
252
		$this->assertTrue($actual);
253
		$actual_contents = file_get_contents($cache_dir . '\tempArray.txt');
254
		$this->assertSame($expected,$actual_contents);
255
		unlink($cache_dir . '\tempArray.txt');
256
		
257
		
258
	}
259
	
260
	public function testwrite_encoded_file( )
261
	{
262
		//execute the method and test if it created file exists
263
		
264
		$cache_dir = "vfs://root";
265
		
266
		//without filename 
267
		$tempArray = Array("filename" =>'soap_array.txt' , "md5" => "523ef67de860fc54794f27117dba4fac" , "data" => "some soap data" );
268
		$actual = write_encoded_file( $tempArray, $cache_dir, "" );
269
		$this->assertFileExists($actual);
270
		unlink($actual);
271
		
272
		
273
		//with filename
274
		$tempArray = Array("md5" => "523ef67de860fc54794f27117dba4fac" , "data" => "some soap data" );
275
		$actual = write_encoded_file( $tempArray, $cache_dir, "soap_array.txt" );
276
		$this->assertFileExists($actual);
277
		unlink($actual);
278
		
279
	}
280
	
281
	public function testcreate_custom_directory()
282
	{
283
		//execute the method and test if it created file/dir exists
284
		
285
		$file = "Test/";
286
		
287
		$vfs = $this->rootFs;
288
		if ( $vfs->hasChild($file)  == true)
289
			rmdir("custom/" . $file);
290
		
291
		$actual = create_custom_directory($file);
292
		$this->assertFileExists($actual);
293
294
		if ( $vfs->hasChild($file)  == true)
295
			rmdir("custom/" . $file);
296
		
297
		
298
	}
299
	
300
	
301
	public function testgenerateMD5array()
302
	{
303
		//execute the method and test if it returns expected values
304
305
		$expected= array (
306
				'data/Relationships/EmailAddressRelationship.php' => '2f04780ddd15f7b65a35c75c303ed5d7',
307
				'data/Relationships/M2MRelationship.php' => 'c320909b5a17d63aafa0d7497fe3c991',
308
				'data/Relationships/One2MBeanRelationship.php' => 'c09fe92826b4c8a3944694098de35027',
309
				'data/Relationships/One2MRelationship.php' => '588ad87910bd9d885fe27da77ad13e30',
310
				'data/Relationships/One2OneBeanRelationship.php' => '765b8785d5ca576a8530db99bdf4d411',
311
				'data/Relationships/One2OneRelationship.php' => '0385f7577687a402d9603ef26984257e',
312
				'data/Relationships/RelationshipFactory.php' => '3bd7cc6998beaf82a13808f54bff1c2d',
313
				'data/Relationships/SugarRelationship.php' => '8d0fa8ae0f41ac34eb5d0c04f0e02825',
314
		); 
315
		
316
		$actual = generateMD5array('data/Relationships/');
317
		$this->assertSame($expected,$actual);
318
			
319
	}
320
	
321
	
322
	public function testmd5DirCompare()
323
	{
324
		//execute the method and test if it returns expected values
325
		
326
		$expected= array ();
327
		
328
		$actual = md5DirCompare('include/MVC/', 'include/MVC/',array('views'));
329
		$this->assertSame($expected,$actual);
330
		
331
	}
332
	
333
	
334
	public function testgetFiles() 
335
	{
336
		//execute the method and test if it returns expected values
337
		
338
		//test without pattern
339
		$expected = array (
340
            'include/MVC/Controller/ControllerFactory.php',
341
            'include/MVC/Controller/file_access_control_map.php',
342
            'include/MVC/Controller/action_file_map.php',
343
            'include/MVC/Controller/action_view_map.php',
344
            'include/MVC/Controller/entry_point_registry.php',
345
            'include/MVC/Controller/SugarController.php',
346
		);
347
		$actual = Array();
348
		getFiles($actual, 'include/MVC/Controller');
349
        sort($actual);
350
        sort($expected);
351
		$this->assertSame($expected,$actual);
352
		
353
		
354
		
355
		
356
		//test with pattern
357
		$expected = array (
358
				'include/MVC/Controller/action_view_map.php',
359
				'include/MVC/View/views/view.ajax.php',
360
				'include/MVC/View/views/view.ajaxui.php',
361
				'include/MVC/View/views/view.classic.config.php',
362
				'include/MVC/View/views/view.classic.php',
363
				'include/MVC/View/views/view.config.php',
364
				'include/MVC/View/views/view.detail.config.php',
365
				'include/MVC/View/views/view.detail.php',
366
				'include/MVC/View/views/view.edit.php',
367
				'include/MVC/View/views/view.favorites.php',
368
				'include/MVC/View/views/view.html.php',
369
				'include/MVC/View/views/view.importvcard.php',
370
				'include/MVC/View/views/view.importvcardsave.php',
371
				'include/MVC/View/views/view.json.php',
372
				'include/MVC/View/views/view.list.php',
373
				'include/MVC/View/views/view.metadata.php',
374
				'include/MVC/View/views/view.modulelistmenu.php',
375
				'include/MVC/View/views/view.multiedit.php',
376
				'include/MVC/View/views/view.noaccess.php',
377
				'include/MVC/View/views/view.popup.php',
378
				'include/MVC/View/views/view.quick.php',
379
				'include/MVC/View/views/view.quickcreate.php',
380
				'include/MVC/View/views/view.quickedit.php',
381
				'include/MVC/View/views/view.serialized.php',
382
				'include/MVC/View/views/view.sugarpdf.config.php',
383
				'include/MVC/View/views/view.sugarpdf.php',
384
				'include/MVC/View/views/view.vcard.php',
385
				'include/MVC/View/views/view.xml.php',
386
		);
387
		$actual = Array();
388
		getFiles($actual, 'include/MVC','@view@');
389
        sort($expected);
390
        sort($actual);
391
		$this->assertEquals($expected,$actual);
392
393
	}
394
	
395
	
396
	public function testreadfile_chunked()
397
	{
398
		//execute the method and test if it returns expected values
399
	
400
		$expected = file_get_contents('config.php');
401
		
402
		
403
		//retbytes parameter false
404
		ob_start();		
405
		$actual = readfile_chunked('config.php',false);		
406
		$renderedContent = ob_get_contents();
407
		ob_end_clean();		 
408
		
409
		$this->assertTrue($actual);
410
		$this->assertSame($expected,$renderedContent);
411
412
		
413
		
414
		//retbytes parameter true/default
415
		ob_start();
416
		$actual = readfile_chunked('config.php');
417
		$renderedContent = ob_get_contents();
418
		ob_end_clean();
419
		
420
		$this->assertEquals($actual,strlen($renderedContent));
421
		$this->assertSame($expected,$renderedContent);
422
		
423
	}
424
	
425
	public function testsugar_rename( )
426
	{
427
		//execute the method and test if it returns true/success
428
	
429
		$dir = 'vfs://root';
430
		$file = "test.txt";
431
		$vfs = $this->rootFs;
432
		if ( $vfs->hasChild($file)  != true)
433
			write_array_to_file('', '', $dir . '/' . $file);
434
		
435
		//test with empty file names
436
		$actual = sugar_rename('','');
437
		$this->assertFalse($actual);
438
		
439
		
440
		//test with valid file names 
441
		$actual = sugar_rename($dir . '/' . $file, $dir . '/' . 'newtest.txt');
442
		$this->assertTrue($actual);
443
		
444
		unlink($dir . '/' . 'newtest.txt');
445
		
446
	}
447
	
448
	public function testfileToHash()
449
	{
450
		//execute the method and test if it returns expected values
451
		
452
		//test with empty filename string
453
		$expected  = "d41d8cd98f00b204e9800998ecf8427e";
454
		$hash = fileToHash("");
455
		$this->assertSame($expected,$hash);
456
		$this->assertSame("",$_SESSION['file2Hash'][$hash]);
457
		
458
		//test with valid filename
459
		$expected = "9e5e2527d69c009a81b8ecd730f3957e";
460
		$hash = fileToHash('config.php');
461
		$this->assertSame($expected,$hash);
462
		$this->assertSame('config.php', $_SESSION['file2Hash'][$hash]);
463
		
464
	}
465
	
466
	public function testhashToFile()
467
	{
468
		//execute the method and test if it returns expected values
469
		
470
		//test with invalid hash.
471
		$actual = hashToFile("");
472
		$this->assertFalse($actual);
473
		
474
		//test with a newly generated hash
475
		$hash = fileToHash('config.php');
476
		$actual = hashToFile($hash);
477
		$this->assertSame('config.php', $actual);
478
	}
479
	
480
	
481
	public function testget_file_extension()
482
	{
483
		//execute the method and test if it returns expected values
484
		
485
		$this->assertSame('', get_file_extension(''));
486
		$this->assertSame('txt', get_file_extension('test.txt'));
487
		$this->assertSame('Txt', get_file_extension('test.ext.Txt',false));
488
		$this->assertSame('txt', get_file_extension('test.ext.TXT',true));
489
		
490
	}
491
	
492
	
493
	public function testget_mime_content_type_from_filename()
494
	{
495
		//execute the method and test if it returns expected values
496
		
497
		$this->assertSame('', get_mime_content_type_from_filename('') );
498
		$this->assertSame('application/octet-stream', get_mime_content_type_from_filename('file.tmp') );
499
		$this->assertSame('text/plain', get_mime_content_type_from_filename('file.txt') );
500
		$this->assertSame('application/x-shockwave-flash', get_mime_content_type_from_filename('file.swf') );
501
		$this->assertSame('video/x-flv', get_mime_content_type_from_filename('file.flv') );	
502
		
503
	}
504
	
505
	public function testcleanFileName()
506
	{
507
		//execute the method and test if it returns expected values
508
		
509
		$this->assertSame("file.txt", cleanFileName("file<?>.txt") );
510
		$this->assertSame("file_1.txt", cleanFileName("file_1<?>.txt") );
511
		$this->assertSame("file.txt", cleanFileName("file.txt") );
512
	}
513
	
514
	public function testcleanDirName()
515
	{
516
		//execute the method and test if it returns expected values
517
		
518
		$this->assertSame("testDir", cleanDirName("./testDir") );
519
		$this->assertSame("testDir", cleanDirName("..\\testDir") );
520
		$this->assertSame("testDir", cleanDirName("\\test/Dir/") );
521
		
522
	}
523
524
}