PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modules/UpgradeWizard/Bug32003_Test.php

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