From 8fc09a892284f2692ec21a4632bc74ed48365d57 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Tue, 2 Oct 2018 13:56:27 -0400 Subject: Bring some tests over --- test/unit/attachment_test.rb | 57 +++++++++++++++ test/unit/content_test.rb | 124 +++++++++++++++++++++++++++----- test/unit/model_test.rb | 30 +++++--- test/unit/plain_text_conversion_test.rb | 92 ++++++++++++++++++++++++ test/unit/trix_attachment_test.rb | 81 +++++++++++++++++++++ 5 files changed, 357 insertions(+), 27 deletions(-) create mode 100644 test/unit/attachment_test.rb create mode 100644 test/unit/plain_text_conversion_test.rb create mode 100644 test/unit/trix_attachment_test.rb (limited to 'test/unit') diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb new file mode 100644 index 0000000000..dec2b8244d --- /dev/null +++ b/test/unit/attachment_test.rb @@ -0,0 +1,57 @@ +require 'test_helper' + +class ActionText::AttachmentTest < ActiveSupport::TestCase + test "from_attachable" do + attachment = ActionText::Attachment.from_attachable(attachable, caption: "Captioned") + assert_equal attachable, attachment.attachable + assert_equal "Captioned", attachment.caption + end + + test "proxies missing methods to attachable" do + attachable.instance_eval { def proxied; "proxied"; end } + attachment = ActionText::Attachment.from_attachable(attachable) + assert_equal "proxied", attachment.proxied + end + + test "proxies #to_param to attachable" do + attachment = ActionText::Attachment.from_attachable(attachable) + assert_equal attachable.to_param, attachment.to_param + end + + # test "converts to TrixAttachment" do + # attachment = attachment_from_html(%Q()) + # + # trix_attachment = attachment.to_trix_attachment + # assert_kind_of ActionText::TrixAttachment, trix_attachment + # + # assert_equal attachable.attachable_sgid, trix_attachment.attributes["sgid"] + # assert_equal attachable.attachable_content_type, trix_attachment.attributes["contentType"] + # assert_equal attachable.filename.to_s, trix_attachment.attributes["filename"] + # assert_equal attachable.byte_size, trix_attachment.attributes["filesize"] + # assert_equal "Captioned", trix_attachment.attributes["caption"] + # + # assert_nil trix_attachment.attributes["content"] + # end + + # test "converts to TrixAttachment with content" do + # attachment = attachment_from_html(%Q()) + # + # trix_attachment = attachment.to_trix_attachment + # assert_kind_of ActionText::TrixAttachment, trix_attachment + # + # assert_equal attachable.attachable_sgid, trix_attachment.attributes["sgid"] + # assert_equal attachable.attachable_content_type, trix_attachment.attributes["contentType"] + # + # assert_not_nil attachable.to_trix_content_attachment_partial_path + # assert_not_nil trix_attachment.attributes["content"] + # end + + private + def attachment_from_html(html) + ActionText::Content.new(html).attachments.first + end + + def attachable + @attachment ||= create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") + end +end diff --git a/test/unit/content_test.rb b/test/unit/content_test.rb index 59beb323cf..8f32a85110 100644 --- a/test/unit/content_test.rb +++ b/test/unit/content_test.rb @@ -1,21 +1,107 @@ -require_relative '../test_helper' - -module ActionText - class ContentTest < ActiveSupport::TestCase - test "plain text conversion" do - message = Message.new(subject: "Greetings", content: "

Hello world

