<commit_before>﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;

namespace OfficeDevPnP.Core.Tests.AppModelExtensions
{
    [TestClass]
    public class PageExtensionsTests
    {
        private string folder = "SitePages";
        private string pageName = "Home.aspx";
        private string publishingPageName = "Happy";
        private string publishingPageTemplate = "BlankWebPartPage";
        private Guid publishingSiteFeatureId = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
        bool deactivatePublishingOnTearDown = false;

        #region Test initialize and cleanup
        public Web Setup(string webTemplate = "STS#0", bool enablePublishingInfrastructure = false)
        {
            using (var ctx = TestCommon.CreateClientContext())
            {
                var name = "WebExtensions";
                ctx.ExecuteQueryRetry();

                ExceptionHandlingScope scope = new ExceptionHandlingScope(ctx);

                Web web;
                Site site;
                site = ctx.Site;
                if (enablePublishingInfrastructure && !site.IsFeatureActive(publishingSiteFeatureId))
                {
                    site.ActivateFeature(publishingSiteFeatureId);
                    deactivatePublishingOnTearDown = true;
                }
                using (scope.StartScope())
                {                    
                    using (scope.StartTry())
                    {
                        web = ctx.Site.OpenWeb(name);
                        web.DeleteObject();
                    }
                    using (scope.StartCatch())
                    {
                        
                        web = ctx.Web.Webs.Add(new WebCreationInformation
                        {
                            Title = name,
                            WebTemplate = webTemplate,
                            Url = name
                        });                        
                    }
                    using (scope.StartFinally())
                    {                        
                        return web;
                    }
                }
            }
        }

        public void Teardown(Web web)
        {
            web.DeleteObject();
            if(deactivatePublishingOnTearDown)
            {
                using (var ctx = TestCommon.CreateClientContext())
                {
                    ctx.Site.DeactivateFeature(publishingSiteFeatureId);
                }
            }
        }
        #endregion

        #region Wiki page tests
        [TestMethod]
        public void CanAddLayoutToWikiPageTest()
        {
            var web = Setup();

            web.AddLayoutToWikiPage(folder, OfficeDevPnP.Core.WikiPageLayout.TwoColumns, pageName);

            Teardown(web);
        }

	    [TestMethod]
        public void CanAddHtmlToWikiPageTest()
        {
            var web = Setup();

            web.AddHtmlToWikiPage(folder, "<h1>I got text</h1>", pageName, 1, 1);

            Teardown(web);
        }

        [TestMethod]
        public void ProveThatWeCanAddHtmlToPageAfterChangingLayoutTest()
        {
            var web = Setup();
            web.AddLayoutToWikiPage(folder, OfficeDevPnP.Core.WikiPageLayout.TwoColumns, pageName);
            web.AddHtmlToWikiPage(folder, "<h1>I got text</h1>", pageName, 1, 1);

            var content = web.GetWikiPageContent(UrlUtility.Combine(UrlUtility.EnsureTrailingSlash(web.ServerRelativeUrl), folder, pageName));

            Assert.IsTrue(content.Contains("<h1>I got text</h1>"));

            Teardown(web);
        }
        #endregion

        #region Publishing page tests
        [TestMethod]
        public void CanCreatePublishingPageTest()
        {
            var web = Setup("CMSPUBLISHING#0",true);
            web.Context.Load(web);
            web.AddPublishingPage(publishingPageName, publishingPageTemplate);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var page = web.GetPublishingPage(string.Format("{0}.aspx",publishingPageName));
            web.Context.Load(page.ListItem, i => i["Title"]);
            web.Context.ExecuteQueryRetry();
            
            Assert.AreEqual(page.ListItem["Title"], publishingPageName);

            Teardown(web);
        }

        [TestMethod]
        public void PublishingPageWithInvalidCharsIsCorrectlyCreatedTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            web.AddPublishingPage("Happy?is&good", publishingPageTemplate);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var page = web.GetPublishingPage(string.Format("{0}.aspx", "Happy-is-good"));
            Assert.IsNotNull(page);

            Teardown(web);
        }

        [TestMethod]
        public void CanCreatePublishedPublishingPageWhenModerationIsEnabledTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            //Ensure that moderation is enabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = true;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate,publish:true);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var page = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            web.Context.Load(page.ListItem, i => i["_ModerationStatus"]);
            web.Context.Load(page.ListItem, i => i.File.MajorVersion);
            web.Context.ExecuteQueryRetry();

