Skip to content

Commit

Permalink
Adding tests for database imports causing duplicated db prefix. Addin…
Browse files Browse the repository at this point in the history
…g a workaround for the duplicated prefix name.

Fixes #962
  • Loading branch information
markstory committed Jul 30, 2010
1 parent a9bb4ee commit 3bda2ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
35 changes: 33 additions & 2 deletions cake/tests/cases/libs/cake_test_fixture.test.php
Expand Up @@ -198,24 +198,55 @@ function testInitDbPrefix() {
$this->assertEqual(count($Fixture->records), count($Source->records)); $this->assertEqual(count($Fixture->records), count($Source->records));


$Fixture =& new CakeTestFixtureImportFixture(); $Fixture =& new CakeTestFixtureImportFixture();
$Fixture->fields = $Fixture->records = null; $Fixture->fields = $Fixture->records = $Fixture->table = null;
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite'); $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite');
$Fixture->init(); $Fixture->init();
$this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created')); $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));

$this->assertEqual($Fixture->table, 'fixture_tests');

$keys = array_flip(ClassRegistry::keys()); $keys = array_flip(ClassRegistry::keys());
$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys)); $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));


$Source->drop($this->db); $Source->drop($this->db);
} }


/**
* test that fixtures don't duplicate the test db prefix.
*
* @return void
*/
function testInitDbPrefixDuplication() {
$this->_initDb();
$backPrefix = $this->db->config['prefix'];
$this->db->config['prefix'] = 'cake_fixture_test_';

$Source =& new CakeTestFixtureTestFixture();
$Source->create($this->db);
$Source->insert($this->db);

$Fixture =& new CakeTestFixtureImportFixture();
$Fixture->fields = $Fixture->records = $Fixture->table = null;
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite');

$Fixture->init();
$this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
$this->assertEqual($Fixture->table, 'fixture_tests');

$Source->drop($this->db);
$this->db->config['prefix'] = $backPrefix;
}

/** /**
* test init with a model that has a tablePrefix declared. * test init with a model that has a tablePrefix declared.
* *
* @return void * @return void
*/ */
function testInitModelTablePrefix() { function testInitModelTablePrefix() {
$this->_initDb(); $this->_initDb();
$hasPrefix = !empty($this->db->config['prefix']);
if ($this->skipIf($hasPrefix, 'Cannot run this test, you have a database connection prefix.')) {
return;
}
$Source =& new CakeTestFixtureTestFixture(); $Source =& new CakeTestFixtureTestFixture();
$Source->create($this->db); $Source->create($this->db);
$Source->insert($this->db); $Source->insert($this->db);
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/lib/cake_test_fixture.php
Expand Up @@ -92,6 +92,11 @@ function init() {
$model->table = $import['table']; $model->table = $import['table'];
$model->tablePrefix = $db->config['prefix']; $model->tablePrefix = $db->config['prefix'];
$this->fields = $model->schema(true); $this->fields = $model->schema(true);
ClassRegistry::flush();
}

if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
$this->table = str_replace($db->config['prefix'], '', $this->table);
} }


if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) { if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
Expand Down

0 comments on commit 3bda2ce

Please sign in to comment.