public void Can_Perform_Update_On_ContentTypeRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                // Act
                IContentType contentType = repository.Get(_textpageContentType.Id);

                contentType.Thumbnail = "Doc2.png";
                contentType.PropertyGroups["content"].PropertyTypes.Add(new PropertyType(ShortStringHelper, "test", ValueStorageType.Ntext, "subtitle")
                {
                    Name        = "Subtitle",
                    Description = "Optional Subtitle",
                    Mandatory   = false,
                    SortOrder   = 1,
                    DataTypeId  = -88,
                    LabelOnTop  = true
                });
                repository.Save(contentType);

                bool dirty = contentType.IsDirty();

                // Assert
                Assert.That(contentType.HasIdentity, Is.True);
                Assert.That(dirty, Is.False);
                Assert.That(contentType.Thumbnail, Is.EqualTo("Doc2.png"));
                Assert.That(contentType.PropertyTypes.Any(x => x.Alias == "subtitle"), Is.True);
                Assert.That(contentType.PropertyTypes.Single(x => x.Alias == "subtitle").LabelOnTop, Is.True);
            }
        }
예제 #2
0
        public void QueryMedia_ContentTypeAliasFilter()
        {
            // we could support this, but it would require an extra join on the query,
            // and we don't absolutely need it now, so leaving it out for now

            // Arrange
            IMediaType     folderMediaType = MediaTypeService.Get(1031);
            IScopeProvider provider        = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                // Act
                for (int i = 0; i < 10; i++)
                {
                    Media folder = MediaBuilder.CreateMediaFolder(folderMediaType, -1);
                    repository.Save(folder);
                }

                string[]             types  = new[] { "Folder" };
                IQuery <IMedia>      query  = provider.CreateQuery <IMedia>().Where(x => types.Contains(x.ContentType.Alias));
                IEnumerable <IMedia> result = repository.Get(query);

                // Assert
                Assert.That(result.Count(), Is.GreaterThanOrEqualTo(11));
            }
        }
        public void Delete_By_User()
        {
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                var repo = new NotificationsRepository((IScopeAccessor)provider);

                var userDto = new UserDto {
                    Email = "test", Login = "******", Password = "******", UserName = "******", UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now
                };
                ScopeAccessor.AmbientScope.Database.Insert(userDto);

                IUser userNew   = Mock.Of <IUser>(e => e.Id == userDto.Id);
                IUser userAdmin = Mock.Of <IUser>(e => e.Id == Constants.Security.SuperUserId);

                for (int i = 0; i < 10; i++)
                {
                    var node = new NodeDto {
                        CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = -1
                    };
                    object       result       = ScopeAccessor.AmbientScope.Database.Insert(node);
                    IEntity      entity       = Mock.Of <IEntity>(e => e.Id == node.NodeId);
                    Notification notification = repo.CreateNotification((i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture));
                }

                int delCount = repo.DeleteNotifications(userAdmin);

                Assert.AreEqual(5, delCount);
            }
        }
예제 #4
0
        public void SaveMedia()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                IMediaType mediaType = mediaTypeRepository.Get(1032);
                Media      image     = MediaBuilder.CreateMediaImage(mediaType, -1);

                // Act
                mediaTypeRepository.Save(mediaType);
                repository.Save(image);

                IMedia fetched = repository.Get(image.Id);

                // Assert
                Assert.That(mediaType.HasIdentity, Is.True);
                Assert.That(image.HasIdentity, Is.True);

                TestHelper.AssertPropertyValuesAreEqual(image, fetched);
            }
        }
예제 #5
0
        public void GetMedia()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                // Act
                IMedia media = repository.Get(_testImage.Id);

                // Assert
                Assert.That(media.Id, Is.EqualTo(_testImage.Id));
                Assert.That(media.CreateDate, Is.GreaterThan(DateTime.MinValue));
                Assert.That(media.UpdateDate, Is.GreaterThan(DateTime.MinValue));
                Assert.That(media.ParentId, Is.Not.EqualTo(0));
                Assert.That(media.Name, Is.EqualTo("Test Image"));
                Assert.That(media.SortOrder, Is.EqualTo(0));
                Assert.That(media.VersionId, Is.Not.EqualTo(0));
                Assert.That(media.ContentTypeId, Is.EqualTo(1032));
                Assert.That(media.Path, Is.Not.Empty);
                Assert.That(media.Properties.Any(), Is.True);
            }
        }
