]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/UpgradeWizard/Bug32003_Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / UpgradeWizard / Bug32003_Test.php
1 <?php
2 class Bug32003_Test extends Sugar_PHPUnit_Framework_TestCase {
3
4 var $controller;        
5 var $original_current_user_id;
6         
7 function setUp() {
8         
9 global $dictionary;     
10         
11 $dictionary['iFrame'] = array('table' => 'iframes'
12                                ,'fields' => array (
13   'id' =>
14   array (
15     'name' => 'id',
16     'vname' => 'LBL_ID',
17     'type' => 'id',
18     'required'=>true,
19   ),
20   'name' =>
21   array (
22     'name' => 'name',
23     'vname' => 'LBL_LIST_NAME',
24     'type' => 'varchar',
25     'len' => '255',
26     'required'=>true,
27     'importable' => 'required',
28   ),
29   'url' =>
30   array (
31     'name' => 'url',
32     'vname' => 'LBL_LIST_URL',
33     'type' => 'varchar',
34     'len' => '255',
35     'required'=>true,
36     'importable' => 'required',
37   ),
38   'type' =>
39   array (
40     'name' => 'type',
41     'vname' => 'LBL_LIST_TYPE',
42     'type' => 'varchar',
43     'len' => '255',
44     'required'=>true,
45   ),
46   'placement' =>
47   array (
48     'name' => 'placement',
49     'vname' => 'LBL_LIST_PLACEMENT',
50     'type' => 'varchar',
51     'len' => '255',
52     'required'=>true,
53     'importable' => 'required',
54   ),
55   'status' =>
56   array (
57     'name' => 'status',
58     'vname' => 'LBL_LIST_STATUS',
59     'type' => 'bool',
60     'required'=>true,
61   ),
62   'deleted' =>
63   array (
64     'name' => 'deleted',
65     'vname' => 'LBL_DELETED',
66     'type' => 'bool',
67     'required'=>true,
68   ),
69   'date_entered' =>
70   array (
71     'name' => 'date_entered',
72     'vname' => 'LBL_DATE_ENTERED',
73     'type' => 'datetime',
74     'required'=>true,
75   ),
76   'date_modified' =>
77   array (
78     'name' => 'date_modified',
79     'vname' => 'LBL_DATE_MODIFIED',
80     'type' => 'datetime',
81     'required'=>true,
82   ),
83   'created_by' =>
84   array (
85     'name' => 'created_by',
86     'rname' => 'user_name',
87     'id_name' => 'modified_user_id',
88     'vname' => 'LBL_ASSIGNED_TO',
89     'type' => 'assigned_user_name',
90     'table' => 'users',
91     'isnull' => 'false',
92     'dbType' => 'id',
93     'required'=>true,
94   ),
95 ), 
96 'indices' => array (
97        array('name' =>'iframespk', 'type' =>'primary', 'fields'=>array('id')),
98        array('name' =>'idx_cont_name', 'type'=>'index', 'fields'=>array('name','deleted'))
99 )
100 );      
101         
102
103 $dictionary['Feed'] = array('table' => 'feeds', 'comment' => 'RSS Feeds'
104                                ,'fields' => array (
105   'id' =>
106   array (
107     'name' => 'id',
108     'vname' => 'LBL_ID',
109     'type' => 'id',
110     'required'=>true,
111     'reportable'=>true,
112     'comment' => 'Unique identifier'
113   ),
114   'deleted' =>
115   array (
116     'name' => 'deleted',
117     'vname' => 'LBL_CREATED_BY',
118     'type' => 'bool',
119     'required'=>true,
120     'reportable'=>false,
121     'comment' => 'Record deletion indicator'
122   ),
123   'date_entered' =>
124   array (
125     'name' => 'date_entered',
126     'vname' => 'LBL_DATE_ENTERED',
127     'type' => 'datetime',
128     'required'=>true,
129     'comment' => 'Date record created'
130   ),
131   'date_modified' =>
132   array (
133     'name' => 'date_modified',
134     'vname' => 'LBL_DATE_MODIFIED',
135     'type' => 'datetime',
136     'required'=>true,
137     'comment' => 'Date record last modified'
138   ),
139   'modified_user_id' =>
140   array (
141     'name' => 'modified_user_id',
142     'rname' => 'user_name',
143     'id_name' => 'modified_user_id',
144     'vname' => 'LBL_ASSIGNED_TO',
145     'type' => 'assigned_user_name',
146     'table' => 'users',
147     'isnull' => 'false',
148     'dbType' => 'id',
149     'required'=>true,
150     'reportable'=>true,
151     'comment' => 'User who last modified record'
152   ),
153   'assigned_user_id' =>
154   array (
155     'name' => 'assigned_user_id',
156     'rname' => 'user_name',
157     'id_name' => 'assigned_user_id',
158     'vname' => 'LBL_ASSIGNED_TO',
159     'type' => 'assigned_user_name',
160     'table' => 'users',
161     'isnull' => 'false',
162     'dbType' => 'id',
163     'reportable'=>true,
164     'comment' => 'User assigned to record'
165   ),
166   'created_by' =>
167   array (
168     'name' => 'created_by',
169     'rname' => 'user_name',
170     'id_name' => 'modified_user_id',
171     'vname' => 'LBL_ASSIGNED_TO',
172     'type' => 'assigned_user_name',
173     'table' => 'users',
174     'isnull' => 'false',
175     'dbType' => 'id',
176     'comment' => 'User that created record'
177   ),
178   'title' =>
179   array (
180     'name' => 'title',
181     'type' => 'varchar',
182     'len' => '100',
183     'vname' => 'LBL_TITLE',
184     'comment' => 'Title of RSS feed'
185   ),
186   'description' =>
187   array (
188     'name' => 'description',
189     'type' => 'text',
190     'vname' => 'LBL_DESCRIPTION',
191     'comment' => 'Description of RSS feed'
192   ),
193   'url' =>
194   array (
195     'name' => 'url',
196     'type' => 'varchar',
197     'len' => '255',
198     'vname' => 'LBL_URL',
199     'comment' => 'URL that represents the RSS feed',
200     'importable' => 'required',
201   ),
202   'created_by_link' =>
203   array (
204         'name' => 'created_by_link',
205     'type' => 'link',
206     'relationship' => 'feeds_created_by',
207     'vname' => 'LBL_CREATED_BY_USER',
208     'link_type' => 'one',
209     'module'=>'Users',
210     'bean_name'=>'User',
211     'source'=>'non-db',
212   ),
213   'modified_user_link' =>
214   array (
215         'name' => 'modified_user_link',
216     'type' => 'link',
217     'relationship' => 'feeds_modified_user',
218     'vname' => 'LBL_MODIFIED_BY_USER',
219     'link_type' => 'one',
220     'module'=>'Users',
221     'bean_name'=>'User',
222     'source'=>'non-db',
223   ),
224   'assigned_user_link' =>
225   array (
226         'name' => 'assigned_user_link',
227     'type' => 'link',
228     'relationship' => 'feeds_assigned_user',
229     'vname' => 'LBL_ASSIGNED_TO_USER',
230     'link_type' => 'one',
231     'module'=>'Users',
232     'bean_name'=>'User',
233     'source'=>'non-db',
234   ),
235 )
236 ,
237  'relationships' => array (
238
239   'feeds_assigned_user' =>
240    array('lhs_module'=> 'Users', 'lhs_table'=> 'users', 'lhs_key' => 'id',
241    'rhs_module'=> 'Feeds', 'rhs_table'=> 'feeds', 'rhs_key' => 'assigned_user_id',
242    'relationship_type'=>'one-to-many')
243
244    ,'feeds_modified_user' =>
245    array('lhs_module'=> 'Users', 'lhs_table'=> 'users', 'lhs_key' => 'id',
246    'rhs_module'=> 'Feeds', 'rhs_table'=> 'feeds', 'rhs_key' => 'modified_user_id',
247    'relationship_type'=>'one-to-many')
248
249    ,'feeds_created_by' =>
250    array('lhs_module'=> 'Users', 'lhs_table'=> 'users', 'lhs_key' => 'id',
251    'rhs_module'=> 'Feeds', 'rhs_table'=> 'feeds', 'rhs_key' => 'created_by',
252    'relationship_type'=>'one-to-many')
253 )
254
255                                                       , 'indices' => array (
256        array('name' =>'feedspk', 'type' =>'primary', 'fields'=>array('id')),
257        array('name' =>'idx_feed_name', 'type'=>'index', 'fields'=>array('title','deleted'))
258                                                       )
259
260                             );
261
262 VardefManager::createVardef('Feeds','Feed', array(
263 ));
264         
265         require_once('include/database/DBManagerFactory.php');
266         $db = DBManagerFactory::getInstance();
267         
268     if(!$db->tableExists('iframes')){
269        $db->createTableParams('iframes', $dictionary['iFrame']['fields'], $dictionary['iFrame']['indices']);
270     }
271     
272     if(!$db->tableExists('feeds')){
273        $db->createTableParams('feeds', $dictionary['Feed']['fields'], $dictionary['Feed']['indices']);
274     }    
275     
276     global $moduleList;
277     $moduleList['Feeds'] = 'Feeds';
278     $moduleList['iFrames'] = 'iFrames';
279     
280         require_once("modules/MySettings/TabController.php");
281         $this->controller = new TabController(); 
282
283         global $current_user;
284         if ( !( $current_user instanceOf User ) ) {
285             $current_user = new User;           
286             $current_user->retrieve('1');   
287         }
288         $this->original_current_user_id = $current_user->id;
289 }       
290
291 function tearDown() {
292         $db = DBManagerFactory::getInstance();
293         if($db->tableExists('iframes')) {
294                 $db->dropTableName('iframes');
295         }
296         
297         if($db->tableExists('feeds')) {
298                 $db->dropTableName('feeds');
299         }       
300         
301     global $moduleList;
302         require_once("modules/MySettings/TabController.php");
303         $this->controller = new TabController();  
304
305     global $moduleList;
306     $keys = array_flip($moduleList);
307     if(in_array('Feeds', $moduleList)) {
308        unset($moduleList[$keys['Feeds']]);
309     }
310     
311     if(in_array('iFrames', $moduleList)) {
312        unset($moduleList[$keys['iFrames']]);
313     }
314     
315     $this->controller->set_system_tabs($moduleList);
316
317     if(file_exists('custom/Extension/application/Ext/Include/Feed.php')) {
318         unlink('custom/Extension/application/Ext/Include/Feed.php');
319     }
320     
321     if(file_exists('custom/Extension/application/Ext/Include/iFrame.php')) {
322         unlink('custom/Extension/application/Ext/Include/iFrame.php');
323     }
324     
325     global $current_user;
326     $current_user->retrieve($this->original_current_user_id);
327 }
328
329
330 function  disabled_post_install_upgrade_with_iframes() {
331         $db = DBManagerFactory::getInstance();
332         $db->query('INSERT into iframes(id, name, url) values (\'' . mktime() . '\', \'test\', \'www.test.com\')');
333     hide_iframes_and_feeds_modules();
334     $this->assertTrue($db->tableExists('iframes'));
335     $this->assertTrue(file_exists('custom/Extension/application/Ext/Include/iFrame.php'));
336 }
337
338
339 function  disabled_post_install_upgrade_without_iframes() {
340     hide_iframes_and_feeds_modules();
341         $db = DBManagerFactory::getInstance();
342     $this->assertTrue(!$db->tableExists('iframes'));  
343
344     $this->assertTrue(!file_exists('custom/Extension/application/Ext/Include/iFrame.php'));
345 }
346
347
348 function  disabled_post_install_upgrade_with_feeds() {
349         $this->markTestSkipped("Skip test_post_install_upgrade_with_feeds");
350         $tabs = $this->controller->get_tabs_system();
351         echo var_export($tabs, true);
352         
353         //If it is hidden, set it to show
354         if(isset($tabs[1]['Feeds'])) {
355            $db = DBManagerFactory::getInstance();
356            $db->query('DELETE FROM config WHERE category = \'MySettings\' AND name = \'tab\'');
357            unset($tabs[1]['Feeds']);
358            $tabs[0]['Feeds'] = 'Feeds';
359            
360            $administration = new Administration();
361            $serialized = base64_encode(serialize($moduleList));
362            $administration->saveSetting('MySettings', 'tab', $serialized);                 
363            $this->controller->set_system_tabs($tabs);
364         }
365
366         /*
367         //It's as if this never changes 
368         $tabs = $this->controller->get_tabs_system();
369         echo var_export($tabs, true);   
370
371     */
372     hide_iframes_and_feeds_modules();
373     $this->assertTrue($db->tableExists('feeds'));  
374     $this->assertTrue(file_exists('custom/Extension/application/Ext/Include/Feed.php'));
375 }
376
377
378 function  disabled_post_install_upgrade_without_feeds() {
379     hide_iframes_and_feeds_modules();
380     $db = DBManagerFactory::getInstance();
381     $this->assertTrue(!$db->tableExists('feeds'));
382     $this->assertTrue(!file_exists('custom/Extension/application/Ext/Include/Feed.php'));
383 }
384
385 /*
386  * Added a minimum of 1 test to make sure that 
387  */
388 function test_donothing() {
389         echo "";
390 }
391
392 function  disabled_dashlets_module_changed() {
393         
394         $db = DBManagerFactory::getInstance();
395         $query = "SELECT id, contents, assigned_user_id FROM user_preferences WHERE deleted = 0 AND category = 'Home'";
396         $result = $db->query($query, true, "Unable to update iFrames and Feeds dashlets!");
397         while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
398                 $content = unserialize(base64_decode($row['contents']));
399                 $assigned_user_id = $row['assigned_user_id'];
400                 $record_id = $row['id'];
401                 $current_user = new User();
402         $current_user->retrieve($row['assigned_user_id']);
403         
404                 if(!empty($content['dashlets']) && !empty($content['pages'])){
405                         $originalDashlets = $content['dashlets'];
406                         $originalPages = $content['pages'];
407                         
408                         //Determine if the original perference has already had the two dashlets or not
409                         foreach($originalDashlets as $key=>$ds){
410
411                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_DISCOVER_SUGAR_PRO'){
412                                    $originalDashlets[$key]['module'] = 'iFrames';
413                                 }
414                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_SUGAR_NEWS'){
415                                    $originalDashlets[$key]['module'] = 'iFrames';
416                                 }
417                         }
418                         
419                         $current_user->setPreference('dashlets', $originalDashlets, 0, 'Home');
420                         $current_user->setPreference('pages', $originalPages, 0, 'Home');       
421                 }
422         } //while       
423         
424         
425         hide_iframes_and_feeds_modules();
426         $result = $db->query($query, true, "Unable to update iFrames and Feeds dashlets!");
427         $not_home_module = false;
428         
429         while ($row = $db->fetchByAssoc($result)) {
430                 $content = unserialize(base64_decode($row['contents']));
431                 $assigned_user_id = $row['assigned_user_id'];
432                 $record_id = $row['id'];
433                 $current_user = new User();
434         $current_user->retrieve($row['assigned_user_id']);
435         
436                 if(!empty($content['dashlets']) && !empty($content['pages'])){
437                         $originalDashlets = $content['dashlets'];
438                         $originalPages = $content['pages'];
439                         
440                         //Determine if the original perference has already had the two dashlets or not
441                         foreach($originalDashlets as $key=>$ds){
442                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_DISCOVER_SUGAR_PRO' && $originalDashlets[$key]['module'] != 'Home') {
443                                    $not_home_module = true;
444                                 }
445                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_SUGAR_NEWS' && $originalDashlets[$key]['module'] != 'Home'){
446                                    $not_home_module = true;
447                                 }
448                         }
449                 }
450         } //while
451
452         $this->assertFalse($not_home_module, 'Assert that dashlet\'s module were correctly set to Home module');
453 }
454
455 }
456
457
458 //BEGIN INLINE METHODS FROM BUILD (post_install.php)
459
460 /**
461  * hide_iframes_and_feeds_modules
462  * This method determines whether or not to hide the iFrames and Feeds module
463  * for an upgrade to 551
464  */
465 function hide_iframes_and_feeds_modules() {
466         global $path;
467         
468     _logThis('Updating the iFrames Dashlets', $path);
469         $query = "SELECT id, contents, assigned_user_id FROM user_preferences WHERE deleted = 0 AND category = 'Home'";
470         $result = $GLOBALS['db']->query($query, true, "Unable to update iFrames and Feeds dashlets!");
471         while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
472                 $content = unserialize(base64_decode($row['contents']));
473                 $assigned_user_id = $row['assigned_user_id'];
474                 $record_id = $row['id'];
475                 $current_user = new User();
476         $current_user->retrieve($row['assigned_user_id']);
477         
478                 if(!empty($content['dashlets']) && !empty($content['pages'])){
479                         $originalDashlets = $content['dashlets'];
480                         $originalPages = $content['pages'];
481                         
482                         //Determine if the original perference has already had the two dashlets or not
483                         foreach($originalDashlets as $key=>$ds){
484                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_DISCOVER_SUGAR_PRO'){
485                                    $originalDashlets[$key]['module'] = 'Home';
486                                 }
487                                 if(!empty($ds['options']['title']) && $ds['options']['title'] == 'LBL_DASHLET_SUGAR_NEWS'){
488                                    $originalDashlets[$key]['module'] = 'Home';
489                                 }
490                         }
491                         
492                         $current_user->setPreference('dashlets', $originalDashlets, 0, 'Home');
493                         $current_user->setPreference('pages', $originalPages, 0, 'Home');       
494                 }
495         } //while       
496         
497         $remove_iframes = false;
498         $remove_feeds = false;
499         
500         //Check if we should remove iframes.  Use the count of entries in iframes table
501         $result = $GLOBALS['db']->query('SELECT count(id) as total from iframes');
502         if(!empty($result)) {
503                 $row = $GLOBALS['db']->fetchByAssoc($result);
504                 if($row['total'] == 0) {
505                    $remove_iframes = true;
506                 }
507         }
508         
509         //Check if we should remove Feeds.  We check if the tab is hidden
510         require_once("modules/MySettings/TabController.php");
511         $controller = new TabController();      
512         $tabs = $controller->get_tabs_system();
513
514         //If the Feeds tab is hidden then remove it
515         if(!isset($tabs[0]['Feeds'])) {
516            $remove_feeds = true;
517         }
518         
519         if($remove_feeds) {
520            //Remove the modules/Feeds files
521            if(is_dir('modules/Feeds')) {
522            _logThis('Removing the Feeds files', $path);
523            rmdir_recursive('modules/Feeds');
524            }
525                 
526            //Drop the table
527            _logThis('Removing the Feeds table', $path);
528            $GLOBALS['db']->dropTableName('feeds');
529         } else {
530            if(file_exists('modules/Feeds')) {
531                    _logThis('Writing Feed.php module to custom/Extension/application/Ext/Include', $path);
532                    write_to_modules_ext_php('Feed', 'Feeds', 'modules/Feeds/Feed.php', true);
533            }
534         }
535         
536         if($remove_iframes) {
537                 //Remove the module/iFrames files
538                 if(is_dir('modules/iFrames')) {
539                 _logThis('Removing the iFrames files', $path);
540                 rmdir_recursive('modules/iFrames');
541                 }
542                 
543                 //Drop the table
544                 _logThis('Removing the iframes table', $path);
545                 $GLOBALS['db']->dropTableName('iframes');
546         } else {
547            if(file_exists('modules/iFrames')) {
548                   _logThis('Writing iFrame.php module to custom/Extension/application/Ext/Include', $path);
549                   write_to_modules_ext_php('iFrame', 'iFrames', 'modules/iFrames/iFrame.php', true);
550            }
551         }       
552 }
553
554 function write_to_modules_ext_php($module, $class, $path, $show=false) {
555         
556         global $beanList, $beanFiles;
557         include('include/modules.php');
558         if(!isset($beanFiles[$module])) {
559                 $str = "<?php \n //WARNING: The contents of this file are auto-generated\n";
560
561                         if(!empty($module) && !empty($class) && !empty($path)){
562                                 $str .= "\$beanList['$module'] = '$class';\n";
563                                 $str .= "\$beanFiles['$class'] = '$path';\n";
564                                 if($show){
565                                         $str .= "\$moduleList[] = '$module';\n";
566                                 }else{
567                                         $str .= "\$modules_exempt_from_availability_check['$module'] = '$module';\n";
568                                         $str .= "\$modInvisList[] = '$module';\n";
569                                 }
570                         }
571
572                 $str.= "\n?>";
573                 if(!file_exists("custom/Extension/application/Ext/Include")) {
574                         mkdir_recursive("custom/Extension/application/Ext/Include", true);
575                 }
576                 
577                 $out = sugar_fopen("custom/Extension/application/Ext/Include/{$module}.php", 'w');
578                 fwrite($out, $str);
579                 fclose($out);
580         }
581
582 }
583
584 function _logThis($string, $path) {
585         //echo $string . "\n";
586         //no-opt
587 }
588
589
590
591
592 ?>