|
[Test] |
|
[Category(TestCategories.Core)] |
|
[IgnoreWhenDbConfig("Per site configuration only allowed in Auto mode")] |
|
[ExpectedNotSupportedExceptionWhenReadOnly] |
|
public void ConfigPersistencePerSiteTests_NativeApi_WorksAsExpected() |
|
{ |
|
// StringProp is site specific and StringProp2 is not site specific |
|
|
|
// updates the all sites values of site specific properties |
|
Config.UpdateSection<DummyConfig>(s => |
|
{ |
|
s.StringProp = "AllSites1"; |
|
s.StringProp2 = "AllSites2"; |
|
}); |
|
|
|
// gets the all sites values |
|
var dummy = Config.Get<DummyConfig>(); |
|
Assert.AreEqual("AllSites1", dummy.StringProp); |
|
Assert.AreEqual("AllSites2", dummy.StringProp2); |
|
|
|
// enters a region where site specific config operations will occur |
|
using (new ConfigSiteContextRegion(SystemManager.CurrentContext.CurrentSite.Id)) |
|
{ |
|
// updates the value for the current site only for site specific properties |
|
// other properties will be ignored |
|
Config.UpdateSection<DummyConfig>(s => |
|
{ |
|
// current config element must be marked as persisting site specific values if not already |
|
// keep in mind that all site specific props for the ConfigElement will start persisting site specific values |
|
// even if they are not changed in the code here, they will get those from all sites values |
|
// ex. if there is StringProp3 that is also site specific it will also start persisting its all sites value |
|
// as site specific as well for current site |
|
s.PersistsSiteSpecificValues = true; |
|
s.StringProp = "SiteSpecific"; |
|
|
|
// this value will not be persisted as the property is not site specific |
|
s.StringProp2 = "SiteSpecific_WillBeIgnored"; |
|
}); |
|
|
|
// gets the values for the current site for all site specific properties |
|
// other properties load their all sites values |
|
var siteSpecificDummy = Config.Get<DummyConfig>(); |
|
Assert.AreEqual("SiteSpecific", siteSpecificDummy.StringProp); |
|
Assert.AreEqual("AllSites2", siteSpecificDummy.StringProp2); |
|
|
|
Config.UpdateSection<DummyConfig>(s => |
|
{ |
|
// removes all site specific values if any and the element will start inheriting again from all sites values for current site |
|
s.PersistsSiteSpecificValues = false; |
|
}); |
|
|
|
siteSpecificDummy = Config.Get<DummyConfig>(); |
|
Assert.AreEqual("AllSites1", siteSpecificDummy.StringProp); |
|
Assert.AreEqual("AllSites2", siteSpecificDummy.StringProp2); |
|
|
|
Config.UpdateSection<DummyConfig>(s => |
|
{ |
|
s.PersistsSiteSpecificValues = true; |
|
s.StringProp = "SiteSpecific"; |
|
}); |
|
} |
|
|
|
// ConfigManager API returns all sites value unless used in ConfigSiteContextRegion |
|
// It also does not cache the configuration object so it is not recommended for general use in custom code when reading values |
|
var valueFromConfigManagerAPI = ConfigManager.GetManager().GetSection<DummyConfig>().StringProp; |
|
Assert.AreEqual("AllSites1", valueFromConfigManagerAPI); |
|
using (new ConfigSiteContextRegion(SystemManager.CurrentContext.CurrentSite.Id)) |
|
{ |
|
valueFromConfigManagerAPI = ConfigManager.GetManager().GetSection<DummyConfig>().StringProp; |
|
Assert.AreEqual("SiteSpecific", valueFromConfigManagerAPI); |
|
} |
|
|
|
// Config API returns current site context values and caches the configuration |
|
// This is the recommended API to use in custom code when reading configuration values |
|
var valueFromConfigAPI = Config.Get<DummyConfig>().StringProp; |
|
Assert.AreEqual("SiteSpecific", valueFromConfigAPI); |
|
} |