private void ValidateAvailableOrExcludedPageTypes(PageTypeDefinition definition, IEnumerable <PageTypeDefinition> allPageTypeDefinitions, bool availblePageTypes) { Type[] pageTypes = availblePageTypes ? definition.Attribute.AvailablePageTypes : definition.Attribute.ExcludedPageTypes; string propertyName = availblePageTypes ? "AvailablePageType" : "ExcludedPageType"; if (pageTypes == null) { return; } foreach (Type pageTypeType in pageTypes) { if (pageTypes.Count(t => t == pageTypeType) > 1) { throw new PageTypeBuilderException(string.Format(CultureInfo.InvariantCulture, "The page type {0}'s {1} attribute contains the type {2} several times.", definition.Type.FullName, propertyName, pageTypeType.FullName)); } if (allPageTypeDefinitions.Count(d => d.Type.GUID == pageTypeType.GUID) == 0 && !pageTypeType.IsSubclassOf(typeof(TypedPageData)) && !pageTypeType.IsInterface) { throw new PageTypeBuilderException(string.Format(CultureInfo.InvariantCulture, "The page type {0} has the type {1} specified in it's {2} attribute " + "which is not a defined page type", definition.Type.FullName, pageTypeType.FullName, propertyName)); } } }
public void GivenPageTypeWithSpecifiedNameAndNoGuid_GetExistingPageType_ReturnsPageTypeReturnedFromPageTypeFactoryLoad() { MockRepository mockRepository = new MockRepository(); Type pageTypeType = typeof(object); PageTypeDefinition pageTypeDefinition = new PageTypeDefinition { Type = pageTypeType, Attribute = new PageTypeAttribute { Name = Guid.NewGuid().ToString() } }; PageTypeRepository fakePageTypeRepository = mockRepository.Stub <PageTypeRepository>(); IPageType pageTypeFromFactory = new NativePageType(); pageTypeFromFactory.ID = 1; fakePageTypeRepository.Expect(factory => factory.Load(pageTypeDefinition.Attribute.Name)).Return(pageTypeFromFactory); fakePageTypeRepository.Replay(); PageTypeUpdater pageTypeUpdater = PageTypeUpdaterFactory.Create( PageTypeDefinitionLocatorFactory.Stub(), fakePageTypeRepository); IPageType returnedPageType = pageTypeUpdater.GetExistingPageType(pageTypeDefinition); fakePageTypeRepository.AssertWasCalled(factory => factory.Load(pageTypeDefinition.Attribute.Name)); Assert.Equal <int>(pageTypeFromFactory.ID, returnedPageType.ID); }
protected internal virtual IPageType CreateNewPageType(PageTypeDefinition definition) { IPageType pageType = PageTypeRepository.CreateNew(); PageTypeAttribute attribute = definition.Attribute; string name = attribute.Name; if (name == null) { name = definition.Type.Name; } pageType.Name = name; if (definition.Attribute.Guid.HasValue) { pageType.GUID = definition.Attribute.Guid.Value; } string filename = attribute.Filename; if (string.IsNullOrEmpty(filename)) { filename = DefaultFilename; } pageType.FileName = filename; PageTypeRepository.Save(pageType); NewlyCreatedPageTypes.Add(pageType); return(pageType); }
public void GivenTypeArray_WhenUpdateAvailablePageTypesCalled_SetsPageTypeAllowedPageTypes() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); definition.Attribute.AvailablePageTypes = new[] { typeof(object) }; IPageType existingPageType = new NativePageType(); MockRepository mocks = new MockRepository(); List <PageTypeDefinition> definitions = new List <PageTypeDefinition>(); PageTypeDefinition definitionToReturn = new PageTypeDefinition { Type = typeof(object), Attribute = new PageTypeAttribute() }; definitions.Add(definitionToReturn); var pageTypeDefinitionLocator = PageTypeDefinitionLocatorFactory.Mock(); pageTypeDefinitionLocator.Setup(locator => locator.GetPageTypeDefinitions()).Returns(definitions); PageTypeUpdater pageTypeUpdater = mocks.PartialMock <PageTypeUpdater>( pageTypeDefinitionLocator.Object, new PageTypeFactory(), new PageTypeValueExtractor(), new PageTypeLocator(new PageTypeFactory())); IPageType allowedPageType = new NativePageType(); allowedPageType.ID = 1; pageTypeUpdater.Stub(updater => updater.GetExistingPageType(definitionToReturn)).Return(allowedPageType); pageTypeUpdater.Replay(); pageTypeUpdater.UpdateAvailablePageTypes(existingPageType, definition.Attribute.AvailablePageTypes); Assert.Equal <int[]>(new[] { 1 }, existingPageType.AllowedPageTypes); }
public void GivenPageType_UpdatePageTypePropertyDefinitions_CallsPageTypePropertyUpdaterUpdatePageTypePropertyDefinitions() { PageTypeDefinition definition = new PageTypeDefinition(); MockRepository fakes = new MockRepository(); PageTypeUpdater pageTypeUpdater = PageTypeUpdaterFactory.Stub(fakes); IPageType pageType = new NativePageType(); PageTypePropertyUpdater pageTypePropertyUpdater = PageTypePropertyUpdaterFactory.Stub(fakes); pageTypePropertyUpdater.Stub(updater => updater.UpdatePageTypePropertyDefinitions(pageType, definition)); pageTypePropertyUpdater.Replay(); IPageTypeLocator pageTypeLocator = fakes.Stub <IPageTypeLocator>(); pageTypeLocator.Stub(locator => locator.GetExistingPageType(definition)).Return(pageType); pageTypeLocator.Replay(); List <PageTypeDefinition> definitions = new List <PageTypeDefinition> { definition }; PageTypeSynchronizer synchronizer = PageTypeSynchronizerFactory.Create(pageTypePropertyUpdater, pageTypeLocator); synchronizer.PageTypeUpdater = pageTypeUpdater; synchronizer.UpdatePageTypePropertyDefinitions(definitions); pageTypePropertyUpdater.AssertWasCalled(updater => updater.UpdatePageTypePropertyDefinitions(pageType, definition)); }
public void GivenPageTypeDefinition_AddPageTypesToResolver_AddsToResolver() { List <PageTypeDefinition> definitions = new List <PageTypeDefinition>(); PageTypeDefinition definition = new PageTypeDefinition { Type = typeof(string), Attribute = new PageTypeAttribute() }; definitions.Add(definition); IPageType pageType = new NativePageType(); pageType.ID = 1; MockRepository fakes = new MockRepository(); IPageTypeLocator pageTypeLocator = fakes.Stub <IPageTypeLocator>(); pageTypeLocator.Stub(locator => locator.GetExistingPageType(definition)).Return(pageType); pageTypeLocator.Replay(); PageTypeResolver resolver = new PageTypeResolver(); PageTypeSynchronizer synchronizer = PageTypeSynchronizerFactory.Create(resolver, pageTypeLocator); synchronizer.AddPageTypesToResolver(definitions); Assert.Equal <Type>(definition.Type, resolver.GetPageTypeType(pageType.ID)); }
protected internal virtual void ValidatePageTypeProperties(PageTypeDefinition definition) { List <PropertyInfo> propertiesForPageType = definition.Type.GetPageTypePropertiesOnClass().ToList(); foreach (PropertyInfo propertyInfo in propertiesForPageType) { ValidatePageTypeProperty(propertyInfo); } // validate any page type property group propery defininitions foreach (PropertyInfo propertyGroupProperty in definition.Type.GetPageTypePropertyGroupProperties()) { PropertyInfo[] propertyGroupProperties = propertyGroupProperty.PropertyType.GetPublicOrPrivateProperties(); foreach (PropertyInfo property in propertyGroupProperties) { PageTypePropertyAttribute attribute = property.GetCustomAttributes <PageTypePropertyAttribute>().FirstOrDefault(); if (attribute == null) { continue; } ValidatePageTypeProperty(property); } } }
public void GivenPageTypeDefinition_ValidatePageTypeProperties_CallsValidatePageTypePropertyForEachPropertyWithPageTypePropertyAttribute() { PageTypeDefinition definition = new PageTypeDefinition { Type = typeof(TestPageType), Attribute = new PageTypeAttribute() }; MockRepository fakes = new MockRepository(); PageTypeDefinitionPropertiesValidator propertiesValidator = fakes.PartialMock <PageTypeDefinitionPropertiesValidator>((PageDefinitionTypeMapper)null); propertiesValidator.Stub( validator => validator.ValidatePageTypeProperty( Arg <PropertyInfo> .Is.Anything)); propertiesValidator.Replay(); propertiesValidator.ValidatePageTypeProperties(definition); int expectedNumberOfCalls = (from property in definition.Type.GetPublicOrPrivateProperties() where property.HasAttribute(typeof(PageTypePropertyAttribute)) select property).Count(); propertiesValidator.AssertWasCalled(validator => validator.ValidatePageTypeProperty( Arg <PropertyInfo> .Is.NotNull), options => options.Repeat.Times(expectedNumberOfCalls)); }
protected internal virtual void UpdatePageType(PageTypeDefinition definition) { IPageType pageType = GetExistingPageType(definition); PageTypeAttribute attribute = definition.Attribute; string oldValueString = SerializeValues(pageType); UpdateName(pageType, definition); UpdateFilename(pageType, attribute); UpdateSortOrder(pageType, attribute); UpdateDescription(pageType, attribute); UpdateIsAvailable(pageType, attribute); UpdateDefaultArchivePageLink(pageType, attribute); UpdateDefaultChildOrderRule(pageType, attribute); UpdateDefaultPageName(pageType, attribute); UpdateDefaultPeerOrder(pageType, attribute); UpdateDefaultStartPublishOffset(pageType, attribute); UpdateDefaultStopPublishOffset(pageType, attribute); UpdateDefaultVisibleInMenu(pageType, attribute); UpdateFrame(pageType, attribute); UpdateAvailablePageTypes(pageType, attribute.AvailablePageTypes); string newValuesString = SerializeValues(pageType); if (newValuesString != oldValueString) { PageTypeFactory.Save(pageType); } }
public void GivenAType_TypeProperty_SetsTypeProperyValue() { Type value = typeof(object); PageTypeDefinition pageTypeDefinition = new PageTypeDefinition(); pageTypeDefinition.Type = value; Assert.Equal <Type>(value, pageTypeDefinition.Type); }
public void UpdatePageType_gets_existing_PageType() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); PageTypeUpdater pageTypeUpdater = CreateFakePageTypeUpdaterWithUpdatePageTypeMethodHelperStubs(); pageTypeUpdater.UpdatePageType(definition); pageTypeUpdater.AssertWasCalled(updater => updater.GetExistingPageType(definition)); }
public void WhenUpdatePageTypeCalled_CallsUpdateAvailablePageTypes() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); PageTypeUpdater pageTypeUpdater = CreateFakePageTypeUpdaterWithUpdatePageTypeMethodHelperStubs(); pageTypeUpdater.UpdatePageType(definition); pageTypeUpdater.AssertWasCalled(updater => updater.UpdateAvailablePageTypes(Arg <IPageType> .Is.Anything, Arg <Type[]> .Is.Anything)); }
public void WhenUpdatePageTypeCalled_CallsPageTypeFactorySave() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); PageTypeUpdater pageTypeUpdater = CreateFakePageTypeUpdaterWithUpdatePageTypeMethodHelperStubs(); pageTypeUpdater.UpdatePageType(definition); pageTypeUpdater.PageTypeRepository.AssertWasCalled(factory => factory.Save(Arg <IPageType> .Is.NotNull)); }
public void WhenUpdatePageTypeCalls_CallsUpdateDefaultVisibleInMenu() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); IPageType existingPageType = new NativePageType(); PageTypeUpdater pageTypeUpdater = CreateFakePageTypeUpdaterWithUpdatePageTypeMethodHelperStubs(); pageTypeUpdater.UpdatePageType(definition); pageTypeUpdater.AssertWasCalled(updater => updater.UpdateDefaultVisibleInMenu(existingPageType, definition.Attribute)); }
public void WhenUpdatePageTypeCalled_CallsUpdateFilename() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); IPageType pageType = new NativePageType(); PageTypeUpdater pageTypeUpdater = CreateFakePageTypeUpdaterWithUpdatePageTypeMethodHelperStubs(); pageTypeUpdater.UpdatePageType(definition); pageTypeUpdater.AssertWasCalled(updater => updater.UpdateFilename(pageType, definition.Attribute)); }
protected internal virtual void ValidatePageTypeProperties(PageTypeDefinition definition) { ValidateNoClashingPropertiesFromInterfaces(definition.Type); IEnumerable <PropertyInfo> propertiesForPageType = definition.Type.GetAllValidPageTypePropertiesFromClassAndImplementedInterfaces(); foreach (PropertyInfo propertyInfo in propertiesForPageType) { ValidatePageTypeProperty(propertyInfo); } }
public virtual void ValidatePageTypeDefinition(PageTypeDefinition definition, IEnumerable <PageTypeDefinition> allPageTypeDefinitions) { ValidateNameLength(definition); ValidateInheritsFromBasePageType(definition); ValidateAvailablePageTypes(definition, allPageTypeDefinitions); PropertiesValidator.ValidatePageTypeProperties(definition); }
public void GivenDefinitionWithNoSpecifiedName_CreateNewPageType_ReturnsNewPageTypeWithNameSetToTheNameOfTheTypeName() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); SetupPageTypeUpdaterWithFakePageTypeFactory(pageTypeUpdater); IPageType returnedPageType = pageTypeUpdater.CreateNewPageType(definition); Assert.Equal(typeof(object).Name, returnedPageType.Name); }
public void WhenCreateNewPageTypeCalled_CallsPageTypeFactorySave() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); SetupPageTypeUpdaterWithFakePageTypeFactory(pageTypeUpdater); pageTypeUpdater.CreateNewPageType(definition); pageTypeUpdater.PageTypeFactory.AssertWasCalled(factory => factory.Save(Arg <IPageType> .Is.NotNull)); }
public void GivenNoNameSetInAttribute_WhenUpdatePageTypeCalled_UpdatesPageTypeNameWithName() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); IPageType pageType = new NativePageType(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); string name = definition.Type.Name; pageTypeUpdater.UpdateName(pageType, definition); Assert.Equal <string>(name, pageType.Name); }
public void GivenAPageTypeAttribute_AttributeProperty_SetsAttributePropertyValue() { PageTypeAttribute attribute = new PageTypeAttribute(); attribute.Description = TestValueUtility.CreateRandomString(); PageTypeDefinition pageTypeDefinition = new PageTypeDefinition(); pageTypeDefinition.Attribute = attribute; Assert.Equal <PageTypeAttribute>(attribute, pageTypeDefinition.Attribute, new PageTypeAttrbitueComparer()); }
public void GivenNoAvailablePageTypes_WhenUpdateAvailablePageTypesCalled_SetsPageTypeAllowedPageTypesToEmptyArray() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); IPageType existingPageType = new NativePageType(); MockRepository mocks = new MockRepository(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); pageTypeUpdater.UpdateAvailablePageTypes(existingPageType, definition.Attribute.AvailablePageTypes); Assert.Equal <int[]>(new int[0], existingPageType.AllowedPageTypes); }
public void GivenValue_UpdateDefaultVisibleInMenu_UpdatesPageTypeDefaultVisibleInMenuWithValue(bool defaultVisibleInMenu) { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); definition.Attribute.DefaultVisibleInMenu = defaultVisibleInMenu; IPageType pageType = new NativePageType(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); pageTypeUpdater.UpdateDefaultVisibleInMenu(pageType, definition.Attribute); Assert.Equal <bool>(defaultVisibleInMenu, pageType.DefaultVisibleInMenu); }
private static List <PropertySettingsUpdater> GetPropertySettingsUpdaters(PageTypeDefinition pageTypeDefinition, PageTypePropertyDefinition propertyDefinition) { var settingsUpdaters = from attribute in GetPropertyAttributes(propertyDefinition, pageTypeDefinition) from interfaceType in attribute.GetType().GetInterfaces() where interfaceType.IsGenericType && typeof(IUpdatePropertySettings <>).IsAssignableFrom(interfaceType.GetGenericTypeDefinition()) let settingsType = interfaceType.GetGenericArguments().First() select new PropertySettingsUpdater(settingsType, attribute); return(settingsUpdaters.ToList()); }
public void GivenDefinitionWithASpecifiedFilename_CreateNewPageType_ReturnsPageTypeWithThatFilename() { PageTypeDefinition definition = PageTypeUpdaterTestsUtility.CreateBasicPageTypeDefinition(); definition.Attribute.Filename = TestValueUtility.CreateRandomString(); PageTypeUpdater pageTypeUpdater = CreatePageTypeUpdater(); SetupPageTypeUpdaterWithFakePageTypeFactory(pageTypeUpdater); IPageType returnedPageType = pageTypeUpdater.CreateNewPageType(definition); Assert.Equal <string>(definition.Attribute.Filename, returnedPageType.FileName); }
protected internal virtual void ValidateInheritsFromBasePageType(PageTypeDefinition definition) { Type typeToCheck = definition.Type; if (!baseTypeForPageTypes.IsAssignableFrom(typeToCheck)) { string errorMessage = "The type {0} has a {1} attribute but does not inherit from {2}"; errorMessage = string.Format(CultureInfo.InvariantCulture, errorMessage, typeToCheck.FullName, typeof(PageTypeAttribute).FullName, baseTypeForPageTypes.FullName); throw new PageTypeBuilderException(errorMessage); } }
protected internal virtual void ValidateNameLength(PageTypeDefinition definition) { if (definition.GetPageTypeName().Length <= MaximumPageTypeNameLength) { return; } string errorMessage = "The page type class {0} has a name that is longer than {1}. EPiServer does not save more than {1} characters and the name is often used to identify page types."; errorMessage = string.Format(CultureInfo.InvariantCulture, errorMessage, definition.Type.Name, MaximumPageTypeNameLength); throw new PageTypeBuilderException(errorMessage); }
public void GivenPageType_UpdatePageTypePropertyDefinitions_CallsGetPageTypePropertyDefinitions() { List <PageTypePropertyDefinition> definitions = new List <PageTypePropertyDefinition>(); PageTypePropertyUpdater pageTypePropertyUpdater = CreatePageTypePropertyUpdater(definitions); IPageType pageType = new NativePageType(); PageTypeDefinition pageTypeDefinition = new PageTypeDefinition(); pageTypePropertyUpdater.UpdatePageTypePropertyDefinitions(pageType, pageTypeDefinition); pageTypePropertyUpdater.PageTypePropertyDefinitionLocator.AssertWasCalled( locator => locator.GetPageTypePropertyDefinitions( pageType, pageTypeDefinition.Type)); }
protected internal virtual void UpdatePropertySettings(PageTypeDefinition pageTypeDefinition, PageTypePropertyDefinition propertyDefinition, PageDefinition pageDefinition) { var prop = pageTypeDefinition.Type.GetProperties().Where(p => p.Name == propertyDefinition.Name).FirstOrDefault (); var attributes = prop.GetCustomAttributes(typeof(PropertySettingsAttribute), true); foreach (var attribute in attributes) { PropertySettingsContainer container; if (pageDefinition.SettingsID == Guid.Empty) { pageDefinition.SettingsID = Guid.NewGuid(); PageDefinitionFactory.Save(pageDefinition); container = new PropertySettingsContainer(pageDefinition.SettingsID); } else { if (!_propertySettingsRepository.TryGetContainer(pageDefinition.SettingsID, out container)) { container = new PropertySettingsContainer(pageDefinition.SettingsID); } } var settingsAttribute = (PropertySettingsAttribute)attribute; var wrapper = container.GetSetting(settingsAttribute.SettingType); if (wrapper == null) { wrapper = new PropertySettingsWrapper(); container.Settings.Add(settingsAttribute.SettingType.FullName, wrapper); } bool settingsAlreadyExists = true; if (wrapper.PropertySettings == null) { wrapper.PropertySettings = (IPropertySettings)Activator.CreateInstance(settingsAttribute.SettingType); settingsAlreadyExists = false; } if (settingsAlreadyExists && !settingsAttribute.OverWriteExistingSettings) { return; } if (settingsAttribute.UpdateSettings(wrapper.PropertySettings) || !settingsAlreadyExists) { _propertySettingsRepository.Save(container); } } }
public void GivenTypesThatInheritFromTypedPageData_ValidatePageTypeDefinitions_DoesNotThrowException() { List <PageTypeDefinition> definitions = new List <PageTypeDefinition>(); PageTypeDefinition validTypeDefinition = new PageTypeDefinition { Type = typeof(TypedPageData), Attribute = new PageTypeAttribute() }; definitions.Add(validTypeDefinition); PageTypeDefinitionValidator definitionValidator = new PageTypeDefinitionValidator(null); definitionValidator.ValidatePageTypeDefinitions(definitions); }