11// Copyright (c) Umbraco.
22// See LICENSE for more details.
33
4- using System . Collections . Generic ;
5- using System . Linq ;
64using NUnit . Framework ;
75using Umbraco . Cms . Core ;
86using Umbraco . Cms . Core . Models ;
97using Umbraco . Cms . Core . Models . Entities ;
108using Umbraco . Cms . Core . Services ;
9+ using Umbraco . Cms . Core . Services . ContentTypeEditing ;
1110using Umbraco . Cms . Infrastructure . Persistence ;
11+ using Umbraco . Cms . Infrastructure . Persistence . Repositories . Implement ;
1212using Umbraco . Cms . Tests . Common . Attributes ;
1313using Umbraco . Cms . Tests . Common . Builders ;
1414using Umbraco . Cms . Tests . Common . Testing ;
1515using Umbraco . Cms . Tests . Integration . Testing ;
16- using Umbraco . Extensions ;
1716
1817namespace Umbraco . Cms . Tests . Integration . Umbraco . Infrastructure . Services ;
1918
@@ -35,7 +34,7 @@ public async Task SetupTestData()
3534 await LanguageService . CreateAsync ( _langEs , Constants . Security . SuperUserKey ) ;
3635 }
3736
38- CreateTestData ( ) ;
37+ await CreateTestData ( ) ;
3938 }
4039
4140 private Language ? _langFr ;
@@ -57,6 +56,10 @@ public async Task SetupTestData()
5756
5857 private IFileService FileService => GetRequiredService < IFileService > ( ) ;
5958
59+ private IContentTypeContainerService ContentTypeContainerService => GetRequiredService < IContentTypeContainerService > ( ) ;
60+
61+ public IContentTypeEditingService ContentTypeEditingService => GetRequiredService < IContentTypeEditingService > ( ) ;
62+
6063 [ Test ]
6164 public void EntityService_Can_Get_Paged_Descendants_Ordering_Path ( )
6265 {
@@ -381,6 +384,43 @@ public void EntityService_Can_Get_Paged_Media_Children()
381384 Assert . That ( total , Is . EqualTo ( 10 ) ) ;
382385 }
383386
387+ [ Test ]
388+ public async Task EntityService_Can_Get_Paged_Document_Type_Children ( )
389+ {
390+ IEnumerable < IEntitySlim > children = EntityService . GetPagedChildren (
391+ _documentTypeRootContainerKey ,
392+ [ UmbracoObjectTypes . DocumentTypeContainer ] ,
393+ [ UmbracoObjectTypes . DocumentTypeContainer , UmbracoObjectTypes . DocumentType ] ,
394+ 0 ,
395+ 10 ,
396+ false ,
397+ out long totalRecords ) ;
398+
399+ Assert . AreEqual ( 3 , totalRecords ) ;
400+ Assert . AreEqual ( 3 , children . Count ( ) ) ;
401+ Assert . IsTrue ( children . Single ( x => x . Key == _documentTypeSubContainer1Key ) . HasChildren ) ; // Has a single folder as a child.
402+ Assert . IsTrue ( children . Single ( x => x . Key == _documentTypeSubContainer2Key ) . HasChildren ) ; // Has a single document type as a child.
403+ Assert . IsFalse ( children . Single ( x => x . Key == _documentType1Key ) . HasChildren ) ; // Is a document type (has no children).
404+ }
405+
406+ [ Test ]
407+ public async Task EntityService_Can_Get_Paged_Document_Type_Children_For_Folders_Only ( )
408+ {
409+ IEnumerable < IEntitySlim > children = EntityService . GetPagedChildren (
410+ _documentTypeRootContainerKey ,
411+ [ UmbracoObjectTypes . DocumentTypeContainer ] ,
412+ [ UmbracoObjectTypes . DocumentTypeContainer ] ,
413+ 0 ,
414+ 10 ,
415+ false ,
416+ out long totalRecords ) ;
417+
418+ Assert . AreEqual ( 2 , totalRecords ) ;
419+ Assert . AreEqual ( 2 , children . Count ( ) ) ;
420+ Assert . IsTrue ( children . Single ( x => x . Key == _documentTypeSubContainer1Key ) . HasChildren ) ; // Has a single folder as a child.
421+ Assert . IsFalse ( children . Single ( x => x . Key == _documentTypeSubContainer2Key ) . HasChildren ) ; // Has a single document type as a child.
422+ }
423+
384424 [ Test ]
385425 [ LongRunning ]
386426 public void EntityService_Can_Get_Paged_Media_Descendants ( )
@@ -738,7 +778,7 @@ public void EntityService_Can_Find_All_ContentTypes_By_UmbracoObjectTypes()
738778 var entities = EntityService . GetAll ( UmbracoObjectTypes . DocumentType ) . ToArray ( ) ;
739779
740780 Assert . That ( entities . Any ( ) , Is . True ) ;
741- Assert . That ( entities . Count ( ) , Is . EqualTo ( 1 ) ) ;
781+ Assert . That ( entities . Count ( ) , Is . EqualTo ( 3 ) ) ;
742782 }
743783
744784 [ Test ]
@@ -748,7 +788,7 @@ public void EntityService_Can_Find_All_ContentTypes_By_UmbracoObjectType_Id()
748788 var entities = EntityService . GetAll ( objectTypeId ) . ToArray ( ) ;
749789
750790 Assert . That ( entities . Any ( ) , Is . True ) ;
751- Assert . That ( entities . Count ( ) , Is . EqualTo ( 1 ) ) ;
791+ Assert . That ( entities . Count ( ) , Is . EqualTo ( 3 ) ) ;
752792 }
753793
754794 [ Test ]
@@ -757,7 +797,7 @@ public void EntityService_Can_Find_All_ContentTypes_By_Type()
757797 var entities = EntityService . GetAll < IContentType > ( ) . ToArray ( ) ;
758798
759799 Assert . That ( entities . Any ( ) , Is . True ) ;
760- Assert . That ( entities . Count ( ) , Is . EqualTo ( 1 ) ) ;
800+ Assert . That ( entities . Count ( ) , Is . EqualTo ( 3 ) ) ;
761801 }
762802
763803 [ Test ]
@@ -885,7 +925,7 @@ public void ReserveId()
885925 private Media _subfolder ;
886926 private Media _subfolder2 ;
887927
888- public void CreateTestData ( )
928+ public async Task CreateTestData ( )
889929 {
890930 if ( _isSetup == false )
891931 {
@@ -942,6 +982,38 @@ public void CreateTestData()
942982 // Create and save sub folder -> 1061
943983 _subfolder2 = MediaBuilder . CreateMediaFolder ( _folderMediaType , _subfolder . Id ) ;
944984 MediaService . Save ( _subfolder2 , - 1 ) ;
985+
986+ // Setup document type folder structure for tests on paged children with or without folders
987+ await CreateStructureForPagedDocumentTypeChildrenTest ( ) ;
945988 }
946989 }
990+
991+ private static readonly Guid _documentTypeRootContainerKey = Guid . NewGuid ( ) ;
992+ private static readonly Guid _documentTypeSubContainer1Key = Guid . NewGuid ( ) ;
993+ private static readonly Guid _documentTypeSubContainer2Key = Guid . NewGuid ( ) ;
994+ private static readonly Guid _documentType1Key = Guid . NewGuid ( ) ;
995+
996+ private async Task CreateStructureForPagedDocumentTypeChildrenTest ( )
997+ {
998+ // Structure created:
999+ // - root container
1000+ // - sub container 1
1001+ // - sub container 1b
1002+ // - sub container 2
1003+ // - doc type 2
1004+ // - doc type 1
1005+ await ContentTypeContainerService . CreateAsync ( _documentTypeRootContainerKey , "Root Container" , null , Constants . Security . SuperUserKey ) ;
1006+ await ContentTypeContainerService . CreateAsync ( _documentTypeSubContainer1Key , "Sub Container 1" , _documentTypeRootContainerKey , Constants . Security . SuperUserKey ) ;
1007+ await ContentTypeContainerService . CreateAsync ( _documentTypeSubContainer2Key , "Sub Container 2" , _documentTypeRootContainerKey , Constants . Security . SuperUserKey ) ;
1008+ await ContentTypeContainerService . CreateAsync ( Guid . NewGuid ( ) , "Sub Container 1b" , _documentTypeSubContainer1Key , Constants . Security . SuperUserKey ) ;
1009+
1010+ var docType1Model = ContentTypeEditingBuilder . CreateBasicContentType ( "umbDocType1" , "Doc Type 1" ) ;
1011+ docType1Model . ContainerKey = _documentTypeRootContainerKey ;
1012+ docType1Model . Key = _documentType1Key ;
1013+ await ContentTypeEditingService . CreateAsync ( docType1Model , Constants . Security . SuperUserKey ) ;
1014+
1015+ var docType2Model = ContentTypeEditingBuilder . CreateBasicContentType ( "umbDocType2" , "Doc Type 2" ) ;
1016+ docType2Model . ContainerKey = _documentTypeSubContainer2Key ;
1017+ await ContentTypeEditingService . CreateAsync ( docType2Model , Constants . Security . SuperUserKey ) ;
1018+ }
9471019}
0 commit comments