public void CopyContentProperties_OutstandingDraftAndNotInOverwriteMode_ReturnsFailureStatus()
        {
            // Arrange
            var testLanguage = CultureInfo.GetCultureInfo(testLanguageCode);

            var sourcePageReference      = new ContentReference(sourcePageId);
            var destinationPageReference = new ContentReference(destinationPageId);

            var request = new ContentCopyRequestDetail
            {
                Source      = sourcePageId,
                Destination = destinationPageId,
                Properties  = new List <string> {
                    "SampleProperty", "Other Property"
                },
                CultureCode = testLanguageCode
            };

            var pendingCommonDraft = new ContentVersion(destinationPageReference, "draft", VersionStatus.CheckedIn, DateTime.Now,
                                                        "test", "sample user", destinationPageId, testLanguageCode, true, true);

            _stubContentVersionRepository.LoadCommonDraft(destinationPageReference, testLanguageCode).Returns(pendingCommonDraft);

            _stubContentLoader.Get <PageData>(sourcePageReference, testLanguage).Returns(new PageData());
            _stubContentLoader.Get <PageData>(destinationPageReference, testLanguage).Returns(new PageData());

            // Act
            var result = _propertyService.CopyContentProperties(request);

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void CopyContentProperties_SameSourceAndDestination_ReturnsFailureStatus()
        {
            // Arrange
            var request = new ContentCopyRequestDetail
            {
                Source      = 1,
                Destination = 1
            };

            // Act
            var result = _propertyService.CopyContentProperties(request);

            // Assert
            Assert.IsFalse(result.Success);
        }
        public ContentResult SubmitCopyRequest(ContentCopyRequestDetail model)
        {
            var copyResult = _propertyService.CopyContentProperties(model);

            return(CamelCaseJson(copyResult));
        }
        public ContentCopyResult CopyContentProperties(ContentCopyRequestDetail copyRequest)
        {
            // Validate user input
            if (copyRequest.Source == copyRequest.Destination)
            {
                return(new ContentCopyResult
                {
                    Status = "Failure. The 'Source' and 'Destination' page cannot be the same."
                });
            }

            if (copyRequest.Properties?.Any() != true)
            {
                return(new ContentCopyResult
                {
                    Status = "Failure. No properties selected to copy."
                });
            }

            var source = _contentLoader.Get <PageData>(new ContentReference(copyRequest.Source), CultureInfo.GetCultureInfo(copyRequest.CultureCode));

            var destination = _contentLoader.Get <PageData>(new ContentReference(copyRequest.Destination), CultureInfo.GetCultureInfo(copyRequest.CultureCode))?.CreateWritableClone();

            // Ensure the destination page can be updated
            var hasDraft = ContentHasOutstandingDraft(copyRequest.Destination, copyRequest.CultureCode);

            if (hasDraft && !copyRequest.CopyAndPublishAutomatically)
            {
                return(new ContentCopyResult
                {
                    Status = "Failure. The destination page already has a draft pending publish. Review and publish it manually or use the 'Publish Automatically' mode."
                });
            }

            // Copy all selected properties across
            foreach (var property in copyRequest.Properties)
            {
                var sourceProp      = source.Property.FirstOrDefault(w => w.Name == property);
                var destinationProp = destination.Property.FirstOrDefault(w => w.Name == property);

                if (destinationProp?.Type == null)
                {
                    return(new ContentCopyResult
                    {
                        Status = $"Failure. The destination page does not have a {sourceProp.Name} property."
                    });
                }

                if (sourceProp.Type != destinationProp.Type)
                {
                    return(new ContentCopyResult
                    {
                        Status = $"Failure. The source and destination {sourceProp.Name} are different content types."
                    });
                }

                destinationProp.Value = sourceProp.Value;
            }

            var saved = _contentRepository.Save(destination, copyRequest.CopyAndPublishAutomatically ? SaveAction.Publish : SaveAction.CheckOut, AccessLevel.Edit);

            return(new ContentCopyResult
            {
                Success = true,
                Status = "Content copied."
            });
        }
        public void CopyContentProperties_ValidInput_ReturnsSuccessCopyStatus()
        {
            // Arrange
            var testLanguage = CultureInfo.GetCultureInfo(testLanguageCode);

            // Source page setup
            var sourcePageReference = new ContentReference(sourcePageId);

            const string firstSourceValue  = "Some source value";
            const string secondSourceValue = "other source value";

            var sourcePage = new PageData
            {
                Property =
                {
                    new PropertyString           {
                        Name = "PageName", Value = "Source Page"
                    },
                    new PropertyContentReference {
                        Name = "PageLink", Value = sourcePageReference
                    },

                    new PropertyString           {
                        Name = firstProperty, Value = firstSourceValue, IsPropertyData = true, IsLanguageSpecific = true
                    },
                    new PropertyString           {
                        Name = secondProperty, Value = secondSourceValue, IsPropertyData = true
                    }
                }
            };

            _stubContentLoader.Get <PageData>(sourcePageReference, testLanguage).Returns(sourcePage);

            // Destination page setup
            var destinationPageReference = new ContentReference(destinationPageId);

            var destinationPage = new PageData
            {
                Property =
                {
                    new PropertyString           {
                        Name = "PageName", Value = "Destination Page"
                    },
                    new PropertyContentReference {
                        Name = "PageLink", Value = destinationPageReference
                    },

                    new PropertyString           {
                        Name = firstProperty, Value = "", IsPropertyData = true, IsLanguageSpecific = true
                    },
                    new PropertyString           {
                        Name = secondProperty, Value = "", IsPropertyData = true
                    }
                }
            };

            _stubContentLoader.Get <PageData>(destinationPageReference, testLanguage).Returns(destinationPage);

            // Copy request setup
            var request = new ContentCopyRequestDetail
            {
                Source      = sourcePageId,
                Destination = destinationPageId,
                Properties  = new List <string> {
                    firstProperty, secondProperty
                },
                CultureCode = testLanguageCode
            };

            // Draft setup
            var pendingCommonDraft = new ContentVersion(destinationPageReference, "draft", VersionStatus.Published, DateTime.Now,
                                                        "test", "sample user", destinationPageId, testLanguageCode, true, true);

            _stubContentVersionRepository.LoadCommonDraft(destinationPageReference, testLanguageCode).Returns(pendingCommonDraft);

            _stubContentRepository.Save(Arg.Any <PageData>(), SaveAction.CheckOut, AccessLevel.Edit).Returns(destinationPageReference);
            // Act
            var result = _propertyService.CopyContentProperties(request);

            // Assert
            Assert.IsTrue(result.Success);
        }
        public void CopyContentProperties_PropertyTypeMismatch_ReturnsFailureStatus()
        {
            // Arrange
            var testLanguage = CultureInfo.GetCultureInfo(testLanguageCode);

            // Source page setup
            var sourcePageReference = new ContentReference(sourcePageId);

            var sourcePage = new PageData
            {
                Property =
                {
                    new PropertyString           {
                        Name = "PageName", Value = "Source Page"
                    },
                    new PropertyContentReference {
                        Name = "PageLink", Value = sourcePageReference
                    },

                    new PropertyString           {
                        Name = firstProperty, Value = "Some value", IsPropertyData = true, IsLanguageSpecific = true
                    }
                }
            };

            _stubContentLoader.Get <PageData>(sourcePageReference, testLanguage).Returns(sourcePage);

            // Destination page setup
            var destinationPageReference = new ContentReference(destinationPageId);

            var destinationPage = new PageData
            {
                Property =
                {
                    new PropertyString           {
                        Name = "PageName", Value = "Destination Page"
                    },
                    new PropertyContentReference {
                        Name = "PageLink", Value = destinationPageReference
                    },

                    new PropertyNumber           {
                        Name = firstProperty, Value = 12345, IsPropertyData = true, IsLanguageSpecific = true
                    }
                }
            };

            _stubContentLoader.Get <PageData>(destinationPageReference, testLanguage).Returns(destinationPage);

            // Copy request setup
            var request = new ContentCopyRequestDetail
            {
                Source      = sourcePageId,
                Destination = destinationPageId,
                Properties  = new List <string> {
                    firstProperty, secondProperty
                },
                CultureCode = testLanguageCode
            };

            // Draft setup
            var pendingCommonDraft = new ContentVersion(destinationPageReference, "draft", VersionStatus.Published, DateTime.Now,
                                                        "test", "sample user", destinationPageId, testLanguageCode, true, true);

            _stubContentVersionRepository.LoadCommonDraft(destinationPageReference, testLanguageCode).Returns(pendingCommonDraft);

            // Act
            var result = _propertyService.CopyContentProperties(request);

            // Assert
            Assert.IsFalse(result.Success);
        }