diff options
Diffstat (limited to 'test/controllers')
| -rw-r--r-- | test/controllers/assets_controller_test.rb | 32 | ||||
| -rw-r--r-- | test/controllers/pages_controller_test.rb | 69 | ||||
| -rw-r--r-- | test/controllers/related_assets_controller_test.rb | 25 |
3 files changed, 106 insertions, 20 deletions
diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index 5f5f6e5..f834541 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb | |||
| @@ -4,17 +4,16 @@ class AssetsControllerTest < ActionController::TestCase | |||
| 4 | 4 | ||
| 5 | def setup | 5 | def setup |
| 6 | login_as :quentin | 6 | login_as :quentin |
| 7 | @existing_asset_ids = Asset.pluck(:id) | ||
| 7 | end | 8 | end |
| 8 | 9 | ||
| 9 | def teardown | 10 | def teardown |
| 10 | # Clean up any files written to disk during tests | 11 | (Asset.pluck(:id) - @existing_asset_ids).each do |id| |
| 11 | Dir.glob(Rails.root.join('public', 'system', 'uploads', 'test_*')).each do |dir| | 12 | dir = Asset.upload_root.join(id.to_s) |
| 13 | raise "Refusing to delete #{dir} -- outside tmp/, Rails.env.test? may be false" unless | ||
| 14 | dir.to_s.start_with?(Rails.root.join("tmp").to_s) | ||
| 12 | FileUtils.rm_rf(dir) | 15 | FileUtils.rm_rf(dir) |
| 13 | end | 16 | 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 | 17 | end |
| 19 | 18 | ||
| 20 | # --- index --- | 19 | # --- index --- |
| @@ -64,15 +63,14 @@ class AssetsControllerTest < ActionController::TestCase | |||
| 64 | 63 | ||
| 65 | # original and all four variants should exist on disk | 64 | # original and all four variants should exist on disk |
| 66 | %w[original medium thumb headline large].each do |style| | 65 | %w[original medium thumb headline large].each do |style| |
| 67 | path = Rails.root.join('public', 'system', 'uploads', | 66 | path = asset.send(:file_path, style) |
| 68 | asset.id.to_s, style, 'test_image.png') | ||
| 69 | assert File.exist?(path), "Expected #{style} variant at #{path}" | 67 | assert File.exist?(path), "Expected #{style} variant at #{path}" |
| 70 | end | 68 | end |
| 71 | end | 69 | end |
| 72 | 70 | ||
| 73 | # --- create with PDF --- | 71 | # --- create with PDF --- |
| 74 | 72 | ||
| 75 | test "create asset with PDF upload generates only original" do | 73 | test "create asset with PDF upload generates rasterized variants" do |
| 76 | uploaded = Rack::Test::UploadedFile.new( | 74 | uploaded = Rack::Test::UploadedFile.new( |
| 77 | Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'), | 75 | Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'), |
| 78 | 'application/pdf' | 76 | 'application/pdf' |
| @@ -83,18 +81,14 @@ class AssetsControllerTest < ActionController::TestCase | |||
| 83 | assert_response :redirect | 81 | assert_response :redirect |
| 84 | 82 | ||
| 85 | asset = Asset.last | 83 | asset = Asset.last |
| 86 | assert_equal 'test_document.pdf', asset.upload_file_name | 84 | original_path = asset.send(:file_path, :original) |
| 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}" | 85 | assert File.exist?(original_path), "Expected original at #{original_path}" |
| 86 | assert_equal 'test_document.pdf', File.basename(original_path) | ||
| 93 | 87 | ||
| 94 | %w[medium thumb headline large].each do |style| | 88 | %w[medium thumb headline large].each do |style| |
| 95 | path = Rails.root.join('public', 'system', 'uploads', | 89 | path = asset.send(:file_path, style) |
| 96 | asset.id.to_s, style, 'test_document.pdf') | 90 | assert File.exist?(path), "Expected a #{style} variant at #{path}" |
| 97 | assert !File.exist?(path), "Expected no #{style} variant for PDF" | 91 | assert_equal '.png', File.extname(path), "Expected #{style} variant to be a PNG, not a PDF" |
| 98 | end | 92 | end |
| 99 | end | 93 | end |
| 100 | 94 | ||
| @@ -137,7 +131,7 @@ class AssetsControllerTest < ActionController::TestCase | |||
| 137 | ) | 131 | ) |
| 138 | post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } | 132 | post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } |
| 139 | asset = Asset.last | 133 | asset = Asset.last |
| 140 | upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s) | 134 | upload_dir = asset.send(:upload_root).join(asset.id.to_s) |
| 141 | assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" | 135 | assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" |
| 142 | 136 | ||
| 143 | assert_difference 'Asset.count', -1 do | 137 | assert_difference 'Asset.count', -1 do |
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb index 3879014..732869b 100644 --- a/test/controllers/pages_controller_test.rb +++ b/test/controllers/pages_controller_test.rb | |||
| @@ -1,5 +1,72 @@ | |||
| 1 | require 'test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | class PagesControllerTest < ActionController::TestCase | 3 | class PagesControllerTest < ActionController::TestCase |
| 4 | # will be removed anyway | 4 | test "preview shows the autosave when a draft and an autosave both exist" do |
| 5 | login_as :quentin | ||
| 6 | |||
| 7 | node = Node.root.children.create!(:slug => "preview_retest") | ||
| 8 | node.draft.update!(:title => "draft title") | ||
| 9 | node.publish_draft! | ||
| 10 | |||
| 11 | node.draft | ||
| 12 | node.lock_for_editing!(users(:quentin)) | ||
| 13 | node.autosave!({ :title => "draft title" }, users(:quentin)) | ||
| 14 | node.save_draft!(users(:quentin)) | ||
| 15 | node.autosave!({ :title => "autosave title" }, users(:quentin)) | ||
| 16 | |||
| 17 | get :preview, params: { :id => node.draft_id } | ||
| 18 | |||
| 19 | assert_response :success | ||
| 20 | assert_match "autosave title", response.body | ||
| 21 | end | ||
| 22 | |||
| 23 | test "preview renders a headlined image on the autosave without crashing" do | ||
| 24 | login_as :quentin | ||
| 25 | |||
| 26 | node = Node.root.children.create!(:slug => "preview_retest_with_image") | ||
| 27 | node.lock_for_editing!(users(:quentin)) | ||
| 28 | node.autosave!({ :title => "draft title" }, users(:quentin)) | ||
| 29 | node.save_draft!(users(:quentin)) | ||
| 30 | node.autosave!({ :title => "autosave title" }, users(:quentin)) | ||
| 31 | |||
| 32 | asset = Asset.create!(:name => "test", :upload_content_type => "image/png") | ||
| 33 | node.autosave.related_assets.create!(:asset_id => asset.id, :position => 1, :headline => true) | ||
| 34 | |||
| 35 | get :preview, params: { :id => node.draft_id } | ||
| 36 | |||
| 37 | assert_response :success | ||
| 38 | assert_match "autosave title", response.body | ||
| 39 | end | ||
| 40 | |||
| 41 | test "preview shows the autosave when no draft exists at all" do | ||
| 42 | login_as :quentin | ||
| 43 | |||
| 44 | node = Node.root.children.create!(:slug => "preview_retest_no_draft") | ||
| 45 | node.draft.destroy | ||
| 46 | node.update_column(:draft_id, nil) | ||
| 47 | |||
| 48 | node.lock_for_editing!(users(:quentin)) | ||
| 49 | node.autosave!({ :title => "autosave only title" }, users(:quentin)) | ||
| 50 | |||
| 51 | asset = Asset.create!(:name => "test", :upload_content_type => "image/png") | ||
| 52 | node.autosave.related_assets.create!(:asset_id => asset.id, :position => 1) | ||
| 53 | |||
| 54 | get :preview, params: { :id => node.autosave_id } | ||
| 55 | |||
| 56 | assert_response :success | ||
| 57 | assert_match "autosave only title", response.body | ||
| 58 | end | ||
| 59 | |||
| 60 | test "preview shows head normally when there is no draft or autosave" do | ||
| 61 | login_as :quentin | ||
| 62 | |||
| 63 | node = Node.root.children.create!(:slug => "preview_retest_head_only") | ||
| 64 | node.draft.update!(:title => "head title") | ||
| 65 | node.publish_draft! | ||
| 66 | |||
| 67 | get :preview, params: { :id => node.head_id } | ||
| 68 | |||
| 69 | assert_response :success | ||
| 70 | assert_match "head title", response.body | ||
| 71 | end | ||
| 5 | end | 72 | end |
diff --git a/test/controllers/related_assets_controller_test.rb b/test/controllers/related_assets_controller_test.rb index 2384adc..ced4b74 100644 --- a/test/controllers/related_assets_controller_test.rb +++ b/test/controllers/related_assets_controller_test.rb | |||
| @@ -134,4 +134,29 @@ class RelatedAssetsControllerTest < ActionController::TestCase | |||
| 134 | assert_response :success | 134 | assert_response :success |
| 135 | assert_not related.reload.headline? | 135 | assert_not related.reload.headline? |
| 136 | end | 136 | end |
| 137 | |||
| 138 | test "search includes PDF assets as headline-eligible candidates" do | ||
| 139 | login_as :quentin | ||
| 140 | node = Node.root.children.create!(:slug => "related_assets_search_pdf_test") | ||
| 141 | asset = Asset.create!(:name => "expert-opinion-searchable", :upload_content_type => "application/pdf") | ||
| 142 | |||
| 143 | get :search, params: { :node_id => node.id, :search_term => "expert-opinion-searchable" } | ||
| 144 | |||
| 145 | assert_response :success | ||
| 146 | ids = JSON.parse(response.body).map { |r| r["id"] } | ||
| 147 | assert_includes ids, asset.id | ||
| 148 | end | ||
| 149 | |||
| 150 | test "search matches by filename as well as name" do | ||
| 151 | login_as :quentin | ||
| 152 | node = Node.root.children.create!(:slug => "related_assets_search_filename_test") | ||
| 153 | asset = Asset.create!(:name => "Untitled", :upload_content_type => "application/pdf", | ||
| 154 | :upload_file_name => "Stellungnahme_Patientendaten_Schutz.pdf") | ||
| 155 | |||
| 156 | get :search, params: { :node_id => node.id, :search_term => "Patientendaten" } | ||
| 157 | |||
| 158 | assert_response :success | ||
| 159 | ids = JSON.parse(response.body).map { |r| r["id"] } | ||
| 160 | assert_includes ids, asset.id | ||
| 161 | end | ||
| 137 | end | 162 | end |
