<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Text;
using Cake.Common.Tests.Fixtures;
using Cake.Common.Tests.Properties;
using Cake.Common.Xml;
using Cake.Testing.Xunit;
using Xunit;

namespace Cake.Common.Tests.Unit.XML
{
    public sealed class XmlPokeTests
    {
        public sealed class ValidateParameters
        {
            [Fact]
            public void Should_Throw_If_FilePath_Is_Null()
            {
                // Given
                var fixture = new XmlPokeFixture(false);

                // When
                var result = Record.Exception(() => fixture.Poke("gibblygook", ""));

                // Then
                AssertEx.IsArgumentNullException(result, "filePath");
            }

            [Fact]
            public void Should_Throw_If_File_Does_Not_Exists()
            {
                // Given
                var fixture = new XmlPokeFixture(false);
                fixture.XmlPath = "/Working/web.config";

                // When
                var result = Record.Exception(() => fixture.Poke("gibblygook", ""));

                // Then
                Assert.IsType<FileNotFoundException>(result);
            }

            [Fact]
            public void Should_Throw_If_No_Xpath()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                var result = Record.Exception(() => fixture.Poke(null, ""));

                // Then
                AssertEx.IsArgumentNullException(result, "xpath");
            }

            [Fact]
            public void Should_Throw_If_Xml_File_Has_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlWithDtd: true);

                // When
                var result = Record.Exception(() => fixture.Poke("/plist/dict/string/text()", ""));

                // Then
                Assert.IsType<System.Xml.XmlException>(result);
            }

            [Fact]
            public void Should_Throw_If_Xml_String_Has_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlExists: false);

                // When
                var result = Record.Exception(() => fixture.PokeString(Resources.XmlPoke_Xml_Dtd, "/plist/dict/string/text()", ""));

                // Then
                Assert.IsType<System.Xml.XmlException>(result);
            }
        }

        public sealed class Transform
        {
            [Fact]
            public void Should_Change_Attribute()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']/@value", "production<EMAIL>");

                // Then
                Assert.True(fixture.TestIsValue(
                    "/configuration/appSettings/add[@key = 'server']/@value",
                    "productionhost.<EMAIL>.<EMAIL>"));
            }

            [Fact]
            public void Should_Remove_Attribute()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.True(fixture.TestIsRemoved(
                    "/configuration/appSettings/add[@key = 'server']"));
            }

            [Fact]
            public void Should_Have_Encoding_UTF8_With_BOM()
            {
                // Given
                var fixture = new XmlPokeFixture();
                fixture.Settings.Encoding = new UTF8Encoding(true);

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.True(fixture.TestIsUTF8WithBOM());
            }

            [Fact]
            public void Should_Have_Encoding_UTF8_Without_BOM()
            {
                // Given
                var fixture = new XmlPokeFixture();
                fixture.Settings.Encoding = new UTF8Encoding(false);

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.False(fixture.TestIsUTF8WithBOM());
            }

            [RuntimeFact(TestRuntime.Clr)]
            public void Should_Change_Attribute_From_Xml_File_With_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlWithDtd: true);
                fixture.Settings.DtdProcessing = XmlDtdProcessing.Parse;

                // When
                fixture.Poke("/plist/dict/string", "Cake Version");

                // Then
                Assert.True(fixture.TestIsValue(
                    "/plist/dict/string/text()",
                    "Cake Version"));
            }

            [RuntimeFact(TestRuntime.CoreClr)]
            public void Should_Throw_On_Net_Core_Change_Attribute_From_Xml_File_With_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlWithDtd: true);
                fixture.Settings.DtdProcessing = XmlDtdProcessing.Parse;

                // When
                var result = Record.Exception(() => fixture.Poke("/plist/dict/string", "Cake Version"));

                // Then
                AssertEx.IsCakeException(result, "DtdProcessing is not available on .NET Core.");
            }<commit_msg>XML with DTD now works<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Text;
using Cake.Common.Tests.Fixtures;
using Cake.Common.Tests.Properties;
using Cake.Common.Xml;
using Cake.Testing.Xunit;
using Xunit;

namespace Cake.Common.Tests.Unit.XML
{
    public sealed class XmlPokeTests
    {
        public sealed class ValidateParameters
        {
            [Fact]
            public void Should_Throw_If_FilePath_Is_Null()
            {
                // Given
                var fixture = new XmlPokeFixture(false);

                // When
                var result = Record.Exception(() => fixture.Poke("gibblygook", ""));

                // Then
                AssertEx.IsArgumentNullException(result, "filePath");
            }

            [Fact]
            public void Should_Throw_If_File_Does_Not_Exists()
            {
                // Given
                var fixture = new XmlPokeFixture(false);
                fixture.XmlPath = "/Working/web.config";

                // When
                var result = Record.Exception(() => fixture.Poke("gibblygook", ""));

                // Then
                Assert.IsType<FileNotFoundException>(result);
            }

            [Fact]
            public void Should_Throw_If_No_Xpath()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                var result = Record.Exception(() => fixture.Poke(null, ""));

                // Then
                AssertEx.IsArgumentNullException(result, "xpath");
            }

            [Fact]
            public void Should_Throw_If_Xml_File_Has_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlWithDtd: true);

                // When
                var result = Record.Exception(() => fixture.Poke("/plist/dict/string/text()", ""));

                // Then
                Assert.IsType<System.Xml.XmlException>(result);
            }

            [Fact]
            public void Should_Throw_If_Xml_String_Has_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlExists: false);

                // When
                var result = Record.Exception(() => fixture.PokeString(Resources.XmlPoke_Xml_Dtd, "/plist/dict/string/text()", ""));

                // Then
                Assert.IsType<System.Xml.XmlException>(result);
            }
        }

        public sealed class Transform
        {
            [Fact]
            public void Should_Change_Attribute()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']/@value", "productionhost.somecompany.com");

                // Then
                Assert.True(fixture.TestIsValue(
                    "/configuration/appSettings/add[@key = 'server']/@value",
                    "productionhost.somecompany.com"));
            }

            [Fact]
            public void Should_Remove_Attribute()
            {
                // Given
                var fixture = new XmlPokeFixture();

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.True(fixture.TestIsRemoved(
                    "/configuration/appSettings/add[@key = 'server']"));
            }

            [Fact]
            public void Should_Have_Encoding_UTF8_With_BOM()
            {
                // Given
                var fixture = new XmlPokeFixture();
                fixture.Settings.Encoding = new UTF8Encoding(true);

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.True(fixture.TestIsUTF8WithBOM());
            }

            [Fact]
            public void Should_Have_Encoding_UTF8_Without_BOM()
            {
                // Given
                var fixture = new XmlPokeFixture();
                fixture.Settings.Encoding = new UTF8Encoding(false);

                // When
                fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

                // Then
                Assert.False(fixture.TestIsUTF8WithBOM());
            }

            [Fact]
            public void Should_Change_Attribute_From_Xml_File_With_Dtd()
            {
                // Given
                var fixture = new XmlPokeFixture(xmlWithDtd: true);
                fixture.Settings.DtdProcessing = XmlDtdProcessing.Parse;

                // When
                fixture.Poke("/plist/dict/string", "Cake Version");

                // Then
                Assert.True(fixture.TestIsValue(
                    "/plist/dict/string/text()",
                    "Cake Version"));
            }
{'fraction_non_alphanumeric': 0.10245216422069478, 'fraction_numerical': 0.001114620100315809, 'mean_word_length': 1.3426892950391645, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6638476', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2607, 'n_words': 514}