") - assert_equal "Hello world", message.content.body.to_plain_text - end - - test "without content" do - message = Message.create!(subject: "Greetings") - assert message.content.body.nil? - end - - test "embed extraction" do - blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") - message = Message.create!(subject: "Greetings", content: ActionText::Content.new("Hello world").append_attachables(blob)) - assert_equal "racecar.jpg", message.content.embeds.first.filename.to_s - end +require 'test_helper' + +class ActionText::ContentTest < ActiveSupport::TestCase + test "equality" do + html = %Q(
test
) + content = ActionText::Content.new(html) + assert_equal content, ActionText::Content.new(html) + assert_not_equal content, html + end + + test "marshal serialization" do + content = ActionText::Content.new("Hello!") + assert_equal content, Marshal.load(Marshal.dump(content)) + end + + test "roundtrips HTML without additional newlines" do + html = %Q(
a
) + content = ActionText::Content.new(html) + assert_equal html, content.to_html end + + test "extracts links" do + html = %Q(1
1) + content = ActionText::Content.new(html) + assert_equal ["http://example.com/1"], content.links + end + + test "extracts attachables" do + attachable = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") + html = %Q() + + content = ActionText::Content.new(html) + assert_equal 1, content.attachments.size + + attachment = content.attachments.first + assert_equal "Captioned", attachment.caption + assert_equal attachable, attachment.attachable + end + + test "extracts remote image attachables" do + html = %Q() + + content = ActionText::Content.new(html) + assert_equal 1, content.attachments.size + + attachment = content.attachments.first + assert_equal "Captioned", attachment.caption + + attachable = attachment.attachable + assert_kind_of ActionText::Attachables::RemoteImage, attachable + assert_equal "http://example.com/cat.jpg", attachable.url + assert_equal "100", attachable.width + assert_equal "100", attachable.height + end + + test "identifies destroyed attachables as missing" do + attachable = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") + html = %Q() + attachable.destroy! + content = ActionText::Content.new(html) + assert_equal 1, content.attachments.size + assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable + end + + # test "extracts missing attachables" do + # html = %Q() + # content = ActionText::Content.new(html) + # assert_equal 1, content.attachments.size + # assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable + # end + # + # test "converts Trix-formatted attachments" do + # html = %Q(
) + # content = ActionText::Content.new(html) + # assert_equal 1, content.attachments.size + # assert_equal %Q(), content.to_html + # end + # + # test "ignores Trix-formatted attachments with malformed JSON" do + # html = %Q(
) + # content = ActionText::Content.new(html) + # assert_equal 0, content.attachments.size + # end + # + # test "minifies attachment markup" do + # html = %Q(
HTML
) + # assert_equal %Q(), ActionText::Content.new(html).to_html + # end + # + # test "canonicalizes attachment gallery markup" do + # attachment_html = %Q() + # html = %Q() + # assert_equal %Q(
#{attachment_html}
), ActionText::Content.new(html).to_html + # end + # + # test "canonicalizes attachment gallery markup with whitespace" do + # attachment_html = %Q(\n \n \n) + # html = %Q() + # assert_equal %Q(
#{attachment_html}
), ActionText::Content.new(html).to_html + # end + # + # test "canonicalizes nested attachment gallery markup" do + # attachment_html = %Q() + # html = %Q(
) + # assert_equal %Q(
#{attachment_html}
), ActionText::Content.new(html).to_html + # end end diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index b7c5333c4d..bdfe88adc8 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -1,10 +1,24 @@ -require_relative '../test_helper' - -module ActionText - class ModelTest < ActiveSupport::TestCase - test "saving content" do - message = Message.create!(subject: "Greetings", content: "

Hello world

") - assert_equal "Hello world", message.content.body.to_plain_text - end +require 'test_helper' + +class ActionText::ModelTest < ActiveSupport::TestCase + test "plain text conversion" do + message = Message.new(subject: "Greetings", content: "

Hello world

