2 require_once 'include/database/DBManagerFactory.php';
3 require_once 'modules/Contacts/Contact.php';
4 require_once 'modules/Cases/Case.php';
6 class DBHelperTest extends Sugar_PHPUnit_Framework_TestCase
11 public function setUp()
13 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
14 $this->_db = &DBManagerFactory::getInstance();
15 $this->_helper = $this->_db->getHelper();
18 public function tearDown()
20 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
21 unset($GLOBALS['current_user']);
22 $this->_db->disconnect();
25 public function testCreateTableSQL()
27 $sql = $this->_helper->createTableSQL(new Contact);
29 $this->assertRegExp("/create\s*table\s*contacts/i",$sql);
32 public function testCreateTableSQLParams()
36 $sql = $this->_helper->createTableSQLParams(
37 $bean->getTableName(),
38 $bean->getFieldDefinitions(),
41 $this->assertRegExp("/create\s*table\s*contacts/i",$sql);
44 public function testInsertSQL()
46 $sql = $this->_helper->insertSQL(new Contact);
48 $this->assertRegExp("/insert\s*into\s*contacts/i",$sql);
54 public function testInsertSQLProperlyDecodesHtmlEntities()
57 $bean->last_name = '"Test"';
59 $sql = $this->_helper->insertSQL($bean);
61 $this->assertNotContains(""",$sql);
64 public function testUpdateSQL()
66 $sql = $this->_helper->updateSQL(new Contact, array("id" => "1"));
68 $this->assertRegExp("/update\s*contacts\s*set/i",$sql);
69 $this->assertRegExp("/where\s*contacts.id\s*=\s*'1'/i",$sql);
75 public function testUpdateSQLProperlyDecodesHtmlEntities()
78 $bean->last_name = '"Test"';
80 $sql = $this->_helper->updateSQL($bean, array("id" => "1"));
82 $this->assertNotContains(""",$sql);
85 public function testDeleteSQL()
87 $sql = $this->_helper->deleteSQL(new Contact, array("id" => "1"));
89 $this->assertRegExp("/update\s*contacts\s*set\s*deleted\s*=\s*1/i",$sql);
90 $this->assertRegExp("/where\s*contacts.id\s*=\s*'1'/i",$sql);
93 public function testRetrieveSQL()
95 $sql = $this->_helper->retrieveSQL(new Contact, array("id" => "1"));
97 $this->assertRegExp("/select\s*\*\s*from\s*contacts/i",$sql);
98 $this->assertRegExp("/where\s*contacts.id\s*=\s*'1'/i",$sql);
101 public function testRetrieveViewSQL()
103 // TODO: write this test
106 public function testCreateIndexSQL()
108 $sql = $this->_helper->createIndexSQL(
110 array('id' => array('name'=>'id')),
113 $this->assertRegExp("/create\s*unique\s*index\s*idx_id\s*on\s*contacts\s*\(\s*id\s*\)/i",$sql);
115 $sql = $this->_helper->createIndexSQL(
117 array('id' => array('name'=>'id')),
121 $this->assertRegExp("/create\s*index\s*idx_id\s*on\s*contacts\s*\(\s*id\s*\)/i",$sql);
123 $sql = $this->_helper->createIndexSQL(
125 array('id' => array('name'=>'id'),'deleted' => array('name'=>'deleted')),
128 $this->assertRegExp("/create\s*unique\s*index\s*idx_id\s*on\s*contacts\s*\(\s*id\s*,\s*deleted\s*\)/i",$sql);
131 public function testGetFieldType()
134 'dbType' => 'varchar',
138 'data_type' => 'email',
141 $this->assertEquals($this->_helper->getFieldType($fieldDef),'varchar');
142 unset($fieldDef['dbType']);
143 $this->assertEquals($this->_helper->getFieldType($fieldDef),'int');
144 unset($fieldDef['dbtype']);
145 $this->assertEquals($this->_helper->getFieldType($fieldDef),'char');
146 unset($fieldDef['type']);
147 $this->assertEquals($this->_helper->getFieldType($fieldDef),'bool');
148 unset($fieldDef['Type']);
149 $this->assertEquals($this->_helper->getFieldType($fieldDef),'email');
151 public function testGetAutoIncrement()
156 $case->retrieve($case->id);
157 $lastAuto = $case->case_number;
158 $this->assertEquals($lastAuto + 1, $this->_helper->getAutoIncrement("cases", "case_number"));
159 $case->deleted = true;
162 public function testSetAutoIncrementStart()
167 $case->retrieve($case->id);
168 $lastAuto = $case->case_number;
169 $case->deleted = true;
171 $newAuto = $lastAuto + 5;
172 $this->_helper->setAutoIncrementStart("cases", "case_number", $newAuto);
173 $case2 = new aCase();
174 $case2->name = "foo2";
176 $case2->retrieve($case2->id);
177 $this->assertEquals($newAuto, $case2->case_number);
178 $case2->deleted = true;
181 public function testAddColumnSQL()
183 $sql = $this->_helper->addColumnSQL(
185 array('foo' => array('name'=>'foo','type'=>'varchar'))
188 $this->assertRegExp("/alter\s*table\s*contacts/i",$sql);
191 public function testAlterColumnSQL()
193 $sql = $this->_helper->alterColumnSQL(
195 array('foo' => array('name'=>'foo','type'=>'varchar'))
198 $this->assertRegExp("/alter\s*table\s*contacts/i",$sql);
201 public function testDropTableSQL()
203 $sql = $this->_helper->dropTableSQL(new Contact);
205 $this->assertRegExp("/drop\s*table.*contacts/i",$sql);
208 public function testDropTableNameSQL()
210 $sql = $this->_helper->dropTableNameSQL('contacts');
212 $this->assertRegExp("/drop\s*table.*contacts/i",$sql);
215 public function testDeleteColumnSQL()
217 $sql = $this->_helper->deleteColumnSQL(
219 array('foo' => array('name'=>'foo','type'=>'varchar'))
221 $this->assertRegExp("/alter\s*table\s*contacts\s*drop\s*column\s*foo/i",$sql);
224 public function testDropColumnSQL()
226 $sql = $this->_helper->dropColumnSQL(
228 array('foo' => array('name'=>'foo','type'=>'varchar'))
230 $this->assertRegExp("/alter\s*table\s*contacts\s*drop\s*column\s*foo/i",$sql);
233 public function testMassageValue()
236 $this->_helper->massageValue(123,array('name'=>'foo','type'=>'int')),
239 if ( $this->_db->dbType == 'mssql'
242 $this->_helper->massageValue("'dog'",array('name'=>'foo','type'=>'varchar')),
247 $this->_helper->massageValue("'dog'",array('name'=>'foo','type'=>'varchar')),
252 public function testGetColumnType()
255 $this->_helper->getColumnType('int'),
260 public function testIsFieldArray()
263 $this->_helper->isFieldArray(array('name'=>'foo','type'=>array('int')))
267 $this->_helper->isFieldArray(array('name'=>'foo','type'=>'int'))
271 $this->_helper->isFieldArray(array('name'=>'foo'))
275 $this->_helper->isFieldArray(1)
279 public function testSaveAuditRecords()
281 // TODO: write this test
284 public function testGetDataChanges()
286 // TODO: write this test
289 public function testQuote()
292 $this->_helper->quote('foobar'),
293 "'".$this->_db->quote('foobar')."'"
297 public function testEscapeQuote()
300 $this->_helper->escape_quote('foobar'),
301 $this->_db->quote('foobar')
305 public function testGetIndices()
307 $indices = $this->_helper->get_indices('contacts');
309 foreach ( $indices as $index ) {
310 $this->assertTrue(!empty($index['name']));
311 $this->assertTrue(!empty($index['type']));
312 $this->assertTrue(!empty($index['fields']));
316 public function testAddDropConstraint()
318 $tablename = 'test' . date("YmdHis");
319 $sql = $this->_helper->add_drop_constraint(
324 'fields' => array('foo'),
329 $this->assertRegExp("/idx_foo/i",$sql);
330 $this->assertRegExp("/foo/i",$sql);
332 $tablename = 'test' . date("YmdHis");
333 $sql = $this->_helper->add_drop_constraint(
338 'fields' => array('foo'),
343 $this->assertRegExp("/idx_foo/i",$sql);
344 $this->assertRegExp("/foo/i",$sql);
345 $this->assertRegExp("/drop/i",$sql);
348 public function testRenameIndex()
350 // TODO: write this test
353 public function testNumberOfColumns()
355 $tablename = 'test' . date("YmdHis");
356 $this->_db->createTableParams($tablename,
367 $this->assertEquals($this->_helper->number_of_columns($tablename),1);
369 $this->_db->dropTableName($tablename);
372 public function testGetColumns()
374 $vardefs = $this->_helper->get_columns('contacts');
376 $this->assertTrue(isset($vardefs['id']));
377 $this->assertTrue(isset($vardefs['id']['name']));
378 $this->assertTrue(isset($vardefs['id']['type']));
381 public function testMassageFieldDefs()
383 // TODO: write this test
389 public function testEmptyPrecision()
391 $sql = $this->_helper->alterColumnSQL(
393 array('compensation_min' =>
396 'name' => 'compensation_min',
397 'vname' => 'LBL_COMPENSATION_MIN',
402 'importable' => 'true',
403 'duplicate_merge' => 'disabled',
404 'duplicate_merge_dom_value' => 0,
413 $this->assertNotRegExp("/float\s*\(18,\s*\)/i",$sql);
414 $this->assertRegExp("/float\s*\(18\)/i",$sql);
420 public function testBlankSpacePrecision()
422 $sql = $this->_helper->alterColumnSQL(
424 array('compensation_min' =>
427 'name' => 'compensation_min',
428 'vname' => 'LBL_COMPENSATION_MIN',
433 'importable' => 'true',
434 'duplicate_merge' => 'disabled',
435 'duplicate_merge_dom_value' => 0,
444 $this->assertNotRegExp("/float\s*\(18,\s*\)/i",$sql);
445 $this->assertRegExp("/float\s*\(18\)/i",$sql);
451 public function testSetPrecision()
453 $sql = $this->_helper->alterColumnSQL(
455 array('compensation_min' =>
458 'name' => 'compensation_min',
459 'vname' => 'LBL_COMPENSATION_MIN',
464 'importable' => 'true',
465 'duplicate_merge' => 'disabled',
466 'duplicate_merge_dom_value' => 0,
475 if ( $this->_db->dbType == 'mssql' )
476 $this->assertRegExp("/float\s*\(18\)/i",$sql);
478 $this->assertRegExp("/float\s*\(18,2\)/i",$sql);
484 public function testSetPrecisionInLen()
486 $sql = $this->_helper->alterColumnSQL(
488 array('compensation_min' =>
491 'name' => 'compensation_min',
492 'vname' => 'LBL_COMPENSATION_MIN',
497 'importable' => 'true',
498 'duplicate_merge' => 'disabled',
499 'duplicate_merge_dom_value' => 0,
506 if ( $this->_db->dbType == 'mssql' )
507 $this->assertRegExp("/float\s*\(18\)/i",$sql);
509 $this->assertRegExp("/float\s*\(18,2\)/i",$sql);
515 public function testEmptyPrecisionMassageFieldDef()
519 'name' => 'compensation_min',
520 'vname' => 'LBL_COMPENSATION_MIN',
525 'importable' => 'true',
526 'duplicate_merge' => 'disabled',
527 'duplicate_merge_dom_value' => 0,
533 $this->_helper->massageFieldDef($fielddef,'mytable');
535 $this->assertEquals("18",$fielddef['len']);
541 public function testBlankSpacePrecisionMassageFieldDef()
545 'name' => 'compensation_min',
546 'vname' => 'LBL_COMPENSATION_MIN',
551 'importable' => 'true',
552 'duplicate_merge' => 'disabled',
553 'duplicate_merge_dom_value' => 0,
559 $this->_helper->massageFieldDef($fielddef,'mytable');
561 $this->assertEquals("18",$fielddef['len']);
567 public function testSetPrecisionMassageFieldDef()
571 'name' => 'compensation_min',
572 'vname' => 'LBL_COMPENSATION_MIN',
577 'importable' => 'true',
578 'duplicate_merge' => 'disabled',
579 'duplicate_merge_dom_value' => 0,
585 $this->_helper->massageFieldDef($fielddef,'mytable');
587 $this->assertEquals("18,2",$fielddef['len']);
593 public function testSetPrecisionInLenMassageFieldDef()
597 'name' => 'compensation_min',
598 'vname' => 'LBL_COMPENSATION_MIN',
603 'importable' => 'true',
604 'duplicate_merge' => 'disabled',
605 'duplicate_merge_dom_value' => 0,
610 $this->_helper->massageFieldDef($fielddef,'mytable');
612 $this->assertEquals("18,2",$fielddef['len']);