            Assert.AreEqual(0, page.ListItem["_ModerationStatus"]);
            Assert.AreEqual(1, page.ListItem.File.MajorVersion);

            Teardown(web);
        }

        [TestMethod]
        public void CanCreatePublishedPublishingPageWhenModerationIsDisabledTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            //Ensure that moderation is disabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = false;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate, publish: true);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var page = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            web.Context.Load(page.ListItem, i => i.File.MajorVersion);
            web.Context.ExecuteQueryRetry();

            Assert.AreEqual(1, page.ListItem.File.MajorVersion);

            Teardown(web);
        }

        [TestMethod]
        public void CreatedPublishingPagesSetsTitleCorrectlyTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            var customTitle = "Bad robot";
            var customPublishingPageName = "Good-robot";
            web.Context.Load(web);
            //Ensure that moderation is enabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = true;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate, publish: true);
            web.AddPublishingPage(customPublishingPageName, publishingPageTemplate, publish: true, title: customTitle);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var pageWithNoTitle = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            var pageWithCustomTitle = web.GetPublishingPage(string.Format("{0}.aspx", customPublishingPageName));
            web.Context.Load(pageWithNoTitle.ListItem, i => i["Title"]);
            web.Context.Load(pageWithCustomTitle.ListItem, i => i["Title"]);
            web.Context.ExecuteQueryRetry();

            //Check that title is set to page name
            Assert.AreEqual(publishingPageName, pageWithNoTitle.ListItem["Title"]);
            //Check that title is set to title
            Assert.AreEqual(customTitle, pageWithCustomTitle.ListItem["Title"]);

            Teardown(web);
        }
        #endregion
    }
}
<commit_msg>test fix due to update in invalid url detection<commit_after>﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;

namespace OfficeDevPnP.Core.Tests.AppModelExtensions
{
    [TestClass]
    public class PageExtensionsTests
    {
        private string folder = "SitePages";
        private string pageName = "Home.aspx";
        private string publishingPageName = "Happy";
        private string publishingPageTemplate = "BlankWebPartPage";
        private Guid publishingSiteFeatureId = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
        bool deactivatePublishingOnTearDown = false;

        #region Test initialize and cleanup
        public Web Setup(string webTemplate = "STS#0", bool enablePublishingInfrastructure = false)
        {
            using (var ctx = TestCommon.CreateClientContext())
            {
                var name = "WebExtensions";
                ctx.ExecuteQueryRetry();

                ExceptionHandlingScope scope = new ExceptionHandlingScope(ctx);

                Web web;
                Site site;
                site = ctx.Site;
                if (enablePublishingInfrastructure && !site.IsFeatureActive(publishingSiteFeatureId))
                {
                    site.ActivateFeature(publishingSiteFeatureId);
                    deactivatePublishingOnTearDown = true;
                }
                using (scope.StartScope())
                {                    
                    using (scope.StartTry())
                    {
                        web = ctx.Site.OpenWeb(name);
                        web.DeleteObject();
                    }
                    using (scope.StartCatch())
                    {
                        
                        web = ctx.Web.Webs.Add(new WebCreationInformation
                        {
                            Title = name,
                            WebTemplate = webTemplate,
                            Url = name
                        });                        
                    }
                    using (scope.StartFinally())
                    {                        
                        return web;
                    }
                }
            }
        }

        public void Teardown(Web web)
        {
            web.DeleteObject();
            if(deactivatePublishingOnTearDown)
            {
                using (var ctx = TestCommon.CreateClientContext())
                {
                    ctx.Site.DeactivateFeature(publishingSiteFeatureId);
                }
            }
        }
        #endregion

        #region Wiki page tests
        [TestMethod]
        public void CanAddLayoutToWikiPageTest()
        {
            var web = Setup();

            web.AddLayoutToWikiPage(folder, OfficeDevPnP.Core.WikiPageLayout.TwoColumns, pageName);

            Teardown(web);
        }

	    [TestMethod]
        public void CanAddHtmlToWikiPageTest()
        {
            var web = Setup();

            web.AddHtmlToWikiPage(folder, "<h1>I got text</h1>", pageName, 1, 1);

            Teardown(web);
        }

