public void CacheActiveForIntsAndGuids()
        {
            MediaTypeRepository mediaTypeRepository;

            var realCache = new AppCaches(
                new ObjectCacheAppCache(),
                new DictionaryAppCache(),
                new IsolatedCaches(t => new ObjectCacheAppCache()));

            var provider = TestObjects.GetScopeProvider(Logger);

            using (var scope = provider.CreateScope())
            {
                var repository = CreateRepository(provider, out mediaTypeRepository, appCaches: realCache);

                var udb = (UmbracoDatabase)scope.Database;

                udb.EnableSqlCount = false;

                var mediaType = MockedContentTypes.CreateSimpleMediaType("umbTextpage1", "Textpage");
                mediaTypeRepository.Save(mediaType);

                var media = MockedMedia.CreateSimpleMedia(mediaType, "hello", -1);
                repository.Save(media);

                udb.EnableSqlCount = true;

                //go get it, this should already be cached since the default repository key is the INT
                var found = repository.Get(media.Id);
                Assert.AreEqual(0, udb.SqlCount);
                //retrieve again, this should use cache
                found = repository.Get(media.Id);
                Assert.AreEqual(0, udb.SqlCount);

                //reset counter
                udb.EnableSqlCount = false;
                udb.EnableSqlCount = true;

                //now get by GUID, this won't be cached yet because the default repo key is not a GUID
                found = repository.Get(media.Key);
                var sqlCount = udb.SqlCount;
                Assert.Greater(sqlCount, 0);
                //retrieve again, this should use cache now
                found = repository.Get(media.Key);
                Assert.AreEqual(sqlCount, udb.SqlCount);
            }
        }
        public void Cache_Active_By_Int_And_Guid()
        {
            var provider   = new PetaPocoUnitOfWorkProvider(Logger);
            var unitOfWork = provider.GetUnitOfWork();
            MediaTypeRepository mediaTypeRepository;

            var realCache = new CacheHelper(
                new ObjectCacheRuntimeCacheProvider(),
                new StaticCacheProvider(),
                new StaticCacheProvider(),
                new IsolatedRuntimeCache(t => new ObjectCacheRuntimeCacheProvider()));

            using (var repository = CreateRepository(unitOfWork, out mediaTypeRepository, cacheHelper: realCache))
            {
                DatabaseContext.Database.DisableSqlCount();

                var mediaType = MockedContentTypes.CreateSimpleMediaType("umbTextpage1", "Textpage");
                var media     = MockedMedia.CreateSimpleMedia(mediaType, "hello", -1);
                mediaTypeRepository.AddOrUpdate(mediaType);
                repository.AddOrUpdate(media);
                unitOfWork.Commit();

                DatabaseContext.Database.EnableSqlCount();

                //go get it, this should already be cached since the default repository key is the INT
                var found = repository.Get(media.Id);
                Assert.AreEqual(0, DatabaseContext.Database.SqlCount);
                //retrieve again, this should use cache
                found = repository.Get(media.Id);
                Assert.AreEqual(0, DatabaseContext.Database.SqlCount);

                //reset counter
                DatabaseContext.Database.DisableSqlCount();
                DatabaseContext.Database.EnableSqlCount();

                //now get by GUID, this won't be cached yet because the default repo key is not a GUID
                found = repository.Get(media.Key);
                var sqlCount = DatabaseContext.Database.SqlCount;
                Assert.Greater(sqlCount, 0);
                //retrieve again, this should use cache now
                found = repository.Get(media.Key);
                Assert.AreEqual(sqlCount, DatabaseContext.Database.SqlCount);
            }
        }
Exemple #3
0
        public void Deleting_Media_Types_With_Hierarchy_Of_Media_Items_Doesnt_Raise_Trashed_Event_For_Deleted_Items()
        {
            MediaService.Trashed += MediaServiceOnTrashed;

            try
            {
                IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1");
                ServiceContext.MediaTypeService.Save(contentType1);
                IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2");
                ServiceContext.MediaTypeService.Save(contentType2);
                IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3");
                ServiceContext.MediaTypeService.Save(contentType3);

                var contentTypes = new[] { contentType1, contentType2, contentType3 };
                var parentId     = -1;

                var ids = new List <int>();

                for (int i = 0; i < 2; i++)
                {
                    for (var index = 0; index < contentTypes.Length; index++)
                    {
                        var contentType = contentTypes[index];
                        var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId);
                        ServiceContext.MediaService.Save(contentItem);
                        parentId = contentItem.Id;

                        ids.Add(contentItem.Id);
                    }
                }

                foreach (var contentType in contentTypes.Reverse())
                {
                    ServiceContext.MediaTypeService.Delete(contentType);
                }
            }
            finally
            {
                MediaService.Trashed -= MediaServiceOnTrashed;
            }
        }
