Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
AutoBuildDatabaseTest.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 
44  {
45  public static function setUpBeforeClass()
46  {
47  parent::setUpBeforeClass();
48  SecurityTestHelper::createSuperAdmin();
49  Yii::app()->gameHelper->muteScoringModelsOnSave();
50  }
51 
52  public static function tearDownAfterClass()
53  {
54  Yii::app()->gameHelper->unmuteScoringModelsOnSave();
55  parent::tearDownAfterClass();
56  }
57 
58  public function testAutoBuildDatabase()
59  {
60  $super = User::getByUsername('super');
61  Yii::app()->user->userModel = $super;
62  $messageLogger = new MessageLogger();
63  $beforeRowCount = DatabaseCompatibilityUtil::getTableRowsCountTotal();
64  InstallUtil::autoBuildDatabase($messageLogger, true);
65  E::deleteAll(); // because if we are running All, C, D and E get autobuild and 2 validators in E cause E to have a record which gets deleted on tearDown.
66  $afterRowCount = DatabaseCompatibilityUtil::getTableRowsCountTotal();
67  $this->assertEquals($beforeRowCount, $afterRowCount);
68  }
69 
73  public function testColumnType()
74  {
75  $rootModels = array();
76  foreach (Module::getModuleObjects() as $module)
77  {
78  $moduleAndDependenciesRootModelNames = $module->getRootModelNamesIncludingDependencies();
79  $rootModels = array_merge( $rootModels,
80  array_diff($moduleAndDependenciesRootModelNames,
81  $rootModels));
82  }
83 
84  foreach ($rootModels as $model)
85  {
86  $meta = $model::getDefaultMetadata();
87  if (isset($meta[$model]['rules']))
88  {
89  $tableName = $model::getTableName();
90  $columns = ZurmoRedBean::$writer->getColumns($tableName);
91  foreach ($meta[$model]['rules'] as $rule)
92  {
93  if (is_array($rule) && count($rule) >= 3)
94  {
95  $attributeName = $rule[0];
96  $validatorName = $rule[1];
97  $validatorParameters = array_slice($rule, 2);
98  switch ($validatorName)
99  {
100  case 'type':
101  if (isset($validatorParameters['type']))
102  {
103  $type = $validatorParameters['type'];
104  $field = strtolower($attributeName);
105  $columnType = false;
106  if (isset($columns[$field]))
107  {
108  $columnType = $columns[$field];
109  }
110  $compareType = null;
111  $compareTypes = $this->getDatabaseTypesByType($type);
112  // Remove brackets from database type
113  $bracketPosition = stripos($columnType, '(');
114  if ($bracketPosition !== false)
115  {
116  $columnType = substr($columnType, 0, $bracketPosition);
117  }
118 
119  $databaseColumnType = strtoupper(trim($columnType));
120  $compareTypeString = implode(',', $compareTypes); // Not Coding Standard
121  if (!in_array($databaseColumnType, $compareTypes))
122  {
123  $this->fail("Actual database type {$databaseColumnType} not in expected types: {$compareTypeString}.");
124  }
125  }
126  break;
127  }
128  }
129  }
130  }
131  }
132  }
133 
137  public function testAutoBuildUpgrade()
138  {
139  // adding Text Field
140  $metadata = Account::getMetadata();
141  $metadata['Account']['members'][] = 'newField';
142  $rules = array('newField', 'type', 'type' => 'string');
143  $metadata['Account']['rules'][] = $rules;
144 
145  $metadata['Account']['members'][] = 'string128';
146  $rules = array('string128', 'type', 'type' => 'string');
147  $metadata['Account']['rules'][] = $rules;
148  $rules = array('string128', 'length', 'min' => 1, 'max' => 128);
149  $metadata['Account']['rules'][] = $rules;
150 
151  $metadata['Account']['members'][] = 'string555';
152  $rules = array('string555', 'type', 'type' => 'string');
153  $metadata['Account']['rules'][] = $rules;
154  $rules = array('string555', 'length', 'min' => 1, 'max' => 555);
155  $metadata['Account']['rules'][] = $rules;
156 
157  $metadata['Account']['members'][] = 'string100000';
158  $rules = array('string100000', 'type', 'type' => 'string');
159  $metadata['Account']['rules'][] = $rules;
160  $rules = array('string100000', 'length', 'min' => 1, 'max' => 100000);
161  $metadata['Account']['rules'][] = $rules;
162 
163  $metadata['Account']['members'][] = 'textField';
164  $rules = array('textField', 'type', 'type' => 'text');
165  $metadata['Account']['rules'][] = $rules;
166 
167  $metadata['Account']['members'][] = 'longTextField';
168  $rules = array('longTextField', 'type', 'type' => 'longtext');
169  $metadata['Account']['rules'][] = $rules;
170 
171  $metadata['Account']['members'][] = 'dateField';
172  $rules = array('dateField', 'type', 'type' => 'date');
173  $metadata['Account']['rules'][] = $rules;
174 
175  $metadata['Account']['members'][] = 'booleanField';
176  $rules = array('booleanField', 'boolean');
177  $metadata['Account']['rules'][] = $rules;
178 
179  $metadata['Account']['members'][] = 'integerField';
180  $rules = array('integerField', 'type', 'type' => 'integer');
181  $metadata['Account']['rules'][] = $rules;
182 
183  $metadata['Account']['members'][] = 'dateTimeField';
184  $rules = array('dateTimeField', 'type', 'type' => 'datetime');
185  $metadata['Account']['rules'][] = $rules;
186 
187  $metadata['Account']['members'][] = 'urlField';
188  $rules = array('urlField', 'url');
189  $metadata['Account']['rules'][] = $rules;
190 
191  $metadata['Account']['members'][] = 'floatField';
192  $rules = array('floatField', 'type', 'type' => 'float');
193  $metadata['Account']['rules'][] = $rules;
194 
195  $metadata['Account']['members'][] = 'blobField';
196  $rules = array('blobField', 'type', 'type' => 'blob');
197  $metadata['Account']['rules'][] = $rules;
198 
199  $metadata['Account']['members'][] = 'longBlobField';
200  $rules = array('longBlobField', 'type', 'type' => 'longblob');
201  $metadata['Account']['rules'][] = $rules;
202 
203  Account::setMetadata($metadata);
204 
205  $super = User::getByUsername('super');
206  Yii::app()->user->userModel = $super;
207  $messageLogger = new MessageLogger();
208  $beforeRowCount = DatabaseCompatibilityUtil::getTableRowsCountTotal();
209  InstallUtil::autoBuildDatabase($messageLogger, true);
210 
211  $afterRowCount = DatabaseCompatibilityUtil::getTableRowsCountTotal();
212  $this->assertEquals($beforeRowCount, $afterRowCount);
213 
214  //Check Account fields
215  $tableName = Account::getTableName();
216  $columns = ZurmoRedBean::$writer->getColumns($tableName);
217  $unsigned = '';
219  {
220  $unsigned = ' unsigned';
221  }
222 
223  $this->assertEquals('text', $columns['newfield']);
224  $this->assertEquals('varchar(128)', $columns['string128']);
225  $this->assertEquals('text', $columns['string555']);
226  $this->assertEquals('longtext', $columns['string100000']);
227  $this->assertEquals('text', $columns['textfield']);
228  $this->assertEquals('date', $columns['datefield']);
229  $this->assertEquals('tinyint(1) unsigned', $columns['booleanfield']);
230  $this->assertEquals('int(11)' . $unsigned, $columns['integerfield']);
231  $this->assertEquals('datetime', $columns['datetimefield']);
232  $this->assertEquals('varchar(255)', $columns['urlfield']);
233  $this->assertEquals('double', $columns['floatfield']);
234  $this->assertEquals('longtext', $columns['longtextfield']);
235  $this->assertEquals('blob', $columns['blobfield']);
236  $this->assertEquals('longblob', $columns['longblobfield']);
237 
238  $account = new Account();
239  $account->name = 'Test Name';
240  $account->owner = $super;
241  $randomString = str_repeat("Aa", 64);
242  $account->string128 = $randomString;
243  $this->assertTrue($account->save());
244 
245  $metadata = Account::getMetadata();
246 
247  foreach ($metadata['Account']['rules'] as $key => $rule)
248  {
249  if ($rule[0] == 'string128' && $rule[1] == 'length')
250  {
251  $metadata['Account']['rules'][$key]['max'] = 64;
252  }
253  }
254  Account::setMetadata($metadata);
255  InstallUtil::autoBuildDatabase($messageLogger, true);
256 
258  $modifiedAccount = Account::getById($account->id);
259 
260  // autobuild should not decrease length or display width of an existing column
261  $this->assertEquals($randomString, $modifiedAccount->string128);
262  $this->assertEquals(128, strlen($modifiedAccount->string128));
263 
264  //Check Account fields
265  $tableName = Account::getTableName();
266  $columns = ZurmoRedBean::$writer->getColumns($tableName);
267  $this->assertEquals('varchar(128)', $columns['string128']);
268  }
269 
275  protected function getDatabaseTypesByType($type)
276  {
277  $typeArray = array(
278  'string' => array('VARCHAR', 'TEXT', 'LONGTEXT'),
279  'text' => array('TEXT'),
280  'longtext' => array('LONGTEXT'),
281  'integer' => array('TINYINT', 'INT', 'INTEGER', 'BIGINT'),
282  'float' => array('FLOAT', 'DOUBLE'),
283  'year' => array('YEAR'),
284  'date' => array('DATE'),
285  'datetime' => array('DATETIME'),
286  'blob' => array('BLOB'),
287  'longblob' => array('LONG_BLOB'),
288  'boolean' => array('TINYINT'),
289  );
290 
291  $databaseTypes = array();
292  if (isset($typeArray[$type]))
293  {
294  $databaseTypes = $typeArray[$type];
295  }
296  return $databaseTypes;
297  }
298  }
299 ?>
static getByUsername($username)
Definition: User.php:49
static forgetAll()
static getModuleObjects()
Definition: Module.php:51
static getById($id, $modelClassName=null)
static getTableName()
static getMetadata()
static setMetadata(array $metadata)
Generated on Fri Jul 3 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.