public void Get_Paged_Items_By_User_Id_With_Query_And_Filter()
        {
            IScopeProvider sp = ScopeProvider;

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                for (int i = 0; i < 100; i++)
                {
                    repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
                    repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
                }

                scope.Complete();
            }

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                IQuery <IAuditItem> query = sp.CreateQuery <IAuditItem>().Where(x => x.UserId == -1);

                try
                {
                    ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
                    ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlCount = true;

                    IEnumerable <IAuditItem> page = repo.GetPagedResultsByQuery(
                        query,
                        0,
                        10,
                        out long total,
                        Direction.Descending,
                        new[] { AuditType.Publish },
                        sp.CreateQuery <IAuditItem>()
                        .Where(x => x.UserId > -2));

                    Assert.AreEqual(10, page.Count());
                    Assert.AreEqual(100, total);
                }
                finally
                {
                    ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = false;
                    ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlCount = false;
                }
            }
        }
예제 #2
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));
            }
        }
예제 #3
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 Get_Paged_Items()
        {
            IScopeProvider sp = ScopeProvider;

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                for (int i = 0; i < 100; i++)
                {
                    repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
                    repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
                }

                scope.Complete();
            }

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                IEnumerable <IAuditItem> page = repo.GetPagedResultsByQuery(sp.CreateQuery <IAuditItem>(), 0, 10, out long total, Direction.Descending, null, null);

                Assert.AreEqual(10, page.Count());
                Assert.AreEqual(200, total);
            }
        }
        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);
            }
        }
예제 #6
0
        public void GetPagedResultsByQuery_FilterMatchingAll()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                IQuery <IMedia> query = provider.CreateQuery <IMedia>().Where(x => x.Level == 2);

                IQuery <IMedia>      filter = provider.CreateQuery <IMedia>().Where(x => x.Name.Contains("Test"));
                IEnumerable <IMedia> result = repository.GetPage(query, 0, 1, out long totalRecords, filter, Ordering.By("SortOrder"));

                // Assert
                Assert.That(totalRecords, Is.EqualTo(2));
                Assert.That(result.Count(), Is.EqualTo(1));
                Assert.That(result.First().Name, Is.EqualTo("Test Image"));
            }
        }
        public void Get_Paged_Items_With_Custom_Filter()
        {
            IScopeProvider sp = ScopeProvider;

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                for (int i = 0; i < 100; i++)
                {
                    repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), "Content created"));
                    repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), "Content published"));
                }

                scope.Complete();
            }

            using (IScope scope = sp.CreateScope())
            {
                var repo = new AuditRepository((IScopeAccessor)sp, _logger);

                IAuditItem[] page = repo.GetPagedResultsByQuery(
                    sp.CreateQuery <IAuditItem>(),
                    0,
                    8,
                    out long total,
                    Direction.Descending,
                    null,
                    sp.CreateQuery <IAuditItem>()
                    .Where(item => item.Comment == "Content created"))
                                    .ToArray();

                Assert.AreEqual(8, page.Length);
                Assert.IsTrue(page.All(x => x.Comment == "Content created"));
                Assert.AreEqual(100, total);
            }
        }
        public void Can_Perform_Count_On_LanguageRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                IQuery <ILanguage> query = provider.CreateQuery <ILanguage>().Where(x => x.IsoCode.StartsWith("D"));
                int count = repository.Count(query);

                // Assert
                Assert.That(count, Is.EqualTo(2));
            }
        }
예제 #9
0
        public void QueryMedia()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                IQuery <IMedia>      query  = provider.CreateQuery <IMedia>().Where(x => x.Level == 2);
                IEnumerable <IMedia> result = repository.Get(query);

                // Assert
                Assert.That(result.Count(), Is.GreaterThanOrEqualTo(2)); // There should be two entities on level 2: File and Media
            }
        }
예제 #10
0
        public void CountMedia()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                int             level  = 2;
                IQuery <IMedia> query  = provider.CreateQuery <IMedia>().Where(x => x.Level == level);
                int             result = repository.Count(query);

                // Assert
                Assert.That(result, Is.GreaterThanOrEqualTo(2));
            }
        }
        public void Can_Perform_GetByQuery_On_LanguageRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                IQuery <ILanguage>      query  = provider.CreateQuery <ILanguage>().Where(x => x.IsoCode == "da-DK");
                IEnumerable <ILanguage> result = repository.Get(query);

                // Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Any(), Is.True);
                Assert.That(result.FirstOrDefault().IsoCode, Is.EqualTo("da-DK"));
            }
        }
예제 #12
0
        public void GetPagedResultsByQuery_AlternateOrder()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                // Act
                IQuery <IMedia>      query  = provider.CreateQuery <IMedia>().Where(x => x.Level == 2);
                IEnumerable <IMedia> result = repository.GetPage(query, 0, 1, out long totalRecords, null, Ordering.By("Name"));

                // Assert
                Assert.That(totalRecords, Is.GreaterThanOrEqualTo(2));
                Assert.That(result.Count(), Is.EqualTo(1));
                Assert.That(result.First().Name, Is.EqualTo("Test File"));
            }
        }
예제 #13
0
        public void Can_Perform_GetByQuery_On_UserGroupRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                CreateAndCommitMultipleUserGroups(repository);

                // Act
                IQuery <IUserGroup>      query  = provider.CreateQuery <IUserGroup>().Where(x => x.Alias == "testGroup1");
                IEnumerable <IUserGroup> result = repository.Get(query);

                // Assert
                Assert.That(result.Count(), Is.GreaterThanOrEqualTo(1));
            }
        }
