Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
DatabaseCompatibilityUtilTest.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 
38  {
39  protected $testDatabaseHostname;
40  protected $testDatabasePort = 3306;
41  protected $testDatabaseName;
42  protected $testDatabaseUsername;
43  protected $testDatabasePassword;
44  protected $temporaryDatabaseHostname;
45  protected $temporaryDatabasePort = 3306;
46  protected $temporaryDatabaseUsername;
47  protected $temporaryDatabasePassword;
48  protected $temporaryDatabaseName;
49  protected $superUserPassword;
50  protected $databaseBackupTestFile;
51 
52  public function __construct()
53  {
54  parent::__construct();
55  list(, $this->temporaryDatabaseHostname, $this->temporaryDatabasePort, $this->temporaryDatabaseName) =
56  array_values(RedBeanDatabase::getDatabaseInfoFromDsnString(Yii::app()->tempDb->connectionString));
57  $this->temporaryDatabaseUsername = Yii::app()->tempDb->username;
58  $this->temporaryDatabasePassword = Yii::app()->tempDb->password;
59  list(, $this->testDatabaseHostname, $this->testDatabasePort, $this->testDatabaseName) =
60  array_values(RedBeanDatabase::getDatabaseInfoFromDsnString(Yii::app()->db->connectionString));
61  $this->testDatabaseUsername = Yii::app()->db->username;
62  $this->testDatabasePassword = Yii::app()->db->password;
63  $this->superUserPassword = 'super';
64  $this->databaseBackupTestFile = INSTANCE_ROOT . '/protected/runtime/databaseBackupTest.sql';
65  }
66 
67  public static function setUpBeforeClass()
68  {
69  parent::setUpBeforeClass();
70  SecurityTestHelper::createSuperAdmin();
71  }
72 
73  public static function getDependentTestModelClassNames()
74  {
75  return array('TestDatabaseBulkInsertModel');
76  }
77 
78  public function setup()
79  {
80  RedBeanDatabase::close();
81  RedBeanDatabase::setup(Yii::app()->db->connectionString,
82  Yii::app()->db->username,
83  Yii::app()->db->password,
84  true);
85  }
86 
87  public function tearDown()
88  {
89  RedBeanDatabase::close();
90  RedBeanDatabase::setup(Yii::app()->db->connectionString,
91  Yii::app()->db->username,
92  Yii::app()->db->password,
93  true);
94  if (is_file($this->databaseBackupTestFile))
95  {
96  unlink($this->databaseBackupTestFile);
97  }
98  }
99 
100  public function testCharLength()
101  {
102  $res = DatabaseCompatibilityUtil::charLength('tempColumn');
103  $this->assertEquals('char_length(tempColumn)', $res);
104  }
105 
106  public function testConcat()
107  {
108  $res = DatabaseCompatibilityUtil::concat(array('column1', 'column2'));
109  $this->assertEquals('concat(column1, column2)', $res);
110  }
111 
112  public function testGetAllTableNames()
113  {
114  ZurmoRedBean::exec("create table if not exists temptable (temptable_id int(11) unsigned not null)");
116  $this->assertTrue(in_array('temptable', $tables));
117  }
118 
119  public function testGetDateFormat()
120  {
121  $this->assertEquals('yyyy-MM-dd', DatabaseCompatibilityUtil::getDateFormat());
122  }
123 
124  public function testGetDateTimeFormat()
125  {
126  $this->assertEquals('yyyy-MM-dd HH:mm:ss', DatabaseCompatibilityUtil::getDateTimeFormat());
127  }
128 
129  public function testGetMaxVarCharLength()
130  {
131  $this->assertEquals(255, DatabaseCompatibilityUtil::getMaxVarCharLength());
132  }
133 
134  public function testLower()
135  {
136  $this->assertEquals('lower(tempColumn)', DatabaseCompatibilityUtil::lower('tempColumn'));
137  }
138 
139  public function testGetQuote()
140  {
141  if (RedBeanDatabase::getDatabaseType() == 'pgsql')
142  {
143  $quoteCharacter = '"';
144  }
145  else
146  {
147  $quoteCharacter = '`';
148  }
149  $this->assertEquals($quoteCharacter, DatabaseCompatibilityUtil::getQuote());
150  }
151 
152  public function testGetTrue()
153  {
154  if (RedBeanDatabase::getDatabaseType() == 'pgsql')
155  {
156  $trueValue = '1';
157  }
158  else
159  {
160  $trueValue = 'true';
161  }
162  $this->assertEquals($trueValue, DatabaseCompatibilityUtil::getTrue());
163  }
164 
165  public function testLength()
166  {
167  $this->assertEquals('length(tempColumn)', DatabaseCompatibilityUtil::length('tempColumn'));
168  }
169 
170  public function testQuoteString()
171  {
172  $string = 'tempColumn';
173  if (RedBeanDatabase::getDatabaseType() == 'pgsql')
174  {
175  $quotedString = '"tempColumn"';
176  }
177  else
178  {
179  $quotedString = '`tempColumn`';
180  }
181  $this->assertEquals($quotedString, DatabaseCompatibilityUtil::quoteString($string));
182  }
183 
184  public function testGetOperatorAndValueWherePart()
185  {
187  $compareQueryPart = "= 1";
188  $this->assertEquals($compareQueryPart, $queryPart);
189 
190  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('equals', 'test@zumrmo.com');
191  $compareQueryPart = "= 'test@zumrmo.com'";
192  $this->assertEquals($compareQueryPart, $queryPart);
193 
194  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('greaterThan', 5);
195  $compareQueryPart = "> 5";
196  $this->assertEquals($compareQueryPart, $queryPart);
197 
198  $exceptionThrowed = false;
199  try
200  {
201  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('oneOf', 'aaa');
202  }
203  catch (NotSupportedException $e)
204  {
205  $exceptionThrowed = true;
206  }
207  $this->assertTrue($exceptionThrowed);
208 
209  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('startsWith', 'aaa');
210  $compareQueryPart = "like 'aaa%'";
211  $this->assertEquals($compareQueryPart, $queryPart);
212 
213  $exceptionThrowed = false;
214  try
215  {
216  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('startsWith', 5);
217  }
218  catch (NotSupportedException $e)
219  {
220  $exceptionThrowed = true;
221  }
222  $this->assertTrue($exceptionThrowed);
223 
224  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('oneOf', array(5, 6, 7));
225  $compareQueryPart = "IN(5,6,7)"; // Not Coding Standard
226  $this->assertEquals($compareQueryPart, $queryPart);
227 
228  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('oneOf', array('a', 'b', 'c'));
229  $compareQueryPart = "IN('a','b','c')"; // Not Coding Standard
230  $this->assertEquals($compareQueryPart, $queryPart);
231  }
232 
233  public function testGetOperatorAndValueWherePartForNullOrEmpty()
234  {
235  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('isNull', null);
236  $compareQueryPart = "IS NULL"; // Not Coding Standard
237  $this->assertEquals($compareQueryPart, $queryPart);
238  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('isNotNull', null);
239  $compareQueryPart = "IS NOT NULL"; // Not Coding Standard
240  $this->assertEquals($compareQueryPart, $queryPart);
241  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('isEmpty', null);
242  $compareQueryPart = "= ''"; // Not Coding Standard
243  $this->assertEquals($compareQueryPart, $queryPart);
244  $queryPart = DatabaseCompatibilityUtil::getOperatorAndValueWherePart('isNotEmpty', null);
245  $compareQueryPart = "!= ''"; // Not Coding Standard
246  $this->assertEquals($compareQueryPart, $queryPart);
247  }
248 
249  public function testResolveToLowerForStringComparison()
250  {
251  $queryPart = DatabaseCompatibilityUtil::resolveToLowerForStringComparison('equals', 'test@zumrmo.com');
252  $compareQueryPart = "= 'test@zumrmo.com'";
253  $this->assertEquals($compareQueryPart, $queryPart);
254 
255  $queryPart = DatabaseCompatibilityUtil::resolveToLowerForStringComparison('greaterThan', '5');
256  $compareQueryPart = "> '5'";
257  $this->assertEquals($compareQueryPart, $queryPart);
258 
259  if (YII_DEBUG)
260  {
261  try
262  {
263  $queryPart = DatabaseCompatibilityUtil::resolveToLowerForStringComparison('greaterThan', 5);
264  $this->fail();
265  }
266  catch (FailedAssertionException $e)
267  {
268  }
269  }
270  else
271  {
272  $queryPart = DatabaseCompatibilityUtil::resolveToLowerForStringComparison('greaterThan', 5);
273  $compareQueryPart = "> '5'";
274  $this->assertEquals($compareQueryPart, $queryPart);
275  }
276 
277  /*
278  try
279  {
280  $databaseConnectionInfo = RedBeanDatabase::getDatabaseInfoFromDsnString($dsn);
281  $this->fail();
282  }
283  catch (NotSupportedException $e)
284  {
285  }
286  */
287  }
288 
289  public function testBulkInsert()
290  {
291  $model = new TestDatabaseBulkInsertModel();
292  $model->number = 9999;
293  $model->string = 'adasd';
294  $model->save();
295  $model->delete();
296 
297  // Test with different quatations.
299  $columnNames = array('number', 'string');
300  $insertData = array(
301  array(999 , 'It\'s string with quatation.'),
302  array(1000 , "It\`s string with quatation."),
303  array(1001 , 'It\'s string with "quatation".')
304  );
305  DatabaseCompatibilityUtil::bulkInsert($tableName, $insertData, $columnNames, 3);
306 
307  $bulkInsertedRows = ZurmoRedBean::getAll("select * from $tableName order by id");
308  $this->assertEquals(count($bulkInsertedRows), 3);
309  for ($i = 0; $i < 3; $i++)
310  {
311  $this->assertEquals($bulkInsertedRows[$i]['number'], $insertData[$i][0]);
312  $this->assertEquals($bulkInsertedRows[$i]['string'], $insertData[$i][1]);
313  }
314 
316 
317  // Test when there are less rows of data then bulk quantity for one loop.
319  $columnNames = array('number', 'string');
320  $numberOfRows = 50;
321  $bulkQuantity = 100;
322  $insertData = $this->createDumpDataForBulkInsert($numberOfRows);
323 
324  DatabaseCompatibilityUtil::bulkInsert($tableName, $insertData, $columnNames, $bulkQuantity);
325  $bulkInsertedRows = ZurmoRedBean::getAll("select * from $tableName order by id");
326  $this->assertEquals(count($bulkInsertedRows), $numberOfRows);
327  for ($i = 0; $i < $numberOfRows; $i++)
328  {
329  $this->assertEquals($bulkInsertedRows[$i]['number'], $insertData[$i][0]);
330  $this->assertEquals($bulkInsertedRows[$i]['string'], $insertData[$i][1]);
331  }
332 
334 
335  // Test when there is much data, for multiple loops of bulk insert.
336  $numberOfRows = 520;
337  $insertData = $this->createDumpDataForBulkInsert($numberOfRows);
338  $bulkQuantity = 100;
339  $importDataForOneLoop = array();
340  foreach ($insertData as $row)
341  {
342  $importDataForOneLoop[] = $row;
343  if (count($importDataForOneLoop) > $bulkQuantity)
344  {
346  $importDataForOneLoop,
347  $columnNames,
348  $bulkQuantity);
349  $importDataForOneLoop = array();
350  }
351  }
352  $this->assertFalse(count($importDataForOneLoop) > $bulkQuantity);
353  if (count($importDataForOneLoop) > 0)
354  {
355  DatabaseCompatibilityUtil::bulkInsert($tableName, $importDataForOneLoop, $columnNames, $bulkQuantity);
356  }
357 
358  $bulkInsertedRows = ZurmoRedBean::getAll("select * from $tableName order by id");
359  $this->assertEquals(count($bulkInsertedRows), $numberOfRows);
360  for ($i = 0; $i < $numberOfRows; $i++)
361  {
362  $this->assertEquals($bulkInsertedRows[$i]['number'], $insertData[$i][0]);
363  $this->assertEquals($bulkInsertedRows[$i]['string'], $insertData[$i][1]);
364  }
365 
367 
368  //Test with shouldTrim
370  $columnNames = array('number', 'string');
371  $insertData = array(
372  array(999 , ' It\'s string with quotation. '),
373  array(1000 , " It\`s string with quotation."),
374  array(1001 , 'It\'s string with "quotation". '),
375  array(1002 , 'It\'s string with "quotation".'),
376  );
377  DatabaseCompatibilityUtil::bulkInsert($tableName, $insertData, $columnNames, 4, true);
378 
379  $bulkInsertedRows = ZurmoRedBean::getAll("select * from $tableName order by id");
380  $this->assertEquals(count($bulkInsertedRows), 4);
381  for ($i = 0; $i < 4; $i++)
382  {
383  $this->assertEquals($bulkInsertedRows[$i]['number'], trim($insertData[$i][0]));
384  $this->assertEquals($bulkInsertedRows[$i]['string'], trim($insertData[$i][1]));
385  }
386 
388  }
389 
390  protected function createDumpDataForBulkInsert($number)
391  {
392  assert('is_numeric($number) && $number > 0'); // Not Coding Standard
393  $data = array();
394  for ($i = 0; $i < $number; $i++)
395  {
396  $data[$i] = array(
397  $i, $this->generateRandString(20)
398  );
399  }
400  return $data;
401  }
402 
403  protected function generateRandString($length)
404  {
405  $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
406  $size = strlen($chars);
407  $str = '';
408  for ($i = 0; $i < $length; $i++)
409  {
410  $str .= $chars[rand(0, $size - 1)];
411  }
412  return $str;
413  }
414 
415  public function testGetDatabaseVersion()
416  {
417  $databaseVersion = DatabaseCompatibilityUtil::getDatabaseVersion('mysql',
418  $this->temporaryDatabaseHostname,
419  $this->temporaryDatabaseUsername,
420  $this->temporaryDatabasePassword,
421  $this->temporaryDatabasePort);
422  $this->assertTrue(strlen($databaseVersion) > 0);
423  }
424 
425  public function testGetDatabaseMaxAllowedPacketsSizeRb()
426  {
428  $this->assertGreaterThan(0, $maxAllowedPacketSize);
429  }
430 
431  public function testGetDatabaseMaxAllowedPacketsSize()
432  {
433  $maxAllowedPacketSize = DatabaseCompatibilityUtil::getDatabaseMaxAllowedPacketsSize('mysql',
434  $this->temporaryDatabaseHostname,
435  $this->temporaryDatabaseUsername,
436  $this->temporaryDatabasePassword,
437  $this->temporaryDatabasePort);
438  $this->assertGreaterThan(0, $maxAllowedPacketSize);
439  }
440 
441  public function testGetDatabaseMaxSpRecursionDepth()
442  {
443  $maxSpRecursionDepth = DatabaseCompatibilityUtil::getDatabaseMaxSpRecursionDepth('mysql',
444  $this->temporaryDatabaseHostname,
445  $this->temporaryDatabaseUsername,
446  $this->temporaryDatabasePassword,
447  $this->temporaryDatabasePort);
448  $this->assertGreaterThanOrEqual(0, $maxSpRecursionDepth);
449  }
450 
451  public function testGetDatabaseThreadStackValue()
452  {
453  $threadStackValue = DatabaseCompatibilityUtil::getDatabaseThreadStackValue('mysql',
454  $this->temporaryDatabaseHostname,
455  $this->temporaryDatabaseUsername,
456  $this->temporaryDatabasePassword,
457  $this->temporaryDatabasePort);
458  $this->assertGreaterThan(0, $threadStackValue);
459  }
460 
461  public function testGetDatabaseOptimizerSearchDepthValue()
462  {
463  $dbOptimizerSearchDepthValue = DatabaseCompatibilityUtil::getDatabaseOptimizerSearchDepthValue('mysql',
464  $this->temporaryDatabaseHostname,
465  $this->temporaryDatabaseUsername,
466  $this->temporaryDatabasePassword,
467  $this->temporaryDatabasePort);
468  $this->assertTrue($dbOptimizerSearchDepthValue !== false);
469  $this->assertGreaterThanOrEqual(0, $dbOptimizerSearchDepthValue);
470  }
471 
472  public function testGetDatabaseLogBinValue()
473  {
474  $databaseLogBinValue = DatabaseCompatibilityUtil::getDatabaseLogBinValue('mysql',
475  $this->temporaryDatabaseHostname,
476  $this->temporaryDatabaseUsername,
477  $this->temporaryDatabasePassword,
478  $this->temporaryDatabasePort);
479  $this->assertTrue($databaseLogBinValue !== false);
480  $this->assertTrue(is_string($databaseLogBinValue));
481  }
482 
483  public function testGetDatabaseLogBinTrustFunctionCreatorsValue()
484  {
486  $this->temporaryDatabaseHostname,
487  $this->temporaryDatabaseUsername,
488  $this->temporaryDatabasePassword,
489  $this->temporaryDatabasePort);
490  $this->assertTrue($logBinTrustValue !== false);
491  $this->assertTrue(is_string($logBinTrustValue));
492  }
493 
494  public function testGetDatabaseDefaultCollation()
495  {
496  $dbDefaultCollation = DatabaseCompatibilityUtil::getDatabaseDefaultCollation('mysql',
497  $this->testDatabaseHostname,
498  $this->testDatabaseName,
499  $this->testDatabaseUsername,
500  $this->testDatabasePassword,
501  $this->testDatabasePort);
502  $this->assertTrue(is_string($dbDefaultCollation));
503  $this->assertTrue(strlen($dbDefaultCollation) > 0);
504  }
505 
506  public function testIsDatabaseStrictMode()
507  {
508  $isDatabaseStrictMode = DatabaseCompatibilityUtil::isDatabaseStrictMode('mysql',
509  $this->temporaryDatabaseHostname,
510  $this->temporaryDatabaseUsername,
511  $this->temporaryDatabasePassword,
512  $this->temporaryDatabasePort);
513  $this->assertTrue(is_bool($isDatabaseStrictMode));
514  }
515 
516  public function testCheckDatabaseSupportsLoadLocalInFile()
517  {
518  $loadLocalInFile = DatabaseCompatibilityUtil::getDatabaseSupportsLoadLocalInFile('mysql',
519  $this->temporaryDatabaseHostname,
520  $this->temporaryDatabaseUsername,
521  $this->temporaryDatabasePassword,
522  $this->temporaryDatabasePort);
523  $this->assertTrue(is_string($loadLocalInFile));
524  $this->assertTrue(in_array($loadLocalInFile, array('ON', 1, 'OFF', 0)));
525  // TODO: @Shoaibi: Critical: @see: https://www.pivotaltracker.com/s/projects/380027/stories/59409504
526  $this->assertEquals($loadLocalInFile, 0);
527  }
528 
529  public function testDatabaseConnection_mysql()
530  {
531  $this->assertTrue(DatabaseCompatibilityUtil::checkDatabaseConnection('mysql',
532  $this->temporaryDatabaseHostname,
533  $this->temporaryDatabaseUsername,
534  $this->temporaryDatabasePassword,
535  $this->temporaryDatabasePort));
536 
537  $compareData = array(1045, "Access denied for user");
539  $this->temporaryDatabaseHostname,
540  $this->temporaryDatabaseUsername,
541  'wrong',
542  $this->temporaryDatabasePort);
543  $result[1] = substr($result[1], 0, 22);
544  $this->assertEquals($compareData, $result);
546  $this->temporaryDatabaseHostname,
547  'nobody',
548  'password',
549  $this->temporaryDatabasePort);
550  $result[1] = substr($result[1], 0, 22);
551  $this->assertEquals($compareData, $result);
552  }
553 
554  public function testCheckDatabaseExists()
555  {
556  // This test cannot run as saltdev. It is therefore skipped on the server.
557  if ($this->temporaryDatabaseUsername == 'root')
558  {
559  $this->assertTrue(DatabaseCompatibilityUtil::checkDatabaseExists('mysql',
560  $this->testDatabaseHostname,
561  $this->testDatabaseUsername,
562  $this->testDatabasePassword,
563  $this->testDatabasePort,
564  $this->testDatabaseName));
565  $this->assertEquals(array(1049, "Unknown database 'junk'"),
567  $this->temporaryDatabaseHostname,
568  $this->temporaryDatabaseUsername,
569  $this->temporaryDatabasePassword,
570  $this->temporaryDatabasePort,
571  'junk'));
572  }
573  }
574 
575  public function testCheckDatabaseUserExists()
576  {
577  // This test cannot run as saltdev. It is therefore skipped on the server.
578  if ($this->temporaryDatabaseUsername == 'root')
579  {
580  $this->assertTrue (DatabaseCompatibilityUtil::checkDatabaseUserExists('mysql',
581  $this->temporaryDatabaseHostname,
582  $this->temporaryDatabaseUsername,
583  $this->temporaryDatabasePassword,
584  $this->temporaryDatabasePort,
585  $this->temporaryDatabaseUsername));
586  $this->assertFalse(DatabaseCompatibilityUtil::checkDatabaseUserExists('mysql',
587  $this->temporaryDatabaseHostname,
588  $this->temporaryDatabaseUsername,
589  $this->temporaryDatabasePassword,
590  $this->temporaryDatabasePort,
591  'dude'));
592  }
593  }
594 
595  public function testCreateDatabase()
596  {
597  $this->assertTrue(DatabaseCompatibilityUtil::createDatabase('mysql',
598  $this->temporaryDatabaseHostname,
599  $this->temporaryDatabaseUsername,
600  $this->temporaryDatabasePassword,
601  $this->temporaryDatabasePort,
602  $this->temporaryDatabaseName));
603  }
604 
605  public function testCreateDatabaseUser()
606  {
607  // This test cannot run as saltdev. It is therefore skipped on the server.
608  if ($this->temporaryDatabaseUsername == 'root')
609  {
610  $this->assertTrue(DatabaseCompatibilityUtil::createDatabase('mysql',
611  $this->temporaryDatabaseHostname,
612  $this->temporaryDatabaseUsername,
613  $this->temporaryDatabasePassword,
614  $this->temporaryDatabasePort,
615  $this->temporaryDatabaseName));
616  $this->assertTrue(DatabaseCompatibilityUtil::createDatabaseUser('mysql',
617  $this->temporaryDatabaseHostname,
618  $this->temporaryDatabaseUsername,
619  $this->temporaryDatabasePassword,
620  $this->temporaryDatabasePort,
621  $this->temporaryDatabaseName,
622  'wacko',
623  'wacked'));
624  $this->assertTrue(DatabaseCompatibilityUtil::createDatabaseUser('mysql',
625  $this->temporaryDatabaseHostname,
626  $this->temporaryDatabaseUsername,
627  $this->temporaryDatabasePassword,
628  $this->temporaryDatabasePort,
629  $this->temporaryDatabaseName,
630  'wacko',
631  ''));
632  }
633  }
634 
635  public function testGetTableRowsCountTotal()
636  {
637  if (RedBeanDatabase::getDatabaseType() == 'mysql')
638  {
639  $tableRowsCountTotal = DatabaseCompatibilityUtil::getTableRowsCountTotal();
640  ZurmoRedBean::exec("create table if not exists temptesttable (temptable_id int(11) unsigned not null)");
641  ZurmoRedBean::exec("insert into temptesttable (temptable_id) values (2)");
642  ZurmoRedBean::exec("insert into temptesttable (temptable_id) values (9)");
643  $tableRowsCountTotalAfterQuery = DatabaseCompatibilityUtil::getTableRowsCountTotal();
644  $this->assertEquals(2, $tableRowsCountTotalAfterQuery - $tableRowsCountTotal);
645  }
646  }
647 
651  public function testGetDatabaseDefaultPort()
652  {
653  $mysqlDatabaseDefaultPort = DatabaseCompatibilityUtil::getDatabaseDefaultPort('mysql');
654  $this->assertEquals(3306, $mysqlDatabaseDefaultPort);
655 
656  $mysqlDatabaseDefaultPort = DatabaseCompatibilityUtil::getDatabaseDefaultPort('pgsql');
657  }
658 
659  public function testDatabaseBackupAndRestore()
660  {
661  // Create new database (zurmo_temp).
662  if (RedBeanDatabase::getDatabaseType() == 'mysql')
663  {
664  $this->assertTrue(DatabaseCompatibilityUtil::createDatabase('mysql',
665  $this->temporaryDatabaseHostname,
666  $this->temporaryDatabaseUsername,
667  $this->temporaryDatabasePassword,
668  $this->temporaryDatabasePort,
669  $this->temporaryDatabaseName));
670  $connection = @mysqli_connect($this->temporaryDatabaseHostname,
671  $this->temporaryDatabaseUsername,
672  $this->temporaryDatabasePassword,
673  '',
674  $this->temporaryDatabasePort);
675  $this->assertTrue(is_object($connection));
676 
677  @mysqli_select_db($connection, $this->temporaryDatabaseName);
678  @mysqli_query($connection, "create table temptable (temptable_id int(11) unsigned not null)");
679  @mysqli_query($connection, "insert into temptable values ('5')");
680  @mysqli_query($connection, "insert into temptable values ('10')");
681  $result = @mysqli_query($connection, "SELECT count(*) from temptable");
682  $totalRows = mysqli_fetch_row($result);
683  @mysqli_close($connection);
684 
685  $this->assertEquals(2, $totalRows[0]);
686 
687  $this->assertTrue(DatabaseCompatibilityUtil::backupDatabase('mysql',
688  $this->temporaryDatabaseHostname,
689  $this->temporaryDatabaseUsername,
690  $this->temporaryDatabasePassword,
691  $this->temporaryDatabasePort,
692  $this->temporaryDatabaseName,
693  $this->databaseBackupTestFile));
694 
695  //Drop database, and restore it from backup.
696  $this->assertTrue(DatabaseCompatibilityUtil::createDatabase('mysql',
697  $this->temporaryDatabaseHostname,
698  $this->temporaryDatabaseUsername,
699  $this->temporaryDatabasePassword,
700  $this->temporaryDatabasePort,
701  $this->temporaryDatabaseName));
702  $this->assertTrue(DatabaseCompatibilityUtil::restoreDatabase('mysql',
703  $this->temporaryDatabaseHostname,
704  $this->temporaryDatabaseUsername,
705  $this->temporaryDatabasePassword,
706  $this->temporaryDatabasePort,
707  $this->temporaryDatabaseName,
708  $this->databaseBackupTestFile));
709  $connection = @mysqli_connect($this->temporaryDatabaseHostname,
710  $this->temporaryDatabaseUsername,
711  $this->temporaryDatabasePassword,
712  '',
713  $this->temporaryDatabasePort);
714 
715  $this->assertTrue(is_object($connection));
716 
717  @mysqli_select_db($connection, $this->temporaryDatabaseName);
718  $result = @mysqli_query($connection, "SELECT count(*) from temptable");
719  $totalRows = mysqli_fetch_row($result);
720 
721  $result = @mysqli_query($connection, "SELECT * from temptable");
722  $rows1 = mysqli_fetch_row($result);
723  $rows2 = mysqli_fetch_row($result);
724 
725  @mysqli_close($connection);
726 
727  $this->assertEquals(2, $totalRows[0]);
728  $this->assertEquals(5, $rows1[0]);
729  $this->assertEquals(10, $rows2[0]);
730  }
731  }
732 
733  public function testMapHintTypeIntoDatabaseColumnType()
734  {
735  if (RedBeanDatabase::getDatabaseType() == 'mysql')
736  {
738  $this->assertEquals('BLOB', $databaseColumnType);
739 
740  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('longblob');
741  $this->assertEquals('LONGBLOB', $databaseColumnType);
742 
743  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('boolean');
744  $this->assertEquals('TINYINT(1) UNSIGNED', $databaseColumnType);
745 
747  $this->assertEquals('DATE', $databaseColumnType);
748 
749  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('datetime');
750  $this->assertEquals('DATETIME', $databaseColumnType);
751 
752  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('string');
753  $this->assertEquals('VARCHAR(255)', $databaseColumnType);
754 
756  $this->assertEquals('TEXT', $databaseColumnType);
757 
758  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('longtext');
759  $this->assertEquals('LONGTEXT', $databaseColumnType);
760 
762  $this->assertEquals('INT(11) UNSIGNED', $databaseColumnType);
763 
764  try
765  {
766  $databaseColumnType = DatabaseCompatibilityUtil::mapHintTypeIntoDatabaseColumnType('invalidType');
767  $this->fail();
768  }
769  catch (NotSupportedException $e)
770  {
771  // Do nothing
772  }
773  }
774  }
775 
776  public function testMakeTimeZoneAdjustmentContent()
777  {
778  Yii::app()->user->userModel = User::getByUsername('super');
779  $this->assertNull(DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent());
780  $tempTimeZone = Yii::app()->user->userModel->timeZone;
781 
782  Yii::app()->user->userModel->timeZone = 'America/Chicago';
783  //Deal with daylight savings time.
784  $timeZoneObject = new DateTimeZone(Yii::app()->user->userModel->timeZone);
785  $offsetInSeconds = $timeZoneObject->getOffset(new DateTime());
786  $this->assertTrue($offsetInSeconds == -18000 || $offsetInSeconds == -21600);
787 
788  $compareContent = ' - INTERVAL ' . abs($offsetInSeconds) . ' SECOND';
789  $this->assertEquals($compareContent, DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent());
790 
791  Yii::app()->user->userModel->timeZone = 'Asia/Tokyo';
792  $compareContent = ' + INTERVAL 32400 SECOND';
793  $this->assertEquals($compareContent, DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent());
794 
795  Yii::app()->user->userModel->timeZone = $tempTimeZone;
796  }
797  }
798 ?>
static bulkInsert($tableName, &$rowsOfColumnValues, &$columnNames, $bulkQuantity, $shouldTrim=false)
static getDatabaseMaxAllowedPacketsSize($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static backupDatabase($databaseType= 'mysql', $host, $username, $password, $port, $databaseName, $backupFilePath)
static getByUsername($username)
Definition: User.php:49
static setup($dsn, $username, $password)
static mapHintTypeIntoDatabaseColumnType($hintType, $length=null)
static getDatabaseLogBinTrustFunctionCreatorsValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseLogBinValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static createDatabase($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName)
static getDatabaseInfoFromDsnString($dsn)
static getDatabaseThreadStackValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseOptimizerSearchDepthValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static isDatabaseStrictMode($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static deleteAll()
static getDatabaseMaxSpRecursionDepth($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseDefaultPort($databaseType= 'mysql')
static checkDatabaseConnection($databaseType, $host, $rootUsername, $rootPassword, $port)
static checkDatabaseUserExists($databaseType, $host, $rootUsername, $rootPassword, $port, $username)
static getDatabaseDefaultCollation($databaseType, $databaseHostname, $databaseName, $databaseUsername, $databasePassword, $databasePort)
static createDatabaseUser($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName, $username, $password)
static getTableName()
static getDatabaseVersion($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getOperatorAndValueWherePart($operatorType, $value)
static checkDatabaseExists($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName)
Generated on Mon Nov 30 2020 07:10:40
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.