Skip to content
This repository
Browse code

FIXED: Issue where $enable_siteconfig_generation flag was not being s…

…et correctly, causing failed test cases_

FIXED: Duplicate entries in test database
  • Loading branch information...
commit a3b3ec8c4eb69928729d43fb5183d0228c2a056b 1 parent 405fa7e
Damian Mooyman authored August 16, 2012 chillu committed September 07, 2012
38  code/model/Translatable.php
@@ -1131,13 +1131,6 @@ function getTranslation($locale, $stage = null) {
1131 1131
 		$translations = $this->getTranslations($locale, $stage);
1132 1132
 		return ($translations) ? $translations->First() : null;
1133 1133
 	}
1134  
-	
1135  
-	/**
1136  
-	 * Enables automatic population of SiteConfig fields using createTranslation if
1137  
-	 * created outside of the Translatable module
1138  
-	 * @var boolean
1139  
-	 */
1140  
-	public static $enable_siteconfig_generation = true;
1141 1134
 
1142 1135
 	/**
1143 1136
 	 * When the SiteConfig object is automatically instantiated, we should ensure that
@@ -1150,12 +1143,14 @@ function getTranslation($locale, $stage = null) {
1150 1143
 	 * @see SiteConfig
1151 1144
 	 */
1152 1145
 	protected function populateSiteConfig() {
1153  
-		// This is required to prevent infinite loop during createTranslation
1154  
-		// If createTranslation is called directly on SiteConfig then this function could
1155  
-		// be called twice, but it will not cause harm for this to happen.
1156  
-		if(!self::$enable_siteconfig_generation)
  1146
+		
  1147
+		// Work-around for population of defaults during test setup (when tables are not created).
  1148
+		// When the database is being setup during testing, singleton('SiteConfig') is called.
  1149
+		// Singleton objects should not call populateDefault() but a bug seems to be allowing it
  1150
+		// to happen anyway. Looking up existing SiteConfig objects without a database crashes horribly
  1151
+		// @todo Remove this hack once this bug is fixed
  1152
+		if(SapphireTest::using_temp_db() && !DB::getConn()->hasTable($this->owner->class))
1157 1153
 			return;
1158  
-		self::$enable_siteconfig_generation = false;
1159 1154
 		
1160 1155
 		// Find the best base translation for site config
1161 1156
 		Translatable::disable_locale_filter();
@@ -1181,19 +1176,28 @@ protected function populateSiteConfig() {
1181 1176
 
1182 1177
 		// Copy fields from translated object (including ID) to the generated object
1183 1178
 		// This is similar to the creation of objects via createTranslation, but we need to
1184  
-		// preserve the ID in order to prevent 
  1179
+		// preserve the ID in order to prevent unnecessary object creation
1185 1180
 		$this->owner->update($stagingConfig->toMap());
1186  
-		
1187  
-		// Re-enable config generation
1188  
-		self::$enable_siteconfig_generation = true;
1189 1181
 	}
  1182
+	
  1183
+	/**
  1184
+	 * Enables automatic population of SiteConfig fields using createTranslation if
  1185
+	 * created outside of the Translatable module
  1186
+	 * @var boolean
  1187
+	 */
  1188
+	public static $enable_siteconfig_generation = true;
1190 1189
 
1191 1190
 	/**
1192 1191
 	 * Hooks into the DataObject::populateDefaults() method 
1193 1192
 	 */
1194 1193
 	public function populateDefaults() {
1195  
-		if (empty($this->owner->ID) && $this->owner instanceof SiteConfig)
  1194
+		if (empty($this->owner->ID) && ($this->owner instanceof SiteConfig) && self::$enable_siteconfig_generation)
  1195
+		{
  1196
+			// Use enable_siteconfig_generation to prevent infinite loop during 
  1197
+			self::$enable_siteconfig_generation = false;
1196 1198
 			$this->populateSiteConfig();
  1199
+			self::$enable_siteconfig_generation = true;
  1200
+		}
1197 1201
 	}
1198 1202
 	
1199 1203
 	/**
43  tests/unit/TranslatableSiteConfigTest.yml
@@ -40,47 +40,4 @@ SiteConfig:
40 40
     Title: Meine Test Seite
41 41
     Locale: de_DE
42 42
     CanEditType: OnlyTheseUsers
43  
-    EditorGroups: =>Group.translators_de
44  
-Permission:
45  
-  cmsmain1:
46  
-    Code: CMS_ACCESS_CMSMain
47  
-  cmsmain2:
48  
-    Code: CMS_ACCESS_CMSMain
49  
-  translate_all1:
50  
-    Code: TRANSLATE_ALL
51  
-  translate_all2:
52  
-    Code: TRANSLATE_ALL
53  
-Group:
54  
-  translators_de:
55  
-    Code: translators_de
56  
-    Permissions: =>Permission.cmsmain1,=>Permission.translate_all1
57  
-  translators_en:
58  
-    Code: translators_en
59  
-    Permissions: =>Permission.cmsmain2,=>Permission.translate_all2
60  
-Member:
61  
-  translator_de:
62  
-    Email: translator_de@test.com
63  
-    Password: test
64  
-    Groups: =>Group.translators_de
65  
-  translator_en:
66  
-    Email: translator_en@test.com
67  
-    Password: test
68  
-    Groups: =>Group.translators_en
69  
-  websiteuser:
70  
-    Email: websiteuser@test.com
71  
-    Password: test
72  
-Page:
73  
-  root_en:
74  
-    URLSegment: root-en
75  
-    Locale: en_US
76  
-SiteConfig:
77  
-  en_US:
78  
-    Title: My test site
79  
-    Locale: en_US
80  
-    CanEditType: OnlyTheseUsers
81  
-    EditorGroups: =>Group.translators_en
82  
-  de_DE:
83  
-    Title: Meine Test Seite
84  
-    Locale: de_DE
85  
-    CanEditType: OnlyTheseUsers
86 43
     EditorGroups: =>Group.translators_de

0 notes on commit a3b3ec8

Please sign in to comment.
Something went wrong with that request. Please try again.