aboutsummaryrefslogtreecommitdiffstats
path: root/actiontext/test/unit
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2019-01-04 19:43:11 -0500
committerGeorge Claghorn <george@basecamp.com>2019-01-04 22:22:49 -0500
commit0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93 (patch)
tree91b8ddcf86b15356cd8d3dee235bf53a1778572b /actiontext/test/unit
parent8a23a0e8c20c0cccf0073906d7dd7f809bfa836d (diff)
parentcfe4674d3637c746cdb3c2b5131e2de498775529 (diff)
downloadrails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.tar.gz
rails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.tar.bz2
rails-0decd2ddc4a94cf522fc8ea8e6c73b9deedfdd93.zip
Import Action Text
Diffstat (limited to 'actiontext/test/unit')
-rw-r--r--actiontext/test/unit/attachment_test.rb60
-rw-r--r--actiontext/test/unit/content_test.rb116
-rw-r--r--actiontext/test/unit/model_test.rb47
-rw-r--r--actiontext/test/unit/plain_text_conversion_test.rb94
-rw-r--r--actiontext/test/unit/trix_attachment_test.rb83
5 files changed, 400 insertions, 0 deletions
diff --git a/actiontext/test/unit/attachment_test.rb b/actiontext/test/unit/attachment_test.rb
new file mode 100644
index 0000000000..65e9d69e84
--- /dev/null
+++ b/actiontext/test/unit/attachment_test.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+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(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>))
+
+ 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
+ attachable = Person.create! name: "Javan"
+ attachment = attachment_from_html(%Q(<action-text-attachment sgid="#{attachable.attachable_sgid}"></action-text-attachment>))
+
+ 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/actiontext/test/unit/content_test.rb b/actiontext/test/unit/content_test.rb
new file mode 100644
index 0000000000..f77f48df14
--- /dev/null
+++ b/actiontext/test/unit/content_test.rb
@@ -0,0 +1,116 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class ActionText::ContentTest < ActiveSupport::TestCase
+ test "equality" do
+ html = "<div>test</div>"
+ content = content_from_html(html)
+ assert_equal content, content_from_html(html)
+ assert_not_equal content, html
+ end
+
+ test "marshal serialization" do
+ content = content_from_html("Hello!")
+ assert_equal content, Marshal.load(Marshal.dump(content))
+ end
+
+ test "roundtrips HTML without additional newlines" do
+ html = "<div>a<br></div>"
+ content = content_from_html(html)
+ assert_equal html, content.to_html
+ end
+
+ test "extracts links" do
+ html = '<a href="http://example.com/1">1</a><br><a href="http://example.com/1">1</a>'
+ content = content_from_html(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(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>)
+
+ content = content_from_html(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 = '<action-text-attachment content-type="image" url="http://example.com/cat.jpg" width="100" height="100" caption="Captioned"></action-text-attachment>'
+
+ content = content_from_html(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(<action-text-attachment sgid="#{attachable.attachable_sgid}"></action-text-attachment>)
+ attachable.destroy!
+ content = content_from_html(html)
+ assert_equal 1, content.attachments.size
+ assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable
+ end
+
+ test "extracts missing attachables" do
+ html = '<action-text-attachment sgid="missing"></action-text-attachment>'
+ content = content_from_html(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(<figure data-trix-attachment='{"sgid":"123","contentType":"text/plain","width":100,"height":100}' data-trix-attributes='{"caption":"Captioned"}'></figure>)
+ content = content_from_html(html)
+ assert_equal 1, content.attachments.size
+ assert_equal '<action-text-attachment sgid="123" content-type="text/plain" width="100" height="100" caption="Captioned"></action-text-attachment>', content.to_html
+ end
+
+ test "ignores Trix-formatted attachments with malformed JSON" do
+ html = %Q(<div data-trix-attachment='{"sgid":"garbage...'></div>)
+ content = content_from_html(html)
+ assert_equal 0, content.attachments.size
+ end
+
+ test "minifies attachment markup" do
+ html = '<action-text-attachment sgid="123"><div>HTML</div></action-text-attachment>'
+ assert_equal '<action-text-attachment sgid="123"></action-text-attachment>', content_from_html(html).to_html
+ end
+
+ test "canonicalizes attachment gallery markup" do
+ attachment_html = '<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>'
+ html = %Q(<div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div>)
+ assert_equal "<div>#{attachment_html}</div>", content_from_html(html).to_html
+ end
+
+ test "canonicalizes attachment gallery markup with whitespace" do
+ attachment_html = %Q(\n <action-text-attachment sgid="1" presentation="gallery"></action-text-attachment>\n <action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>\n)
+ html = %Q(<div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div>)
+ assert_equal "<div>#{attachment_html}</div>", content_from_html(html).to_html
+ end
+
+ test "canonicalizes nested attachment gallery markup" do
+ attachment_html = '<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>'
+ html = %Q(<blockquote><div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div></blockquote>)
+ assert_equal "<blockquote><div>#{attachment_html}</div></blockquote>", content_from_html(html).to_html
+ end
+
+ private
+ def content_from_html(html)
+ ActionText::Content.new(html).tap do |content|
+ assert_nothing_raised { content.to_s }
+ end
+ end
+end
diff --git a/actiontext/test/unit/model_test.rb b/actiontext/test/unit/model_test.rb
new file mode 100644
index 0000000000..122a20700b
--- /dev/null
+++ b/actiontext/test/unit/model_test.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class ActionText::ModelTest < ActiveSupport::TestCase
+ test "html conversion" do
+ message = Message.new(subject: "Greetings", content: "<h1>Hello world</h1>")
+ assert_equal %Q(<div class="trix-content">\n <h1>Hello world</h1>\n</div>\n), "#{message.content}"
+ end
+
+ test "plain text conversion" do
+ message = Message.new(subject: "Greetings", content: "<h1>Hello world</h1>")
+ assert_equal "Hello world", message.content.to_plain_text
+ end
+
+ test "without content" do
+ message = Message.create!(subject: "Greetings")
+ assert message.content.nil?
+ assert message.content.blank?
+ assert message.content.empty?
+ assert_not message.content.present?
+ end
+
+ test "with blank content" do
+ message = Message.create!(subject: "Greetings", content: "")
+ assert_not message.content.nil?
+ assert message.content.blank?
+ assert message.content.empty?
+ assert_not message.content.present?
+ 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: "<h1>Hello world</h1>")
+ assert_equal "Hello world", message.content.to_plain_text
+ end
+
+ test "save body" do
+ message = Message.create(subject: "Greetings", body: "<h1>Hello world</h1>")
+ assert_equal "Hello world", message.body.to_plain_text
+ end
+end
diff --git a/actiontext/test/unit/plain_text_conversion_test.rb b/actiontext/test/unit/plain_text_conversion_test.rb
new file mode 100644
index 0000000000..53a1029caf
--- /dev/null
+++ b/actiontext/test/unit/plain_text_conversion_test.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class ActionText::PlainTextConversionTest < ActiveSupport::TestCase
+ test "<p> tags are separated by two new lines" do
+ assert_converted_to(
+ "Hello world!\n\nHow are you?",
+ "<p>Hello world!</p><p>How are you?</p>"
+ )
+ end
+
+ test "<blockquote> tags are separated by two new lines" do
+ assert_converted_to(
+ "“Hello world!”\n\n“How are you?”",
+ "<blockquote>Hello world!</blockquote><blockquote>How are you?</blockquote>"
+ )
+ end
+
+ test "<ol> tags are separated by two new lines" do
+ assert_converted_to(
+ "Hello world!\n\n1. list1\n\n1. list2\n\nHow are you?",
+ "<p>Hello world!</p><ol><li>list1</li></ol><ol><li>list2</li></ol><p>How are you?</p>"
+ )
+ end
+
+ test "<ul> tags are separated by two new lines" do
+ assert_converted_to(
+ "Hello world!\n\n• list1\n\n• list2\n\nHow are you?",
+ "<p>Hello world!</p><ul><li>list1</li></ul><ul><li>list2</li></ul><p>How are you?</p>"
+ )
+ end
+
+ test "<h1> tags are separated by two new lines" do
+ assert_converted_to(
+ "Hello world!\n\nHow are you?",
+ "<h1>Hello world!</h1><div>How are you?</div>"
+ )
+ end
+
+ test "<li> tags are separated by one new line" do
+ assert_converted_to(
+ "• one\n• two\n• three",
+ "<ul><li>one</li><li>two</li><li>three</li></ul>"
+ )
+ end
+
+ test "<li> tags without a parent list" do
+ assert_converted_to(
+ "• one\n• two\n• three",
+ "<li>one</li><li>two</li><li>three</li>"
+ )
+ end
+
+ test "<br> tags are separated by one new line" do
+ assert_converted_to(
+ "Hello world!\none\ntwo\nthree",
+ "<p>Hello world!<br>one<br>two<br>three</p>"
+ )
+ end
+
+ test "<div> tags are separated by one new line" do
+ assert_converted_to(
+ "Hello world!\nHow are you?",
+ "<div>Hello world!</div><div>How are you?</div>"
+ )
+ end
+
+ test "<action-text-attachment> tags are converted to their plain-text representation" do
+ assert_converted_to(
+ "Hello world! [Cat]",
+ 'Hello world! <action-text-attachment url="http://example.com/cat.jpg" content-type="image" caption="Cat"></action-text-attachment>'
+ )
+ end
+
+ test "preserves non-linebreak whitespace after text" do
+ assert_converted_to(
+ "Hello world!",
+ "<div><strong>Hello </strong>world!</div>"
+ )
+ end
+
+ test "preserves trailing linebreaks after text" do
+ assert_converted_to(
+ "Hello\nHow are you?",
+ "<strong>Hello<br></strong>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/actiontext/test/unit/trix_attachment_test.rb b/actiontext/test/unit/trix_attachment_test.rb
new file mode 100644
index 0000000000..81d015750e
--- /dev/null
+++ b/actiontext/test/unit/trix_attachment_test.rb
@@ -0,0 +1,83 @@
+# frozen_string_literal: true
+
+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