Example #1
0
        /// <summary>
        /// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility
        /// </summary>
        public override void Save()
        {
            SaveEventArgs e = new SaveEventArgs();

            FireBeforeSave(e);

            if (!e.Cancel)
            {
                if (MasterContentType != 0)
                {
                    _mediaType.ParentId = MasterContentType;
                }

                foreach (var masterContentType in MasterContentTypes)
                {
                    var contentType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType(masterContentType);
                    _mediaType.AddContentType(contentType);
                }

                var current = User.GetCurrent();
                int userId  = current == null ? 0 : current.Id;
                ApplicationContext.Current.Services.ContentTypeService.Save(_mediaType, userId);

                //Ensure that MediaTypes are reloaded from db by clearing cache
                InMemoryCacheProvider.Current.Clear();
                RuntimeCacheProvider.Current.Clear();

                base.Save();

                FireAfterSave(e);
            }
        }
        public void Can_Copy_MediaType_To_New_Parent_By_Performing_Clone()
        {
            // Arrange
            MediaType parentMediaType1 = MediaTypeBuilder.CreateSimpleMediaType("parent1", "Parent1");

            MediaTypeService.Save(parentMediaType1);
            MediaType parentMediaType2 = MediaTypeBuilder.CreateSimpleMediaType("parent2", "Parent2", null, true);

            MediaTypeService.Save(parentMediaType2);
            var mediaType = MediaTypeBuilder.CreateImageMediaType("Image2") as IMediaType;

            MediaTypeService.Save(mediaType);

            // Act
            IMediaType clone = mediaType.DeepCloneWithResetIdentities("newcategory");

            Assert.IsNotNull(clone);
            clone.RemoveContentType("parent1");
            clone.AddContentType(parentMediaType2);
            clone.ParentId = parentMediaType2.Id;
            MediaTypeService.Save(clone);

            // Assert
            Assert.That(clone.HasIdentity, Is.True);

            IMediaType clonedMediaType   = MediaTypeService.Get(clone.Id);
            IMediaType originalMediaType = MediaTypeService.Get(mediaType.Id);

            Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent2")), Is.True);
            Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent1")), Is.False);

            Assert.AreEqual(clonedMediaType.Path, "-1," + parentMediaType2.Id + "," + clonedMediaType.Id);
            Assert.AreEqual(clonedMediaType.PropertyTypes.Count(), originalMediaType.PropertyTypes.Count());

            Assert.AreNotEqual(clonedMediaType.ParentId, originalMediaType.ParentId);
            Assert.AreEqual(clonedMediaType.ParentId, parentMediaType2.Id);

            Assert.AreNotEqual(clonedMediaType.Id, originalMediaType.Id);
            Assert.AreNotEqual(clonedMediaType.Key, originalMediaType.Key);
            Assert.AreNotEqual(clonedMediaType.Path, originalMediaType.Path);

            Assert.AreNotEqual(clonedMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id, originalMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id);
            Assert.AreNotEqual(clonedMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id, originalMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id);
        }