예제 #6
0
        public void ScopeContextEnlistAgain(bool complete)
        {
            ScopeProvider scopeProvider = ScopeProvider;

            bool?completed  = null;
            bool?completed2 = null;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                scopeProvider.Context.Enlist("name", c =>
                {
                    completed = c;

                    // at that point the scope is gone, but the context is still there
                    IScopeContext ambientContext = scopeProvider.AmbientContext;
                    ambientContext.Enlist("another", c2 => completed2 = c2);
                });
                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);
            Assert.IsNotNull(completed);
            Assert.AreEqual(complete, completed.Value);
            Assert.AreEqual(complete, completed2.Value);
        }
예제 #7
0
        public void GivenChildThread_WhenParentDisposedBeforeChild_ParentScopeThrows()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            Assert.IsNull(ScopeProvider.AmbientScope);
            IScope mainScope = scopeProvider.CreateScope();

            var t = Task.Run(() =>
            {
                Console.WriteLine("Child Task start: " + scopeProvider.AmbientScope.InstanceId);

                // This will push the child scope to the top of the Stack
                IScope nested = scopeProvider.CreateScope();
                Console.WriteLine("Child Task scope created: " + scopeProvider.AmbientScope.InstanceId);
                Thread.Sleep(5000); // block for a bit to ensure the parent task is disposed first
                Console.WriteLine("Child Task before dispose: " + scopeProvider.AmbientScope.InstanceId);
                nested.Dispose();
                Console.WriteLine("Child Task after dispose: " + scopeProvider.AmbientScope.InstanceId);
            });

            // provide some time for the child thread to start so the ambient context is copied in AsyncLocal
            Thread.Sleep(2000);

            // now dispose the main without waiting for the child thread to join
            Console.WriteLine("Parent Task disposing: " + scopeProvider.AmbientScope.InstanceId);

            // This will throw because at this stage a child scope has been created which means
            // it is the Ambient (top) scope but here we're trying to dispose the non top scope.
            Assert.Throws <InvalidOperationException>(() => mainScope.Dispose());
            Task.WaitAll(t);        // wait for the child to dispose
            mainScope.Dispose();    // now it's ok
            Console.WriteLine("Parent Task disposed: " + scopeProvider.AmbientScope?.InstanceId);
        }
        public void Cant_Create_Duplicate_Domain_Name()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                ILanguage lang    = LanguageRepository.GetByIsoCode("en-AU");
                IContent  content = DocumentRepository.Get(contentId);

                var domain1 = (IDomain) new UmbracoDomain("test.com")
                {
                    RootContentId = content.Id, LanguageId = lang.Id
                };
                repo.Save(domain1);

                var domain2 = (IDomain) new UmbracoDomain("test.com")
                {
                    RootContentId = content.Id, LanguageId = lang.Id
                };

                Assert.Throws <DuplicateNameException>(() => repo.Save(domain2));
            }
        }
        public void Can_Delete()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                ILanguage lang    = LanguageRepository.GetByIsoCode("en-AU");
                IContent  content = DocumentRepository.Get(contentId);

                var domain = (IDomain) new UmbracoDomain("test.com")
                {
                    RootContentId = content.Id, LanguageId = lang.Id
                };
                repo.Save(domain);

                repo.Delete(domain);

                // re-get
                domain = repo.Get(domain.Id);

                Assert.IsNull(domain);
            }
        }
        public void Can_Verify_AllowedChildContentTypes_On_ContentType()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                ContentType subpageContentType       = ContentTypeBuilder.CreateSimpleContentType("umbSubpage", "Subpage");
                ContentType simpleSubpageContentType = ContentTypeBuilder.CreateSimpleContentType("umbSimpleSubpage", "Simple Subpage");
                repository.Save(subpageContentType);
                repository.Save(simpleSubpageContentType);

                // Act
                IContentType contentType = repository.Get(_simpleContentType.Id);
                contentType.AllowedContentTypes = new List <ContentTypeSort>
                {
                    new ContentTypeSort(new Lazy <int>(() => subpageContentType.Id), 0, subpageContentType.Alias),
                    new ContentTypeSort(new Lazy <int>(() => simpleSubpageContentType.Id), 1, simpleSubpageContentType.Alias)
                };
                repository.Save(contentType);

                // Assert
                IContentType updated = repository.Get(_simpleContentType.Id);

                Assert.That(updated.AllowedContentTypes.Any(), Is.True);
                Assert.That(updated.AllowedContentTypes.Any(x => x.Alias == subpageContentType.Alias), Is.True);
                Assert.That(updated.AllowedContentTypes.Any(x => x.Alias == simpleSubpageContentType.Alias), Is.True);
            }
        }
        public void Can_Verify_Usage_Of_New_PropertyType_On_Content()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                IContentType contentType = repository.Get(_textpageContentType.Id);

                Content subpage = ContentBuilder.CreateTextpageContent(contentType, "Text Page 1", contentType.Id);
                DocumentRepository.Save(subpage);

                PropertyGroup propertyGroup = contentType.PropertyGroups.First(x => x.Name == "Meta");
                propertyGroup.PropertyTypes.Add(new PropertyType(ShortStringHelper, "test", ValueStorageType.Ntext, "metaAuthor")
                {
                    Name = "Meta Author", Description = string.Empty, Mandatory = false, SortOrder = 1, DataTypeId = -88
                });
                repository.Save(contentType);

                // Act
                IContent content = DocumentRepository.Get(subpage.Id);
                content.SetValue("metaAuthor", "John Doe");
                DocumentRepository.Save(content);

                // Assert
                IContent updated = DocumentRepository.Get(subpage.Id);
                Assert.That(updated.GetValue("metaAuthor").ToString(), Is.EqualTo("John Doe"));
                Assert.That(contentType.PropertyTypes.Count(), Is.EqualTo(5));
                Assert.That(contentType.PropertyTypes.Any(x => x.Alias == "metaAuthor"), Is.True);
            }
        }
        public void Maps_Templates_Correctly()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                var templateRepo = new TemplateRepository((IScopeAccessor)provider, AppCaches.Disabled, LoggerFactory.CreateLogger <TemplateRepository>(), FileSystems, IOHelper, ShortStringHelper, Mock.Of <IViewHelper>());
                ContentTypeRepository repository = ContentTypeRepository;
                Template[]            templates  = new[]
                {
                    new Template(ShortStringHelper, "test1", "test1"),
                    new Template(ShortStringHelper, "test2", "test2"),
                    new Template(ShortStringHelper, "test3", "test3")
                };
                foreach (Template template in templates)
                {
                    templateRepo.Save(template);
                }

                ContentType contentType = ContentTypeBuilder.CreateSimpleContentType();
                contentType.AllowedTemplates = new[] { templates[0], templates[1] };
                contentType.SetDefaultTemplate(templates[0]);
                repository.Save(contentType);

                // re-get
                IContentType result = repository.Get(contentType.Id);

                Assert.AreEqual(2, result.AllowedTemplates.Count());
                Assert.AreEqual(templates[0].Id, result.DefaultTemplate.Id);
            }
        }
        public void Can_Perform_Query_On_ContentTypeRepository_Sort_By_Name()
        {
            IContentType contentType;

            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;
                contentType = repository.Get(_textpageContentType.Id);
                ContentType child1 = ContentTypeBuilder.CreateSimpleContentType("abc", "abc", contentType, randomizeAliases: true, defaultTemplateId: 0);
                repository.Save(child1);
                ContentType child3 = ContentTypeBuilder.CreateSimpleContentType("zyx", "zyx", contentType, randomizeAliases: true, defaultTemplateId: 0);
                repository.Save(child3);
                ContentType child2 = ContentTypeBuilder.CreateSimpleContentType("a123", "a123", contentType, randomizeAliases: true, defaultTemplateId: 0);
                repository.Save(child2);

                scope.Complete();
            }

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                // Act
                IEnumerable <IContentType> contentTypes = repository.Get(provider.CreateQuery <IContentType>().Where(x => x.ParentId == contentType.Id));

                // Assert
                Assert.That(contentTypes.Count(), Is.EqualTo(3));
                Assert.AreEqual("a123", contentTypes.ElementAt(0).Name);
                Assert.AreEqual("abc", contentTypes.ElementAt(1).Name);
                Assert.AreEqual("zyx", contentTypes.ElementAt(2).Name);
            }
        }
        public void Can_Perform_Delete_With_Heirarchy_On_ContentTypeRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;
                ContentType           ctMain     = ContentTypeBuilder.CreateSimpleContentType(defaultTemplateId: 0);
                ContentType           ctChild1   = ContentTypeBuilder.CreateSimpleContentType("child1", "Child 1", ctMain, randomizeAliases: true, defaultTemplateId: 0);
                ContentType           ctChild2   = ContentTypeBuilder.CreateSimpleContentType("child2", "Child 2", ctChild1, randomizeAliases: true, defaultTemplateId: 0);

                repository.Save(ctMain);
                repository.Save(ctChild1);
                repository.Save(ctChild2);

                // Act
                IContentType resolvedParent = repository.Get(ctMain.Id);
                repository.Delete(resolvedParent);

                // Assert
                Assert.That(repository.Exists(ctMain.Id), Is.False);
                Assert.That(repository.Exists(ctChild1.Id), Is.False);
                Assert.That(repository.Exists(ctChild2.Id), Is.False);
            }
        }