예제 #14
0
        public void Can_Perform_Count_On_UserGroupRepository()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                IUserGroup[] userGroups = CreateAndCommitMultipleUserGroups(repository);

                // Act
                IQuery <IUserGroup> query = provider.CreateQuery <IUserGroup>().Where(x => x.Alias == "testGroup1" || x.Alias == "testGroup2");
                int result = repository.Count(query);

                // Assert
                Assert.That(result, Is.GreaterThanOrEqualTo(2));
            }
        }
예제 #15
0
        public void QueryMember()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

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

                var     key    = Guid.NewGuid();
                IMember member = CreateTestMember(key: key);

                // Act
                IQuery <IMember>      query  = provider.CreateQuery <IMember>().Where(x => x.Key == key);
                IEnumerable <IMember> result = repository.Get(query);

                // Assert
                Assert.That(result.Count(), Is.EqualTo(1));
                Assert.That(result.First().Id, Is.EqualTo(member.Id));
            }
        }
예제 #16
0
        public void QueryMember_WithSubQuery()
        {
            IScopeProvider provider = ScopeProvider;

            using (provider.CreateScope())
            {
                IQuery <IMember> query = provider.CreateQuery <IMember>().Where(x =>
                                                                                ((Member)x).LongStringPropertyValue.Contains("1095") &&
                                                                                ((Member)x).PropertyTypeAlias == "headshot");

                Sql <ISqlContext> sqlSubquery = GetSubquery();
                var translator             = new SqlTranslator <IMember>(sqlSubquery, query);
                Sql <ISqlContext> subquery = translator.Translate();
                Sql <ISqlContext> sql      = GetBaseQuery(false)
                                             .Append("WHERE umbracoNode.id IN (" + subquery.SQL + ")", subquery.Arguments)
                                             .OrderByDescending <ContentVersionDto>(x => x.VersionDate)
                                             .OrderBy <NodeDto>(x => x.SortOrder);

                Debug.Print(sql.SQL);
                Assert.That(sql.SQL, Is.Not.Empty);
            }
        }
        public void Get_Paged_Mixed_Entities_By_Ids()
        {
            // Create content
            IContentService     contentService     = GetRequiredService <IContentService>();
            IContentTypeService contentTypeService = GetRequiredService <IContentTypeService>();
            var         createdContent             = new List <IContent>();
            ContentType contentType = ContentTypeBuilder.CreateBasicContentType("blah");

            contentTypeService.Save(contentType);
            for (int i = 0; i < 10; i++)
            {
                Content c1 = ContentBuilder.CreateBasicContent(contentType);
                contentService.Save(c1);
                createdContent.Add(c1);
            }

            // Create media
            IMediaService     mediaService     = GetRequiredService <IMediaService>();
            IMediaTypeService mediaTypeService = GetRequiredService <IMediaTypeService>();
            var       createdMedia             = new List <IMedia>();
            MediaType imageType = MediaTypeBuilder.CreateImageMediaType("myImage");

            mediaTypeService.Save(imageType);
            for (int i = 0; i < 10; i++)
            {
                Media c1 = MediaBuilder.CreateMediaImage(imageType, -1);
                mediaService.Save(c1);
                createdMedia.Add(c1);
            }

            // Create members
            IMemberService     memberService     = GetRequiredService <IMemberService>();
            IMemberTypeService memberTypeService = GetRequiredService <IMemberTypeService>();
            MemberType         memberType        = MemberTypeBuilder.CreateSimpleMemberType("simple");

            memberTypeService.Save(memberType);
            var createdMembers = MemberBuilder.CreateMultipleSimpleMembers(memberType, 10).ToList();

            memberService.Save(createdMembers);

            IScopeProvider provider = ScopeProvider;

            using (provider.CreateScope())
            {
                EntityRepository repo = CreateRepository((IScopeAccessor)provider);

                IEnumerable <int> ids = createdContent.Select(x => x.Id).Concat(createdMedia.Select(x => x.Id)).Concat(createdMembers.Select(x => x.Id));

                System.Guid[] objectTypes = new[] { Constants.ObjectTypes.Document, Constants.ObjectTypes.Media, Constants.ObjectTypes.Member };

                IQuery <IUmbracoEntity> query = provider.CreateQuery <IUmbracoEntity>()
                                                .WhereIn(e => e.Id, ids);

                var entities = repo.GetPagedResultsByQuery(query, objectTypes, 0, 20, out long totalRecords, null, null).ToList();

                Assert.AreEqual(20, entities.Count);
                Assert.AreEqual(30, totalRecords);

                // add the next page
                entities.AddRange(repo.GetPagedResultsByQuery(query, objectTypes, 1, 20, out totalRecords, null, null));

                Assert.AreEqual(30, entities.Count);
                Assert.AreEqual(30, totalRecords);

                var contentEntities = entities.OfType <IDocumentEntitySlim>().ToList();
                var mediaEntities   = entities.OfType <IMediaEntitySlim>().ToList();
                var memberEntities  = entities.OfType <IMemberEntitySlim>().ToList();

                Assert.AreEqual(10, contentEntities.Count);
                Assert.AreEqual(10, mediaEntities.Count);
                Assert.AreEqual(10, memberEntities.Count);
            }
        }