PageRenderTime 42ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modules/UpgradeWizard/RepairDatabaseTest.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 130 lines | 66 code | 21 blank | 43 comment | 8 complexity | b49b874afe56bc9eaa9232a5371098a2 MD5 | raw file
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  5. *
  6. * This program 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 SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program 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 SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25. *
  26. * The interactive user interfaces in modified source and object code 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 "Powered by
  32. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33. * technical reasons, the Appropriate Legal Notices must display the words
  34. * "Powered by SugarCRM".
  35. ********************************************************************************/
  36. require_once 'include/database/DBManagerFactory.php';
  37. class RepairDatabaseTest extends Sugar_PHPUnit_Framework_TestCase
  38. {
  39. var $db;
  40. public function setUp()
  41. {
  42. $this->markTestSkipped('Skip for now');
  43. $this->db = DBManagerFactory::getInstance();
  44. if($this->db->dbType == 'mysql')
  45. {
  46. $sql = 'ALTER TABLE meetings ALTER COLUMN status SET DEFAULT NULL';
  47. $sql2 = 'ALTER TABLE calls ALTER COLUMN status SET DEFAULT NULL';
  48. $sql3 = 'ALTER TABLE tasks ALTER COLUMN status SET DEFAULT NULL';
  49. //Run the SQL
  50. $this->db->query($sql);
  51. $this->db->query($sql2);
  52. $this->db->query($sql3);
  53. }
  54. }
  55. public function tearDown()
  56. {
  57. if($this->db->dbType == 'mysql')
  58. {
  59. $sql = "ALTER TABLE meetings ALTER COLUMN status SET DEFAULT 'Planned'";
  60. $sql2 = "ALTER TABLE calls ALTER COLUMN status SET DEFAULT 'Planned'";
  61. $sql3 = "ALTER TABLE tasks ALTER COLUMN status SET DEFAULT 'Not Started'";
  62. //Run the SQL
  63. $this->db->query($sql);
  64. $this->db->query($sql2);
  65. $this->db->query($sql3);
  66. }
  67. }
  68. public function testRepairTableParams()
  69. {
  70. if($this->db->dbType != 'mysql')
  71. {
  72. $this->markTestSkipped('Skip if not mysql db');
  73. return;
  74. }
  75. $bean = new Meeting();
  76. $result = $this->getRepairTableParamsResult($bean);
  77. $this->assertRegExp('/ALTER TABLE meetings\s+?modify column status varchar\(100\) DEFAULT \'Planned\' NULL/i', $result);
  78. /*
  79. $bean = new Call();
  80. $result = $this->getRepairTableParamsResult($bean);
  81. $this->assertTrue(!empty($result));
  82. $this->assertRegExp('/ALTER TABLE calls\s+?modify column status varchar\(100\) DEFAULT \'Planned\' NULL/i', $result);
  83. */
  84. $bean = new Task();
  85. $result = $this->getRepairTableParamsResult($bean);
  86. $this->assertTrue(!empty($result));
  87. $this->assertRegExp('/ALTER TABLE tasks\s+?modify column status varchar\(100\) DEFAULT \'Not Started\' NULL/i', $result);
  88. }
  89. private function getRepairTableParamsResult($bean)
  90. {
  91. $indices = $bean->getIndices();
  92. $fielddefs = $bean->getFieldDefinitions();
  93. $tablename = $bean->getTableName();
  94. //Clean the indicies to prevent duplicate definitions
  95. $new_indices = array();
  96. foreach($indices as $ind_def)
  97. {
  98. $new_indices[$ind_def['name']] = $ind_def;
  99. }
  100. global $dictionary;
  101. $engine=null;
  102. if (isset($dictionary[$bean->getObjectName()]['engine']) && !empty($dictionary[$bean->getObjectName()]['engine']) )
  103. {
  104. $engine = $dictionary[$bean->getObjectName()]['engine'];
  105. }
  106. $result = $this->db->repairTableParams($bean->table_name, $fielddefs, $new_indices, false, $engine);
  107. return $result;
  108. }
  109. }