예제 #15
0
        public void Transaction()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            using (IScope scope = scopeProvider.CreateScope())
            {
                ScopeAccessor.AmbientScope.Database.Execute("CREATE TABLE tmp3 (id INT, name NVARCHAR(64))");
                scope.Complete();
            }

            using (IScope scope = scopeProvider.CreateScope())
            {
                ScopeAccessor.AmbientScope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
                string n = ScopeAccessor.AmbientScope.Database.ExecuteScalar <string>("SELECT name FROM tmp3 WHERE id=1");
                Assert.AreEqual("a", n);
            }

            using (IScope scope = scopeProvider.CreateScope())
            {
                string n = ScopeAccessor.AmbientScope.Database.ExecuteScalar <string>("SELECT name FROM tmp3 WHERE id=1");
                Assert.IsNull(n);
            }

            using (IScope scope = scopeProvider.CreateScope())
            {
                ScopeAccessor.AmbientScope.Database.Execute("INSERT INTO tmp3 (id, name) VALUES (1, 'a')");
                scope.Complete();
            }

            using (IScope scope = scopeProvider.CreateScope())
            {
                string n = ScopeAccessor.AmbientScope.Database.ExecuteScalar <string>("SELECT name FROM tmp3 WHERE id=1");
                Assert.AreEqual("a", n);
            }
        }
        public void Get_By_Name()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                ILanguage lang    = LanguageRepository.GetByIsoCode("en-AU");
                IContent  content = DocumentRepository.Get(contentId);

                for (int i = 0; i < 10; i++)
                {
                    var domain = (IDomain) new UmbracoDomain("test" + i + ".com")
                    {
                        RootContentId = content.Id, LanguageId = lang.Id
                    };
                    repo.Save(domain);
                }

                IDomain found = repo.GetByName("test1.com");

                Assert.IsNotNull(found);
            }
        }
