Skip to content

Commit

Permalink
FIXED: Issue where $enable_siteconfig_generation flag was not being s…
Browse files Browse the repository at this point in the history
…et correctly, causing failed test cases_

FIXED: Duplicate entries in test database
  • Loading branch information
tractorcow authored and chillu committed Sep 7, 2012
1 parent 405fa7e commit a3b3ec8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 60 deletions.
38 changes: 21 additions & 17 deletions code/model/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1131,13 +1131,6 @@ function getTranslation($locale, $stage = null) {
$translations = $this->getTranslations($locale, $stage);
return ($translations) ? $translations->First() : null;
}

/**
* Enables automatic population of SiteConfig fields using createTranslation if
* created outside of the Translatable module
* @var boolean
*/
public static $enable_siteconfig_generation = true;

/**
* When the SiteConfig object is automatically instantiated, we should ensure that
Expand All @@ -1150,12 +1143,14 @@ function getTranslation($locale, $stage = null) {
* @see SiteConfig
*/
protected function populateSiteConfig() {
// This is required to prevent infinite loop during createTranslation
// If createTranslation is called directly on SiteConfig then this function could
// be called twice, but it will not cause harm for this to happen.
if(!self::$enable_siteconfig_generation)

// Work-around for population of defaults during test setup (when tables are not created).
// When the database is being setup during testing, singleton('SiteConfig') is called.
// Singleton objects should not call populateDefault() but a bug seems to be allowing it
// to happen anyway. Looking up existing SiteConfig objects without a database crashes horribly
// @todo Remove this hack once this bug is fixed
if(SapphireTest::using_temp_db() && !DB::getConn()->hasTable($this->owner->class))
return;
self::$enable_siteconfig_generation = false;

// Find the best base translation for site config
Translatable::disable_locale_filter();
Expand All @@ -1181,19 +1176,28 @@ protected function populateSiteConfig() {

// Copy fields from translated object (including ID) to the generated object
// This is similar to the creation of objects via createTranslation, but we need to
// preserve the ID in order to prevent
// preserve the ID in order to prevent unnecessary object creation
$this->owner->update($stagingConfig->toMap());

// Re-enable config generation
self::$enable_siteconfig_generation = true;
}

/**
* Enables automatic population of SiteConfig fields using createTranslation if
* created outside of the Translatable module
* @var boolean
*/
public static $enable_siteconfig_generation = true;

/**
* Hooks into the DataObject::populateDefaults() method
*/
public function populateDefaults() {
if (empty($this->owner->ID) && $this->owner instanceof SiteConfig)
if (empty($this->owner->ID) && ($this->owner instanceof SiteConfig) && self::$enable_siteconfig_generation)
{
// Use enable_siteconfig_generation to prevent infinite loop during
self::$enable_siteconfig_generation = false;
$this->populateSiteConfig();
self::$enable_siteconfig_generation = true;
}
}

/**
Expand Down
43 changes: 0 additions & 43 deletions tests/unit/TranslatableSiteConfigTest.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
Permission:
cmsmain1:
Code: CMS_ACCESS_CMSMain
cmsmain2:
Code: CMS_ACCESS_CMSMain
translate_all1:
Code: TRANSLATE_ALL
translate_all2:
Code: TRANSLATE_ALL
Group:
translators_de:
Code: translators_de
Permissions: =>Permission.cmsmain1,=>Permission.translate_all1
translators_en:
Code: translators_en
Permissions: =>Permission.cmsmain2,=>Permission.translate_all2
Member:
translator_de:
Email: translator_de@test.com
Password: test
Groups: =>Group.translators_de
translator_en:
Email: translator_en@test.com
Password: test
Groups: =>Group.translators_en
websiteuser:
Email: websiteuser@test.com
Password: test
Page:
root_en:
URLSegment: root-en
Locale: en_US
SiteConfig:
en_US:
Title: My test site
Locale: en_US
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.translators_en
de_DE:
Title: Meine Test Seite
Locale: de_DE
CanEditType: OnlyTheseUsers
EditorGroups: =>Group.translators_de
Permission:
cmsmain1:
Code: CMS_ACCESS_CMSMain
Expand Down

0 comments on commit a3b3ec8

Please sign in to comment.