public Swindler(Type @class, string memberName, object cheat) { _accessor = new PrivateType(@class); _memberName = memberName; _originalValue = _accessor.GetStaticFieldOrProperty(memberName); _accessor.SetStaticFieldOrProperty(_memberName, cheat); }
private void InitialDataTestPrivate(Action callback, bool withSecurity) { Cache.Reset(); ContentTypeManager.Reset(); Providers.Instance.ResetBlobProviders(); var builder = CreateRepositoryBuilderForTest(); Indexing.IsOuterSearchEngineEnabled = true; Cache.Reset(); ContentTypeManager.Reset(); using (Repository.Start(builder)) { using (new SystemAccount()) { if (withSecurity) { var sdbp = new PrivateType(typeof(MemoryDataProvider)); var db = (DatabaseStorage)sdbp.GetStaticFieldOrProperty("Storage"); db.Aces.Clear(); SecurityHandler.CreateAclEditor() .Allow(Identifiers.PortalRootId, Identifiers.AdministratorsGroupId, false, PermissionType.BuiltInPermissionTypes) .Allow(Identifiers.PortalRootId, Identifiers.AdministratorUserId, false, PermissionType.BuiltInPermissionTypes) .Apply(); } callback(); } } }
public void InstanceTest() { try { InvisibleForm actual; actual = InvisibleForm.Instance; Assert.IsNotNull(actual, "InvisibleForm.Instance returned null"); // Reset and try again PrivateType invisibleForm = new PrivateType(typeof(InvisibleForm)); invisibleForm.SetStaticFieldOrProperty("instanceForm", null); actual = InvisibleForm.Instance; Assert.IsNotNull(actual, "InvisibleForm.Instance returned null"); Assert.AreSame(actual, invisibleForm.GetStaticFieldOrProperty("instanceForm"), "Second call to the property should return the same opbject instance."); Form f = actual as Form; Assert.IsFalse(f.Visible, "InvisibleForm should not be visible"); } catch (Exception ex) { // Use try catch to test a workaround on CI build (AppVeyor) Console.WriteLine(ex.Message); } }
public SearchEngineSwindler(ISearchEngine searchEngine) { var storageContextAcc = new PrivateType(typeof(StorageContext)); var storageContextInstance = storageContextAcc.GetStaticFieldOrProperty("Instance"); _accessor = new PrivateObject(storageContextInstance); _originalSearchEngine = _accessor.GetField(_memberName); _accessor.SetField(_memberName, searchEngine); }
public void InstanceTest() { InvisibleForm actual; actual = InvisibleForm.Instance; Assert.IsNotNull(actual, "InvisibleForm.Instance returned null"); // Reset and try again PrivateType invisibleForm = new PrivateType(typeof(InvisibleForm)); invisibleForm.SetStaticFieldOrProperty("instanceForm", null); actual = InvisibleForm.Instance; Assert.IsNotNull(actual, "InvisibleForm.Instance returned null"); Assert.AreSame(actual, invisibleForm.GetStaticFieldOrProperty("instanceForm"), "Second call to the property should return the same opbject instance."); Form f = actual as Form; Assert.IsFalse(f.Visible, "InvisibleForm should not be visible"); }
public void FieldValidation_Password() { string ctd; bool isValid; FieldValidationResult validationResult; PrivateType fieldSettingAccessor = new PrivateType(typeof(PasswordFieldSetting)); string minLengthError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("MinLengthName"); string maxLengthError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("MaxLengthName"); string regexError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("RegexName"); //string compulsoryError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("CompulsoryName"); //==== ctd = @"<?xml version='1.0' encoding='utf-8'?> <ContentType name='ValidatedContent' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'> <Fields> <Field name='PasswordTest' type='Password'> <Configuration> <MinLength>3</MinLength> <MaxLength>8</MaxLength> <Regex>^[a-zA-Z0-9]*$</Regex> </Configuration> </Field> </Fields> </ContentType>"; ContentTypeInstaller.InstallContentType(ctd); SNC.Content content = SNC.Content.CreateNew("ValidatedContent", _testRoot, "abc"); content["PasswordTest"] = new PasswordField.PasswordData { Text = "ab" }; isValid = content.IsValid; validationResult = content.Fields["PasswordTest"].ValidationResult; Assert.IsFalse(isValid, "#1"); Assert.IsTrue(validationResult.Category == ShortTextFieldSetting.MinLengthName, "#2"); Assert.IsTrue((int)content.Fields["PasswordTest"].ValidationResult.GetParameter(ShortTextFieldSetting.MinLengthName) == 3, "#3"); content["PasswordTest"] = new PasswordField.PasswordData { Text = "abcdefgh01" }; isValid = content.IsValid; Assert.IsFalse(isValid, "#4"); Assert.IsTrue(content.Fields["PasswordTest"].ValidationResult.Category == ShortTextFieldSetting.MaxLengthName, "#5"); Assert.IsTrue((int)content.Fields["PasswordTest"].ValidationResult.GetParameter(ShortTextFieldSetting.MaxLengthName) == 8, "#6"); content["PasswordTest"] = new PasswordField.PasswordData { Text = "!@#$%^" }; isValid = content.IsValid; Assert.IsFalse(isValid, "#7"); Assert.IsTrue(content.Fields["PasswordTest"].ValidationResult.Category == ShortTextFieldSetting.RegexName, "#8"); content["PasswordTest"] = new PasswordField.PasswordData { Text = "Correct" }; isValid = content.IsValid; Assert.IsTrue(isValid, "#9"); }
public void FieldValidation_Xml() { string ctd; bool isValid; FieldValidationResult validationResult; PrivateType fieldSettingAccessor = new PrivateType(typeof(XmlFieldSetting)); string compulsoryError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("CompulsoryName"); string namespaceError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("ExpectedXmlNamespaceName"); string notWellformedXmlError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("NotWellformedXmlName"); //==== ctd = @"<?xml version='1.0' encoding='utf-8'?> <ContentType name='ContentWithXmlField' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'> <Fields> <Field name='XmlTest' type='Xml'> <Configuration> <ExpectedXmlNamespace>htp://example.com/namespace</ExpectedXmlNamespace> </Configuration> </Field> </Fields> </ContentType>"; ContentTypeInstaller.InstallContentType(ctd); SNC.Content content = SNC.Content.CreateNew("ContentWithXmlField", _testRoot, null); //---- content["XmlTest"] = null; Assert.IsTrue(content.IsValid, "#1"); //---- content["XmlTest"] = "<a xmlns='htp://example.com/namespace'><b>content</b></a>"; Assert.IsTrue(content.IsValid, "#2"); //---- content["XmlTest"] = ""; isValid = content.IsValid; validationResult = content.Fields["XmlTest"].ValidationResult; Assert.IsFalse(isValid, "#3"); Assert.IsTrue(validationResult.Category == notWellformedXmlError, "#4"); //---- content["XmlTest"] = "<a xmlns='htp://example.com/namespace'><b>content<b></a>"; isValid = content.IsValid; validationResult = content.Fields["XmlTest"].ValidationResult; Assert.IsFalse(isValid, "#5"); Assert.IsTrue(validationResult.Category == notWellformedXmlError, "#6"); //---- content["XmlTest"] = "<a><b>content</b></a>"; isValid = content.IsValid; validationResult = content.Fields["XmlTest"].ValidationResult; Assert.IsFalse(isValid, "#7"); Assert.IsTrue(validationResult.Category == namespaceError, "#8"); //======================= ctd = @"<?xml version='1.0' encoding='utf-8'?> <ContentType name='ContentWithXmlField' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'> <Fields> <Field name='XmlTest' type='Xml'> <Configuration> <Compulsory>true</Compulsory> <ExpectedXmlNamespace>htp://example.com/namespace</ExpectedXmlNamespace> </Configuration> </Field> </Fields> </ContentType>"; ContentTypeInstaller.InstallContentType(ctd); content = SNC.Content.CreateNew("ContentWithXmlField", _testRoot, null); content["XmlTest"] = null; isValid = content.IsValid; validationResult = content.Fields["XmlTest"].ValidationResult; Assert.IsFalse(isValid, "#9"); Assert.IsTrue(validationResult.Category == compulsoryError, "#10"); //======================= content["XmlTest"] = "<a xmlns='htp://example.com/namespace'><b>content</b></a>"; var sb = new StringBuilder(); var writer = XmlWriter.Create(sb); writer.WriteStartDocument(); writer.WriteStartElement("root"); content.Fields["XmlTest"].Export(writer, new ExportContext("/Root", "c:\\")); writer.WriteEndElement(); writer.Flush(); writer.Close(); Assert.IsTrue(sb.ToString() == "<?xml version=\"1.0\" encoding=\"utf-16\"?><root><XmlTest><a xmlns='htp://example.com/namespace'><b>content</b></a></XmlTest></root>", "#11"); }
public void FieldValidation_ShortText_Compulsory() { string ctd; bool isValid; FieldValidationResult validationResult; PrivateType fieldSettingAccessor = new PrivateType(typeof(ShortTextFieldSetting)); string compulsoryError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("CompulsoryName"); //==== ctd = @"<?xml version='1.0' encoding='utf-8'?> <ContentType name='ValidatedContent' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'> <Fields> <Field name='ShortTextTest' type='ShortText'> <Configuration> <Compulsory>true</Compulsory> <MinLength>3</MinLength> <MaxLength>8</MaxLength> <Regex>^[a-zA-Z0-9]*$</Regex> </Configuration> </Field> </Fields> </ContentType>"; ContentTypeInstaller.InstallContentType(ctd); SNC.Content content = SNC.Content.CreateNew("ValidatedContent", _testRoot, "abc"); content["ShortTextTest"] = null; isValid = content.IsValid; validationResult = content.Fields["ShortTextTest"].ValidationResult; Assert.IsFalse(isValid, "#1"); Assert.IsTrue(validationResult.Category == compulsoryError, "#2"); content["ShortTextTest"] = ""; isValid = content.IsValid; validationResult = content.Fields["ShortTextTest"].ValidationResult; Assert.IsFalse(isValid, "#3"); Assert.IsTrue(validationResult.Category == compulsoryError, "#4"); content["ShortTextTest"] = "Correct"; isValid = content.IsValid; Assert.IsTrue(isValid, "#5"); }
public TP GetStatic <TP>(string fieldOrProperty) { return((TP)PrivateType.GetStaticFieldOrProperty(fieldOrProperty)); }