예제 #17
0
        public void ScopeContextEnlist(bool complete)
        {
            ScopeProvider scopeProvider = ScopeProvider;

            bool?         completed      = null;
            IScope        ambientScope   = null;
            IScopeContext ambientContext = null;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                scopeProvider.Context.Enlist("name", c =>
                {
                    completed      = c;
                    ambientScope   = scopeProvider.AmbientScope;
                    ambientContext = scopeProvider.AmbientContext;
                });
                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);
            Assert.IsNotNull(completed);
            Assert.AreEqual(complete, completed.Value);
            Assert.IsNull(ambientScope);      // the scope is gone
            Assert.IsNotNull(ambientContext); // the context is still there
        }
        public void Get_All_Ids()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                ILanguage lang    = LanguageRepository.GetByIsoCode("en-AU");
                IContent  content = DocumentRepository.Get(contentId);

                var ids = new List <int>();
                for (int i = 0; i < 10; i++)
                {
                    var domain = (IDomain) new UmbracoDomain("test " + i + ".com")
                    {
                        RootContentId = content.Id, LanguageId = lang.Id
                    };
                    repo.Save(domain);
                    ids.Add(domain.Id);
                }

                IEnumerable <IDomain> all = repo.GetMany(ids.Take(8).ToArray());

                Assert.AreEqual(8, all.Count());
            }
        }
