summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-06-26 01:59:57 +0200
committererdgeist <erdgeist@erdgeist.org>2026-06-26 01:59:57 +0200
commitc06723ee715512c2033c7786c48f15674585b56b (patch)
tree46d074bde9a4fc61f0a76cbc601007ed4412ec61 /test
parent0818a3057b0a91e422158d828026c941b4e10622 (diff)
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
Diffstat (limited to 'test')
-rw-r--r--test/controllers/assets_controller_test.rb170
-rw-r--r--test/controllers/revisions_controller_test.rb2
-rw-r--r--test/fixtures/files/test_document.pdfbin0 -> 9246 bytes
-rw-r--r--test/fixtures/files/test_image.pngbin0 -> 49854 bytes
-rw-r--r--test/models/page_test.rb2
-rw-r--r--test/models/user_test.rb4
-rw-r--r--test/test_helper.rb67
7 files changed, 174 insertions, 71 deletions
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 @@
1require 'test_helper' 1require 'test_helper'
2 2
3class AssetsControllerTest < ActionController::TestCase 3class AssetsControllerTest < ActionController::TestCase
4
5 def setup
6 login_as :quentin
7 end
8
9 def teardown
10 # Clean up any files written to disk during tests
11 Dir.glob(Rails.root.join('public', 'system', 'uploads', 'test_*')).each do |dir|
12 FileUtils.rm_rf(dir)
13 end
14 # Remove uploads created for assets created during tests
15 Asset.where("upload_file_name IS NOT NULL").where("id > 1000000").each do |a|
16 FileUtils.rm_rf(Rails.root.join('public', 'system', 'uploads', a.id.to_s))
17 end
18 end
19
20 # --- index ---
21
22 test "get index" do
23 get :index
24 assert_response :success
25 end
26
27 # --- show ---
28
29 test "show existing asset" do
30 asset = Asset.create!(
31 name: 'Test asset',
32 upload_file_name: 'test_image.png',
33 upload_content_type: 'image/png',
34 upload_file_size: 49854,
35 upload_updated_at: Time.current
36 )
37 get :show, params: { id: asset.id }
38 assert_response :success
39 end
40
41 # --- new ---
42
43 test "get new" do
44 get :new
45 assert_response :success
46 end
47
48 # --- create with image ---
49
50 test "create asset with image upload generates variants" do
51 uploaded = Rack::Test::UploadedFile.new(
52 Rails.root.join('test', 'fixtures', 'files', 'test_image.png'),
53 'image/png'
54 )
55 assert_difference 'Asset.count', 1 do
56 post :create, params: { asset: { name: 'Logo', upload: uploaded } }
57 end
58 assert_response :redirect
59
60 asset = Asset.last
61 assert_equal 'test_image.png', asset.upload_file_name
62 assert_equal 'image/png', asset.upload_content_type
63 assert asset.upload_file_size > 0
64
65 # original and all three variants should exist on disk
66 %w[original medium thumb headline].each do |style|
67 path = Rails.root.join('public', 'system', 'uploads',
68 asset.id.to_s, style, 'test_image.png')
69 assert File.exist?(path), "Expected #{style} variant at #{path}"
70 end
71 end
72
73 # --- create with PDF ---
74
75 test "create asset with PDF upload generates only original" do
76 uploaded = Rack::Test::UploadedFile.new(
77 Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'),
78 'application/pdf'
79 )
80 assert_difference 'Asset.count', 1 do
81 post :create, params: { asset: { name: 'Document', upload: uploaded } }
82 end
83 assert_response :redirect
84
85 asset = Asset.last
86 assert_equal 'test_document.pdf', asset.upload_file_name
87 assert_equal 'application/pdf', asset.upload_content_type
88
89 # only original should exist, no image variants
90 original_path = Rails.root.join('public', 'system', 'uploads',
91 asset.id.to_s, 'original', 'test_document.pdf')
92 assert File.exist?(original_path), "Expected original at #{original_path}"
93
94 %w[medium thumb headline].each do |style|
95 path = Rails.root.join('public', 'system', 'uploads',
96 asset.id.to_s, style, 'test_document.pdf')
97 assert !File.exist?(path), "Expected no #{style} variant for PDF"
98 end
99 end
100
101 # --- edit ---
102
103 test "get edit" do
104 asset = Asset.create!(
105 name: 'Edit me',
106 upload_file_name: 'test_image.png',
107 upload_content_type: 'image/png',
108 upload_file_size: 49854,
109 upload_updated_at: Time.current
110 )
111 get :edit, params: { id: asset.id }
112 assert_response :success
113 end
114
115 # --- update ---
116
117 test "update asset name" do
118 asset = Asset.create!(
119 name: 'Old name',
120 upload_file_name: 'test_image.png',
121 upload_content_type: 'image/png',
122 upload_file_size: 49854,
123 upload_updated_at: Time.current
124 )
125 put :update, params: { id: asset.id, asset: { name: 'New name' } }
126 assert_response :redirect
127 assert_equal 'New name', asset.reload.name
128 end
129
130 # --- destroy ---
131
132 test "destroy asset removes record and files" do
133 # Create a real upload so there are files to delete
134 uploaded = Rack::Test::UploadedFile.new(
135 Rails.root.join('test', 'fixtures', 'files', 'test_image.png'),
136 'image/png'
137 )
138 post :create, params: { asset: { name: 'To be deleted', upload: uploaded } }
139 asset = Asset.last
140 upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s)
141 assert Dir.exist?(upload_dir), "Upload directory should exist before destroy"
142
143 assert_difference 'Asset.count', -1 do
144 delete :destroy, params: { id: asset.id }
145 end
146 assert_response :redirect
147 assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy"
148 end
149
150 # --- URL helpers ---
151
152 test "upload url returns correct path for original" do
153 asset = Asset.create!(
154 name: 'URL test',
155 upload_file_name: 'logo.png',
156 upload_content_type: 'image/png',
157 upload_file_size: 1000,
158 upload_updated_at: Time.current
159 )
160 assert_equal "/system/uploads/#{asset.id}/original/logo.png", asset.upload.url
161 assert_equal "/system/uploads/#{asset.id}/thumb/logo.png", asset.upload.url(:thumb)
162 assert_equal "/system/uploads/#{asset.id}/medium/logo.png", asset.upload.url(:medium)
163 assert_equal "/system/uploads/#{asset.id}/headline/logo.png", asset.upload.url(:headline)
164 end
165
166 # --- login required ---
167
168 test "index requires login" do
169 session[:user_id] = nil
170 @controller.instance_variable_set(:@current_user, nil)
171 get :index
172 assert_response :redirect
173 end
4end 174end
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
12 @node.publish_draft! 12 @node.publish_draft!
13 @node.find_or_create_draft @user 13 @node.find_or_create_draft @user
14 draft = @node.draft 14 draft = @node.draft
15 draft.update_attributes(:body => "second") 15 draft.update(:body => "second")
16 @node.publish_draft! 16 @node.publish_draft!
17 end 17 end
18 18
diff --git a/test/fixtures/files/test_document.pdf b/test/fixtures/files/test_document.pdf
new file mode 100644
index 0000000..f373820
--- /dev/null
+++ b/test/fixtures/files/test_document.pdf
Binary files differ
diff --git a/test/fixtures/files/test_image.png b/test/fixtures/files/test_image.png
new file mode 100644
index 0000000..08eaf96
--- /dev/null
+++ b/test/fixtures/files/test_image.png
Binary files 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
120 120
121 english = page.translations.select {|x| x.locale == :en}.first 121 english = page.translations.select {|x| x.locale == :en}.first
122 Page::Translation.record_timestamps = false 122 Page::Translation.record_timestamps = false
123 english.update_attributes(:updated_at => (Time.now+25.hours)) 123 english.update(:updated_at => (Time.now+25.hours))
124 Page::Translation.record_timestamps = true 124 Page::Translation.record_timestamps = true
125 assert_equal 1, Page.find_with_outdated_translations.count 125 assert_equal 1, Page.find_with_outdated_translations.count
126 126
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
42 end 42 end
43 43
44 def test_should_reset_password 44 def test_should_reset_password
45 users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password') 45 users(:quentin).update(:password => 'new password', :password_confirmation => 'new password')
46 assert_equal users(:quentin), User.authenticate('quentin', 'new password') 46 assert_equal users(:quentin), User.authenticate('quentin', 'new password')
47 end 47 end
48 48
49 def test_should_not_rehash_password 49 def test_should_not_rehash_password
50 users(:quentin).update_attributes(:login => 'quentin2') 50 users(:quentin).update(:login => 'quentin2')
51 assert_equal users(:quentin), User.authenticate('quentin2', 'monkey') 51 assert_equal users(:quentin), User.authenticate('quentin2', 'monkey')
52 end 52 end
53 53
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
17 end 17 end
18end 18end
19 19
20class Integer
21 def days
22 ActiveSupport::Duration.new(self * 86400, [[:days, self]])
23 end
24 alias :day :days
25
26 def weeks
27 ActiveSupport::Duration.new(self * 7 * 86400, [[:days, self * 7]])
28 end
29 alias :week :weeks
30
31 def hours
32 ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
33 end
34 alias :hour :hours
35
36 def minutes
37 ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
38 end
39 alias :minute :minutes
40
41 def seconds
42 ActiveSupport::Duration.new(self, [[:seconds, self]])
43 end
44 alias :second :seconds
45
46 def months
47 ActiveSupport::Duration.new(self * 30 * 86400, [[:months, self]])
48 end
49 alias :month :months
50
51 def years
52 ActiveSupport::Duration.new((self * 365.25 * 86400).to_i, [[:years, self]])
53 end
54 alias :year :years
55end
56
57class Float
58 def days
59 ActiveSupport::Duration.new((self * 86400).to_i, [[:days, self]])
60 end
61 alias :day :days
62
63 def hours
64 ActiveSupport::Duration.new((self * 3600).to_i, [[:seconds, (self * 3600).to_i]])
65 end
66 alias :hour :hours
67
68 def minutes
69 ActiveSupport::Duration.new((self * 60).to_i, [[:seconds, (self * 60).to_i]])
70 end
71 alias :minute :minutes
72end
73
74require 'arel'
75module Arel
76 module Visitors
77 [ToSql, DepthFirst].each do |visitor|
78 visitor.class_eval do
79 def visit_Integer(o, collector = nil)
80 collector ? collector << o.to_s : o.to_s
81 end
82 end
83 end
84 end
85end
86
87class ActiveSupport::TestCase 20class ActiveSupport::TestCase
88 21
89 include AuthenticatedTestHelper 22 include AuthenticatedTestHelper