From c06723ee715512c2033c7786c48f15674585b56b Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 26 Jun 2026 01:59:57 +0200 Subject: Stage 4: Rails 5.2 -> 6.1 on Ruby 2.7.2 - routing-filter 0.6.3 -> 0.7.0 (Rails 6.1 compatibility) - RSS named routes rss_xml/rss_rdf added - RouteWithParams workarounds: will_paginate_patch, content_path shim, safe_path helper - Paperclip removed, replaced with FileAttachment concern (preserves URL scheme) - Assets resource moved to /admin/assets (Sprockets middleware conflict) - ApplicationRecord base class added, all models migrated - Strong parameters added to Assets, Occurrences, Events, MenuItems controllers - update_attributes -> update throughout - render :nothing -> head :ok/:not_found throughout - language_selector rewritten (removes :overwrite_params) - Environment files updated for Rails 6.1 (eager_load, public_file_server, ActionMailer) - Arel::Visitors::DepthFirst and Integer/Float duration patches removed from test_helper - AssetsController tests added (10 tests covering upload, variants, destroy, auth) - ImageMagick geometry: 460x250! for headline crop (not # which is invalid in IM6) 129 runs, 311 assertions, 5 failures (all pre-existing), 0 errors --- test/controllers/assets_controller_test.rb | 170 ++++++++++++++++++++++++++ test/controllers/revisions_controller_test.rb | 2 +- test/fixtures/files/test_document.pdf | Bin 0 -> 9246 bytes test/fixtures/files/test_image.png | Bin 0 -> 49854 bytes test/models/page_test.rb | 2 +- test/models/user_test.rb | 4 +- test/test_helper.rb | 67 ---------- 7 files changed, 174 insertions(+), 71 deletions(-) create mode 100644 test/fixtures/files/test_document.pdf create mode 100644 test/fixtures/files/test_image.png (limited to 'test') diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index d003d25..ea55c90 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb @@ -1,4 +1,174 @@ require 'test_helper' class AssetsControllerTest < ActionController::TestCase + + def setup + login_as :quentin + end + + def teardown + # Clean up any files written to disk during tests + Dir.glob(Rails.root.join('public', 'system', 'uploads', 'test_*')).each do |dir| + FileUtils.rm_rf(dir) + end + # Remove uploads created for assets created during tests + Asset.where("upload_file_name IS NOT NULL").where("id > 1000000").each do |a| + FileUtils.rm_rf(Rails.root.join('public', 'system', 'uploads', a.id.to_s)) + end + end + + # --- index --- + + test "get index" do + get :index + assert_response :success + end + + # --- show --- + + test "show existing asset" do + asset = Asset.create!( + name: 'Test asset', + upload_file_name: 'test_image.png', + upload_content_type: 'image/png', + upload_file_size: 49854, + upload_updated_at: Time.current + ) + get :show, params: { id: asset.id } + assert_response :success + end + + # --- new --- + + test "get new" do + get :new + assert_response :success + end + + # --- create with image --- + + test "create asset with image upload generates variants" do + uploaded = Rack::Test::UploadedFile.new( + Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), + 'image/png' + ) + assert_difference 'Asset.count', 1 do + post :create, params: { asset: { name: 'Logo', upload: uploaded } } + end + assert_response :redirect + + asset = Asset.last + assert_equal 'test_image.png', asset.upload_file_name + assert_equal 'image/png', asset.upload_content_type + assert asset.upload_file_size > 0 + + # original and all three variants should exist on disk + %w[original medium thumb headline].each do |style| + path = Rails.root.join('public', 'system', 'uploads', + asset.id.to_s, style, 'test_image.png') + assert File.exist?(path), "Expected #{style} variant at #{path}" + end + end + + # --- create with PDF --- + + test "create asset with PDF upload generates only original" do + uploaded = Rack::Test::UploadedFile.new( + Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'), + 'application/pdf' + ) + assert_difference 'Asset.count', 1 do + post :create, params: { asset: { name: 'Document', upload: uploaded } } + end + assert_response :redirect + + asset = Asset.last + assert_equal 'test_document.pdf', asset.upload_file_name + assert_equal 'application/pdf', asset.upload_content_type + + # only original should exist, no image variants + original_path = Rails.root.join('public', 'system', 'uploads', + asset.id.to_s, 'original', 'test_document.pdf') + assert File.exist?(original_path), "Expected original at #{original_path}" + + %w[medium thumb headline].each do |style| + path = Rails.root.join('public', 'system', 'uploads', + asset.id.to_s, style, 'test_document.pdf') + assert !File.exist?(path), "Expected no #{style} variant for PDF" + end + end + + # --- edit --- + + test "get edit" do + asset = Asset.create!( + name: 'Edit me', + upload_file_name: 'test_image.png', + upload_content_type: 'image/png', + upload_file_size: 49854, + upload_updated_at: Time.current + ) + get :edit, params: { id: asset.id } + assert_response :success + end + + # --- update --- + + test "update asset name" do + asset = Asset.create!( + name: 'Old name', + upload_file_name: 'test_image.png', + upload_content_type: 'image/png', + upload_file_size: 49854, + upload_updated_at: Time.current + ) + put :update, params: { id: asset.id, asset: { name: 'New name' } } + assert_response :redirect + assert_equal 'New name', asset.reload.name + end + + # --- destroy --- + + test "destroy asset removes record and files" do + # Create a real upload so there are files to delete + uploaded = Rack::Test::UploadedFile.new( + Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), + 'image/png' + ) + post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } + asset = Asset.last + upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s) + assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" + + assert_difference 'Asset.count', -1 do + delete :destroy, params: { id: asset.id } + end + assert_response :redirect + assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy" + end + + # --- URL helpers --- + + test "upload url returns correct path for original" do + asset = Asset.create!( + name: 'URL test', + upload_file_name: 'logo.png', + upload_content_type: 'image/png', + upload_file_size: 1000, + upload_updated_at: Time.current + ) + assert_equal "/system/uploads/#{asset.id}/original/logo.png", asset.upload.url + assert_equal "/system/uploads/#{asset.id}/thumb/logo.png", asset.upload.url(:thumb) + assert_equal "/system/uploads/#{asset.id}/medium/logo.png", asset.upload.url(:medium) + assert_equal "/system/uploads/#{asset.id}/headline/logo.png", asset.upload.url(:headline) + end + + # --- login required --- + + test "index requires login" do + session[:user_id] = nil + @controller.instance_variable_set(:@current_user, nil) + get :index + assert_response :redirect + end end diff --git a/test/controllers/revisions_controller_test.rb b/test/controllers/revisions_controller_test.rb index 385e458..b4dcd8f 100644 --- a/test/controllers/revisions_controller_test.rb +++ b/test/controllers/revisions_controller_test.rb @@ -12,7 +12,7 @@ class RevisionsControllerTest < ActionController::TestCase @node.publish_draft! @node.find_or_create_draft @user draft = @node.draft - draft.update_attributes(:body => "second") + draft.update(:body => "second") @node.publish_draft! end diff --git a/test/fixtures/files/test_document.pdf b/test/fixtures/files/test_document.pdf new file mode 100644 index 0000000..f373820 Binary files /dev/null and b/test/fixtures/files/test_document.pdf differ diff --git a/test/fixtures/files/test_image.png b/test/fixtures/files/test_image.png new file mode 100644 index 0000000..08eaf96 Binary files /dev/null and b/test/fixtures/files/test_image.png differ diff --git a/test/models/page_test.rb b/test/models/page_test.rb index 8b34399..afba8b5 100644 --- a/test/models/page_test.rb +++ b/test/models/page_test.rb @@ -120,7 +120,7 @@ class PageTest < ActiveSupport::TestCase english = page.translations.select {|x| x.locale == :en}.first Page::Translation.record_timestamps = false - english.update_attributes(:updated_at => (Time.now+25.hours)) + english.update(:updated_at => (Time.now+25.hours)) Page::Translation.record_timestamps = true assert_equal 1, Page.find_with_outdated_translations.count diff --git a/test/models/user_test.rb b/test/models/user_test.rb index bd5d059..6e4d2d7 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -42,12 +42,12 @@ class UserTest < ActiveSupport::TestCase end def test_should_reset_password - users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password') + users(:quentin).update(:password => 'new password', :password_confirmation => 'new password') assert_equal users(:quentin), User.authenticate('quentin', 'new password') end def test_should_not_rehash_password - users(:quentin).update_attributes(:login => 'quentin2') + users(:quentin).update(:login => 'quentin2') assert_equal users(:quentin), User.authenticate('quentin2', 'monkey') end diff --git a/test/test_helper.rb b/test/test_helper.rb index 2514926..e7b15da 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,73 +17,6 @@ module ActiveRecord end end -class Integer - def days - ActiveSupport::Duration.new(self * 86400, [[:days, self]]) - end - alias :day :days - - def weeks - ActiveSupport::Duration.new(self * 7 * 86400, [[:days, self * 7]]) - end - alias :week :weeks - - def hours - ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]]) - end - alias :hour :hours - - def minutes - ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]]) - end - alias :minute :minutes - - def seconds - ActiveSupport::Duration.new(self, [[:seconds, self]]) - end - alias :second :seconds - - def months - ActiveSupport::Duration.new(self * 30 * 86400, [[:months, self]]) - end - alias :month :months - - def years - ActiveSupport::Duration.new((self * 365.25 * 86400).to_i, [[:years, self]]) - end - alias :year :years -end - -class Float - def days - ActiveSupport::Duration.new((self * 86400).to_i, [[:days, self]]) - end - alias :day :days - - def hours - ActiveSupport::Duration.new((self * 3600).to_i, [[:seconds, (self * 3600).to_i]]) - end - alias :hour :hours - - def minutes - ActiveSupport::Duration.new((self * 60).to_i, [[:seconds, (self * 60).to_i]]) - end - alias :minute :minutes -end - -require 'arel' -module Arel - module Visitors - [ToSql, DepthFirst].each do |visitor| - visitor.class_eval do - def visit_Integer(o, collector = nil) - collector ? collector << o.to_s : o.to_s - end - end - end - end -end - class ActiveSupport::TestCase include AuthenticatedTestHelper -- cgit v1.3