        [TestMethod]
        public void ProveThatWeCanAddHtmlToPageAfterChangingLayoutTest()
        {
            var web = Setup();
            web.AddLayoutToWikiPage(folder, OfficeDevPnP.Core.WikiPageLayout.TwoColumns, pageName);
            web.AddHtmlToWikiPage(folder, "<h1>I got text</h1>", pageName, 1, 1);

            var content = web.GetWikiPageContent(UrlUtility.Combine(UrlUtility.EnsureTrailingSlash(web.ServerRelativeUrl), folder, pageName));

            Assert.IsTrue(content.Contains("<h1>I got text</h1>"));

            Teardown(web);
        }
        #endregion

        #region Publishing page tests
        [TestMethod]
        public void CanCreatePublishingPageTest()
        {
            var web = Setup("CMSPUBLISHING#0",true);
            web.Context.Load(web);
            web.AddPublishingPage(publishingPageName, publishingPageTemplate);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var page = web.GetPublishingPage(string.Format("{0}.aspx",publishingPageName));
            web.Context.Load(page.ListItem, i => i["Title"]);
            web.Context.ExecuteQueryRetry();
            
            Assert.AreEqual(page.ListItem["Title"], publishingPageName);

            Teardown(web);
        }

        [TestMethod]
        public void PublishingPageWithInvalidCharsIsCorrectlyCreatedTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            web.AddPublishingPage("Happy?is:good", publishingPageTemplate);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var page = web.GetPublishingPage(string.Format("{0}.aspx", "Happy-is-good"));
            Assert.IsNotNull(page);

            Teardown(web);
        }

        [TestMethod]
        public void CanCreatePublishedPublishingPageWhenModerationIsEnabledTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            //Ensure that moderation is enabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = true;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate,publish:true);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var page = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            web.Context.Load(page.ListItem, i => i["_ModerationStatus"]);
            web.Context.Load(page.ListItem, i => i.File.MajorVersion);
            web.Context.ExecuteQueryRetry();

            Assert.AreEqual(0, page.ListItem["_ModerationStatus"]);
            Assert.AreEqual(1, page.ListItem.File.MajorVersion);

            Teardown(web);
        }

        [TestMethod]
        public void CanCreatePublishedPublishingPageWhenModerationIsDisabledTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            web.Context.Load(web);
            //Ensure that moderation is disabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = false;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate, publish: true);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var page = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            web.Context.Load(page.ListItem, i => i.File.MajorVersion);
            web.Context.ExecuteQueryRetry();

            Assert.AreEqual(1, page.ListItem.File.MajorVersion);

            Teardown(web);
        }

        [TestMethod]
        public void CreatedPublishingPagesSetsTitleCorrectlyTest()
        {
            var web = Setup("CMSPUBLISHING#0", true);
            var customTitle = "Bad robot";
            var customPublishingPageName = "Good-robot";
            web.Context.Load(web);
            //Ensure that moderation is enabled
            var pagesLibrary = web.Lists.GetByTitle("Pages");
            pagesLibrary.EnableModeration = true;
            pagesLibrary.Update();
            web.Context.ExecuteQueryRetry();
            web.AddPublishingPage(publishingPageName, publishingPageTemplate, publish: true);
            web.AddPublishingPage(customPublishingPageName, publishingPageTemplate, publish: true, title: customTitle);
            web.Context.Load(web, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();
            var pageWithNoTitle = web.GetPublishingPage(string.Format("{0}.aspx", publishingPageName));
            var pageWithCustomTitle = web.GetPublishingPage(string.Format("{0}.aspx", customPublishingPageName));
            web.Context.Load(pageWithNoTitle.ListItem, i => i["Title"]);
            web.Context.Load(pageWithCustomTitle.ListItem, i => i["Title"]);
            web.Context.ExecuteQueryRetry();

            //Check that title is set to page name
            Assert.AreEqual(publishingPageName, pageWithNoTitle.ListItem["Title"]);
            //Check that title is set to title
            Assert.AreEqual(customTitle, pageWithCustomTitle.ListItem["Title"]);

            Teardown(web);
        }
        #endregion
    }
}

{'fraction_non_alphanumeric': 0.09815028901734105, 'fraction_numerical': 0.005317919075144509, 'mean_word_length': 1.8563645368994552, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637313', 'n_tokens_mistral': 4631, 'n_tokens_neox': 4356, 'n_words': 760}