aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2018-10-02 13:56:27 -0400
committerJavan Makhmali <javan@javan.us>2018-10-02 13:56:27 -0400
commit8fc09a892284f2692ec21a4632bc74ed48365d57 (patch)
tree3a31a959395f9a2a54e206a063a81367cf01a84f /test
parentdea30ecd0bf4137e8c75976499df3fdddde8f844 (diff)
downloadrails-8fc09a892284f2692ec21a4632bc74ed48365d57.tar.gz
rails-8fc09a892284f2692ec21a4632bc74ed48365d57.tar.bz2
rails-8fc09a892284f2692ec21a4632bc74ed48365d57.zip
Bring some tests over
Diffstat (limited to 'test')
-rw-r--r--test/unit/attachment_test.rb57
-rw-r--r--test/unit/content_test.rb124
-rw-r--r--test/unit/model_test.rb30
-rw-r--r--test/unit/plain_text_conversion_test.rb92
-rw-r--r--test/unit/trix_attachment_test.rb81
5 files changed, 357 insertions, 27 deletions
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(<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
+ # 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/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: "<h1>Hello world</h1>")
- 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(<div>test</div>)
+ 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(<div>a<br></div>)
+ content = ActionText::Content.new(html)
+ assert_equal html, content.to_html
end
+
+ test "extracts links" do
+ html = %Q(<a href="http://example.com/1">1</a><br><a href="http://example.com/1">1</a>)
+ 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(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>)
+
+ 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(<action-text-attachment content-type="image" url="http://example.com/cat.jpg" width="100" height="100" caption="Captioned"></action-text-attachment>)
+
+ 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(<action-text-attachment sgid="#{attachable.attachable_sgid}"></action-text-attachment>)
+ 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(<action-text-attachment sgid="missing"></action-text-attachment>)
+ # 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(<figure data-trix-attachment='{"sgid":"123","contentType":"text/plain","width":100,"height":100}' data-trix-attributes='{"caption":"Captioned"}'></figure>)
+ # content = ActionText::Content.new(html)
+ # assert_equal 1, content.attachments.size
+ # assert_equal %Q(<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 = ActionText::Content.new(html)
+ # assert_equal 0, content.attachments.size
+ # end
+ #
+ # test "minifies attachment markup" do
+ # html = %Q(<action-text-attachment sgid="123"><div>HTML</div></action-text-attachment>)
+ # assert_equal %Q(<action-text-attachment sgid="123"></action-text-attachment>), ActionText::Content.new(html).to_html
+ # end
+ #
+ # test "canonicalizes attachment gallery markup" do
+ # attachment_html = %Q(<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 %Q(<div>#{attachment_html}</div>), ActionText::Content.new(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 %Q(<div>#{attachment_html}</div>), ActionText::Content.new(html).to_html
+ # end
+ #
+ # test "canonicalizes nested attachment gallery markup" do
+ # attachment_html = %Q(<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 %Q(<blockquote><div>#{attachment_html}</div></blockquote>), 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: "<h1>Hello world</h1>")
- 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: "<h1>Hello world</h1>")
+ 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: "<h1>Hello world</h1>")
+ 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 "<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]",
+ %Q(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!",
+ %Q(<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/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