예제 #19
0
        public void ScopeContextException()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            bool?completed = null;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                IScope detached = scopeProvider.CreateDetachedScope();
                scopeProvider.AttachScope(detached);

                // the exception does not prevent other enlisted items to run
                // *and* it does not prevent the scope from properly going down
                scopeProvider.Context.Enlist("name", c => throw new Exception("bang"));
                scopeProvider.Context.Enlist("other", c => completed = c);
                detached.Complete();
                Assert.Throws <AggregateException>(() => detached.Dispose());

                // even though disposing of the scope has thrown, it has exited
                // properly ie it has removed itself, and the app remains clean
                Assert.AreSame(scope, scopeProvider.AmbientScope);
                scope.Complete();
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);

            Assert.IsNotNull(completed);
            Assert.AreEqual(true, completed);
        }
        public void Get_All_Without_Wildcards()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                ILanguage lang    = LanguageRepository.GetByIsoCode("en-AU");
                IContent  content = DocumentRepository.Get(contentId);

                for (int i = 0; i < 10; i++)
                {
                    var domain = (IDomain) new UmbracoDomain((i % 2 == 0) ? "test " + i + ".com" : ("*" + i))
                    {
                        RootContentId = content.Id,
                        LanguageId    = lang.Id
                    };
                    repo.Save(domain);
                }

                IEnumerable <IDomain> all = repo.GetAll(false);

                Assert.AreEqual(5, all.Count());
            }
        }
        public void Can_Persist_Member_Type()
        {
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MemberTypeRepository repository = CreateRepository(provider);

                var memberType = (IMemberType)MemberTypeBuilder.CreateSimpleMemberType();
                repository.Save(memberType);

                IMemberType sut = repository.Get(memberType.Id);

                Dictionary <string, PropertyType> standardProps = ConventionsHelper.GetStandardPropertyTypeStubs(ShortStringHelper);

                Assert.That(sut, Is.Not.Null);
                Assert.That(sut.PropertyGroups.Count, Is.EqualTo(2));
                Assert.That(sut.PropertyTypes.Count(), Is.EqualTo(3 + standardProps.Count));

                Assert.That(sut.PropertyGroups.Any(x => x.HasIdentity == false || x.Id == 0), Is.False);
                Assert.That(sut.PropertyTypes.Any(x => x.HasIdentity == false || x.Id == 0), Is.False);

                TestHelper.AssertPropertyValuesAreEqual(sut, memberType);
            }
        }
        public void Can_Create_And_Get_By_Id_Empty_lang()
        {
            int contentId = CreateTestData("en-AU", out ContentType ct);

            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                DomainRepository repo = CreateRepository(provider);

                IContent content = DocumentRepository.Get(contentId);

                var domain = (IDomain) new UmbracoDomain("test.com")
                {
                    RootContentId = content.Id
                };
                repo.Save(domain);

                // re-get
                domain = repo.Get(domain.Id);

                Assert.NotNull(domain);
                Assert.IsTrue(domain.HasIdentity);
                Assert.Greater(domain.Id, 0);
                Assert.AreEqual("test.com", domain.DomainName);
                Assert.AreEqual(content.Id, domain.RootContentId);
                Assert.IsFalse(domain.LanguageId.HasValue);
            }
        }