Exemple #4
0
        public void Deleting_Media_Type_With_Hierarchy_Of_Media_Items_Moves_Orphaned_Media_To_Recycle_Bin()
        {
            IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1");

            ServiceContext.MediaTypeService.Save(contentType1);
            IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2");

            ServiceContext.MediaTypeService.Save(contentType2);
            IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3");

            ServiceContext.MediaTypeService.Save(contentType3);

            var contentTypes = new[] { contentType1, contentType2, contentType3 };
            var parentId     = -1;

            var ids = new List <int>();

            for (int i = 0; i < 2; i++)
            {
                for (var index = 0; index < contentTypes.Length; index++)
                {
                    var contentType = contentTypes[index];
                    var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId);
                    ServiceContext.MediaService.Save(contentItem);
                    parentId = contentItem.Id;

                    ids.Add(contentItem.Id);
                }
            }

            //delete the first content type, all other content of different content types should be in the recycle bin
            ServiceContext.MediaTypeService.Delete(contentTypes[0]);

            var found = ServiceContext.MediaService.GetByIds(ids);

            Assert.AreEqual(4, found.Count());
            foreach (var content in found)
            {
                Assert.IsTrue(content.Trashed);
            }
        }
Exemple #5
0
        public void Return_List_Of_Content_Items_Where_Media_Item_Referenced()
        {
            var mt = MockedContentTypes.CreateSimpleMediaType("testMediaType", "Test Media Type");

            ServiceContext.MediaTypeService.Save(mt);
            var m1 = MockedMedia.CreateSimpleMedia(mt, "hello 1", -1);

            ServiceContext.MediaService.Save(m1);

            var ct = MockedContentTypes.CreateTextPageContentType("richTextTest");

            ct.AllowedTemplates = Enumerable.Empty <ITemplate>();
            ServiceContext.ContentTypeService.Save(ct);

            void createContentWithMediaRefs()
            {
                var content = MockedContent.CreateTextpageContent(ct, "my content 2", -1);

                //'bodyText' is a property with a RTE property editor which we knows automatically tracks relations
                content.Properties["bodyText"].SetValue(@"<p>
        <img src='/media/12312.jpg' data-udi='umb://media/" + m1.Key.ToString("N") + @"' />
</p>");
                ServiceContext.ContentService.Save(content);
            }

            for (var i = 0; i < 6; i++)
            {
                createContentWithMediaRefs(); //create 6 content items referencing the same media
            }
            var relations = ServiceContext.RelationService.GetByChildId(m1.Id, Constants.Conventions.RelationTypes.RelatedMediaAlias).ToList();

            Assert.AreEqual(6, relations.Count);

            var entities = ServiceContext.RelationService.GetParentEntitiesFromRelations(relations).ToList();

            Assert.AreEqual(6, entities.Count);
        }
Exemple #6
0
        public void Can_Update_Media_Property_Values()
        {
            IMediaType mediaType = MockedContentTypes.CreateSimpleMediaType("test", "Test");

            ServiceContext.MediaTypeService.Save(mediaType);
            IMedia media = MockedMedia.CreateSimpleMedia(mediaType, "hello", -1);

            media.SetValue("title", "title of mine");
            media.SetValue("bodyText", "hello world");
            ServiceContext.MediaService.Save(media);

            // re-get
            media = ServiceContext.MediaService.GetById(media.Id);
            media.SetValue("title", "another title of mine");          // Change a value
            media.SetValue("bodyText", null);                          // Clear a value
            media.SetValue("author", "new author");                    // Add a value
            ServiceContext.MediaService.Save(media);

            // re-get
            media = ServiceContext.MediaService.GetById(media.Id);
            Assert.AreEqual("another title of mine", media.GetValue("title"));
            Assert.IsNull(media.GetValue("bodyText"));
            Assert.AreEqual("new author", media.GetValue("author"));
        }
        public void Can_Perform_Update_On_UserRepository()
        {
            var ct = MockedContentTypes.CreateBasicContentType("test");
            var mt = MockedContentTypes.CreateSimpleMediaType("testmedia", "TestMedia");

            // Arrange
            var provider = TestObjects.GetScopeProvider(Logger);

            using (var scope = provider.CreateScope())
            {
                var userRepository      = CreateRepository(provider);
                var contentRepository   = CreateContentRepository(provider, out var contentTypeRepo);
                var mediaRepository     = CreateMediaRepository(provider, out var mediaTypeRepo);
                var userGroupRepository = CreateUserGroupRepository(provider);

                contentTypeRepo.Save(ct);
                mediaTypeRepo.Save(mt);

                var content = MockedContent.CreateBasicContent(ct);
                var media   = MockedMedia.CreateSimpleMedia(mt, "asdf", -1);

                contentRepository.Save(content);
                mediaRepository.Save(media);

                var user = CreateAndCommitUserWithGroup(userRepository, userGroupRepository);

                // Act
                var resolved = (User)userRepository.Get(user.Id);

                resolved.Name = "New Name";
                //the db column is not used, default permissions are taken from the user type's permissions, this is a getter only
                //resolved.DefaultPermissions = "ZYX";
                resolved.Language         = "fr";
                resolved.IsApproved       = false;
                resolved.RawPasswordValue = "new";
                resolved.IsLockedOut      = true;
                resolved.StartContentIds  = new[] { content.Id };
                resolved.StartMediaIds    = new[] { media.Id };
                resolved.Email            = "*****@*****.**";
                resolved.Username         = "******";

                userRepository.Save(resolved);

                var updatedItem = (User)userRepository.Get(user.Id);

                // Assert
                Assert.That(updatedItem.Id, Is.EqualTo(resolved.Id));
                Assert.That(updatedItem.Name, Is.EqualTo(resolved.Name));
                Assert.That(updatedItem.Language, Is.EqualTo(resolved.Language));
                Assert.That(updatedItem.IsApproved, Is.EqualTo(resolved.IsApproved));
                Assert.That(updatedItem.RawPasswordValue, Is.EqualTo(resolved.RawPasswordValue));
                Assert.That(updatedItem.IsLockedOut, Is.EqualTo(resolved.IsLockedOut));
                Assert.IsTrue(updatedItem.StartContentIds.UnsortedSequenceEqual(resolved.StartContentIds));
                Assert.IsTrue(updatedItem.StartMediaIds.UnsortedSequenceEqual(resolved.StartMediaIds));
                Assert.That(updatedItem.Email, Is.EqualTo(resolved.Email));
                Assert.That(updatedItem.Username, Is.EqualTo(resolved.Username));
                Assert.That(updatedItem.AllowedSections.Count(), Is.EqualTo(resolved.AllowedSections.Count()));
                foreach (var allowedSection in resolved.AllowedSections)
                {
                    Assert.IsTrue(updatedItem.AllowedSections.Contains(allowedSection));
                }
            }
        }
Exemple #8
0
        public void Can_Perform_Update_On_UserRepository()
        {
            var ct      = MockedContentTypes.CreateBasicContentType("test");
            var content = MockedContent.CreateBasicContent(ct);
            var mt      = MockedContentTypes.CreateSimpleMediaType("testmedia", "TestMedia");
            var media   = MockedMedia.CreateSimpleMedia(mt, "asdf", -1);

            // Arrange
            var provider   = new PetaPocoUnitOfWorkProvider(Logger);
            var unitOfWork = provider.GetUnitOfWork();
            IContentTypeRepository contentTypeRepo;
            IMediaTypeRepository   mediaTypeRepo;

            using (var contentRepository = CreateContentRepository(unitOfWork, out contentTypeRepo))
                using (var mediaRepository = CreateMediaRepository(unitOfWork, out mediaTypeRepo))
                    using (contentTypeRepo)
                        using (mediaTypeRepo)
                            using (var userRepository = CreateRepository(unitOfWork))
                                using (var userGroupRepository = CreateUserGroupRepository(unitOfWork))
                                {
                                    contentTypeRepo.AddOrUpdate(ct);
                                    mediaTypeRepo.AddOrUpdate(mt);
                                    unitOfWork.Commit();

                                    contentRepository.AddOrUpdate(content);
                                    mediaRepository.AddOrUpdate(media);
                                    unitOfWork.Commit();

                                    var user = CreateAndCommitUserWithGroup(userRepository, userGroupRepository, unitOfWork);

                                    // Act
                                    var resolved = (User)userRepository.Get((int)user.Id);

                                    resolved.Name             = "New Name";
                                    resolved.Language         = "fr";
                                    resolved.IsApproved       = false;
                                    resolved.RawPasswordValue = "new";
                                    resolved.IsLockedOut      = true;
                                    resolved.StartContentIds  = new[] { content.Id };
                                    resolved.StartMediaIds    = new[] { media.Id };
                                    resolved.Email            = "*****@*****.**";
                                    resolved.Username         = "******";

                                    userRepository.AddOrUpdate(resolved);
                                    unitOfWork.Commit();
                                    var updatedItem = (User)userRepository.Get((int)user.Id);

                                    // Assert
                                    Assert.That(updatedItem.Id, Is.EqualTo(resolved.Id));
                                    Assert.That(updatedItem.Name, Is.EqualTo(resolved.Name));
                                    Assert.That(updatedItem.Language, Is.EqualTo(resolved.Language));
                                    Assert.That(updatedItem.IsApproved, Is.EqualTo(resolved.IsApproved));
                                    Assert.That(updatedItem.RawPasswordValue, Is.EqualTo(resolved.RawPasswordValue));
                                    Assert.That(updatedItem.IsLockedOut, Is.EqualTo(resolved.IsLockedOut));
                                    Assert.IsTrue(updatedItem.StartContentIds.UnsortedSequenceEqual(resolved.StartContentIds));
                                    Assert.IsTrue(updatedItem.StartMediaIds.UnsortedSequenceEqual(resolved.StartMediaIds));
                                    Assert.That(updatedItem.Email, Is.EqualTo(resolved.Email));
                                    Assert.That(updatedItem.Username, Is.EqualTo(resolved.Username));
                                    Assert.That(updatedItem.AllowedSections.Count(), Is.EqualTo(2));
                                    Assert.IsTrue(updatedItem.AllowedSections.Contains("content"));
                                    Assert.IsTrue(updatedItem.AllowedSections.Contains("media"));
                                }
        }