<commit_before>﻿using System;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;

namespace Umbraco.Tests.Models
{
    [TestFixture]
    public class VariationTests
    {
        [SetUp]
        public void SetUp()
        {
            // annoying, but content type wants short string helper ;(
            SettingsForTests.Reset();

            // well, this is also annoying, but...
            // validating a value is performed by its data editor,
            // based upon the configuration in the data type, so we
            // need to be able to retrieve them all...

            Current.Reset();

            var configs = new Configs();
            configs.Add(SettingsForTests.GetDefaultGlobalSettings);
            configs.Add(SettingsForTests.GetDefaultUmbracoSettings);

            var factory = Mock.Of<IFactory>();
            Current.Factory = factory;

            var dataEditors = new DataEditorCollection(new IDataEditor[]
            {
                new DataEditor(Mock.Of<ILogger>()) { Alias = "editor", ExplicitValueEditor = new DataValueEditor("view") }
            });
            var propertyEditors = new PropertyEditorCollection(dataEditors);

            var dataType = Mock.Of<IDataType>();
            Mock.Get(dataType)
                .Setup(x => x.Configuration)
                .Returns(null);

            var dataTypeService = Mock.Of<IDataTypeService>();
            Mock.Get(dataTypeService)
                .Setup(x => x.GetDataType(It.IsAny<int>()))
                .Returns<int>(x => dataType);

            var serviceContext = ServiceContext.CreatePartial(
                dataTypeService: dataTypeService,
                localizedTextService: Mock.Of<ILocalizedTextService>());

            Mock.Get(factory)
                .Setup(x => x.GetInstance(It.IsAny<Type>()))
                .Returns<Type>(x =>
                {
                    if (x == typeof(Configs)) return configs;
                    if (x == typeof(PropertyEditorCollection)) return propertyEditors;
                    if (x == typeof(ServiceContext)) return serviceContext;
                    if (x == typeof(ILocalizedTextService)) return serviceContext.LocalizationService;
                    throw new NotSupportedException(x.FullName);
                });
        }

