a grand renaming so that the most significant portion of the name comes first
This commit is contained in:
@@ -0,0 +1,462 @@
|
||||
<?php
|
||||
/**
|
||||
* AclBehaviorTest file
|
||||
*
|
||||
* Test the Acl Behavior
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2006-2010, Cake Software Foundation, Inc.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
|
||||
* @link http://cakephp.org CakePHP Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.tests.model.behaviors.acl
|
||||
* @since CakePHP v 1.2.0.4487
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::import('Behavior', 'Acl');
|
||||
App::import('Core', 'db_acl');
|
||||
|
||||
/**
|
||||
* Test Person class - self joined model
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||
*/
|
||||
class AclPerson extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'AclPerson';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'people';
|
||||
|
||||
/**
|
||||
* actsAs property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $actsAs = array('Acl' => 'requester');
|
||||
|
||||
/**
|
||||
* belongsTo property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $belongsTo = array(
|
||||
'Mother' => array(
|
||||
'className' => 'AclPerson',
|
||||
'foreignKey' => 'mother_id',
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* hasMany property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $hasMany = array(
|
||||
'Child' => array(
|
||||
'className' => 'AclPerson',
|
||||
'foreignKey' => 'mother_id'
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* parentNode method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function parentNode() {
|
||||
if (!$this->id && empty($this->data)) {
|
||||
return null;
|
||||
}
|
||||
if (isset($this->data['AclPerson']['mother_id'])) {
|
||||
$motherId = $this->data['AclPerson']['mother_id'];
|
||||
} else {
|
||||
$motherId = $this->field('mother_id');
|
||||
}
|
||||
if (!$motherId) {
|
||||
return null;
|
||||
} else {
|
||||
return array('AclPerson' => array('id' => $motherId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AclUser class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||
*/
|
||||
class AclUser extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'User';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'users';
|
||||
|
||||
/**
|
||||
* actsAs property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $actsAs = array('Acl');
|
||||
|
||||
/**
|
||||
* parentNode
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function parentNode() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AclPost class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||
*/
|
||||
class AclPost extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'Post';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'posts';
|
||||
|
||||
/**
|
||||
* actsAs property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $actsAs = array('Acl' => 'Controlled');
|
||||
|
||||
/**
|
||||
* parentNode
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function parentNode() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AclBehaviorTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.controller.components
|
||||
*/
|
||||
class AclBehaviorTestCase extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* Aco property
|
||||
*
|
||||
* @var Aco
|
||||
* @access public
|
||||
*/
|
||||
var $Aco;
|
||||
|
||||
/**
|
||||
* Aro property
|
||||
*
|
||||
* @var Aro
|
||||
* @access public
|
||||
*/
|
||||
var $Aro;
|
||||
|
||||
/**
|
||||
* fixtures property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
|
||||
|
||||
/**
|
||||
* Set up the test
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function startTest() {
|
||||
Configure::write('Acl.database', 'test_suite');
|
||||
|
||||
$this->Aco =& new Aco();
|
||||
$this->Aro =& new Aro();
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function tearDown() {
|
||||
ClassRegistry::flush();
|
||||
unset($this->Aro, $this->Aco);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Setup of AclBehavior
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testSetup() {
|
||||
$User =& new AclUser();
|
||||
$this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
|
||||
$this->assertEqual($User->Behaviors->Acl->settings['User']['type'], 'requester');
|
||||
$this->assertTrue(is_object($User->Aro));
|
||||
|
||||
$Post =& new AclPost();
|
||||
$this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
|
||||
$this->assertEqual($Post->Behaviors->Acl->settings['Post']['type'], 'controlled');
|
||||
$this->assertTrue(is_object($Post->Aco));
|
||||
}
|
||||
|
||||
/**
|
||||
* test After Save
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testAfterSave() {
|
||||
$Post =& new AclPost();
|
||||
$data = array(
|
||||
'Post' => array(
|
||||
'author_id' => 1,
|
||||
'title' => 'Acl Post',
|
||||
'body' => 'post body',
|
||||
'published' => 1
|
||||
),
|
||||
);
|
||||
$Post->save($data);
|
||||
$result = $this->Aco->find('first', array(
|
||||
'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
|
||||
));
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEqual($result['Aco']['model'], 'Post');
|
||||
$this->assertEqual($result['Aco']['foreign_key'], $Post->id);
|
||||
|
||||
$aroData = array(
|
||||
'Aro' => array(
|
||||
'model' => 'AclPerson',
|
||||
'foreign_key' => 2,
|
||||
'parent_id' => null
|
||||
)
|
||||
);
|
||||
$this->Aro->save($aroData);
|
||||
|
||||
$Person =& new AclPerson();
|
||||
$data = array(
|
||||
'AclPerson' => array(
|
||||
'name' => 'Trent',
|
||||
'mother_id' => 2,
|
||||
'father_id' => 3,
|
||||
),
|
||||
);
|
||||
$Person->save($data);
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
|
||||
));
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEqual($result['Aro']['parent_id'], 5);
|
||||
|
||||
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
|
||||
$this->assertEqual(count($node), 2);
|
||||
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
|
||||
$this->assertEqual($node[1]['Aro']['parent_id'], null);
|
||||
|
||||
$aroData = array(
|
||||
'Aro' => array(
|
||||
'model' => 'AclPerson',
|
||||
'foreign_key' => 1,
|
||||
'parent_id' => null
|
||||
)
|
||||
);
|
||||
$this->Aro->create();
|
||||
$this->Aro->save($aroData);
|
||||
|
||||
$Person->read(null, 8);
|
||||
$Person->set('mother_id', 1);
|
||||
$Person->save();
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
|
||||
));
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEqual($result['Aro']['parent_id'], 7);
|
||||
|
||||
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
|
||||
$this->assertEqual(sizeof($node), 2);
|
||||
$this->assertEqual($node[0]['Aro']['parent_id'], 7);
|
||||
$this->assertEqual($node[1]['Aro']['parent_id'], null);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that an afterSave on an update does not cause parent_id to become null.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testAfterSaveUpdateParentIdNotNull() {
|
||||
$aroData = array(
|
||||
'Aro' => array(
|
||||
'model' => 'AclPerson',
|
||||
'foreign_key' => 2,
|
||||
'parent_id' => null
|
||||
)
|
||||
);
|
||||
$this->Aro->save($aroData);
|
||||
|
||||
$Person =& new AclPerson();
|
||||
$data = array(
|
||||
'AclPerson' => array(
|
||||
'name' => 'Trent',
|
||||
'mother_id' => 2,
|
||||
'father_id' => 3,
|
||||
),
|
||||
);
|
||||
$Person->save($data);
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
|
||||
));
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEqual($result['Aro']['parent_id'], 5);
|
||||
|
||||
$Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
|
||||
));
|
||||
$this->assertEqual($result['Aro']['parent_id'], 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test After Delete
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testAfterDelete() {
|
||||
$aroData = array(
|
||||
'Aro' => array(
|
||||
'model' => 'AclPerson',
|
||||
'foreign_key' => 2,
|
||||
'parent_id' => null
|
||||
)
|
||||
);
|
||||
$this->Aro->save($aroData);
|
||||
$Person =& new AclPerson();
|
||||
$data = array(
|
||||
'AclPerson' => array(
|
||||
'name' => 'Trent',
|
||||
'mother_id' => 2,
|
||||
'father_id' => 3,
|
||||
),
|
||||
);
|
||||
$Person->save($data);
|
||||
$id = $Person->id;
|
||||
$node = $Person->node();
|
||||
$this->assertEqual(count($node), 2);
|
||||
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
|
||||
$this->assertEqual($node[1]['Aro']['parent_id'], null);
|
||||
|
||||
$Person->delete($id);
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
|
||||
));
|
||||
$this->assertTrue(empty($result));
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
|
||||
));
|
||||
$this->assertFalse(empty($result));
|
||||
|
||||
$data = array(
|
||||
'AclPerson' => array(
|
||||
'name' => 'Trent',
|
||||
'mother_id' => 2,
|
||||
'father_id' => 3,
|
||||
),
|
||||
);
|
||||
$Person->save($data);
|
||||
$id = $Person->id;
|
||||
$Person->delete(2);
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
|
||||
));
|
||||
$this->assertTrue(empty($result));
|
||||
|
||||
$result = $this->Aro->find('first', array(
|
||||
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
|
||||
));
|
||||
$this->assertTrue(empty($result));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Node()
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testNode() {
|
||||
$Person =& new AclPerson();
|
||||
$aroData = array(
|
||||
'Aro' => array(
|
||||
'model' => 'AclPerson',
|
||||
'foreign_key' => 2,
|
||||
'parent_id' => null
|
||||
)
|
||||
);
|
||||
$this->Aro->save($aroData);
|
||||
|
||||
$Person->id = 2;
|
||||
$result = $Person->node();
|
||||
$this->assertTrue(is_array($result));
|
||||
$this->assertEqual(count($result), 1);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,898 @@
|
||||
<?php
|
||||
/**
|
||||
* TranslateBehaviorTest file
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The Open Group Test Suite License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||
* @since CakePHP(tm) v 1.2.0.5669
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
||||
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
|
||||
}
|
||||
|
||||
App::import('Core', array('AppModel', 'Model'));
|
||||
require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
|
||||
|
||||
/**
|
||||
* TranslateBehaviorTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||
*/
|
||||
class TranslateBehaviorTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* autoFixtures property
|
||||
*
|
||||
* @var bool false
|
||||
* @access public
|
||||
*/
|
||||
var $autoFixtures = false;
|
||||
|
||||
/**
|
||||
* fixtures property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fixtures = array(
|
||||
'core.translated_item', 'core.translate', 'core.translate_table',
|
||||
'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag',
|
||||
'core.translate_with_prefix'
|
||||
);
|
||||
|
||||
/**
|
||||
* endTest method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function endTest() {
|
||||
ClassRegistry::flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* testTranslateModel method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testTranslateModel() {
|
||||
$TestModel =& new Tag();
|
||||
$TestModel->translateTable = 'another_i18n';
|
||||
$TestModel->Behaviors->attach('Translate', array('title'));
|
||||
$translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
|
||||
$this->assertEqual($translateModel->name, 'I18nModel');
|
||||
$this->assertEqual($translateModel->useTable, 'another_i18n');
|
||||
|
||||
$TestModel =& new User();
|
||||
$TestModel->Behaviors->attach('Translate', array('title'));
|
||||
$translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
|
||||
$this->assertEqual($translateModel->name, 'I18nModel');
|
||||
$this->assertEqual($translateModel->useTable, 'i18n');
|
||||
|
||||
$TestModel =& new TranslatedArticle();
|
||||
$translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
|
||||
$this->assertEqual($translateModel->name, 'TranslateArticleModel');
|
||||
$this->assertEqual($translateModel->useTable, 'article_i18n');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
|
||||
$this->assertEqual($translateModel->name, 'TranslateTestModel');
|
||||
$this->assertEqual($translateModel->useTable, 'i18n');
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleFalsePlain method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleFalsePlain() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = false;
|
||||
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array('TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('slug')));
|
||||
$expected = array(
|
||||
array('TranslatedItem' => array('slug' => 'first_translated')),
|
||||
array('TranslatedItem' => array('slug' => 'second_translated')),
|
||||
array('TranslatedItem' => array('slug' => 'third_translated'))
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleFalseAssociations method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleFalseAssociations() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = false;
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
|
||||
'Title' => array(
|
||||
array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
|
||||
array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
|
||||
array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
|
||||
),
|
||||
'Content' => array(
|
||||
array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
|
||||
array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
|
||||
array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
|
||||
$TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug')));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
|
||||
'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
|
||||
'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 2, 'slug' => 'second_translated'),
|
||||
'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
|
||||
'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 3, 'slug' => 'third_translated'),
|
||||
'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
|
||||
'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleSingle method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleSingle() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all');
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 2,
|
||||
'slug' => 'second_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #2',
|
||||
'content' => 'Content #2'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 3,
|
||||
'slug' => 'third_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #3',
|
||||
'content' => 'Content #3'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleSingleWithConditions method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleSingleWithConditions() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$result = $TestModel->find('all', array('conditions' => array('slug' => 'first_translated')));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all', array('conditions' => "TranslatedItem.slug = 'first_translated'"));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleSingleAssociations method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleSingleAssociations() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title #1',
|
||||
'content' => 'Content #1'
|
||||
),
|
||||
'Title' => array(
|
||||
array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
|
||||
array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
|
||||
array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
|
||||
),
|
||||
'Content' => array(
|
||||
array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
|
||||
array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
|
||||
array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
|
||||
$TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('TranslatedItem.title')));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1'),
|
||||
'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
|
||||
'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 2, 'locale' => 'eng', 'title' => 'Title #2'),
|
||||
'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
|
||||
'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array('id' => 3, 'locale' => 'eng', 'title' => 'Title #3'),
|
||||
'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
|
||||
'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLocaleMultiple method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLocaleMultiple() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = array('deu', 'eng', 'cze');
|
||||
$delete = array(
|
||||
array('locale' => 'deu'),
|
||||
array('foreign_key' => 1, 'field' => 'title', 'locale' => 'eng'),
|
||||
array('foreign_key' => 1, 'field' => 'content', 'locale' => 'cze'),
|
||||
array('foreign_key' => 2, 'field' => 'title', 'locale' => 'cze'),
|
||||
array('foreign_key' => 2, 'field' => 'content', 'locale' => 'eng'),
|
||||
array('foreign_key' => 3, 'field' => 'title')
|
||||
);
|
||||
$I18nModel =& ClassRegistry::getObject('TranslateTestModel');
|
||||
$I18nModel->deleteAll(array('or' => $delete));
|
||||
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'deu',
|
||||
'title' => 'Titulek #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('slug', 'title', 'content')));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'deu',
|
||||
'title' => 'Titulek #1',
|
||||
'content' => 'Content #1'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'slug' => 'second_translated',
|
||||
'locale' => 'deu',
|
||||
'title' => 'Title #2',
|
||||
'content' => 'Obsah #2'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedItem' => array(
|
||||
'slug' => 'third_translated',
|
||||
'locale' => 'deu',
|
||||
'title' => '',
|
||||
'content' => 'Content #3'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMissingTranslation method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testMissingTranslation() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'rus';
|
||||
$result = $TestModel->read(null, 1);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$TestModel->locale = array('rus');
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'rus',
|
||||
'title' => '',
|
||||
'content' => ''
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testTranslatedFindList method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testTranslatedFindList() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'deu';
|
||||
$TestModel->displayField = 'title';
|
||||
$result = $TestModel->find('list', array('recursive' => 1));
|
||||
$expected = array(1 => 'Titel #1', 2 => 'Titel #2', 3 => 'Titel #3');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// MSSQL trigger an error and stops the page even if the debug = 0
|
||||
if ($this->db->config['driver'] != 'mssql') {
|
||||
$debug = Configure::read('debug');
|
||||
Configure::write('debug', 0);
|
||||
|
||||
$result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
|
||||
$this->assertEqual($result, array());
|
||||
|
||||
$result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
|
||||
$this->assertEqual($result, array());
|
||||
Configure::write('debug', $debug);
|
||||
}
|
||||
|
||||
$result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'before'));
|
||||
$expected = array(1 => null, 2 => null, 3 => null);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testReadSelectedFields method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testReadSelectedFields() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$result = $TestModel->find('all', array('fields' => array('slug', 'TranslatedItem.content')));
|
||||
$expected = array(
|
||||
array('TranslatedItem' => array('slug' => 'first_translated', 'locale' => 'eng', 'content' => 'Content #1')),
|
||||
array('TranslatedItem' => array('slug' => 'second_translated', 'locale' => 'eng', 'content' => 'Content #2')),
|
||||
array('TranslatedItem' => array('slug' => 'third_translated', 'locale' => 'eng', 'content' => 'Content #3'))
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug', 'content')));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$TestModel->locale = array('eng', 'deu', 'cze');
|
||||
$delete = array(array('locale' => 'deu'), array('field' => 'content', 'locale' => 'eng'));
|
||||
$I18nModel =& ClassRegistry::getObject('TranslateTestModel');
|
||||
$I18nModel->deleteAll(array('or' => $delete));
|
||||
|
||||
$result = $TestModel->find('all', array('fields' => array('title', 'content')));
|
||||
$expected = array(
|
||||
array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #1', 'content' => 'Obsah #1')),
|
||||
array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #2', 'content' => 'Obsah #2')),
|
||||
array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #3', 'content' => 'Obsah #3'))
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSaveCreate method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSaveCreate() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'spa';
|
||||
$data = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4', 'content' => 'Contenido #4');
|
||||
$TestModel->create($data);
|
||||
$TestModel->save();
|
||||
$result = $TestModel->read();
|
||||
$expected = array('TranslatedItem' => array_merge($data, array('id' => $TestModel->id, 'locale' => 'spa')));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSaveUpdate method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSaveUpdate() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'spa';
|
||||
$oldData = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4');
|
||||
$TestModel->create($oldData);
|
||||
$TestModel->save();
|
||||
$id = $TestModel->id;
|
||||
$newData = array('id' => $id, 'content' => 'Contenido #4');
|
||||
$TestModel->create($newData);
|
||||
$TestModel->save();
|
||||
$result = $TestModel->read(null, $id);
|
||||
$expected = array('TranslatedItem' => array_merge($oldData, $newData, array('locale' => 'spa')));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMultipleCreate method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testMultipleCreate() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'deu';
|
||||
$data = array(
|
||||
'slug' => 'new_translated',
|
||||
'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
|
||||
'content' => array('eng' => 'New content', 'spa' => 'Nuevo contenido')
|
||||
);
|
||||
$TestModel->create($data);
|
||||
$TestModel->save();
|
||||
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
$TestModel->locale = array('eng', 'spa');
|
||||
|
||||
$result = $TestModel->read();
|
||||
$expected = array(
|
||||
'TranslatedItem' => array('id' => 4, 'slug' => 'new_translated', 'locale' => 'eng', 'title' => 'New title', 'content' => 'New content'),
|
||||
'Title' => array(
|
||||
array('id' => 21, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'New title'),
|
||||
array('id' => 22, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'Nuevo leyenda')
|
||||
),
|
||||
'Content' => array(
|
||||
array('id' => 19, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'New content'),
|
||||
array('id' => 20, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'Nuevo contenido')
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMultipleUpdate method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testMultipleUpdate() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$TestModel->validate['title'] = 'notEmpty';
|
||||
$data = array('TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
|
||||
'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
|
||||
));
|
||||
$TestModel->create();
|
||||
$TestModel->save($data);
|
||||
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array('id' => '1', 'slug' => 'first_translated', 'locale' => 'eng', 'title' => 'New Title #1', 'content' => 'New Content #1'),
|
||||
'Title' => array(
|
||||
array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'New Title #1'),
|
||||
array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Neue Titel #1'),
|
||||
array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Novy Titulek #1')
|
||||
),
|
||||
'Content' => array(
|
||||
array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'New Content #1'),
|
||||
array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Neue Inhalt #1'),
|
||||
array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Novy Obsah #1')
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$TestModel->unbindTranslation($translations);
|
||||
$TestModel->bindTranslation(array('title', 'content'), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMixedCreateUpdateWithArrayLocale method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testMixedCreateUpdateWithArrayLocale() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = array('cze', 'deu');
|
||||
$data = array('TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'title' => array('eng' => 'Updated Title #1', 'spa' => 'Nuevo leyenda #1'),
|
||||
'content' => 'Upraveny obsah #1'
|
||||
));
|
||||
$TestModel->create();
|
||||
$TestModel->save($data);
|
||||
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'locale' => 'cze', 'title' => 'Titulek #1', 'content' => 'Upraveny obsah #1'),
|
||||
'Title' => array(
|
||||
array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Updated Title #1'),
|
||||
array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
|
||||
array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'),
|
||||
array('id' => 19, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Nuevo leyenda #1')
|
||||
),
|
||||
'Content' => array(
|
||||
array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
|
||||
array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
|
||||
array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Upraveny obsah #1')
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testValidation method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testValidation() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->locale = 'eng';
|
||||
$TestModel->validate['title'] = '/Only this title/';
|
||||
$data = array('TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
|
||||
'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
|
||||
));
|
||||
$TestModel->create();
|
||||
$this->assertFalse($TestModel->save($data));
|
||||
$this->assertEqual($TestModel->validationErrors['title'], 'This field cannot be left blank');
|
||||
|
||||
$TestModel->locale = 'eng';
|
||||
$TestModel->validate['title'] = '/Only this title/';
|
||||
$data = array('TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'title' => array('eng' => 'Only this title', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
|
||||
'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
|
||||
));
|
||||
$TestModel->create();
|
||||
$this->assertTrue($TestModel->save($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* testAttachDetach method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testAttachDetach() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$Behavior =& $this->Model->Behaviors->Translate;
|
||||
|
||||
$TestModel->unbindTranslation();
|
||||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
|
||||
$result = array_keys($TestModel->hasMany);
|
||||
$expected = array('Title', 'Content');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$TestModel->Behaviors->detach('Translate');
|
||||
$result = array_keys($TestModel->hasMany);
|
||||
$expected = array();
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = isset($TestModel->Behaviors->Translate);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$result = isset($Behavior->settings[$TestModel->alias]);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$result = isset($Behavior->runtime[$TestModel->alias]);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$TestModel->Behaviors->attach('Translate', array('title' => 'Title', 'content' => 'Content'));
|
||||
$result = array_keys($TestModel->hasMany);
|
||||
$expected = array('Title', 'Content');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = isset($TestModel->Behaviors->Translate);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$Behavior = $TestModel->Behaviors->Translate;
|
||||
|
||||
$result = isset($Behavior->settings[$TestModel->alias]);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$result = isset($Behavior->runtime[$TestModel->alias]);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testAnotherTranslateTable method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testAnotherTranslateTable() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem', 'TranslateTable');
|
||||
|
||||
$TestModel =& new TranslatedItemWithTable();
|
||||
$TestModel->locale = 'eng';
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedItemWithTable' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Another Title #1',
|
||||
'content' => 'Another Content #1'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testTranslateWithAssociations method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testTranslateWithAssociations() {
|
||||
$this->loadFixtures('TranslateArticle', 'TranslatedArticle', 'User', 'Comment', 'ArticlesTag', 'Tag');
|
||||
|
||||
$TestModel =& new TranslatedArticle();
|
||||
$TestModel->locale = 'eng';
|
||||
$recursive = $TestModel->recursive;
|
||||
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedArticle' => array(
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title (eng) #1',
|
||||
'body' => 'Body (eng) #1'
|
||||
),
|
||||
'User' => array(
|
||||
'id' => 1,
|
||||
'user' => 'mariano',
|
||||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
|
||||
'created' => '2007-03-17 01:16:23',
|
||||
'updated' => '2007-03-17 01:18:31'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $TestModel->find('all', array('recursive' => -1));
|
||||
$expected = array(
|
||||
array(
|
||||
'TranslatedArticle' => array(
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title (eng) #1',
|
||||
'body' => 'Body (eng) #1'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedArticle' => array(
|
||||
'id' => 2,
|
||||
'user_id' => 3,
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:41:23',
|
||||
'updated' => '2007-03-18 10:43:31',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title (eng) #2',
|
||||
'body' => 'Body (eng) #2'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'TranslatedArticle' => array(
|
||||
'id' => 3,
|
||||
'user_id' => 1,
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:43:23',
|
||||
'updated' => '2007-03-18 10:45:31',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title (eng) #3',
|
||||
'body' => 'Body (eng) #3'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
$this->assertEqual($TestModel->recursive, $recursive);
|
||||
|
||||
$TestModel->recursive = -1;
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(
|
||||
'TranslatedArticle' => array(
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31',
|
||||
'locale' => 'eng',
|
||||
'title' => 'Title (eng) #1',
|
||||
'body' => 'Body (eng) #1'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testTranslateTableWithPrefix method
|
||||
* Tests that is possible to have a translation model with a custom tablePrefix
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testTranslateTableWithPrefix() {
|
||||
$this->loadFixtures('TranslateWithPrefix', 'TranslatedItem');
|
||||
$TestModel =& new TranslatedItem2;
|
||||
$TestModel->locale = 'eng';
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array('TranslatedItem' => array(
|
||||
'id' => 1,
|
||||
'slug' => 'first_translated',
|
||||
'locale' => 'eng',
|
||||
'content' => 'Content #1',
|
||||
'title' => 'Title #1'
|
||||
));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test infinite loops not occuring with unbindTranslation()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testUnbindTranslationInfinteLoop() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
|
||||
$TestModel =& new TranslatedItem();
|
||||
$TestModel->Behaviors->detach('Translate');
|
||||
$TestModel->actsAs = array();
|
||||
$TestModel->Behaviors->attach('Translate');
|
||||
$TestModel->bindTranslation(array('title', 'content'), true);
|
||||
$result = $TestModel->unbindTranslation();
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,979 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for Schema database management
|
||||
*
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The Open Group Test Suite License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @since CakePHP(tm) v 1.2.0.5550
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
App::import('Model', 'CakeSchema', false);
|
||||
|
||||
/**
|
||||
* Test for Schema database management
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
*/
|
||||
class MyAppSchema extends CakeSchema {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'MyApp'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'MyApp';
|
||||
|
||||
/**
|
||||
* connection property
|
||||
*
|
||||
* @var string 'test_suite'
|
||||
* @access public
|
||||
*/
|
||||
var $connection = 'test_suite';
|
||||
|
||||
/**
|
||||
* comments property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $comments = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'user_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
|
||||
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
);
|
||||
|
||||
/**
|
||||
* posts property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $posts = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
|
||||
'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
|
||||
'body' => array('type' => 'text', 'null' => true, 'default' => null),
|
||||
'summary' => array('type' => 'text', 'null' => true),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
);
|
||||
|
||||
/**
|
||||
* setup method
|
||||
*
|
||||
* @param mixed $version
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setup($version) {
|
||||
}
|
||||
|
||||
/**
|
||||
* teardown method
|
||||
*
|
||||
* @param mixed $version
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function teardown($version) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TestAppSchema class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class TestAppSchema extends CakeSchema {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'MyApp'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'MyApp';
|
||||
|
||||
/**
|
||||
* comments property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $comments = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0,'key' => 'primary'),
|
||||
'article_id' => array('type' => 'integer', 'null' => false),
|
||||
'user_id' => array('type' => 'integer', 'null' => false),
|
||||
'comment' => array('type' => 'text', 'null' => true, 'default' => null),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* posts property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $posts = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'body' => array('type' => 'text', 'null' => true, 'default' => null),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* posts_tags property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $posts_tags = array(
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
|
||||
'tag_id' => array('type' => 'string', 'null' => false, 'key' => 'primary'),
|
||||
'indexes' => array('posts_tag' => array('column' => array('tag_id', 'post_id'), 'unique' => 1)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* tags property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $tags = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'tag' => array('type' => 'string', 'null' => false),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* datatypes property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $datatypes = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
'tableParameters' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* setup method
|
||||
*
|
||||
* @param mixed $version
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setup($version) {
|
||||
}
|
||||
|
||||
/**
|
||||
* teardown method
|
||||
*
|
||||
* @param mixed $version
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function teardown($version) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SchmeaPost class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaPost extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'SchemaPost'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'SchemaPost';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string 'posts'
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'posts';
|
||||
|
||||
/**
|
||||
* hasMany property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $hasMany = array('SchemaComment');
|
||||
|
||||
/**
|
||||
* hasAndBelongsToMany property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $hasAndBelongsToMany = array('SchemaTag');
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaComment class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaComment extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'SchemaComment'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'SchemaComment';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string 'comments'
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'comments';
|
||||
|
||||
/**
|
||||
* belongsTo property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $belongsTo = array('SchemaPost');
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaTag class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaTag extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'SchemaTag'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'SchemaTag';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string 'tags'
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'tags';
|
||||
|
||||
/**
|
||||
* hasAndBelongsToMany property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $hasAndBelongsToMany = array('SchemaPost');
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaDatatype class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaDatatype extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'SchemaDatatype'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'SchemaDatatype';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string 'datatypes'
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'datatypes';
|
||||
}
|
||||
|
||||
/**
|
||||
* Testdescribe class
|
||||
*
|
||||
* This class is defined purely to inherit the cacheSources variable otherwise
|
||||
* testSchemaCreatTable will fail if listSources has already been called and
|
||||
* its source cache populated - I.e. if the test is run within a group
|
||||
*
|
||||
* @uses CakeTestModel
|
||||
* @package
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class Testdescribe extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'Testdescribe'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'Testdescribe';
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaCrossDatabase class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaCrossDatabase extends CakeTestModel {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'SchemaCrossDatabase'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'SchemaCrossDatabase';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string 'posts'
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = 'cross_database';
|
||||
|
||||
/**
|
||||
* useDbConfig property
|
||||
*
|
||||
* @var string 'test2'
|
||||
* @access public
|
||||
*/
|
||||
var $useDbConfig = 'test2';
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaCrossDatabaseFixture class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaCrossDatabaseFixture extends CakeTestFixture {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'CrossDatabase'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'CrossDatabase';
|
||||
|
||||
/**
|
||||
* table property
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
var $table = 'cross_database';
|
||||
|
||||
/**
|
||||
* fields property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fields = array(
|
||||
'id' => array('type' => 'integer', 'key' => 'primary'),
|
||||
'name' => 'string'
|
||||
);
|
||||
|
||||
/**
|
||||
* records property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $records = array(
|
||||
array('id' => 1, 'name' => 'First'),
|
||||
array('id' => 2, 'name' => 'Second'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* SchemaPrefixAuthUser class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model
|
||||
*/
|
||||
class SchemaPrefixAuthUser extends CakeTestModel {
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
var $name = 'SchemaPrefixAuthUser';
|
||||
/**
|
||||
* table prefix
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
var $tablePrefix = 'auth_';
|
||||
/**
|
||||
* useTable
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
var $useTable = 'users';
|
||||
}
|
||||
|
||||
/**
|
||||
* CakeSchemaTest
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
*/
|
||||
class CakeSchemaTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* fixtures property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fixtures = array(
|
||||
'core.post', 'core.tag', 'core.posts_tag', 'core.test_plugin_comment',
|
||||
'core.datatype', 'core.auth_user', 'core.author',
|
||||
'core.test_plugin_article', 'core.user', 'core.comment'
|
||||
);
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function startTest() {
|
||||
$this->Schema = new TestAppSchema();
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function tearDown() {
|
||||
@unlink(TMP . 'tests' . DS .'schema.php');
|
||||
unset($this->Schema);
|
||||
ClassRegistry::flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaName method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaName() {
|
||||
$Schema = new CakeSchema();
|
||||
$this->assertEqual(strtolower($Schema->name), strtolower(APP_DIR));
|
||||
|
||||
Configure::write('App.dir', 'Some.name.with.dots');
|
||||
$Schema = new CakeSchema();
|
||||
$this->assertEqual($Schema->name, 'SomeNameWithDots');
|
||||
|
||||
Configure::write('App.dir', 'app');
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaRead method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaRead() {
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaPost', 'SchemaComment', 'SchemaTag', 'SchemaDatatype')
|
||||
));
|
||||
unset($read['tables']['missing']);
|
||||
|
||||
$expected = array('comments', 'datatypes', 'posts', 'posts_tags', 'tags');
|
||||
$this->assertEqual(array_keys($read['tables']), $expected);
|
||||
foreach ($read['tables'] as $table => $fields) {
|
||||
$this->assertEqual(array_keys($fields), array_keys($this->Schema->tables[$table]));
|
||||
}
|
||||
|
||||
$this->assertEqual(
|
||||
$read['tables']['datatypes']['float_field'],
|
||||
$this->Schema->tables['datatypes']['float_field']
|
||||
);
|
||||
|
||||
$SchemaPost =& ClassRegistry::init('SchemaPost');
|
||||
$SchemaPost->table = 'sts';
|
||||
$SchemaPost->tablePrefix = 'po';
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaPost')
|
||||
));
|
||||
$this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix %s');
|
||||
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaComment', 'SchemaTag', 'SchemaPost')
|
||||
));
|
||||
$this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing %s');
|
||||
}
|
||||
|
||||
/**
|
||||
* test read() with tablePrefix properties.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaReadWithTablePrefix() {
|
||||
$model =& new SchemaPrefixAuthUser();
|
||||
|
||||
$Schema =& new CakeSchema();
|
||||
$read = $Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaPrefixAuthUser')
|
||||
));
|
||||
unset($read['tables']['missing']);
|
||||
$this->assertTrue(isset($read['tables']['auth_users']), 'auth_users key missing %s');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* test reading schema with config prefix.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaReadWithConfigPrefix() {
|
||||
$db =& ConnectionManager::getDataSource('test_suite');
|
||||
$config = $db->config;
|
||||
$config['prefix'] = 'schema_test_prefix_';
|
||||
ConnectionManager::create('schema_prefix', $config);
|
||||
$read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
|
||||
$this->assertTrue(empty($read['tables']));
|
||||
}
|
||||
|
||||
/**
|
||||
* test reading schema from plugins.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaReadWithPlugins() {
|
||||
App::objects('model', null, false);
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
|
||||
$Schema =& new CakeSchema();
|
||||
$Schema->plugin = 'TestPlugin';
|
||||
$read = $Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => true
|
||||
));
|
||||
unset($read['tables']['missing']);
|
||||
$this->assertTrue(isset($read['tables']['auth_users']));
|
||||
$this->assertTrue(isset($read['tables']['authors']));
|
||||
$this->assertTrue(isset($read['tables']['test_plugin_comments']));
|
||||
$this->assertTrue(isset($read['tables']['posts']));
|
||||
$this->assertEqual(count($read['tables']), 4);
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* test reading schema with tables from another database.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaReadWithCrossDatabase() {
|
||||
$config = new DATABASE_CONFIG();
|
||||
$skip = $this->skipIf(
|
||||
!isset($config->test) || !isset($config->test2),
|
||||
'%s Primary and secondary test databases not configured, skipping cross-database '
|
||||
.'join tests.'
|
||||
.' To run these tests, you must define $test and $test2 in your database configuration.'
|
||||
);
|
||||
if ($skip) {
|
||||
return;
|
||||
}
|
||||
|
||||
$db2 =& ConnectionManager::getDataSource('test2');
|
||||
$fixture = new SchemaCrossDatabaseFixture();
|
||||
$fixture->create($db2);
|
||||
$fixture->insert($db2);
|
||||
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test_suite',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaCrossDatabase', 'SchemaPost')
|
||||
));
|
||||
$this->assertTrue(isset($read['tables']['posts']));
|
||||
$this->assertFalse(isset($read['tables']['cross_database']), 'Cross database should not appear');
|
||||
$this->assertFalse(isset($read['tables']['missing']['cross_database']), 'Cross database should not appear');
|
||||
|
||||
$read = $this->Schema->read(array(
|
||||
'connection' => 'test2',
|
||||
'name' => 'TestApp',
|
||||
'models' => array('SchemaCrossDatabase', 'SchemaPost')
|
||||
));
|
||||
$this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
|
||||
$this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
|
||||
$this->assertTrue(isset($read['tables']['cross_database']));
|
||||
|
||||
$fixture->drop($db2);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that tables are generated correctly
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testGenerateTable() {
|
||||
$fields = array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'body' => array('type' => 'text', 'null' => true, 'default' => null),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||
);
|
||||
$result = $this->Schema->generateTable('posts', $fields);
|
||||
$this->assertPattern('/var \$posts/', $result);
|
||||
|
||||
eval(substr($result, 4));
|
||||
$this->assertEqual($posts, $fields);
|
||||
}
|
||||
/**
|
||||
* testSchemaWrite method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaWrite() {
|
||||
$write = $this->Schema->write(array('name' => 'MyOtherApp', 'tables' => $this->Schema->tables, 'path' => TMP . 'tests'));
|
||||
$file = file_get_contents(TMP . 'tests' . DS .'schema.php');
|
||||
$this->assertEqual($write, $file);
|
||||
|
||||
require_once( TMP . 'tests' . DS .'schema.php');
|
||||
$OtherSchema = new MyOtherAppSchema();
|
||||
$this->assertEqual($this->Schema->tables, $OtherSchema->tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaComparison method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaComparison() {
|
||||
$New = new MyAppSchema();
|
||||
$compare = $New->compare($this->Schema);
|
||||
$expected = array(
|
||||
'comments' => array(
|
||||
'add' => array(
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
|
||||
),
|
||||
'drop' => array(
|
||||
'article_id' => array('type' => 'integer', 'null' => false),
|
||||
'tableParameters' => array(),
|
||||
),
|
||||
'change' => array(
|
||||
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
|
||||
)
|
||||
),
|
||||
'posts' => array(
|
||||
'add' => array(
|
||||
'summary' => array('type' => 'text', 'null' => 1),
|
||||
),
|
||||
'drop' => array(
|
||||
'tableParameters' => array(),
|
||||
),
|
||||
'change' => array(
|
||||
'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
|
||||
'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
|
||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1')
|
||||
)
|
||||
),
|
||||
);
|
||||
$this->assertEqual($expected, $compare);
|
||||
|
||||
$tables = array(
|
||||
'missing' => array(
|
||||
'categories' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
|
||||
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
|
||||
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
|
||||
)
|
||||
),
|
||||
'ratings' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
|
||||
'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
|
||||
'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
|
||||
'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
|
||||
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
|
||||
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
|
||||
)
|
||||
);
|
||||
$compare = $New->compare($this->Schema, $tables);
|
||||
$expected = array(
|
||||
'ratings' => array(
|
||||
'add' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
|
||||
'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
|
||||
'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
|
||||
'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
|
||||
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
|
||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
|
||||
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($expected, $compare);
|
||||
}
|
||||
|
||||
/**
|
||||
* test comparing '' and null and making sure they are different.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testCompareEmptyStringAndNull() {
|
||||
$One =& new CakeSchema(array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
|
||||
'name' => array('type' => 'string', 'null' => false, 'default' => '')
|
||||
)
|
||||
));
|
||||
$Two =& new CakeSchema(array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
|
||||
'name' => array('type' => 'string', 'null' => false, 'default' => null)
|
||||
)
|
||||
));
|
||||
$compare = $One->compare($Two);
|
||||
$expected = array(
|
||||
'posts' => array(
|
||||
'change' => array(
|
||||
'name' => array('type' => 'string', 'null' => false, 'default' => null)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($expected, $compare);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test comparing tableParameters and indexes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testTableParametersAndIndexComparison() {
|
||||
$old = array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true)
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'latin1',
|
||||
'collate' => 'latin1_general_ci'
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'comment' => array('type' => 'text'),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
'post_id' => array('column' => 'post_id'),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'engine' => 'InnoDB',
|
||||
'charset' => 'latin1',
|
||||
'collate' => 'latin1_general_ci'
|
||||
)
|
||||
)
|
||||
);
|
||||
$new = array(
|
||||
'posts' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'author_id' => array('type' => 'integer', 'null' => false),
|
||||
'title' => array('type' => 'string', 'null' => false),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
'author_id' => array('column' => 'author_id'),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
'engine' => 'MyISAM'
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
||||
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'comment' => array('type' => 'text'),
|
||||
'indexes' => array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => true),
|
||||
),
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci'
|
||||
)
|
||||
)
|
||||
);
|
||||
$compare = $this->Schema->compare($old, $new);
|
||||
$expected = array(
|
||||
'posts' => array(
|
||||
'add' => array(
|
||||
'indexes' => array('author_id' => array('column' => 'author_id')),
|
||||
),
|
||||
'change' => array(
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
'engine' => 'MyISAM'
|
||||
)
|
||||
)
|
||||
),
|
||||
'comments' => array(
|
||||
'drop' => array(
|
||||
'indexes' => array('post_id' => array('column' => 'post_id')),
|
||||
),
|
||||
'change' => array(
|
||||
'tableParameters' => array(
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_general_ci',
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($compare, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaLoading method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaLoading() {
|
||||
$Other =& $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
|
||||
$this->assertEqual($Other->name, 'MyOtherApp');
|
||||
$this->assertEqual($Other->tables, $this->Schema->tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* test loading schema files inside of plugins.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaLoadingFromPlugin() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
$Other =& $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin'));
|
||||
$this->assertEqual($Other->name, 'TestPluginApp');
|
||||
$this->assertEqual(array_keys($Other->tables), array('acos'));
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testSchemaCreateTable method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSchemaCreateTable() {
|
||||
$db =& ConnectionManager::getDataSource('test_suite');
|
||||
$db->cacheSources = false;
|
||||
|
||||
$Schema =& new CakeSchema(array(
|
||||
'connection' => 'test_suite',
|
||||
'testdescribes' => array(
|
||||
'id' => array('type' => 'integer', 'key' => 'primary'),
|
||||
'int_null' => array('type' => 'integer', 'null' => true),
|
||||
'int_not_null' => array('type' => 'integer', 'null' => false),
|
||||
),
|
||||
));
|
||||
$sql = $db->createSchema($Schema);
|
||||
|
||||
$col = $Schema->tables['testdescribes']['int_null'];
|
||||
$col['name'] = 'int_null';
|
||||
$column = $this->db->buildColumn($col);
|
||||
$this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
|
||||
|
||||
$col = $Schema->tables['testdescribes']['int_not_null'];
|
||||
$col['name'] = 'int_not_null';
|
||||
$column = $this->db->buildColumn($col);
|
||||
$this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
|
||||
}
|
||||
}
|
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
/**
|
||||
* Connection Manager tests
|
||||
*
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The Open Group Test Suite License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @since CakePHP(tm) v 1.2.0.5550
|
||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||
*/
|
||||
App::import('Core', 'ConnectionManager');
|
||||
|
||||
/**
|
||||
* ConnectionManagerTest
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.models
|
||||
*/
|
||||
class ConnectionManagerTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setUp() {
|
||||
$this->ConnectionManager =& ConnectionManager::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function tearDown() {
|
||||
unset($this->ConnectionManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* testInstantiation method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testInstantiation() {
|
||||
$this->assertTrue(is_a($this->ConnectionManager, 'ConnectionManager'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testEnumConnectionObjects method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testEnumConnectionObjects() {
|
||||
$sources = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertTrue(count($sources) >= 1);
|
||||
|
||||
$connections = array('default', 'test', 'test_suite');
|
||||
$this->assertTrue(count(array_intersect(array_keys($sources), $connections)) >= 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetDataSource method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetDataSource() {
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertTrue(count(array_keys($connections) >= 1));
|
||||
|
||||
$source = ConnectionManager::getDataSource(key($connections));
|
||||
$this->assertTrue(is_object($source));
|
||||
|
||||
$this->expectError(new PatternExpectation('/Non-existent data source/i'));
|
||||
|
||||
$source = ConnectionManager::getDataSource('non_existent_source');
|
||||
$this->assertEqual($source, null);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetPluginDataSource method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetPluginDataSource() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
|
||||
$name = 'test_source';
|
||||
$config = array('datasource' => 'TestPlugin.TestSource');
|
||||
$connection = ConnectionManager::create($name, $config);
|
||||
|
||||
$this->assertTrue(class_exists('TestSource'));
|
||||
$this->assertEqual($connection->configKeyName, $name);
|
||||
$this->assertEqual($connection->config, $config);
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetPluginDataSourceAndPluginDriver method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetPluginDataSourceAndPluginDriver() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
|
||||
$name = 'test_plugin_source_and_driver';
|
||||
$config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'TestPlugin.TestDriver');
|
||||
|
||||
$connection = ConnectionManager::create($name, $config);
|
||||
|
||||
$this->assertTrue(class_exists('TestSource'));
|
||||
$this->assertTrue(class_exists('TestDriver'));
|
||||
$this->assertEqual($connection->configKeyName, $name);
|
||||
$this->assertEqual($connection->config, $config);
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetLocalDataSourceAndPluginDriver method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetLocalDataSourceAndPluginDriver() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
||||
));
|
||||
|
||||
$name = 'test_local_source_and_plugin_driver';
|
||||
$config = array('datasource' => 'dbo', 'driver' => 'TestPlugin.DboDummy');
|
||||
|
||||
$connection = ConnectionManager::create($name, $config);
|
||||
|
||||
$this->assertTrue(class_exists('DboSource'));
|
||||
$this->assertTrue(class_exists('DboDummy'));
|
||||
$this->assertEqual($connection->configKeyName, $name);
|
||||
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetPluginDataSourceAndLocalDriver method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetPluginDataSourceAndLocalDriver() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
|
||||
'datasources' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS)
|
||||
));
|
||||
|
||||
$name = 'test_plugin_source_and_local_driver';
|
||||
$config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'local_driver');
|
||||
|
||||
$connection = ConnectionManager::create($name, $config);
|
||||
|
||||
$this->assertTrue(class_exists('TestSource'));
|
||||
$this->assertTrue(class_exists('TestLocalDriver'));
|
||||
$this->assertEqual($connection->configKeyName, $name);
|
||||
$this->assertEqual($connection->config, $config);
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* testSourceList method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSourceList() {
|
||||
$sources = ConnectionManager::sourceList();
|
||||
$this->assertTrue(count($sources) >= 1);
|
||||
|
||||
$connections = array('default', 'test', 'test_suite');
|
||||
$this->assertTrue(count(array_intersect($sources, $connections)) >= 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetSourceName method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetSourceName() {
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$name = key($connections);
|
||||
$source = ConnectionManager::getDataSource($name);
|
||||
$result = ConnectionManager::getSourceName($source);
|
||||
|
||||
$this->assertEqual($result, $name);
|
||||
|
||||
$source =& new StdClass();
|
||||
$result = ConnectionManager::getSourceName($source);
|
||||
$this->assertEqual($result, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* testLoadDataSource method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testLoadDataSource() {
|
||||
$connections = array(
|
||||
array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'),
|
||||
array('classname' => 'DboMysqli', 'filename' => 'dbo' . DS . 'dbo_mysqli'),
|
||||
array('classname' => 'DboMssql', 'filename' => 'dbo' . DS . 'dbo_mssql'),
|
||||
array('classname' => 'DboOracle', 'filename' => 'dbo' . DS . 'dbo_oracle'),
|
||||
);
|
||||
|
||||
foreach ($connections as $connection) {
|
||||
$exists = class_exists($connection['classname']);
|
||||
$loaded = ConnectionManager::loadDataSource($connection);
|
||||
$this->assertEqual($loaded, !$exists, "%s Failed loading the {$connection['classname']} datasource");
|
||||
}
|
||||
|
||||
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
|
||||
$this->expectError(new PatternExpectation('/Unable to import DataSource class/i'));
|
||||
|
||||
$loaded = ConnectionManager::loadDataSource($connection);
|
||||
$this->assertEqual($loaded, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* testCreateDataSource method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testCreateDataSourceWithIntegrationTests() {
|
||||
$name = 'test_created_connection';
|
||||
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertTrue(count(array_keys($connections) >= 1));
|
||||
|
||||
$source = ConnectionManager::getDataSource(key($connections));
|
||||
$this->assertTrue(is_object($source));
|
||||
|
||||
$config = $source->config;
|
||||
$connection = ConnectionManager::create($name, $config);
|
||||
|
||||
$this->assertTrue(is_object($connection));
|
||||
$this->assertEqual($name, $connection->configKeyName);
|
||||
$this->assertEqual($name, ConnectionManager::getSourceName($connection));
|
||||
|
||||
$source = ConnectionManager::create(null, array());
|
||||
$this->assertEqual($source, null);
|
||||
|
||||
$source = ConnectionManager::create('another_test', array());
|
||||
$this->assertEqual($source, null);
|
||||
|
||||
$config = array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql');
|
||||
$source = ConnectionManager::create(null, $config);
|
||||
$this->assertEqual($source, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* testConnectionData method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testConnectionData() {
|
||||
App::build(array(
|
||||
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
|
||||
'datasources' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS)
|
||||
));
|
||||
|
||||
$expected = array(
|
||||
'filename' => 'test2_source',
|
||||
'classname' => 'Test2Source',
|
||||
'parent' => '',
|
||||
'plugin' => ''
|
||||
);
|
||||
|
||||
ConnectionManager::create('connection1', array('datasource' => 'Test2'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertEqual($expected, $connections['connection1']);
|
||||
|
||||
ConnectionManager::create('connection2', array('datasource' => 'Test2Source'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertEqual($expected, $connections['connection2']);
|
||||
|
||||
ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.Test'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$expected['filename'] = 'test_source';
|
||||
$expected['classname'] = 'TestSource';
|
||||
$expected['plugin'] = 'TestPlugin';
|
||||
$this->assertEqual($expected, $connections['connection3']);
|
||||
|
||||
ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertEqual($expected, $connections['connection4']);
|
||||
|
||||
ConnectionManager::create('connection5', array('datasource' => 'Test2Other'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$expected['filename'] = 'test2_other_source';
|
||||
$expected['classname'] = 'Test2OtherSource';
|
||||
$expected['plugin'] = '';
|
||||
$this->assertEqual($expected, $connections['connection5']);
|
||||
|
||||
ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertEqual($expected, $connections['connection6']);
|
||||
|
||||
ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOther'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$expected['filename'] = 'test_other_source';
|
||||
$expected['classname'] = 'TestOtherSource';
|
||||
$expected['plugin'] = 'TestPlugin';
|
||||
$this->assertEqual($expected, $connections['connection7']);
|
||||
|
||||
ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource'));
|
||||
$connections = ConnectionManager::enumConnectionObjects();
|
||||
$this->assertEqual($expected, $connections['connection8']);
|
||||
}
|
||||
}
|
@@ -0,0 +1,677 @@
|
||||
<?php
|
||||
/**
|
||||
* DboMssqlTest file
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs
|
||||
* @since CakePHP(tm) v 1.2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
||||
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
|
||||
}
|
||||
require_once LIBS.'model'.DS.'model.php';
|
||||
require_once LIBS.'model'.DS.'datasources'.DS.'datasource.php';
|
||||
require_once LIBS.'model'.DS.'datasources'.DS.'dbo_source.php';
|
||||
require_once LIBS.'model'.DS.'datasources'.DS.'dbo'.DS.'dbo_mssql.php';
|
||||
|
||||
/**
|
||||
* DboMssqlTestDb class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources.dbo
|
||||
*/
|
||||
class DboMssqlTestDb extends DboMssql {
|
||||
|
||||
/**
|
||||
* simulated property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $simulated = array();
|
||||
|
||||
/**
|
||||
* simalate property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $simulate = true;
|
||||
/**
|
||||
* fetchAllResultsStack
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fetchAllResultsStack = array();
|
||||
|
||||
/**
|
||||
* execute method
|
||||
*
|
||||
* @param mixed $sql
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
function _execute($sql) {
|
||||
if ($this->simulate) {
|
||||
$this->simulated[] = $sql;
|
||||
return null;
|
||||
} else {
|
||||
return parent::_execute($sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fetchAll method
|
||||
*
|
||||
* @param mixed $sql
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
function _matchRecords(&$model, $conditions = null) {
|
||||
return $this->conditions(array('id' => array(1, 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* fetchAll method
|
||||
*
|
||||
* @param mixed $sql
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
function fetchAll($sql, $cache = true, $modelName = null) {
|
||||
$result = parent::fetchAll($sql, $cache, $modelName);
|
||||
if (!empty($this->fetchAllResultsStack)) {
|
||||
return array_pop($this->fetchAllResultsStack);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastQuery method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function getLastQuery() {
|
||||
return $this->simulated[count($this->simulated) - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* getPrimaryKey method
|
||||
*
|
||||
* @param mixed $model
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function getPrimaryKey($model) {
|
||||
return parent::_getPrimaryKey($model);
|
||||
}
|
||||
/**
|
||||
* clearFieldMappings method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function clearFieldMappings() {
|
||||
$this->__fieldMappings = array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MssqlTestModel class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources
|
||||
*/
|
||||
class MssqlTestModel extends Model {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'MssqlTestModel'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'MssqlTestModel';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var bool false
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = false;
|
||||
|
||||
/**
|
||||
* _schema property
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
var $_schema = array(
|
||||
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
|
||||
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
|
||||
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
|
||||
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
||||
);
|
||||
|
||||
/**
|
||||
* belongsTo property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $belongsTo = array(
|
||||
'MssqlClientTestModel' => array(
|
||||
'foreignKey' => 'client_id'
|
||||
)
|
||||
);
|
||||
/**
|
||||
* find method
|
||||
*
|
||||
* @param mixed $conditions
|
||||
* @param mixed $fields
|
||||
* @param mixed $order
|
||||
* @param mixed $recursive
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function find($conditions = null, $fields = null, $order = null, $recursive = null) {
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* findAll method
|
||||
*
|
||||
* @param mixed $conditions
|
||||
* @param mixed $fields
|
||||
* @param mixed $order
|
||||
* @param mixed $recursive
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* setSchema method
|
||||
*
|
||||
* @param array $schema
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setSchema($schema) {
|
||||
$this->_schema = $schema;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MssqlClientTestModel class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources
|
||||
*/
|
||||
class MssqlClientTestModel extends Model {
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'MssqlAssociatedTestModel'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'MssqlClientTestModel';
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var bool false
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = false;
|
||||
/**
|
||||
* _schema property
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
var $_schema = array(
|
||||
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
|
||||
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* DboMssqlTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources.dbo
|
||||
*/
|
||||
class DboMssqlTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* The Dbo instance to be tested
|
||||
*
|
||||
* @var DboSource
|
||||
* @access public
|
||||
*/
|
||||
var $db = null;
|
||||
|
||||
/**
|
||||
* autoFixtures property
|
||||
*
|
||||
* @var bool false
|
||||
* @access public
|
||||
*/
|
||||
var $autoFixtures = false;
|
||||
/**
|
||||
* fixtures property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $fixtures = array('core.category');
|
||||
/**
|
||||
* Skip if cannot connect to mssql
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function skip() {
|
||||
$this->_initDb();
|
||||
$this->skipUnless($this->db->config['driver'] == 'mssql', '%s SQL Server connection not available');
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure all fixtures tables are being created
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function start() {
|
||||
$this->db->simulate = false;
|
||||
parent::start();
|
||||
$this->db->simulate = true;
|
||||
}
|
||||
/**
|
||||
* Make sure all fixtures tables are being dropped
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function end() {
|
||||
$this->db->simulate = false;
|
||||
parent::end();
|
||||
$this->db->simulate = true;
|
||||
}
|
||||
/**
|
||||
* Sets up a Dbo class instance for testing
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function setUp() {
|
||||
$db = ConnectionManager::getDataSource('test_suite');
|
||||
$this->db = new DboMssqlTestDb($db->config);
|
||||
$this->model = new MssqlTestModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function tearDown() {
|
||||
unset($this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
* testQuoting method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testQuoting() {
|
||||
$expected = "1.2";
|
||||
$result = $this->db->value(1.2, 'float');
|
||||
$this->assertIdentical($expected, $result);
|
||||
|
||||
$expected = "'1,2'";
|
||||
$result = $this->db->value('1,2', 'float');
|
||||
$this->assertIdentical($expected, $result);
|
||||
|
||||
$expected = 'NULL';
|
||||
$result = $this->db->value('', 'integer');
|
||||
$this->assertIdentical($expected, $result);
|
||||
|
||||
$expected = 'NULL';
|
||||
$result = $this->db->value('', 'float');
|
||||
$this->assertIdentical($expected, $result);
|
||||
|
||||
$expected = 'NULL';
|
||||
$result = $this->db->value('', 'binary');
|
||||
$this->assertIdentical($expected, $result);
|
||||
}
|
||||
/**
|
||||
* testFields method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testFields() {
|
||||
$fields = array(
|
||||
'[MssqlTestModel].[id] AS [MssqlTestModel__0]',
|
||||
'[MssqlTestModel].[client_id] AS [MssqlTestModel__1]',
|
||||
'[MssqlTestModel].[name] AS [MssqlTestModel__2]',
|
||||
'[MssqlTestModel].[login] AS [MssqlTestModel__3]',
|
||||
'[MssqlTestModel].[passwd] AS [MssqlTestModel__4]',
|
||||
'[MssqlTestModel].[addr_1] AS [MssqlTestModel__5]',
|
||||
'[MssqlTestModel].[addr_2] AS [MssqlTestModel__6]',
|
||||
'[MssqlTestModel].[zip_code] AS [MssqlTestModel__7]',
|
||||
'[MssqlTestModel].[city] AS [MssqlTestModel__8]',
|
||||
'[MssqlTestModel].[country] AS [MssqlTestModel__9]',
|
||||
'[MssqlTestModel].[phone] AS [MssqlTestModel__10]',
|
||||
'[MssqlTestModel].[fax] AS [MssqlTestModel__11]',
|
||||
'[MssqlTestModel].[url] AS [MssqlTestModel__12]',
|
||||
'[MssqlTestModel].[email] AS [MssqlTestModel__13]',
|
||||
'[MssqlTestModel].[comments] AS [MssqlTestModel__14]',
|
||||
'CONVERT(VARCHAR(20), [MssqlTestModel].[last_login], 20) AS [MssqlTestModel__15]',
|
||||
'[MssqlTestModel].[created] AS [MssqlTestModel__16]',
|
||||
'CONVERT(VARCHAR(20), [MssqlTestModel].[updated], 20) AS [MssqlTestModel__17]'
|
||||
);
|
||||
|
||||
$result = $this->db->fields($this->model);
|
||||
$expected = $fields;
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->db->clearFieldMappings();
|
||||
$result = $this->db->fields($this->model, null, 'MssqlTestModel.*');
|
||||
$expected = $fields;
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->db->clearFieldMappings();
|
||||
$result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
|
||||
$expected = array_merge($fields, array(
|
||||
'[AnotherModel].[id] AS [AnotherModel__18]',
|
||||
'[AnotherModel].[name] AS [AnotherModel__19]'));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$this->db->clearFieldMappings();
|
||||
$result = $this->db->fields($this->model, null, array('*', 'MssqlClientTestModel.*'));
|
||||
$expected = array_merge($fields, array(
|
||||
'[MssqlClientTestModel].[id] AS [MssqlClientTestModel__18]',
|
||||
'[MssqlClientTestModel].[name] AS [MssqlClientTestModel__19]',
|
||||
'[MssqlClientTestModel].[email] AS [MssqlClientTestModel__20]',
|
||||
'CONVERT(VARCHAR(20), [MssqlClientTestModel].[created], 20) AS [MssqlClientTestModel__21]',
|
||||
'CONVERT(VARCHAR(20), [MssqlClientTestModel].[updated], 20) AS [MssqlClientTestModel__22]'));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDistinctFields method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testDistinctFields() {
|
||||
$result = $this->db->fields($this->model, null, array('DISTINCT Car.country_code'));
|
||||
$expected = array('DISTINCT [Car].[country_code] AS [Car__0]');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
|
||||
$expected = array('DISTINCT [Car].[country_code] AS [Car__1]');
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDistinctWithLimit method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testDistinctWithLimit() {
|
||||
$this->db->read($this->model, array(
|
||||
'fields' => array('DISTINCT MssqlTestModel.city', 'MssqlTestModel.country'),
|
||||
'limit' => 5
|
||||
));
|
||||
$result = $this->db->getLastQuery();
|
||||
$this->assertPattern('/^SELECT DISTINCT TOP 5/', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDescribe method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testDescribe() {
|
||||
$MssqlTableDescription = array(
|
||||
0 => array(
|
||||
0 => array(
|
||||
'Default' => '((0))',
|
||||
'Field' => 'count',
|
||||
'Key' => 0,
|
||||
'Length' => '4',
|
||||
'Null' => 'NO',
|
||||
'Type' => 'integer',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->db->fetchAllResultsStack = array($MssqlTableDescription);
|
||||
$dummyModel = $this->model;
|
||||
$result = $this->db->describe($dummyModel);
|
||||
$expected = array(
|
||||
'count' => array(
|
||||
'type' => 'integer',
|
||||
'null' => false,
|
||||
'default' => '0',
|
||||
'length' => 4
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testBuildColumn
|
||||
*
|
||||
* @return unknown_type
|
||||
* @access public
|
||||
*/
|
||||
function testBuildColumn() {
|
||||
$column = array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[id] int IDENTITY (1, 1) NOT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'client_id', 'type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[client_id] int DEFAULT 0 NOT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[client_id] int NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// 'name' => 'type' format for columns
|
||||
$column = array('type' => 'integer', 'name' => 'client_id');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[client_id] int NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('type' => 'string', 'name' => 'name');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) DEFAULT \'\' NOT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) NOT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) NOT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
|
||||
$result = $this->db->buildColumn($column);
|
||||
$expected = '[name] varchar(255) DEFAULT \'\'';
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testBuildIndex method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testBuildIndex() {
|
||||
$indexes = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'client_id' => array('column' => 'client_id', 'unique' => 1)
|
||||
);
|
||||
$result = $this->db->buildIndex($indexes, 'items');
|
||||
$expected = array(
|
||||
'PRIMARY KEY ([id])',
|
||||
'ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id]);'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$indexes = array('client_id' => array('column' => 'client_id'));
|
||||
$result = $this->db->buildIndex($indexes, 'items');
|
||||
$this->assertEqual($result, array());
|
||||
|
||||
$indexes = array('client_id' => array('column' => array('client_id', 'period_id'), 'unique' => 1));
|
||||
$result = $this->db->buildIndex($indexes, 'items');
|
||||
$expected = array('ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id], [period_id]);');
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testUpdateAllSyntax method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testUpdateAllSyntax() {
|
||||
$fields = array('MssqlTestModel.client_id' => '[MssqlTestModel].[client_id] + 1');
|
||||
$conditions = array('MssqlTestModel.updated <' => date('2009-01-01 00:00:00'));
|
||||
$this->db->update($this->model, $fields, null, $conditions);
|
||||
|
||||
$result = $this->db->getLastQuery();
|
||||
$this->assertNoPattern('/MssqlTestModel/', $result);
|
||||
$this->assertPattern('/^UPDATE \[mssql_test_models\]/', $result);
|
||||
$this->assertPattern('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetPrimaryKey method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testGetPrimaryKey() {
|
||||
// When param is a model
|
||||
$result = $this->db->getPrimaryKey($this->model);
|
||||
$this->assertEqual($result, 'id');
|
||||
|
||||
$schema = $this->model->schema();
|
||||
unset($schema['id']['key']);
|
||||
$this->model->setSchema($schema);
|
||||
$result = $this->db->getPrimaryKey($this->model);
|
||||
$this->assertNull($result);
|
||||
|
||||
// When param is a table name
|
||||
$this->db->simulate = false;
|
||||
$this->loadFixtures('Category');
|
||||
$result = $this->db->getPrimaryKey('categories');
|
||||
$this->assertEqual($result, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* testInsertMulti
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testInsertMulti() {
|
||||
$fields = array('id', 'name', 'login');
|
||||
$values = array('(1, \'Larry\', \'PhpNut\')', '(2, \'Renan\', \'renan.saddam\')');
|
||||
$this->db->simulated = array();
|
||||
$this->db->insertMulti($this->model, $fields, $values);
|
||||
$result = $this->db->simulated;
|
||||
$expected = array(
|
||||
'SET IDENTITY_INSERT [mssql_test_models] ON',
|
||||
'INSERT INTO [mssql_test_models] ([id], [name], [login]) VALUES (1, \'Larry\', \'PhpNut\')',
|
||||
'INSERT INTO [mssql_test_models] ([id], [name], [login]) VALUES (2, \'Renan\', \'renan.saddam\')',
|
||||
'SET IDENTITY_INSERT [mssql_test_models] OFF'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$fields = array('name', 'login');
|
||||
$values = array('(\'Larry\', \'PhpNut\')', '(\'Renan\', \'renan.saddam\')');
|
||||
$this->db->simulated = array();
|
||||
$this->db->insertMulti($this->model, $fields, $values);
|
||||
$result = $this->db->simulated;
|
||||
$expected = array(
|
||||
'INSERT INTO [mssql_test_models] ([name], [login]) VALUES (\'Larry\', \'PhpNut\')',
|
||||
'INSERT INTO [mssql_test_models] ([name], [login]) VALUES (\'Renan\', \'renan.saddam\')'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testLastError
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testLastError() {
|
||||
$debug = Configure::read('debug');
|
||||
Configure::write('debug', 0);
|
||||
|
||||
$this->db->simulate = false;
|
||||
$query = 'SELECT [name] FROM [categories]';
|
||||
$this->assertTrue($this->db->execute($query) !== false);
|
||||
$this->assertNull($this->db->lastError());
|
||||
|
||||
$query = 'SELECT [inexistent_field] FROM [categories]';
|
||||
$this->assertFalse($this->db->execute($query));
|
||||
$this->assertNotNull($this->db->lastError());
|
||||
|
||||
$query = 'SELECT [name] FROM [categories]';
|
||||
$this->assertTrue($this->db->execute($query) !== false);
|
||||
$this->assertNull($this->db->lastError());
|
||||
|
||||
Configure::write('debug', $debug);
|
||||
}
|
||||
}
|
@@ -0,0 +1,866 @@
|
||||
<?php
|
||||
/**
|
||||
* DboMysqlTest file
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs
|
||||
* @since CakePHP(tm) v 1.2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql'));
|
||||
|
||||
Mock::generatePartial('DboMysql', 'QueryMockDboMysql', array('query'));
|
||||
|
||||
/**
|
||||
* DboMysqlTestDb class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources
|
||||
*/
|
||||
class DboMysqlTestDb extends DboMysql {
|
||||
|
||||
/**
|
||||
* simulated property
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $simulated = array();
|
||||
|
||||
/**
|
||||
* testing property
|
||||
*
|
||||
* @var bool true
|
||||
* @access public
|
||||
*/
|
||||
var $testing = true;
|
||||
|
||||
/**
|
||||
* execute method
|
||||
*
|
||||
* @param mixed $sql
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
function _execute($sql) {
|
||||
if ($this->testing) {
|
||||
$this->simulated[] = $sql;
|
||||
return null;
|
||||
}
|
||||
return parent::_execute($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastQuery method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function getLastQuery() {
|
||||
return $this->simulated[count($this->simulated) - 1];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MysqlTestModel class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources
|
||||
*/
|
||||
class MysqlTestModel extends Model {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string 'MysqlTestModel'
|
||||
* @access public
|
||||
*/
|
||||
var $name = 'MysqlTestModel';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var bool false
|
||||
* @access public
|
||||
*/
|
||||
var $useTable = false;
|
||||
|
||||
/**
|
||||
* find method
|
||||
*
|
||||
* @param mixed $conditions
|
||||
* @param mixed $fields
|
||||
* @param mixed $order
|
||||
* @param mixed $recursive
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function find($conditions = null, $fields = null, $order = null, $recursive = null) {
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* findAll method
|
||||
*
|
||||
* @param mixed $conditions
|
||||
* @param mixed $fields
|
||||
* @param mixed $order
|
||||
* @param mixed $recursive
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* schema method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function schema() {
|
||||
return array(
|
||||
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
|
||||
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
|
||||
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
|
||||
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DboMysqlTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.model.datasources.dbo
|
||||
*/
|
||||
class DboMysqlTest extends CakeTestCase {
|
||||
var $fixtures = array('core.binary_test');
|
||||
/**
|
||||
* The Dbo instance to be tested
|
||||
*
|
||||
* @var DboSource
|
||||
* @access public
|
||||
*/
|
||||
var $Db = null;
|
||||
|
||||
/**
|
||||
* Skip if cannot connect to mysql
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function skip() {
|
||||
$this->_initDb();
|
||||
$this->skipUnless($this->db->config['driver'] == 'mysql', '%s MySQL connection not available');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a Dbo class instance for testing
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function startTest() {
|
||||
$db = ConnectionManager::getDataSource('test_suite');
|
||||
$this->model = new MysqlTestModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a Dbo class instance for testing
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function tearDown() {
|
||||
unset($this->model);
|
||||
ClassRegistry::flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* startCase
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function startCase() {
|
||||
$this->_debug = Configure::read('debug');
|
||||
Configure::write('debug', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* endCase
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function endCase() {
|
||||
Configure::write('debug', $this->_debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Dbo value method
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function testQuoting() {
|
||||
$result = $this->db->fields($this->model);
|
||||
$expected = array(
|
||||
'`MysqlTestModel`.`id`',
|
||||
'`MysqlTestModel`.`client_id`',
|
||||
'`MysqlTestModel`.`name`',
|
||||
'`MysqlTestModel`.`login`',
|
||||
'`MysqlTestModel`.`passwd`',
|
||||
'`MysqlTestModel`.`addr_1`',
|
||||
'`MysqlTestModel`.`addr_2`',
|
||||
'`MysqlTestModel`.`zip_code`',
|
||||
'`MysqlTestModel`.`city`',
|
||||
'`MysqlTestModel`.`country`',
|
||||
'`MysqlTestModel`.`phone`',
|
||||
'`MysqlTestModel`.`fax`',
|
||||
'`MysqlTestModel`.`url`',
|
||||
'`MysqlTestModel`.`email`',
|
||||
'`MysqlTestModel`.`comments`',
|
||||
'`MysqlTestModel`.`last_login`',
|
||||
'`MysqlTestModel`.`created`',
|
||||
'`MysqlTestModel`.`updated`'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$expected = 1.2;
|
||||
$result = $this->db->value(1.2, 'float');
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = "'1,2'";
|
||||
$result = $this->db->value('1,2', 'float');
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = "'4713e29446'";
|
||||
$result = $this->db->value('4713e29446');
|
||||
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = 'NULL';
|
||||
$result = $this->db->value('', 'integer');
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = 'NULL';
|
||||
$result = $this->db->value('', 'boolean');
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = 10010001;
|
||||
$result = $this->db->value(10010001);
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$expected = "'00010010001'";
|
||||
$result = $this->db->value('00010010001');
|
||||
$this->assertEqual($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that localized floats don't cause trouble.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testLocalizedFloats() {
|
||||
$restore = setlocale(LC_ALL, null);
|
||||
setlocale(LC_ALL, 'de_DE');
|
||||
|
||||
$result = $this->db->value(3.141593, 'float');
|
||||
$this->assertEqual((string)$result, '3.141593');
|
||||
|
||||
$result = $this->db->value(3.141593);
|
||||
$this->assertEqual((string)$result, '3.141593');
|
||||
|
||||
setlocale(LC_ALL, $restore);
|
||||
}
|
||||
|
||||
/**
|
||||
* testTinyintCasting method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testTinyintCasting() {
|
||||
$this->db->cacheSources = false;
|
||||
$this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
|
||||
|
||||
$this->model = new CakeTestModel(array(
|
||||
'name' => 'Tinyint', 'table' => 'tinyint', 'ds' => 'test_suite'
|
||||
));
|
||||
|
||||
$result = $this->model->schema();
|
||||
$this->assertEqual($result['bool']['type'], 'boolean');
|
||||
$this->assertEqual($result['small_int']['type'], 'integer');
|
||||
|
||||
$this->assertTrue($this->model->save(array('bool' => 5, 'small_int' => 5)));
|
||||
$result = $this->model->find('first');
|
||||
$this->assertIdentical($result['Tinyint']['bool'], '1');
|
||||
$this->assertIdentical($result['Tinyint']['small_int'], '5');
|
||||
$this->model->deleteAll(true);
|
||||
|
||||
$this->assertTrue($this->model->save(array('bool' => 0, 'small_int' => 100)));
|
||||
$result = $this->model->find('first');
|
||||
$this->assertIdentical($result['Tinyint']['bool'], '0');
|
||||
$this->assertIdentical($result['Tinyint']['small_int'], '100');
|
||||
$this->model->deleteAll(true);
|
||||
|
||||
$this->assertTrue($this->model->save(array('bool' => true, 'small_int' => 0)));
|
||||
$result = $this->model->find('first');
|
||||
$this->assertIdentical($result['Tinyint']['bool'], '1');
|
||||
$this->assertIdentical($result['Tinyint']['small_int'], '0');
|
||||
$this->model->deleteAll(true);
|
||||
|
||||
$this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testIndexDetection method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testIndexDetection() {
|
||||
$this->db->cacheSources = false;
|
||||
|
||||
$name = $this->db->fullTableName('simple');
|
||||
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
|
||||
$expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
|
||||
$result = $this->db->index('simple', false);
|
||||
$this->assertEqual($expected, $result);
|
||||
$this->db->query('DROP TABLE ' . $name);
|
||||
|
||||
$name = $this->db->fullTableName('with_a_key');
|
||||
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
|
||||
$expected = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
|
||||
);
|
||||
$result = $this->db->index('with_a_key', false);
|
||||
$this->assertEqual($expected, $result);
|
||||
$this->db->query('DROP TABLE ' . $name);
|
||||
|
||||
$name = $this->db->fullTableName('with_two_keys');
|
||||
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));');
|
||||
$expected = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
|
||||
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
|
||||
);
|
||||
$result = $this->db->index('with_two_keys', false);
|
||||
$this->assertEqual($expected, $result);
|
||||
$this->db->query('DROP TABLE ' . $name);
|
||||
|
||||
$name = $this->db->fullTableName('with_compound_keys');
|
||||
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));');
|
||||
$expected = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
|
||||
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
|
||||
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
|
||||
);
|
||||
$result = $this->db->index('with_compound_keys', false);
|
||||
$this->assertEqual($expected, $result);
|
||||
$this->db->query('DROP TABLE ' . $name);
|
||||
|
||||
$name = $this->db->fullTableName('with_multiple_compound_keys');
|
||||
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));');
|
||||
$expected = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
|
||||
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
|
||||
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
|
||||
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
|
||||
);
|
||||
$result = $this->db->index('with_multiple_compound_keys', false);
|
||||
$this->assertEqual($expected, $result);
|
||||
$this->db->query('DROP TABLE ' . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* testBuildColumn method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testBuildColumn() {
|
||||
$restore = $this->db->columns;
|
||||
$this->db->columns = array('varchar(255)' => 1);
|
||||
$data = array(
|
||||
'name' => 'testName',
|
||||
'type' => 'varchar(255)',
|
||||
'default',
|
||||
'null' => true,
|
||||
'key',
|
||||
'comment' => 'test'
|
||||
);
|
||||
$result = $this->db->buildColumn($data);
|
||||
$expected = '`testName` DEFAULT NULL COMMENT \'test\'';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$data = array(
|
||||
'name' => 'testName',
|
||||
'type' => 'varchar(255)',
|
||||
'default',
|
||||
'null' => true,
|
||||
'key',
|
||||
'charset' => 'utf8',
|
||||
'collate' => 'utf8_unicode_ci'
|
||||
);
|
||||
$result = $this->db->buildColumn($data);
|
||||
$expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL';
|
||||
$this->assertEqual($result, $expected);
|
||||
$this->db->columns = $restore;
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL 4.x returns index data in a different format,
|
||||
* Using a mock ensure that MySQL 4.x output is properly parsed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testIndexOnMySQL4Output() {
|
||||
$name = $this->db->fullTableName('simple');
|
||||
|
||||
$mockDbo =& new QueryMockDboMysql($this);
|
||||
$columnData = array(
|
||||
array('0' => array(
|
||||
'Table' => 'with_compound_keys',
|
||||
'Non_unique' => '0',
|
||||
'Key_name' => 'PRIMARY',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'id',
|
||||
'Collation' => 'A',
|
||||
'Cardinality' => '0',
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => '',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => ''
|
||||
)),
|
||||
array('0' => array(
|
||||
'Table' => 'with_compound_keys',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'pointless_bool',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'bool',
|
||||
'Collation' => 'A',
|
||||
'Cardinality' => NULL,
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => 'YES',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => ''
|
||||
)),
|
||||
array('0' => array(
|
||||
'Table' => 'with_compound_keys',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'pointless_small_int',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'small_int',
|
||||
'Collation' => 'A',
|
||||
'Cardinality' => NULL,
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => 'YES',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => ''
|
||||
)),
|
||||
array('0' => array(
|
||||
'Table' => 'with_compound_keys',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'one_way',
|
||||
'Seq_in_index' => '1',
|
||||
'Column_name' => 'bool',
|
||||
'Collation' => 'A',
|
||||
'Cardinality' => NULL,
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => 'YES',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => ''
|
||||
)),
|
||||
array('0' => array(
|
||||
'Table' => 'with_compound_keys',
|
||||
'Non_unique' => '1',
|
||||
'Key_name' => 'one_way',
|
||||
'Seq_in_index' => '2',
|
||||
'Column_name' => 'small_int',
|
||||
'Collation' => 'A',
|
||||
'Cardinality' => NULL,
|
||||
'Sub_part' => NULL,
|
||||
'Packed' => NULL,
|
||||
'Null' => 'YES',
|
||||
'Index_type' => 'BTREE',
|
||||
'Comment' => ''
|
||||
))
|
||||
);
|
||||
$mockDbo->setReturnValue('query', $columnData, array('SHOW INDEX FROM ' . $name));
|
||||
|
||||
$result = $mockDbo->index($name, false);
|
||||
$expected = array(
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
||||
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
|
||||
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
|
||||
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testColumn method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testColumn() {
|
||||
$result = $this->db->column('varchar(50)');
|
||||
$expected = 'string';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('text');
|
||||
$expected = 'text';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('int(11)');
|
||||
$expected = 'integer';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('int(11) unsigned');
|
||||
$expected = 'integer';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('tinyint(1)');
|
||||
$expected = 'boolean';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('boolean');
|
||||
$expected = 'boolean';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('float');
|
||||
$expected = 'float';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('float unsigned');
|
||||
$expected = 'float';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('double unsigned');
|
||||
$expected = 'float';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->db->column('decimal(14,7) unsigned');
|
||||
$expected = 'float';
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testAlterSchemaIndexes method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testAlterSchemaIndexes() {
|
||||
App::import('Model', 'CakeSchema');
|
||||
$this->db->cacheSources = $this->db->testing = false;
|
||||
|
||||
$schema1 =& new CakeSchema(array(
|
||||
'name' => 'AlterTest1',
|
||||
'connection' => 'test_suite',
|
||||
'altertest' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
|
||||
'group1' => array('type' => 'integer', 'null' => true),
|
||||
'group2' => array('type' => 'integer', 'null' => true)
|
||||
)));
|
||||
$this->db->query($this->db->createSchema($schema1));
|
||||
|
||||
$schema2 =& new CakeSchema(array(
|
||||
'name' => 'AlterTest2',
|
||||
'connection' => 'test_suite',
|
||||
'altertest' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
|
||||
'group1' => array('type' => 'integer', 'null' => true),
|
||||
'group2' => array('type' => 'integer', 'null' => true),
|
||||
'indexes' => array(
|
||||
'name_idx' => array('column' => 'name', 'unique' => 0),
|
||||
'group_idx' => array('column' => 'group1', 'unique' => 0),
|
||||
'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0),
|
||||
'PRIMARY' => array('column' => 'id', 'unique' => 1))
|
||||
)));
|
||||
$this->db->query($this->db->alterSchema($schema2->compare($schema1)));
|
||||
|
||||
$indexes = $this->db->index('altertest');
|
||||
$this->assertEqual($schema2->tables['altertest']['indexes'], $indexes);
|
||||
|
||||
// Change three indexes, delete one and add another one
|
||||
$schema3 =& new CakeSchema(array(
|
||||
'name' => 'AlterTest3',
|
||||
'connection' => 'test_suite',
|
||||
'altertest' => array(
|
||||
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
||||
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
|
||||
'group1' => array('type' => 'integer', 'null' => true),
|
||||
'group2' => array('type' => 'integer', 'null' => true),
|
||||
'indexes' => array(
|
||||
'name_idx' => array('column' => 'name', 'unique' => 1),
|
||||
'group_idx' => array('column' => 'group2', 'unique' => 0),
|
||||
'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0),
|
||||
'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0))
|
||||
)));
|
||||
|
||||
$this->db->query($this->db->alterSchema($schema3->compare($schema2)));
|
||||
|
||||
$indexes = $this->db->index('altertest');
|
||||
$this->assertEqual($schema3->tables['altertest']['indexes'], $indexes);
|
||||
|
||||
// Compare us to ourself.
|
||||
$this->assertEqual($schema3->compare($schema3), array());
|
||||
|
||||
// Drop the indexes
|
||||
$this->db->query($this->db->alterSchema($schema1->compare($schema3)));
|
||||
|
||||
$indexes = $this->db->index('altertest');
|
||||
$this->assertEqual(array(), $indexes);
|
||||
|
||||
$this->db->query($this->db->dropSchema($schema1));
|
||||
}
|
||||
/**
|
||||
* test saving and retrieval of blobs
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testBlobSaving() {
|
||||
$this->db->cacheSources = false;
|
||||
$data = "GIF87ab |