diff --git a/code/model/Translatable.php b/code/model/Translatable.php index 5e24328..b41a5e4 100755 --- a/code/model/Translatable.php +++ b/code/model/Translatable.php @@ -1131,13 +1131,6 @@ class Translatable extends DataExtension implements PermissionProvider { $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 @@ -1150,12 +1143,14 @@ class Translatable extends DataExtension implements PermissionProvider { * @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(); @@ -1181,19 +1176,28 @@ class Translatable extends DataExtension implements PermissionProvider { // 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; + } } /** diff --git a/tests/unit/TranslatableSiteConfigTest.yml b/tests/unit/TranslatableSiteConfigTest.yml index 7f5151d..f0a52cc 100644 --- a/tests/unit/TranslatableSiteConfigTest.yml +++ b/tests/unit/TranslatableSiteConfigTest.yml @@ -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