-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathContentServiceTests.cs
3685 lines (2982 loc) · 148 KB
/
ContentServiceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Diagnostics;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
using Umbraco.Cms.Tests.Common.Attributes;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Extensions;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Language = Umbraco.Cms.Core.Models.Language;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
/// <summary>
/// Tests covering all methods in the ContentService class.
/// </summary>
[TestFixture]
[UmbracoTest(
Database = UmbracoTestOptions.Database.NewSchemaPerTest,
PublishedRepositoryEvents = true,
WithApplication = true)]
public class ContentServiceTests : UmbracoIntegrationTestWithContent
{
[SetUp]
public void Setup() => ContentRepositoryBase.ThrowOnWarning = true;
[TearDown]
public void Teardown() => ContentRepositoryBase.ThrowOnWarning = false;
// TODO: Add test to verify there is only ONE newest document/content in {Constants.DatabaseSchema.Tables.Document} table after updating.
// TODO: Add test to delete specific version (with and without deleting prior versions) and versions by date.
private IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
private ILocalizedTextService LocalizedTextService => GetRequiredService<ILocalizedTextService>();
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
private IAuditService AuditService => GetRequiredService<IAuditService>();
private IUserService UserService => GetRequiredService<IUserService>();
private IUserGroupService UserGroupService => GetRequiredService<IUserGroupService>();
private IRelationService RelationService => GetRequiredService<IRelationService>();
private ILocalizedTextService TextService => GetRequiredService<ILocalizedTextService>();
private ITagService TagService => GetRequiredService<ITagService>();
private IPublicAccessService PublicAccessService => GetRequiredService<IPublicAccessService>();
private IDomainService DomainService => GetRequiredService<IDomainService>();
private INotificationService NotificationService => GetRequiredService<INotificationService>();
private PropertyEditorCollection PropertyEditorCollection => GetRequiredService<PropertyEditorCollection>();
private IDocumentRepository DocumentRepository => GetRequiredService<IDocumentRepository>();
private IJsonSerializer Serializer => GetRequiredService<IJsonSerializer>();
private IValueEditorCache ValueEditorCache => GetRequiredService<IValueEditorCache>();
protected override void CustomTestSetup(IUmbracoBuilder builder) => builder
.AddNotificationHandler<ContentPublishingNotification, ContentNotificationHandler>()
.AddNotificationHandler<ContentCopyingNotification, ContentNotificationHandler>()
.AddNotificationHandler<ContentCopiedNotification, ContentNotificationHandler>()
.AddNotificationHandler<ContentSavingNotification, ContentNotificationHandler>();
[Test]
public void Create_Blueprint()
{
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
var blueprint = ContentBuilder.CreateTextpageContent(contentType, "hello", Constants.System.Root);
blueprint.SetValue("title", "blueprint 1");
blueprint.SetValue("bodyText", "blueprint 2");
blueprint.SetValue("keywords", "blueprint 3");
blueprint.SetValue("description", "blueprint 4");
ContentService.SaveBlueprint(blueprint);
var found = ContentService.GetBlueprintsForContentTypes().ToArray();
Assert.AreEqual(1, found.Length);
// ensures it's not found by normal content
var contentFound = ContentService.GetById(found[0].Id);
Assert.IsNull(contentFound);
}
[Test]
public void Delete_Blueprint()
{
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
var blueprint = ContentBuilder.CreateTextpageContent(contentType, "hello", Constants.System.Root);
blueprint.SetValue("title", "blueprint 1");
blueprint.SetValue("bodyText", "blueprint 2");
blueprint.SetValue("keywords", "blueprint 3");
blueprint.SetValue("description", "blueprint 4");
ContentService.SaveBlueprint(blueprint);
ContentService.DeleteBlueprint(blueprint);
var found = ContentService.GetBlueprintsForContentTypes().ToArray();
Assert.AreEqual(0, found.Length);
}
[Test]
public void Create_Content_From_Blueprint()
{
using (var scope = ScopeProvider.CreateScope(autoComplete: true))
{
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
var blueprint = ContentBuilder.CreateTextpageContent(contentType, "hello", Constants.System.Root);
blueprint.SetValue("title", "blueprint 1");
blueprint.SetValue("bodyText", "blueprint 2");
blueprint.SetValue("keywords", "blueprint 3");
blueprint.SetValue("description", "blueprint 4");
ContentService.SaveBlueprint(blueprint);
var fromBlueprint = ContentService.CreateContentFromBlueprint(blueprint, "hello world");
ContentService.Save(fromBlueprint);
Assert.IsTrue(fromBlueprint.HasIdentity);
Assert.AreEqual("blueprint 1", fromBlueprint.Properties["title"].GetValue());
Assert.AreEqual("blueprint 2", fromBlueprint.Properties["bodyText"].GetValue());
Assert.AreEqual("blueprint 3", fromBlueprint.Properties["keywords"].GetValue());
Assert.AreEqual("blueprint 4", fromBlueprint.Properties["description"].GetValue());
}
}
[Test]
[LongRunning]
public void Get_All_Blueprints()
{
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var ct1 = ContentTypeBuilder.CreateTextPageContentType("ct1", defaultTemplateId: template.Id);
FileService.SaveTemplate(ct1.DefaultTemplate);
ContentTypeService.Save(ct1);
var ct2 = ContentTypeBuilder.CreateTextPageContentType("ct2", defaultTemplateId: template.Id);
FileService.SaveTemplate(ct2.DefaultTemplate);
ContentTypeService.Save(ct2);
for (var i = 0; i < 10; i++)
{
var blueprint =
ContentBuilder.CreateTextpageContent(i % 2 == 0 ? ct1 : ct2, "hello" + i, Constants.System.Root);
ContentService.SaveBlueprint(blueprint);
}
var found = ContentService.GetBlueprintsForContentTypes().ToArray();
Assert.AreEqual(10, found.Length);
found = ContentService.GetBlueprintsForContentTypes(ct1.Id).ToArray();
Assert.AreEqual(5, found.Length);
found = ContentService.GetBlueprintsForContentTypes(ct2.Id).ToArray();
Assert.AreEqual(5, found.Length);
}
[Test]
[LongRunning]
public async Task Perform_Scheduled_Publishing()
{
var langUk = new LanguageBuilder()
.WithCultureInfo("en-GB")
.WithIsDefault(true)
.Build();
var langFr = new LanguageBuilder()
.WithCultureInfo("fr-FR")
.Build();
await LanguageService.CreateAsync(langFr, Constants.Security.SuperUserKey);
await LanguageService.CreateAsync(langUk, Constants.Security.SuperUserKey);
var ctInvariant = ContentTypeBuilder.CreateBasicContentType("invariantPage");
ContentTypeService.Save(ctInvariant);
var ctVariant = ContentTypeBuilder.CreateBasicContentType("variantPage");
ctVariant.Variations = ContentVariation.Culture;
ContentTypeService.Save(ctVariant);
var now = DateTime.Now;
// 10x invariant content, half is scheduled to be published in 5 seconds, the other half is scheduled to be unpublished in 5 seconds
var invariant = new List<IContent>();
for (var i = 0; i < 10; i++)
{
var c = ContentBuilder.CreateBasicContent(ctInvariant);
c.Name = "name" + i;
if (i % 2 == 0)
{
var contentSchedule =
ContentScheduleCollection.CreateWithEntry(now.AddSeconds(5), null); // release in 5 seconds
var r = ContentService.Save(c, contentSchedule: contentSchedule);
Assert.IsTrue(r.Success, r.Result.ToString());
}
else
{
ContentService.Save(c);
var r = ContentService.Publish(c, c.AvailableCultures.ToArray());
var contentSchedule =
ContentScheduleCollection.CreateWithEntry(null, now.AddSeconds(5)); // expire in 5 seconds
ContentService.PersistContentSchedule(c, contentSchedule);
Assert.IsTrue(r.Success, r.Result.ToString());
}
invariant.Add(c);
}
// 10x variant content, half is scheduled to be published in 5 seconds, the other half is scheduled to be unpublished in 5 seconds
var variant = new List<IContent>();
var alternatingCulture = langFr.IsoCode;
for (var i = 0; i < 10; i++)
{
var c = ContentBuilder.CreateBasicContent(ctVariant);
c.SetCultureName("name-uk" + i, langUk.IsoCode);
c.SetCultureName("name-fr" + i, langFr.IsoCode);
if (i % 2 == 0)
{
var contentSchedule =
ContentScheduleCollection.CreateWithEntry(alternatingCulture, now.AddSeconds(5),
null); // release in 5 seconds
var r = ContentService.Save(c, contentSchedule: contentSchedule);
Assert.IsTrue(r.Success, r.Result.ToString());
alternatingCulture = alternatingCulture == langFr.IsoCode ? langUk.IsoCode : langFr.IsoCode;
}
else
{
ContentService.Save(c);
var r = ContentService.Publish(c, c.AvailableCultures.ToArray());
var contentSchedule =
ContentScheduleCollection.CreateWithEntry(alternatingCulture, null,
now.AddSeconds(5)); // expire in 5 seconds
ContentService.PersistContentSchedule(c, contentSchedule);
Assert.IsTrue(r.Success, r.Result.ToString());
}
variant.Add(c);
}
var runSched = ContentService.PerformScheduledPublish(
now.AddMinutes(1)).ToList(); // process anything scheduled before a minute from now
// this is 21 because the test data installed before this test runs has a scheduled item!
Assert.AreEqual(21, runSched.Count);
Assert.AreEqual(
20,
runSched.Count(x => x.Success),
string.Join(Environment.NewLine, runSched.Select(x => $"{x.Entity.Name} - {x.Result}")));
Assert.AreEqual(
5,
runSched.Count(x => x.Result == PublishResultType.SuccessPublish),
string.Join(Environment.NewLine, runSched.Select(x => $"{x.Entity.Name} - {x.Result}")));
Assert.AreEqual(
5,
runSched.Count(x => x.Result == PublishResultType.SuccessUnpublish),
string.Join(Environment.NewLine, runSched.Select(x => $"{x.Entity.Name} - {x.Result}")));
Assert.AreEqual(
5,
runSched.Count(x => x.Result == PublishResultType.SuccessPublishCulture),
string.Join(Environment.NewLine, runSched.Select(x => $"{x.Entity.Name} - {x.Result}")));
Assert.AreEqual(
5,
runSched.Count(x => x.Result == PublishResultType.SuccessUnpublishCulture),
string.Join(Environment.NewLine, runSched.Select(x => $"{x.Entity.Name} - {x.Result}")));
// re-run the scheduled publishing, there should be no results
runSched = ContentService.PerformScheduledPublish(
now.AddMinutes(1)).ToList();
Assert.AreEqual(0, runSched.Count);
}
[Test]
public void Remove_Scheduled_Publishing_Date()
{
// Arrange
// Act
var content = ContentService.CreateAndSave("Test", Constants.System.Root, "umbTextpage");
var contentSchedule = ContentScheduleCollection.CreateWithEntry(null, DateTime.Now.AddHours(2));
ContentService.Save(content, Constants.Security.SuperUserId, contentSchedule);
Assert.AreEqual(1, contentSchedule.FullSchedule.Count);
contentSchedule = ContentService.GetContentScheduleByContentId(content.Id);
var sched = contentSchedule.FullSchedule;
Assert.AreEqual(1, sched.Count);
Assert.AreEqual(1, sched.Count(x => x.Culture == string.Empty));
contentSchedule.Clear(ContentScheduleAction.Expire);
ContentService.Save(content, Constants.Security.SuperUserId, contentSchedule);
// Assert
contentSchedule = ContentService.GetContentScheduleByContentId(content.Id);
sched = contentSchedule.FullSchedule;
Assert.AreEqual(0, sched.Count);
Assert.IsTrue(ContentService.Publish(content, content.AvailableCultures.ToArray()).Success);
}
[Test]
[LongRunning]
public void Get_Top_Version_Ids()
{
// Arrange
// Act
var content = ContentService.CreateAndSave("Test", Constants.System.Root, "umbTextpage");
for (var i = 0; i < 20; i++)
{
content.SetValue("bodyText", "hello world " + Guid.NewGuid());
ContentService.Save(content);
ContentService.Publish(content, content.AvailableCultures.ToArray());
}
// Assert
var allVersions = ContentService.GetVersionIds(content.Id, int.MaxValue);
Assert.AreEqual(21, allVersions.Count());
var topVersions = ContentService.GetVersionIds(content.Id, 4);
Assert.AreEqual(4, topVersions.Count());
}
[Test]
[LongRunning]
public void Get_By_Ids_Sorted()
{
// Arrange
// Act
var results = new List<IContent>();
for (var i = 0; i < 20; i++)
{
results.Add(ContentService.CreateAndSave("Test", Constants.System.Root, "umbTextpage", -1));
}
var sortedGet = ContentService.GetByIds(new[] { results[10].Id, results[5].Id, results[12].Id })
.ToArray();
// Assert
Assert.AreEqual(sortedGet[0].Id, results[10].Id);
Assert.AreEqual(sortedGet[1].Id, results[5].Id);
Assert.AreEqual(sortedGet[2].Id, results[12].Id);
}
[Test]
public void Count_All()
{
// Arrange
// Act
for (var i = 0; i < 20; i++)
{
ContentService.CreateAndSave("Test", Constants.System.Root, "umbTextpage");
}
// Assert
Assert.AreEqual(25, ContentService.Count());
}
[Test]
public void Count_By_Content_Type()
{
// Arrange
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType =
ContentTypeBuilder.CreateSimpleContentType("umbBlah", "test Doc Type", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
// Act
for (var i = 0; i < 20; i++)
{
ContentService.CreateAndSave("Test", Constants.System.Root, "umbBlah");
}
// Assert
Assert.AreEqual(20, ContentService.Count("umbBlah"));
}
[Test]
public void Count_Children()
{
// Arrange
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType =
ContentTypeBuilder.CreateSimpleContentType("umbBlah", "test Doc Type", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
var parent = ContentService.CreateAndSave("Test", Constants.System.Root, "umbBlah");
// Act
for (var i = 0; i < 20; i++)
{
ContentService.CreateAndSave("Test", parent, "umbBlah");
}
// Assert
Assert.AreEqual(20, ContentService.CountChildren(parent.Id));
}
[Test]
public void Count_Descendants()
{
// Arrange
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType =
ContentTypeBuilder.CreateSimpleContentType("umbBlah", "test Doc Type", defaultTemplateId: template.Id);
ContentTypeService.Save(contentType);
var parent = ContentService.CreateAndSave("Test", Constants.System.Root, "umbBlah");
// Act
var current = parent;
for (var i = 0; i < 20; i++)
{
current = ContentService.CreateAndSave("Test", current, "umbBlah");
}
// Assert
Assert.AreEqual(20, ContentService.CountDescendants(parent.Id));
}
[Test]
public void GetAncestors_Returns_Empty_List_When_Path_Is_Null()
{
// Arrange
// Act
var current = new Mock<IContent>();
var res = ContentService.GetAncestors(current.Object);
// Assert
Assert.IsEmpty(res);
}
[Test]
public void Can_Remove_Property_Type()
{
// Arrange
// Act
var content = ContentService.Create("Test", Constants.System.Root, "umbTextpage");
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.HasIdentity, Is.False);
}
[Test]
public void Can_Create_Content()
{
// Arrange
// Act
var content = ContentService.Create("Test", Constants.System.Root, "umbTextpage");
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.HasIdentity, Is.False);
}
public void Can_Create_Content_Without_Explicitly_Set_User()
{
// Arrange
// Act
var content = ContentService.Create("Test", Constants.System.Root, "umbTextpage");
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.HasIdentity, Is.False);
Assert.That(content.CreatorId,
Is.EqualTo(Constants.Security
.SuperUserId)); // Default to -1 aka SuperUser (unknown) since we didn't explicitly set this in the Create call
}
[Test]
public void Can_Save_New_Content_With_Explicit_User()
{
var user = new UserBuilder().Build();
UserService.Save(user);
var content = new Content("Test", Constants.System.Root, ContentTypeService.Get("umbTextpage"));
// Act
ContentService.Save(content, user.Id);
// Assert
Assert.That(content.CreatorId, Is.EqualTo(user.Id));
Assert.That(content.WriterId, Is.EqualTo(user.Id));
}
[Test]
public void Cannot_Create_Content_With_Non_Existing_ContentType_Alias() =>
Assert.Throws<Exception>(() => ContentService.Create("Test", Constants.System.Root, "umbAliasDoesntExist"));
[Test]
public void Cannot_Save_Content_With_Empty_Name()
{
// Arrange
var content = new Content(string.Empty, Constants.System.Root, ContentTypeService.Get("umbTextpage"));
// Act & Assert
Assert.Throws<InvalidOperationException>(() => ContentService.Save(content));
}
[Test]
public void Can_Get_Content_By_Id()
{
// Arrange
// Act
var content = ContentService.GetById(Textpage.Id);
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.Id, Is.EqualTo(Textpage.Id));
}
[Test]
public void Can_Get_Content_By_Guid_Key()
{
// Arrange
// Act
var content = ContentService.GetById(new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"));
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.Id, Is.EqualTo(Textpage.Id));
}
[Test]
public void Can_Get_Content_By_Level()
{
// Arrange
// Act
var contents = ContentService.GetByLevel(2).ToList();
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.GreaterThanOrEqualTo(2));
}
[Test]
[LongRunning]
public void Can_Get_All_Versions_Of_Content()
{
var parent = ContentService.GetById(Textpage.Id);
Assert.IsFalse(parent.Published);
ContentService.Save(parent); // publishing parent, so Text Page 2 can be updated.
ContentService.Publish(parent, parent.AvailableCultures.ToArray());
var content = ContentService.GetById(Subpage.Id);
Assert.IsFalse(content.Published);
var versions = ContentService.GetVersions(Subpage.Id).ToList();
Assert.AreEqual(1, versions.Count);
var version1 = content.VersionId;
Console.WriteLine($"1 e={content.VersionId} p={content.PublishedVersionId}");
content.Name = "Text Page 2 Updated";
content.SetValue("author", "Jane Doe");
ContentService.Save(content); // publishes the current version, creates a version
ContentService.Publish(content, content.AvailableCultures.ToArray());
var version2 = content.VersionId;
Console.WriteLine($"2 e={content.VersionId} p={content.PublishedVersionId}");
content.Name = "Text Page 2 ReUpdated";
content.SetValue("author", "Bob Hope");
ContentService.Save(content); // publishes again, creates a version
ContentService.Publish(content, content.AvailableCultures.ToArray());
var version3 = content.VersionId;
Console.WriteLine($"3 e={content.VersionId} p={content.PublishedVersionId}");
var content1 = ContentService.GetById(content.Id);
Assert.AreEqual("Bob Hope", content1.GetValue("author"));
Assert.AreEqual("Bob Hope", content1.GetValue("author", published: true));
content.Name = "Text Page 2 ReReUpdated";
content.SetValue("author", "John Farr");
ContentService.Save(content); // no new version
content1 = ContentService.GetById(content.Id);
Assert.AreEqual("John Farr", content1.GetValue("author"));
Assert.AreEqual("Bob Hope", content1.GetValue("author", published: true));
versions = ContentService.GetVersions(Subpage.Id).ToList();
Assert.AreEqual(3, versions.Count);
// versions come with most recent first
Assert.AreEqual(version3, versions[0].VersionId); // the edited version
Assert.AreEqual(version2, versions[1].VersionId); // the published version
Assert.AreEqual(version1, versions[2].VersionId); // the previously published version
// p is always the same, published version
// e is changing, actual version we're loading
Console.WriteLine();
foreach (var version in ((IEnumerable<IContent>)versions).Reverse())
{
Console.WriteLine($"+ e={((Content)version).VersionId} p={((Content)version).PublishedVersionId}");
}
// and proper values
// first, the current (edited) version, with edited and published versions
Assert.AreEqual("John Farr", versions[0].GetValue("author")); // current version has the edited value
Assert.AreEqual(
"Bob Hope",
versions[0].GetValue("author", published: true)); // and the published published value
// then, the current (published) version, with edited == published
Assert.AreEqual("Bob Hope", versions[1].GetValue("author")); // own edited version
Assert.AreEqual("Bob Hope", versions[1].GetValue("author", published: true)); // and published
// then, the first published version - with values as 'edited'
Assert.AreEqual("Jane Doe", versions[2].GetValue("author")); // own edited version
Assert.AreEqual("Bob Hope", versions[2].GetValue("author", published: true)); // and published
}
[Test]
public void Can_Get_Root_Content()
{
// Arrange
// Act
var contents = ContentService.GetRootContent().ToList();
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.EqualTo(1));
}
[Test]
[LongRunning]
public void Can_Get_Content_For_Expiration()
{
// Arrange
var root = ContentService.GetById(Textpage.Id);
ContentService.Publish(root!, root!.AvailableCultures.ToArray());
var content = ContentService.GetById(Subpage.Id);
var contentSchedule = ContentScheduleCollection.CreateWithEntry(null, DateTime.Now.AddSeconds(1));
ContentService.PersistContentSchedule(content!, contentSchedule);
ContentService.Publish(content, content.AvailableCultures.ToArray());
// Act
Thread.Sleep(new TimeSpan(0, 0, 0, 2));
var contents = ContentService.GetContentForExpiration(DateTime.Now).ToList();
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.EqualTo(1));
}
[Test]
public void Can_Get_Content_For_Release()
{
// Arrange
// Act
var contents = ContentService.GetContentForRelease(DateTime.Now).ToList();
// Assert
Assert.That(DateTime.Now.AddMinutes(-5) <= DateTime.Now);
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.EqualTo(1));
}
[Test]
public void Can_Get_Content_In_RecycleBin()
{
// Arrange
// Act
var contents = ContentService.GetPagedContentInRecycleBin(0, int.MaxValue, out var _).ToList();
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.EqualTo(1));
}
[Test]
public void Can_Unpublish_Content()
{
// Arrange
var content = ContentService.GetById(Textpage.Id);
Assert.IsNotNull(content);
var published = ContentService.Publish(content, content.AvailableCultures.ToArray(), userId: -1);
// Act
var unpublished = ContentService.Unpublish(content, userId: -1);
// Assert
Assert.That(published.Success, Is.True);
Assert.That(unpublished.Success, Is.True);
Assert.That(content.Published, Is.False);
Assert.AreEqual(PublishResultType.SuccessUnpublish, unpublished.Result);
}
[Test]
public void Can_Unpublish_Content_Variation()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
// re-get
content = ContentService.GetById(content.Id);
Assert.IsTrue(saved.Success);
Assert.IsTrue(published.Success);
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
var unpublished = ContentService.Unpublish(content, langFr.IsoCode);
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
// re-get
content = ContentService.GetById(content.Id);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
}
[Test]
[LongRunning]
public void Can_Publish_Culture_After_Last_Culture_Unpublished()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.AreEqual(PublishedState.Published, content.PublishedState);
// re-get
content = ContentService.GetById(content.Id);
var unpublished = ContentService.Unpublish(content, langUk.IsoCode); // first culture
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
content = ContentService.GetById(content.Id);
unpublished = ContentService.Unpublish(content, langFr.IsoCode); // last culture
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishLastCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
content = ContentService.GetById(content.Id);
published = ContentService.Publish(content, new[] { langUk.IsoCode });
Assert.AreEqual(PublishedState.Published, content.PublishedState);
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
content = ContentService.GetById(content.Id); // reget
Assert.AreEqual(PublishedState.Published, content.PublishedState);
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
}
[Test]
public void Unpublish_All_Cultures_Has_Unpublished_State()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(saved.Success);
Assert.IsTrue(published.Success);
Assert.AreEqual(PublishedState.Published, content.PublishedState);
// re-get
content = ContentService.GetById(content.Id);
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.AreEqual(PublishedState.Published, content.PublishedState);
var unpublished = ContentService.Unpublish(content, langFr.IsoCode); // first culture
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.AreEqual(PublishedState.Published, content.PublishedState); // still published
// re-get
content = ContentService.GetById(content.Id);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
unpublished = ContentService.Unpublish(content, langUk.IsoCode); // last culture
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishLastCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
Assert.AreEqual(PublishedState.Unpublished,
content.PublishedState); // the last culture was unpublished so the document should also reflect this
// re-get
content = ContentService.GetById(content.Id);
Assert.AreEqual(PublishedState.Unpublished, content.PublishedState); // just double checking
Assert.IsFalse(content.IsCulturePublished(langFr.IsoCode));
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
}
[Test]
public async Task Unpublishing_Mandatory_Language_Unpublishes_Document()
{
var langUk = new LanguageBuilder()
.WithCultureInfo("en-GB")
.WithIsDefault(true)
.WithIsMandatory(true)
.Build();
var langFr = new LanguageBuilder()
.WithCultureInfo("fr-FR")
.Build();
await LanguageService.CreateAsync(langFr, Constants.Security.SuperUserKey);
await LanguageService.CreateAsync(langUk, Constants.Security.SuperUserKey);
var contentType = ContentTypeBuilder.CreateBasicContentType();
contentType.Variations = ContentVariation.Culture;
ContentTypeService.Save(contentType);
IContent content = new Content("content", Constants.System.Root, contentType);
content.SetCultureName("content-fr", langFr.IsoCode);
content.SetCultureName("content-en", langUk.IsoCode);
var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(saved.Success);
Assert.IsTrue(published.Success);
Assert.AreEqual(PublishedState.Published, content.PublishedState);
// re-get
content = ContentService.GetById(content.Id);
var unpublished = ContentService.Unpublish(content, langUk.IsoCode); // unpublish mandatory lang
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishMandatoryCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode)); // remains published
Assert.AreEqual(PublishedState.Unpublished, content.PublishedState);
}
[Test]
public void Unpublishing_Already_Unpublished_Culture()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(saved.Success);
Assert.IsTrue(published.Success);
Assert.AreEqual(PublishedState.Published, content.PublishedState);
// re-get
content = ContentService.GetById(content.Id);
var unpublished = ContentService.Unpublish(content, langUk.IsoCode);
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishCulture, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
content = ContentService.GetById(content.Id);
// Change some data since Unpublish should always Save
content.SetCultureName("content-en-updated", langUk.IsoCode);
unpublished = ContentService.Unpublish(content, langUk.IsoCode); // unpublish again
Assert.IsTrue(unpublished.Success);
Assert.AreEqual(PublishResultType.SuccessUnpublishAlready, unpublished.Result);
Assert.IsFalse(content.IsCulturePublished(langUk.IsoCode));
content = ContentService.GetById(content.Id);
// ensure that even though the culture was already unpublished that the data was still persisted
Assert.AreEqual("content-en-updated", content.GetCultureName(langUk.IsoCode));
}
[Test]
public void Publishing_No_Cultures_Still_Saves()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Assert.IsTrue(content.IsCulturePublished(langFr.IsoCode));
Assert.IsTrue(content.IsCulturePublished(langUk.IsoCode));
Assert.IsTrue(saved.Success);
Assert.IsTrue(published.Success);
Assert.AreEqual(PublishedState.Published, content.PublishedState);
// re-get
content = ContentService.GetById(content.Id);
// Change some data since SaveAndPublish should always Save
content.SetCultureName("content-en-updated", langUk.IsoCode);
saved = ContentService.Save(content);
published = ContentService.Publish(content, new string[] { }); // publish without cultures
Assert.IsTrue(saved.Success);
Assert.AreEqual(PublishResultType.FailedPublishNothingToPublish, published.Result);
// re-get
content = ContentService.GetById(content.Id);
// ensure that even though nothing was published that the data was still persisted
Assert.AreEqual("content-en-updated", content.GetCultureName(langUk.IsoCode));
}
[Test]
public async Task Pending_Invariant_Property_Changes_Affect_Default_Language_Edited_State()
{
// Arrange
var langGb = new LanguageBuilder()
.WithCultureInfo("en-GB")
.WithIsDefault(true)
.Build();
var langFr = new LanguageBuilder()
.WithCultureInfo("fr-FR")
.Build();
await LanguageService.CreateAsync(langFr, Constants.Security.SuperUserKey);
await LanguageService.CreateAsync(langGb, Constants.Security.SuperUserKey);
var contentType = ContentTypeBuilder.CreateMetaContentType();
contentType.Variations = ContentVariation.Culture;
foreach (var prop in contentType.PropertyTypes)
{
prop.Variations = ContentVariation.Culture;
}
var keywordsProp = contentType.PropertyTypes.Single(x => x.Alias == "metakeywords");
keywordsProp.Variations = ContentVariation.Nothing; // this one is invariant
ContentTypeService.Save(contentType);
IContent content = new Content("content", Constants.System.Root, contentType);
content.SetCultureName("content-en", langGb.IsoCode);
content.SetCultureName("content-fr", langFr.IsoCode);
Assert.IsTrue(ContentService.Save(content).Success);
Assert.IsTrue(ContentService.Publish(content, new[] { langGb.IsoCode, langFr.IsoCode }).Success);