From 111fe48cc33d2c5a12aaf024ca1f7ec5b101cb1f Mon Sep 17 00:00:00 2001
From: Katsuya Hidaka <hidakatsuya@gmail.com>
Date: Sun, 28 Apr 2024 13:21:25 +0900
Subject: [PATCH 1/2] Add tests for current alt attribute behavior in images

---
 test/helpers/application_helper_test.rb       | 33 +++++++++++++++++++
 .../common_mark/application_helper_test.rb    | 28 ++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb
index cdbad8bc8..443ad4698 100644
--- a/test/helpers/application_helper_test.rb
+++ b/test/helpers/application_helper_test.rb
@@ -198,6 +198,39 @@ class ApplicationHelperTest < Redmine::HelperTest
     end
   end
 
+  def test_attached_image_alt_attribute_with_textile
+    attachments = Attachment.all
+    with_settings text_formatting: 'textile' do
+      # When alt text is set
+      assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?" title="alt text" alt="alt text" />],
+        textilizable('!logo.gif(alt text)!', attachments: attachments)
+
+      # When alt text and style are set
+      assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?" style="width:100px;" title="alt text" alt="alt text" />],
+        textilizable('!{width:100px}logo.gif(alt text)!', attachments: attachments)
+
+      # When alt text is not set
+      assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?" />],
+        textilizable('!logo.gif!', attachments: attachments)
+
+      # When alt text is not set and the attachment has no description
+      assert_match %r[<img src=".+?" alt="" loading=".+?" />],
+        textilizable('!testfile.PNG!', attachments: attachments)
+
+      # When no matching attachments are found
+      assert_match %r[<img src=".+?" alt="" />],
+        textilizable('!no-match.jpg!', attachments: attachments)
+      assert_match %r[<img src=".+?" alt="alt text" />],
+        textilizable('!no-match.jpg(alt text)!', attachments: attachments)
+
+      # When no attachment is registered
+      assert_match %r[<img src=".+?" alt="" />],
+        textilizable('!logo.gif!', attachments: [])
+      assert_match %r[<img src=".+?" alt="alt text" />],
+        textilizable('!logo.gif(alt text)!', attachments: [])
+    end
+  end
+
   def test_attached_images_on_issue
     issue = Issue.generate!
     attachment_1 = Attachment.generate!(:file => mock_file_with_options(:original_filename => "attached_on_issue.png"), :container => issue)
diff --git a/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb b/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
index b5c79335b..c198f64cd 100644
--- a/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
+++ b/test/unit/lib/redmine/wiki_formatting/common_mark/application_helper_test.rb
@@ -62,5 +62,33 @@ class Redmine::WikiFormatting::CommonMark::ApplicationHelperTest < Redmine::Help
       end
     end
 
+    def test_attached_image_alt_attribute_with_madkrown
+      attachments = Attachment.all
+      with_settings text_formatting: 'common_mark' do
+        # When alt text is set
+        assert_match %r[<img src=".+?" alt="alt text" loading=".+?">],
+          textilizable('![alt text](logo.gif)', attachments: attachments)
+
+        # When alt text is not set
+        assert_match %r[<img src=".+?" title="This is a logo" alt="This is a logo" loading=".+?">],
+          textilizable('![](logo.gif)', attachments: attachments)
+
+        # When alt text is not set and the attachment has no description
+        assert_match %r[<img src=".+?" alt="" loading=".+?">],
+          textilizable('![](testfile.PNG)', attachments: attachments)
+
+        # When no matching attachments are found
+        assert_match %r[<img src=".+?" alt="">],
+          textilizable('![](no-match.jpg)', attachments: attachments)
+        assert_match %r[<img src=".+?" alt="alt text">],
+          textilizable('![alt text](no-match.jpg)', attachments: attachments)
+
+        # When no attachment is registered
+        assert_match %r[<img src=".+?" alt="">],
+          textilizable('![](logo.gif)', attachments: [])
+        assert_match %r[<img src=".+?" alt="alt text">],
+          textilizable('![alt text](logo.gif)', attachments: [])
+      end
+    end
   end
 end
-- 
2.43.2