        [Test]
        public void ValidateVariationTests()
        {

            // All tests:
            // 1. if exact is set to true: culture cannot be null when the ContentVariation.Culture flag is set
            // 2. if wildcards is set to false: fail when "*" is passed in as either culture or segment.
            // 3. ContentVariation flag is ignored when wildcards are used.
            // 4. Empty string is considered the same as null            

            #region Nothing

            Assert4A(ContentVariation.Nothing, null, null, true);
            Assert4A(ContentVariation.Nothing, null, "", true);
            Assert4B(ContentVariation.Nothing, null, "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, null, "segment", false);
            Assert4A(ContentVariation.Nothing, "", null, true);
            Assert4A(ContentVariation.Nothing, "", "", true);
            Assert4B(ContentVariation.Nothing, "", "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, "", "segment", false);
            Assert4B(ContentVariation.Nothing, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Nothing, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Nothing, "*", "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, "*", "segment", false);
            Assert4A(ContentVariation.Nothing, "culture", null, false);
            Assert4A(ContentVariation.Nothing, "culture", "", false);
            Assert4A(ContentVariation.Nothing, "culture", "*", false);
            Assert4A(ContentVariation.Nothing, "culture", "segment", false);

            #endregion

            #region Culture

            Assert4B(ContentVariation.Culture, null, null, false, true, false, true);
            Assert4B(ContentVariation.Culture, null, "", false, true, false, true);
            Assert4B(ContentVariation.Culture, null, "*", false, false, false, true);
            Assert4A(ContentVariation.Culture, null, "segment", false);
            Assert4B(ContentVariation.Culture, "", null, false, true, false, true);
            Assert4B(ContentVariation.Culture, "", "", false, true, false, true);
            Assert4B(ContentVariation.Culture, "", "*", false, false, false, true);
            Assert4A(ContentVariation.Culture, "", "segment", false);
            Assert4B(ContentVariation.Culture, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Culture, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Culture, "*", "*", true, false, false, true);
            Assert4A(ContentVariation.Culture, "*", "segment", false);
            Assert4A(ContentVariation.Culture, "culture", null, true);
            Assert4A(ContentVariation.Culture, "culture", "", true);
            Assert4B(ContentVariation.Culture, "culture", "*", true, false, false, true);
            Assert4A(ContentVariation.Culture, "culture", "segment", false);

            #endregion

            #region Segment

            Assert4B(ContentVariation.Segment, null, null, true, true, true, true);
            Assert4B(ContentVariation.Segment, null, "", true, true, true, true);
            Assert4B(ContentVariation.Segment, null, "*", true, false, false, true);
            Assert4A(ContentVariation.Segment, null, "segment", true);
            Assert4B(ContentVariation.Segment, "", null, true, true, true, true);
            Assert4B(ContentVariation.Segment, "", "", true, true, true, true);
            Assert4B(ContentVariation.Segment, "", "*", true, false, false, true);
            Assert4A(ContentVariation.Segment, "", "segment", true);
            Assert4B(ContentVariation.Segment, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "*", true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "segment", true, false, false, true);
            Assert4A(ContentVariation.Segment, "culture", null, false);
            Assert4A(ContentVariation.Segment, "culture", "", false);
            Assert4A(ContentVariation.Segment, "culture", "*", false);
            Assert4A(ContentVariation.Segment, "culture", "segment", false);

            #endregion

            #region CultureAndSegment
            
            Assert4B(ContentVariation.CultureAndSegment, null, null, false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "*", false, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "segment", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", null, false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "*", false, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "segment", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", null, true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "*", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "segment", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", null, true, true, true, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "", true, true, true, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "*", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "segment", true, true, true, true);

            #endregion
        }

        private static void Assert4B(ContentVariation v, string culture, string segment, bool ew, bool nn, bool en, bool nw)
        {
            Assert.AreEqual(ew, v.ValidateVariation(culture, segment, true, true, false));
            Assert.AreEqual(nn, v.ValidateVariation(culture, segment, false, false, false));
            Assert.AreEqual(en, v.ValidateVariation(culture, segment, true, false, false));
            Assert.AreEqual(nw, v.ValidateVariation(culture, segment, false, true, false));
        }

        private static void Assert4A(ContentVariation v, string culture, string segment, bool xx)
        {
            Assert4B(v, culture, segment, xx, xx, xx, xx);
        }

        [Test]
        public void PropertyTests()
        {
            var propertyType = new PropertyType("editor", ValueStorageType.Nvarchar) { Alias = "prop" };
            var prop = new Property(propertyType);<commit_msg>Updates tests to rename assertion method parameters including code comments<commit_after>﻿using System;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;

namespace Umbraco.Tests.Models
{
    [TestFixture]
    public class VariationTests
    {
        [SetUp]
        public void SetUp()
        {
            // annoying, but content type wants short string helper ;(
            SettingsForTests.Reset();

            // well, this is also annoying, but...
            // validating a value is performed by its data editor,
            // based upon the configuration in the data type, so we
            // need to be able to retrieve them all...

            Current.Reset();

            var configs = new Configs();
            configs.Add(SettingsForTests.GetDefaultGlobalSettings);
            configs.Add(SettingsForTests.GetDefaultUmbracoSettings);

            var factory = Mock.Of<IFactory>();
            Current.Factory = factory;

            var dataEditors = new DataEditorCollection(new IDataEditor[]
            {
                new DataEditor(Mock.Of<ILogger>()) { Alias = "editor", ExplicitValueEditor = new DataValueEditor("view") }
            });
            var propertyEditors = new PropertyEditorCollection(dataEditors);

            var dataType = Mock.Of<IDataType>();
            Mock.Get(dataType)
                .Setup(x => x.Configuration)
                .Returns(null);

            var dataTypeService = Mock.Of<IDataTypeService>();
            Mock.Get(dataTypeService)
                .Setup(x => x.GetDataType(It.IsAny<int>()))
                .Returns<int>(x => dataType);

            var serviceContext = ServiceContext.CreatePartial(
                dataTypeService: dataTypeService,
                localizedTextService: Mock.Of<ILocalizedTextService>());

            Mock.Get(factory)
                .Setup(x => x.GetInstance(It.IsAny<Type>()))
                .Returns<Type>(x =>
                {
                    if (x == typeof(Configs)) return configs;
                    if (x == typeof(PropertyEditorCollection)) return propertyEditors;
                    if (x == typeof(ServiceContext)) return serviceContext;
                    if (x == typeof(ILocalizedTextService)) return serviceContext.LocalizationService;
                    throw new NotSupportedException(x.FullName);
                });
        }

        [Test]
        public void ValidateVariationTests()
        {

            // All tests:
            // 1. if exact is set to true: culture cannot be null when the ContentVariation.Culture flag is set
            // 2. if wildcards is set to false: fail when "*" is passed in as either culture or segment.
            // 3. ContentVariation flag is ignored when wildcards are used.
            // 4. Empty string is considered the same as null            

            #region Nothing

            Assert4A(ContentVariation.Nothing, null, null, true);
            Assert4A(ContentVariation.Nothing, null, "", true);
            Assert4B(ContentVariation.Nothing, null, "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, null, "segment", false);
            Assert4A(ContentVariation.Nothing, "", null, true);
            Assert4A(ContentVariation.Nothing, "", "", true);
            Assert4B(ContentVariation.Nothing, "", "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, "", "segment", false);
            Assert4B(ContentVariation.Nothing, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Nothing, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Nothing, "*", "*", true, false, false, true);
            Assert4A(ContentVariation.Nothing, "*", "segment", false);
            Assert4A(ContentVariation.Nothing, "culture", null, false);
            Assert4A(ContentVariation.Nothing, "culture", "", false);
            Assert4A(ContentVariation.Nothing, "culture", "*", false);
            Assert4A(ContentVariation.Nothing, "culture", "segment", false);

            #endregion

            #region Culture

            Assert4B(ContentVariation.Culture, null, null, false, true, false, true);
            Assert4B(ContentVariation.Culture, null, "", false, true, false, true);
            Assert4B(ContentVariation.Culture, null, "*", false, false, false, true);
            Assert4A(ContentVariation.Culture, null, "segment", false);
            Assert4B(ContentVariation.Culture, "", null, false, true, false, true);
            Assert4B(ContentVariation.Culture, "", "", false, true, false, true);
            Assert4B(ContentVariation.Culture, "", "*", false, false, false, true);
            Assert4A(ContentVariation.Culture, "", "segment", false);
            Assert4B(ContentVariation.Culture, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Culture, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Culture, "*", "*", true, false, false, true);
            Assert4A(ContentVariation.Culture, "*", "segment", false);
            Assert4A(ContentVariation.Culture, "culture", null, true);
            Assert4A(ContentVariation.Culture, "culture", "", true);
            Assert4B(ContentVariation.Culture, "culture", "*", true, false, false, true);
            Assert4A(ContentVariation.Culture, "culture", "segment", false);

            #endregion

            #region Segment

            Assert4B(ContentVariation.Segment, null, null, true, true, true, true);
            Assert4B(ContentVariation.Segment, null, "", true, true, true, true);
            Assert4B(ContentVariation.Segment, null, "*", true, false, false, true);
            Assert4A(ContentVariation.Segment, null, "segment", true);
            Assert4B(ContentVariation.Segment, "", null, true, true, true, true);
            Assert4B(ContentVariation.Segment, "", "", true, true, true, true);
            Assert4B(ContentVariation.Segment, "", "*", true, false, false, true);
            Assert4A(ContentVariation.Segment, "", "segment", true);
            Assert4B(ContentVariation.Segment, "*", null, true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "", true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "*", true, false, false, true);
            Assert4B(ContentVariation.Segment, "*", "segment", true, false, false, true);
            Assert4A(ContentVariation.Segment, "culture", null, false);
            Assert4A(ContentVariation.Segment, "culture", "", false);
            Assert4A(ContentVariation.Segment, "culture", "*", false);
            Assert4A(ContentVariation.Segment, "culture", "segment", false);

            #endregion

            #region CultureAndSegment
            
            Assert4B(ContentVariation.CultureAndSegment, null, null, false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "*", false, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, null, "segment", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", null, false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "*", false, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "", "segment", false, true, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", null, true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "*", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "*", "segment", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", null, true, true, true, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "", true, true, true, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "*", true, false, false, true);
            Assert4B(ContentVariation.CultureAndSegment, "culture", "segment", true, true, true, true);

            #endregion
        }

        /// <summary>
        /// Asserts the result of <see cref="ContentVariationExtensions.ValidateVariation(ContentVariation, string, string, bool, bool, bool)"/> 
        /// </summary>
        /// <param name="variation"></param>
        /// <param name="culture"></param>
        /// <param name="segment"></param>
        /// <param name="exactAndWildcards">Validate using Exact + Wildcards flags</param>
        /// <param name="nonExactAndNoWildcards">Validate using non Exact + no Wildcard flags</param>
        /// <param name="exactAndNoWildcards">Validate using Exact + no Wildcard flags</param>
        /// <param name="nonExactAndWildcards">Validate using non Exact + Wildcard flags</param>
        private static void Assert4B(ContentVariation variation, string culture, string segment,
            bool exactAndWildcards, bool nonExactAndNoWildcards, bool exactAndNoWildcards, bool nonExactAndWildcards)
        {
            Assert.AreEqual(exactAndWildcards, variation.ValidateVariation(culture, segment, true, true, false));
            Assert.AreEqual(nonExactAndNoWildcards, variation.ValidateVariation(culture, segment, false, false, false));
            Assert.AreEqual(exactAndNoWildcards, variation.ValidateVariation(culture, segment, true, false, false));
            Assert.AreEqual(nonExactAndWildcards, variation.ValidateVariation(culture, segment, false, true, false));
        }

        /// <summary>
        /// Asserts the result of <see cref="ContentVariationExtensions.ValidateVariation(ContentVariation, string, string, bool, bool, bool)"/>
        /// where expectedResult matches all combinations of Exact + Wildcard
        /// </summary>
        /// <param name="variation"></param>
        /// <param name="culture"></param>
        /// <param name="segment"></param>
        /// <param name="expectedResult"></param>
        private static void Assert4A(ContentVariation variation, string culture, string segment, bool expectedResult)
        {
            Assert4B(variation, culture, segment, expectedResult, expectedResult, expectedResult, expectedResult);
        }

        [Test]
        public void PropertyTests()
        {
            var propertyType = new PropertyType("editor", ValueStorageType.Nvarchar) { Alias = "prop" };
            var prop = new Property(propertyType);
{'fraction_non_alphanumeric': 0.12504917387883557, 'fraction_numerical': 0.006982690794649882, 'mean_word_length': 2.6387546967257114, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6260145', 'n_tokens_mistral': 5427, 'n_tokens_neox': 5295, 'n_words': 1306}