") + assert_equal "Hello world", message.content.body.to_plain_text + end + + test "without content" do + message = Message.create!(subject: "Greetings") + assert message.content.body.nil? + end + + test "embed extraction" do + blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") + message = Message.create!(subject: "Greetings", content: ActionText::Content.new("Hello world").append_attachables(blob)) + assert_equal "racecar.jpg", message.content.embeds.first.filename.to_s + end + + test "saving content" do + message = Message.create!(subject: "Greetings", content: "

Hello world

") + assert_equal "Hello world", message.content.body.to_plain_text end end diff --git a/test/unit/plain_text_conversion_test.rb b/test/unit/plain_text_conversion_test.rb new file mode 100644 index 0000000000..47c95b6bc3 --- /dev/null +++ b/test/unit/plain_text_conversion_test.rb @@ -0,0 +1,92 @@ +require 'test_helper' + +class ActionText::PlainTextConversionTest < ActiveSupport::TestCase + test "

tags are separated by two new lines" do + assert_converted_to( + "Hello world!\n\nHow are you?", + "

Hello world!

How are you?

" + ) + end + + test "
tags are separated by two new lines" do + assert_converted_to( + "“Hello world!”\n\n“How are you?”", + "
Hello world!
How are you?
" + ) + end + + test "
    tags are separated by two new lines" do + assert_converted_to( + "Hello world!\n\n1. list1\n\n1. list2\n\nHow are you?", + "

    Hello world!

    1. list1
    1. list2

    How are you?

    " + ) + end + + test "
      tags are separated by two new lines" do + assert_converted_to( + "Hello world!\n\n• list1\n\n• list2\n\nHow are you?", + "

      Hello world!

      • list1
      • list2

      How are you?

      " + ) + end + + test "

      tags are separated by two new lines" do + assert_converted_to( + "Hello world!\n\nHow are you?", + "

      Hello world!

      How are you?
      " + ) + end + + test "
    • tags are separated by one new line" do + assert_converted_to( + "• one\n• two\n• three", + "
      • one
      • two
      • three
      " + ) + end + + test "
    • tags without a parent list" do + assert_converted_to( + "• one\n• two\n• three", + "
    • one
    • two
    • three
    • " + ) + end + + test "
      tags are separated by one new line" do + assert_converted_to( + "Hello world!\none\ntwo\nthree", + "

      Hello world!
      one
      two
      three

      " + ) + end + + test "
      tags are separated by one new line" do + assert_converted_to( + "Hello world!\nHow are you?", + "
      Hello world!
      How are you?
      " + ) + end + + test " tags are converted to their plain-text representation" do + assert_converted_to( + "Hello world! [Cat]", + %Q(Hello world! ) + ) + end + + test "preserves non-linebreak whitespace after text" do + assert_converted_to( + "Hello world!", + %Q(
      Hello world!
      ) + ) + end + + test "preserves trailing linebreaks after text" do + assert_converted_to( + "Hello\nHow are you?", + "Hello
      How are you?" + ) + end + + private + def assert_converted_to(plain_text, html) + assert_equal plain_text, ActionText::Content.new(html).to_plain_text + end +end diff --git a/test/unit/trix_attachment_test.rb b/test/unit/trix_attachment_test.rb new file mode 100644 index 0000000000..fe834c8663 --- /dev/null +++ b/test/unit/trix_attachment_test.rb @@ -0,0 +1,81 @@ +require 'test_helper' + +class ActionText::TrixAttachmentTest < ActiveSupport::TestCase + test "from_attributes" do + attributes = { + "data-trix-attachment" => { + "sgid" => "123", + "contentType" => "text/plain", + "href" => "http://example.com/", + "filename" => "example.txt", + "filesize" => 12345, + "previewable" => true + }, + "data-trix-attributes" => { + "caption" => "hello" + } + } + + attachment = attachment( + sgid: "123", + content_type: "text/plain", + href: "http://example.com/", + filename: "example.txt", + filesize: "12345", + previewable: "true", + caption: "hello" + ) + + assert_attachment_json_attributes(attachment, attributes) + end + + test "previewable is typecast" do + assert_attachment_attribute(attachment(previewable: ""), "previewable", false) + assert_attachment_attribute(attachment(previewable: false), "previewable", false) + assert_attachment_attribute(attachment(previewable: "false"), "previewable", false) + assert_attachment_attribute(attachment(previewable: "garbage"), "previewable", false) + assert_attachment_attribute(attachment(previewable: true), "previewable", true) + assert_attachment_attribute(attachment(previewable: "true"), "previewable", true) + end + + test "filesize is typecast when integer-like" do + assert_attachment_attribute(attachment(filesize: 123), "filesize", 123) + assert_attachment_attribute(attachment(filesize: "123"), "filesize", 123) + assert_attachment_attribute(attachment(filesize: "3.5 MB"), "filesize", "3.5 MB") + assert_attachment_attribute(attachment(filesize: nil), "filesize", nil) + assert_attachment_attribute(attachment(filesize: ""), "filesize", "") + end + + test "#attributes strips unmappable attributes" do + attributes = { + "sgid" => "123", + "caption" => "hello" + } + + attachment = attachment(sgid: "123", caption: "hello", nonexistent: "garbage") + assert_attachment_attributes(attachment, attributes) + end + + def assert_attachment_attribute(attachment, name, value) + if value.nil? + assert_nil(attachment.attributes[name]) + else + assert_equal(value, attachment.attributes[name]) + end + end + + def assert_attachment_attributes(attachment, attributes) + assert_equal(attributes, attachment.attributes) + end + + def assert_attachment_json_attributes(attachment, attributes) + attributes.each do |name, expected| + actual = JSON.parse(attachment.node[name]) + assert_equal(expected, actual) + end + end + + def attachment(**attributes) + ActionText::TrixAttachment.from_attributes(attributes) + end +end -- cgit v1.2.3