Project

General

Profile

Defect #30441 » attachments-unicode-case.diff

fix and tests - Evgeny Seliverstov, 2019-01-16 13:47

View differences:

app/helpers/application_helper.rb Wed Jan 16 07:20:49 2019 +0000 → app/helpers/application_helper.rb Wed Jan 16 15:40:26 2019 +0300
724 724
    attachments += obj.attachments if obj.respond_to?(:attachments)
725 725
    if attachments.present?
726 726
      text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
727
        filename, ext, alt, alttext = $1.downcase, $2, $3, $4
727
        filename, ext, alt, alttext = $1, $2, $3, $4
728 728
        # search for the picture in attachments
729 729
        if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
730 730
          image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
test/fixtures/attachments.yml Wed Jan 16 07:20:49 2019 +0000 → test/fixtures/attachments.yml Wed Jan 16 15:40:26 2019 +0300
268 268
  filename: root_attachment.txt
269 269
  filesize: 54
270 270
  author_id: 2
271
attachments_021:
272
  content_type: image/png
273
  downloads: 0
274
  created_on: 2011-02-23 16:14:50 +09:00
275
  disk_filename: 101223161450_testfile_2.png
276
  disk_directory: "2010/12"
277
  container_id: 14
278
  digest: 6bc2963e8d7ea0d3e68d12d1fba3d6ca
279
  id: 21
280
  container_type: Issue
281
  description: ""
282
  filename: TestТест.png
283
  filesize: 3582
284
  author_id: 2
test/unit/attachment_test.rb Wed Jan 16 07:20:49 2019 +0000 → test/unit/attachment_test.rb Wed Jan 16 15:40:26 2019 +0300
410 410
    set_tmp_attachments_directory
411 411
  end
412 412

  
413
  def test_latest_should_get_with_unicode_downcase
414
    set_fixtures_attachments_directory
415

  
416
    # first unicode letter (\xd0\xa2) is uppercase
417
    string = "Test\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82.png".force_encoding("UTF-8")
418
    assert_equal true, string.valid_encoding?
419

  
420
    a1 = Attachment.find(21)
421
    assert_equal string, a1.filename
422

  
423
    # ensure that string#casecmp fails to compare unicode strings case-sensitive
424
    assert_not_equal 0, string.casecmp(string.downcase)
425

  
426
    # search with exact string succeeds
427
    la1 = Attachment.latest_attach([a1], string)
428
    assert_not_nil la1
429

  
430
    # using downcase should fail because of casecmp
431
    la2 = Attachment.latest_attach([a1], string.downcase)
432
    assert_nil la2
433

  
434
    set_tmp_attachments_directory
435
  end
436

  
413 437
  def test_latest_attach_should_not_error_with_string_with_invalid_encoding
414 438
    string = "width:50\xFE-Image.jpg".force_encoding('UTF-8')
415 439
    assert_equal false, string.valid_encoding?
test/unit/helpers/application_helper_test.rb Wed Jan 16 07:20:49 2019 +0000 → test/unit/helpers/application_helper_test.rb Wed Jan 16 15:40:26 2019 +0300
160 160
    end
161 161
  end
162 162

  
163
  def test_attached_images_with_textile_and_uppercase_non_ascii_filename
164
    # first unicode letter (\xd0\xa2) is uppercase
165
    string = "Test\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82.png".force_encoding("UTF-8")
166
    attachment = Attachment.generate!(:filename => string)
167
    with_settings :text_formatting => 'textile' do
168
      assert_include %(<img src="/attachments/download/#{attachment.id}/Test%D0%A2%D0%B5%D1%81%D1%82.png" alt="" />),
169
        textilizable("!" + string + "!)", :attachments => [attachment])
170
    end
171
  end
172

  
163 173
  def test_attached_images_with_markdown_and_non_ascii_filename
164 174
    skip unless Object.const_defined?(:Redcarpet)
165 175

  
......
170 180
    end
171 181
  end
172 182

  
183
  def test_attached_images_with_markdown_and_uppercase_non_ascii_filename
184
    skip unless Object.const_defined?(:Redcarpet)
185

  
186
    # first unicode letter (\xd0\xa2) is uppercase
187
    string = "Test\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82.png".force_encoding("UTF-8")
188
    attachment = Attachment.generate!(:filename => string)
189
    with_settings :text_formatting => 'markdown' do
190
      assert_include %(<img src="/attachments/download/#{attachment.id}/Test%D0%A2%D0%B5%D1%81%D1%82.png" alt="" />),
191
        textilizable("![](" + string + ")", :attachments => [attachment])
192
    end
193
  end
194

  
173 195
  def test_attached_images_with_hires_naming
174 196
    attachment = Attachment.generate!(:filename => 'image@2x.png')
175 197
      assert_equal %(<p><img src="/attachments/download/#{attachment.id}/image@2x.png" srcset="/attachments/download/#{attachment.id}/image@2x.png 2x" alt="" /></p>),
(1-1/2)