예제 #23
0
        public void SaveMediaMultiple()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                IMediaType mediaType = mediaTypeRepository.Get(1032);
                Media      file      = MediaBuilder.CreateMediaFile(mediaType, -1);

                // Act
                repository.Save(file);

                Media image = MediaBuilder.CreateMediaImage(mediaType, -1);
                repository.Save(image);

                // Assert
                Assert.That(file.HasIdentity, Is.True);
                Assert.That(image.HasIdentity, Is.True);
                Assert.That(file.Name, Is.EqualTo("Test File"));
                Assert.That(image.Name, Is.EqualTo("Test Image"));
                Assert.That(file.ContentTypeId, Is.EqualTo(mediaType.Id));
                Assert.That(image.ContentTypeId, Is.EqualTo(mediaType.Id));
            }
        }
예제 #24
0
        public void GivenChildThread_WhenChildDisposedBeforeParent_OK()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            Assert.IsNull(ScopeProvider.AmbientScope);
            IScope mainScope = scopeProvider.CreateScope();

            // Task.Run will flow the execution context unless ExecutionContext.SuppressFlow() is explicitly called.
            // This is what occurs in normal async behavior since it is expected to await (and join) the main thread,
            // but if Task.Run is used as a fire and forget thread without being done correctly then the Scope will
            // flow to that thread.
            var t = Task.Run(() =>
            {
                Console.WriteLine("Child Task start: " + scopeProvider.AmbientScope.InstanceId);
                IScope nested = scopeProvider.CreateScope();
                Console.WriteLine("Child Task before dispose: " + scopeProvider.AmbientScope.InstanceId);
                nested.Dispose();
                Console.WriteLine("Child Task after disposed: " + scopeProvider.AmbientScope.InstanceId);
            });

            Console.WriteLine("Parent Task waiting: " + scopeProvider.AmbientScope?.InstanceId);
            Task.WaitAll(t);
            Console.WriteLine("Parent Task disposing: " + scopeProvider.AmbientScope.InstanceId);
            mainScope.Dispose();
            Console.WriteLine("Parent Task disposed: " + scopeProvider.AmbientScope?.InstanceId);

            Assert.Pass();
        }
예제 #25
0
        public void QueryMedia_ContentTypeIdFilter()
        {
            // Arrange
            IMediaType     folderMediaType = MediaTypeService.Get(1031);
            IScopeProvider provider        = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                // Act
                for (int i = 0; i < 10; i++)
                {
                    Media folder = MediaBuilder.CreateMediaFolder(folderMediaType, -1);
                    repository.Save(folder);
                }

                int[]                types  = new[] { 1031 };
                IQuery <IMedia>      query  = provider.CreateQuery <IMedia>().Where(x => types.Contains(x.ContentTypeId));
                IEnumerable <IMedia> result = repository.Get(query);

                // Assert
                Assert.That(result.Count(), Is.GreaterThanOrEqualTo(11));
            }
        }
예제 #26
0
        public void NestedCreateScopeContext()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);
                Assert.IsNotNull(scopeProvider.AmbientContext);

                IScopeContext context;
                using (IScope nested = scopeProvider.CreateScope())
                {
                    Assert.IsInstanceOf <Scope>(nested);
                    Assert.IsNotNull(scopeProvider.AmbientScope);
                    Assert.AreSame(nested, scopeProvider.AmbientScope);
                    Assert.AreSame(scope, ((Scope)nested).ParentScope);

                    Assert.IsNotNull(scopeProvider.Context);
                    Assert.IsNotNull(scopeProvider.AmbientContext);
                    context = scopeProvider.Context;
                }

                Assert.IsNotNull(scopeProvider.AmbientContext);
                Assert.AreSame(context, scopeProvider.AmbientContext);
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(scopeProvider.AmbientContext);
        }
예제 #27
0
        public void GetAllMedia()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                MediaRepository repository = CreateRepository(provider, out MediaTypeRepository mediaTypeRepository);

                // Act
                IEnumerable <IMedia> medias = repository.GetMany();

                // Assert
                Assert.That(medias, Is.Not.Null);
                Assert.That(medias.Any(), Is.True);
                Assert.That(medias.Count(), Is.GreaterThanOrEqualTo(3));

                medias = repository.GetMany(medias.Select(x => x.Id).ToArray());
                Assert.That(medias, Is.Not.Null);
                Assert.That(medias.Any(), Is.True);
                Assert.That(medias.Count(), Is.GreaterThanOrEqualTo(3));

                medias = ((IReadRepository <Guid, IMedia>)repository).GetMany(medias.Select(x => x.Key).ToArray());
                Assert.That(medias, Is.Not.Null);
                Assert.That(medias.Any(), Is.True);
                Assert.That(medias.Count(), Is.GreaterThanOrEqualTo(3));
            }
        }
