diff test/unit/attachment_test.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children bb32da3bea34 622f24f53b42 261b3d9a4903
line wrap: on
line diff
--- a/test/unit/attachment_test.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/test/unit/attachment_test.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,7 +1,7 @@
 # encoding: utf-8
 #
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -38,6 +38,10 @@
     set_tmp_attachments_directory
   end
 
+  def test_container_for_new_attachment_should_be_nil
+    assert_nil Attachment.new.container
+  end
+
   def test_create
     a = Attachment.new(:container => Issue.find(1),
                        :file => uploaded_test_file("testfile.txt", "text/plain"),
@@ -52,6 +56,31 @@
     assert_equal 59, File.size(a.diskfile)
   end
 
+  def test_size_should_be_validated_for_new_file
+    with_settings :attachment_max_size => 0 do
+      a = Attachment.new(:container => Issue.find(1),
+                         :file => uploaded_test_file("testfile.txt", "text/plain"),
+                         :author => User.find(1))
+      assert !a.save
+    end
+  end
+
+  def test_size_should_not_be_validated_when_copying
+    a = Attachment.create!(:container => Issue.find(1),
+                           :file => uploaded_test_file("testfile.txt", "text/plain"),
+                           :author => User.find(1))
+    with_settings :attachment_max_size => 0 do
+      copy = a.copy
+      assert copy.save
+    end
+  end
+
+  def test_description_length_should_be_validated
+    a = Attachment.new(:description => 'a' * 300)
+    assert !a.save
+    assert_not_nil a.errors[:description]
+  end
+
   def test_destroy
     a = Attachment.new(:container => Issue.find(1),
                        :file => uploaded_test_file("testfile.txt", "text/plain"),
@@ -69,6 +98,22 @@
     assert !File.exist?(diskfile)
   end
 
+  def test_destroy_should_not_delete_file_referenced_by_other_attachment
+    a = Attachment.create!(:container => Issue.find(1),
+                           :file => uploaded_test_file("testfile.txt", "text/plain"),
+                           :author => User.find(1))
+    diskfile = a.diskfile
+
+    copy = a.copy
+    copy.save!
+
+    assert File.exists?(diskfile)
+    a.destroy
+    assert File.exists?(diskfile)
+    copy.destroy
+    assert !File.exists?(diskfile)
+  end
+
   def test_create_should_auto_assign_content_type
     a = Attachment.new(:container => Issue.find(1),
                        :file => uploaded_test_file("testfile.txt", ""),
@@ -105,6 +150,24 @@
     assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
   end
 
+  def test_title
+    a = Attachment.new(:filename => "test.png")
+    assert_equal "test.png", a.title
+
+    a = Attachment.new(:filename => "test.png", :description => "Cool image")
+    assert_equal "test.png (Cool image)", a.title
+  end
+
+  def test_prune_should_destroy_old_unattached_attachments
+    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
+    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
+    Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
+
+    assert_difference 'Attachment.count', -2 do
+      Attachment.prune
+    end
+  end
+
   context "Attachmnet.attach_files" do
     should "attach the file" do
       issue = Issue.first
@@ -129,7 +192,7 @@
     should "add unsaved files to the object as unsaved attachments" do
       # Max size of 0 to force Attachment creation failures
       with_settings(:attachment_max_size => 0) do
-        @project = Project.generate!
+        @project = Project.find(1)
         response = Attachment.attach_files(@project, {
                                              '1' => {'file' => mock_file, 'description' => 'test'},
                                              '2' => {'file' => mock_file, 'description' => 'test'}
@@ -145,7 +208,7 @@
   end
 
   def test_latest_attach
-    Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
+    set_fixtures_attachments_directory
     a1 = Attachment.find(16)
     assert_equal "testfile.png", a1.filename
     assert a1.readable?
@@ -165,4 +228,28 @@
 
     set_tmp_attachments_directory
   end
+
+  def test_thumbnailable_should_be_true_for_images
+    assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
+  end
+
+  def test_thumbnailable_should_be_true_for_non_images
+    assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
+  end
+
+  if convert_installed?
+    def test_thumbnail_should_generate_the_thumbnail
+      set_fixtures_attachments_directory
+      attachment = Attachment.find(16)
+      Attachment.clear_thumbnails
+
+      assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
+        thumbnail = attachment.thumbnail
+        assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
+        assert File.exists?(thumbnail)
+      end
+    end
+  else
+    puts '(ImageMagick convert not available)'
+  end
 end