public void TODAY_with_format()
        {
            // Arrange
            var content = "The current year is: [TODAY |yyyy]";

            // Act
            var result = CakeMailContentParser.Parse(content, null);

            // Assert
            result.ShouldBe($"The current year is: {DateTime.UtcNow.Year}");
        }
        public void TODAY_without_format()
        {
            // Arrange
            var content = "The current date is: [TODAY]";

            // Act
            var result = CakeMailContentParser.Parse(content, null);

            // Assert
            Assert.StartsWith("The current date is: ", content);
        }
        public void Returns_empty_string_when_content_is_empty_string()
        {
            // Arrange
            var content = string.Empty;

            // Act
            var result = CakeMailContentParser.Parse(content, null);

            // Assert
            result.ShouldBeEmpty();
        }
        public void Field_without_fallback_is_successfully_merged_when_data_provided()
        {
            // Arrange
            var content = "Dear [firstname]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", "Bob" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Dear Bob");
        }
        public void IF_false_without_ELSE()
        {
            // Arrange
            var content = "[IF `firstname` = \"Robert\"]Yes[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", "Bob" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBeEmpty();
        }
        public void IF_true()
        {
            // Arrange
            var content = "[IF `firstname` = \"Bob\"]Yes[ELSE]No[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", "Bob" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Yes");
        }
        public void Double_with_format()
        {
            // Arrange
            var content = "We drove [miles_driven|0.00] miles today";
            var data    = new Dictionary <string, object>
            {
                { "miles_driven", (double)12.345 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("We drove 12.35 miles today");
        }
        public void Long_with_format()
        {
            // Arrange
            var content = "We drove [miles_driven|#,#] miles during this trip";
            var data    = new Dictionary <string, object>
            {
                { "miles_driven", (long)12345 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("We drove 12,345 miles during this trip");
        }
        public void IF_false_with_ELSEIF_false()
        {
            // Arrange
            var content = "[IF `firstname` = \"Robert\"]Yes - Robert[ELSEIF `firstname` = \"Bob\"]Yes - Bob[ELSE]No[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", "Jim" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("No");
        }
        public void Datetime_with_format()
        {
            // Arrange
            var content = "Thank you for being a customer since [customer_since | yyyy]";
            var data    = new Dictionary <string, object>
            {
                { "customer_since", new DateTime(2017, 4, 30, 4, 57, 0) }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Thank you for being a customer since 2017");
        }
        public void Fallback_is_merged_when_null_value_is_specified()
        {
            // Arrange
            var content = "Dear [firstname,friend]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", null }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Dear friend");
        }
        public void Numeric_CAKEMAIL_double()
        {
            // Arrange
            var content = "[IF `amount` == 2.0]Yes[ELSE]No[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "amount", Math.Sqrt(2) * Math.Sqrt(2) }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Yes");
        }
        public void Fallback_is_merged_when_data_is_omitted()
        {
            // Arrange
            var content = "Dear [firstname,friend]";
            var data    = new Dictionary <string, object>
            {
                { "lastname", "Smith" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Dear friend");
        }
        public void Numeric_CAKEMAIL_BUG_FIX_true()
        {
            // Arrange
            var content = "[IF `age` >= 18]Adult[ELSE]Minor[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "age", 18 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Adult");
        }
        public void Numeric_true()
        {
            // Arrange
            var content = "[IF `age` > \"18\"]Yes[ELSE]No[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "age", 25 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Yes");
        }
        public void Two_conditions_one_true_one_false_OR()
        {
            // Arrange
            var content = "[IF `firstname` = \"Jim\" OR `lastname` = \"Halpert\"]Yes[ELSE]No[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "firstname", "John" },
                { "lastname", "Halpert" }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Yes");
        }
        public void Numeric_CAKEMAIL_BUG_Fix_false()
        {
            // The CakeMail.RestClient allows comparing merge fields with numeric values (e.g.: [ IF `age` >= 18]content for adults[ELSE]content for minors[ENDIF])
            // which solves the problem I describe in the Numeric_CAKEMAIL_BUG unit test.

            // Arrange
            var content = "[IF `age` >= 18]Adult[ELSE]Minor[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "age", 9 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Minor");
        }
Example #18
0
        /// <summary>
        /// Send a one-off email. Track opens and clicks.
        /// </summary>
        /// <param name="userKey">User Key of the user who initiates the call.</param>
        /// <param name="trackingId">ID for tracking purposes.</param>
        /// <param name="recipientEmailAddress">The email address of the recipient.</param>
        /// <param name="subject">Subject of the relay</param>
        /// <param name="html">HTML content of the relay.</param>
        /// <param name="text">Text content of the relay.</param>
        /// <param name="senderEmail">Email address of the sender of the relay.</param>
        /// <param name="senderName">Name of the sender of the relay.</param>
        /// <param name="mergeData">Data to be merged into the content of the email</param>
        /// <param name="encoding">Encoding to be used for the relay. Possible values: 'utf-8', 'iso-8859-x'</param>
        /// <param name="clientId">Client ID of the client in which the relay is located.</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>True if the email is sent</returns>
        public Task <bool> SendWithTrackingAsync(string userKey, long trackingId, string recipientEmailAddress, string subject, string html, string text, string senderEmail, string senderName = null, IDictionary <string, object> mergeData = null, MessageEncoding?encoding = null, long?clientId = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            subject = CakeMailContentParser.Parse(subject, mergeData);
            html    = CakeMailContentParser.Parse(html, mergeData);
            text    = CakeMailContentParser.Parse(text, mergeData);

            var parameters = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("user_key", userKey),
                new KeyValuePair <string, object>("tracking_id", trackingId),
                new KeyValuePair <string, object>("email", recipientEmailAddress),
                new KeyValuePair <string, object>("subject", subject),
                new KeyValuePair <string, object>("html_message", html),
                new KeyValuePair <string, object>("text_message", text),
                new KeyValuePair <string, object>("sender_email", senderEmail),
                new KeyValuePair <string, object>("track_opening", "true"),
                new KeyValuePair <string, object>("track_clicks_in_html", "true"),
                new KeyValuePair <string, object>("track_clicks_in_text", "true")
            };

            if (senderName != null)
            {
                parameters.Add(new KeyValuePair <string, object>("sender_name", senderName));
            }
            if (encoding.HasValue)
            {
                parameters.Add(new KeyValuePair <string, object>("encoding", encoding.Value.GetEnumMemberValue()));
            }
            if (clientId.HasValue)
            {
                parameters.Add(new KeyValuePair <string, object>("client_id", clientId.Value));
            }

            return(_client
                   .PostAsync("Relay/Send")
                   .WithFormUrlEncodedBody(parameters)
                   .WithCancellationToken(cancellationToken)
                   .AsCakeMailObject <bool>());
        }
        public void Numeric_CAKEMAIL_BUG()
        {
            // CakeMail only allows comparing merge fields with string values (e.g.:[ IF `age` >= "18"]content for adults[ELSE]content for minors[ENDIF])
            // which does not work properly in some cases. Let's say we want to display display content for adults aged 18 or over and different content
            // for minors and let's say you have a recipient age 9. We will treat 9 as a string (per the CakeMail logic) and compare it to the string "18".
            // Well... guess what: the character "9" is greater than the character "1" (which is the first character in the string "18") and therefore this
            // person who is clearly underage would receive an email with content intended to adults. Conversely, if you have a recipient age 100, this
            // person would receive an email with the content for minors because "0" (the second character in the string "100") is smaller than "8" (the
            // second character in the string "18").

            // Arrange
            var content = "[IF `age` > \"18\"]Adult[ELSE]Minor[ENDIF]";
            var data    = new Dictionary <string, object>
            {
                { "age", 9 }
            };

            // Act
            var result = CakeMailContentParser.Parse(content, data);

            // Assert
            result.ShouldBe("Adult");
        }