예제 #28
0
        public void NestedCreateScopeDatabase()
        {
            ScopeProvider scopeProvider = ScopeProvider;

            IUmbracoDatabase database;

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope())
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);
                database = ScopeAccessor.AmbientScope.Database; // populates scope's database
                Assert.IsNotNull(database);
                Assert.IsNotNull(database.Connection);          // in a transaction
                using (IScope nested = scopeProvider.CreateScope())
                {
                    Assert.IsInstanceOf <Scope>(nested);
                    Assert.IsNotNull(scopeProvider.AmbientScope);
                    Assert.AreSame(nested, scopeProvider.AmbientScope);
                    Assert.AreSame(scope, ((Scope)nested).ParentScope);
                    Assert.AreSame(database, ScopeAccessor.AmbientScope.Database);
                }

                Assert.IsNotNull(database.Connection); // still
            }

            Assert.IsNull(scopeProvider.AmbientScope);
            Assert.IsNull(database.Connection); // poof gone
        }
        public void CreateNotification()
        {
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                var repo = new NotificationsRepository((IScopeAccessor)provider);

                var node = new NodeDto // create bogus item so we can add a notification
                {
                    CreateDate     = DateTime.Now,
                    Level          = 1,
                    NodeObjectType = Constants.ObjectTypes.ContentItem,
                    ParentId       = -1,
                    Path           = "-1,123",
                    SortOrder      = 1,
                    Text           = "hello",
                    Trashed        = false,
                    UniqueId       = Guid.NewGuid(),
                    UserId         = Constants.Security.SuperUserId
                };
                object  result = ScopeAccessor.AmbientScope.Database.Insert(node);
                IEntity entity = Mock.Of <IEntity>(e => e.Id == node.NodeId);
                IUser   user   = Mock.Of <IUser>(e => e.Id == node.UserId);

                Notification notification = repo.CreateNotification(user, entity, "A");

                Assert.AreEqual("A", notification.Action);
                Assert.AreEqual(node.NodeId, notification.EntityId);
                Assert.AreEqual(node.NodeObjectType, notification.EntityType);
                Assert.AreEqual(node.UserId, notification.UserId);
            }
        }
        public void Can_Perform_Add_On_ContentTypeRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                // Act
                ContentType contentType = ContentTypeBuilder.CreateSimpleContentType("test", "Test", propertyGroupAlias: "testGroup", propertyGroupName: "testGroup");
                ContentTypeRepository.Save(contentType);

                IContentType fetched = ContentTypeRepository.Get(contentType.Id);

                // Assert
                Assert.That(contentType.HasIdentity, Is.True);
                Assert.That(contentType.PropertyGroups.All(x => x.HasIdentity), Is.True);
                Assert.That(contentType.PropertyTypes.All(x => x.HasIdentity), Is.True);
                Assert.That(contentType.Path.Contains(","), Is.True);
                Assert.That(contentType.SortOrder, Is.GreaterThan(0));

                Assert.That(contentType.PropertyGroups.ElementAt(0).Name == "testGroup", Is.True);
                int groupId = contentType.PropertyGroups.ElementAt(0).Id;
                Assert.That(contentType.PropertyTypes.All(x => x.PropertyGroupId.Value == groupId), Is.True);

                foreach (IPropertyType propertyType in contentType.PropertyTypes)
                {
                    Assert.AreNotEqual(propertyType.Key, Guid.Empty);
                }

                TestHelper.AssertPropertyValuesAreEqual(fetched, contentType, ignoreProperties: new[] { "DefaultTemplate", "AllowedTemplates", "UpdateDate", "HistoryCleanup" });
            }
        }