diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/controllers/assets_controller_test.rb | 73 | ||||
| -rw-r--r-- | test/controllers/nodes_controller_test.rb | 39 | ||||
| -rw-r--r-- | test/controllers/pages_controller_test.rb | 69 | ||||
| -rw-r--r-- | test/controllers/related_assets_controller_test.rb | 25 | ||||
| -rw-r--r-- | test/models/asset_test.rb | 12 | ||||
| -rw-r--r-- | test/models/helpers/content_helper_test.rb | 46 | ||||
| -rw-r--r-- | test/models/node_attach_asset_test.rb | 107 | ||||
| -rw-r--r-- | test/models/node_test.rb | 35 | ||||
| -rw-r--r-- | test/models/page_test.rb | 8 | ||||
| -rw-r--r-- | test/models/related_asset_test.rb | 16 |
10 files changed, 372 insertions, 58 deletions
diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index 5f5f6e5..05fc6de 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,19 +81,56 @@ 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" |
| 92 | end | ||
| 93 | end | ||
| 94 | |||
| 95 | # --- create with attach --- | ||
| 96 | |||
| 97 | test "create with node_id attaches the asset to the node's draft" do | ||
| 98 | node = Node.root.children.create!(:slug => "asset_attach_target") | ||
| 99 | |||
| 100 | post :create, params: { asset: { name: 'Attach me' }, node_id: node.id } | ||
| 101 | |||
| 102 | assert_response :redirect | ||
| 103 | asset = Asset.last | ||
| 104 | assert_includes node.draft.assets.reload, asset | ||
| 105 | assert_match /attached/, flash[:notice] | ||
| 106 | end | ||
| 107 | |||
| 108 | test "create against a foreign-locked node keeps the asset but refuses the attach" do | ||
| 109 | node = Node.root.children.create!(:slug => "asset_attach_locked") | ||
| 110 | node.lock_for_editing!(users(:aaron)) | ||
| 111 | |||
| 112 | assert_difference 'Asset.count', 1 do | ||
| 113 | post :create, params: { asset: { name: 'Orphaned for now' }, node_id: node.id } | ||
| 98 | end | 114 | end |
| 115 | |||
| 116 | assert_empty node.draft.assets.reload | ||
| 117 | assert_equal node_path(node), flash[:locked_node_path] | ||
| 118 | assert_equal "aaron", flash[:locked_by] | ||
| 119 | end | ||
| 120 | |||
| 121 | test "create with headline against a page that has one keeps the incumbent and warns" do | ||
| 122 | node = Node.root.children.create!(:slug => "asset_attach_headline") | ||
| 123 | incumbent = Asset.create!(:name => 'Incumbent', :upload_content_type => 'image/png') | ||
| 124 | node.draft.related_assets.create!(:asset => incumbent, :headline => true) | ||
| 125 | |||
| 126 | uploaded = Rack::Test::UploadedFile.new( | ||
| 127 | Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), 'image/png') | ||
| 128 | post :create, params: { asset: { name: 'Challenger', upload: uploaded }, | ||
| 129 | node_id: node.id, headline: "1" } | ||
| 130 | |||
| 131 | assert_includes node.draft.assets.reload, Asset.last | ||
| 132 | assert_equal incumbent, node.draft.reload.headline_asset | ||
| 133 | assert_equal node_path(node), flash[:headline_kept_path] | ||
| 99 | end | 134 | end |
| 100 | 135 | ||
| 101 | # --- edit --- | 136 | # --- edit --- |
| @@ -137,7 +172,7 @@ class AssetsControllerTest < ActionController::TestCase | |||
| 137 | ) | 172 | ) |
| 138 | post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } | 173 | post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } |
| 139 | asset = Asset.last | 174 | asset = Asset.last |
| 140 | upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s) | 175 | upload_dir = asset.send(:upload_root).join(asset.id.to_s) |
| 141 | assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" | 176 | assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" |
| 142 | 177 | ||
| 143 | assert_difference 'Asset.count', -1 do | 178 | assert_difference 'Asset.count', -1 do |
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb index ddc4565..d777108 100644 --- a/test/controllers/nodes_controller_test.rb +++ b/test/controllers/nodes_controller_test.rb | |||
| @@ -79,6 +79,35 @@ class NodesControllerTest < ActionController::TestCase | |||
| 79 | end | 79 | end |
| 80 | end | 80 | end |
| 81 | 81 | ||
| 82 | test "create with asset_id attaches the asset to the new draft, as headline when asked" do | ||
| 83 | login_as :quentin | ||
| 84 | asset = Asset.create!(:name => 'Birth attachment', :upload_content_type => 'image/png') | ||
| 85 | |||
| 86 | post :create, params: { :kind => "generic", :parent_id => Node.root.id, | ||
| 87 | :title => "Born Attached", | ||
| 88 | :asset_id => asset.id, :asset_headline => "1" } | ||
| 89 | |||
| 90 | assert_response :redirect | ||
| 91 | node = Node.last | ||
| 92 | assert_includes node.draft.assets, asset | ||
| 93 | assert_equal asset, node.draft.headline_asset | ||
| 94 | assert_match /attached/, flash[:notice] | ||
| 95 | end | ||
| 96 | |||
| 97 | test "the attach notice survives the redirect into the editor" do | ||
| 98 | login_as :quentin | ||
| 99 | asset = Asset.create!(:name => 'Flash survivor', :upload_content_type => 'image/png') | ||
| 100 | |||
| 101 | post :create, params: { :kind => "generic", :parent_id => Node.root.id, | ||
| 102 | :title => "Notice Carrier", :asset_id => asset.id } | ||
| 103 | assert_redirected_to edit_node_path(Node.last) | ||
| 104 | |||
| 105 | get :edit, params: { :id => Node.last.id } | ||
| 106 | assert_response :success | ||
| 107 | assert_match /attached/, flash[:notice] | ||
| 108 | assert_no_match /ready to edit/, flash[:notice] | ||
| 109 | end | ||
| 110 | |||
| 82 | test "editing a node" do | 111 | test "editing a node" do |
| 83 | login_as :quentin | 112 | login_as :quentin |
| 84 | 113 | ||
| @@ -409,7 +438,7 @@ class NodesControllerTest < ActionController::TestCase | |||
| 409 | 438 | ||
| 410 | get :show, params: { id: node.id } | 439 | get :show, params: { id: node.id } |
| 411 | assert_response :success | 440 | assert_response :success |
| 412 | assert_select "a", text: "add event" | 441 | assert_select "a", text: "Add event" |
| 413 | assert_select "a[href*='tag_list=open-day']" | 442 | assert_select "a[href*='tag_list=open-day']" |
| 414 | assert_select "a[href*='auto_tag_source=erfa-detail']" | 443 | assert_select "a[href*='auto_tag_source=erfa-detail']" |
| 415 | end | 444 | end |
| @@ -420,7 +449,7 @@ class NodesControllerTest < ActionController::TestCase | |||
| 420 | 449 | ||
| 421 | get :show, params: { id: node.id } | 450 | get :show, params: { id: node.id } |
| 422 | assert_response :success | 451 | assert_response :success |
| 423 | assert_select "a", text: "add event" | 452 | assert_select "a", text: "Add event" |
| 424 | assert_select "a[href*='tag_list=']", count: 0 | 453 | assert_select "a[href*='tag_list=']", count: 0 |
| 425 | end | 454 | end |
| 426 | 455 | ||
| @@ -514,12 +543,6 @@ class NodesControllerTest < ActionController::TestCase | |||
| 514 | assert_includes assigns(:nodes), chaostreff_node | 543 | assert_includes assigns(:nodes), chaostreff_node |
| 515 | end | 544 | end |
| 516 | 545 | ||
| 517 | test "recent combined with a search term does not raise an ambiguous column error" do | ||
| 518 | login_as :quentin | ||
| 519 | get :recent, params: { :q => "Zombies" } | ||
| 520 | assert_response :success | ||
| 521 | end | ||
| 522 | |||
| 523 | test "drafts combined with a search term does not raise an ambiguous column error" do | 546 | test "drafts combined with a search term does not raise an ambiguous column error" do |
| 524 | login_as :quentin | 547 | login_as :quentin |
| 525 | get :drafts, params: { :q => "Zombies" } | 548 | get :drafts, params: { :q => "Zombies" } |
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 |
diff --git a/test/models/asset_test.rb b/test/models/asset_test.rb index d246abe..ab1cc5d 100644 --- a/test/models/asset_test.rb +++ b/test/models/asset_test.rb | |||
| @@ -41,4 +41,16 @@ class AssetTest < ActiveSupport::TestCase | |||
| 41 | test "license_key may be blank" do | 41 | test "license_key may be blank" do |
| 42 | assert Asset.new(:license_key => nil).valid? | 42 | assert Asset.new(:license_key => nil).valid? |
| 43 | end | 43 | end |
| 44 | |||
| 45 | test "pdf? is true only for application/pdf" do | ||
| 46 | assert Asset.new(:upload_content_type => "application/pdf").pdf? | ||
| 47 | assert_not Asset.new(:upload_content_type => "image/png").pdf? | ||
| 48 | end | ||
| 49 | |||
| 50 | test "show_credit? is false for a PDF even with every credit field present" do | ||
| 51 | asset = Asset.new(:name => "demo", :upload_content_type => "application/pdf", | ||
| 52 | :creator => "Jane Doe", :source_url => "https://example.org", :license_key => "cc_by_4") | ||
| 53 | assert asset.has_credit? | ||
| 54 | assert_not asset.show_credit? | ||
| 55 | end | ||
| 44 | end | 56 | end |
diff --git a/test/models/helpers/content_helper_test.rb b/test/models/helpers/content_helper_test.rb index d6c7b43..c69ef4e 100644 --- a/test/models/helpers/content_helper_test.rb +++ b/test/models/helpers/content_helper_test.rb | |||
| @@ -79,4 +79,50 @@ class ContentHelperTest < ActionView::TestCase | |||
| 79 | 79 | ||
| 80 | I18n.with_locale(:en) { assert_match t(:open_gallery), headline_image } | 80 | I18n.with_locale(:en) { assert_match t(:open_gallery), headline_image } |
| 81 | end | 81 | end |
| 82 | |||
| 83 | test "headline_image renders a document card for a PDF headline, not a lightbox image" do | ||
| 84 | node = Node.root.children.create!(:slug => "headline_image_pdf_test") | ||
| 85 | asset = Asset.create!(:name => "Expert Opinion", :upload_content_type => "application/pdf") | ||
| 86 | node.draft.assets << asset | ||
| 87 | node.draft.related_assets.find_by(:asset_id => asset.id).update!(:headline => true) | ||
| 88 | @page = node.draft | ||
| 89 | |||
| 90 | result = headline_image | ||
| 91 | |||
| 92 | assert_match "headline_document_card", result | ||
| 93 | assert_match "Expert Opinion", result | ||
| 94 | assert_no_match "data-gallery", result | ||
| 95 | end | ||
| 96 | |||
| 97 | test "headline_image lists other attached PDFs below the headline" do | ||
| 98 | node = Node.root.children.create!(:slug => "headline_image_multi_pdf_test") | ||
| 99 | headline_pdf = Asset.create!(:name => "Main Filing", :upload_content_type => "application/pdf") | ||
| 100 | other_pdf = Asset.create!(:name => "Supplementary Exhibit", :upload_content_type => "application/pdf") | ||
| 101 | node.draft.assets << headline_pdf | ||
| 102 | node.draft.assets << other_pdf | ||
| 103 | node.draft.related_assets.find_by(:asset_id => headline_pdf.id).update!(:headline => true) | ||
| 104 | @page = node.draft | ||
| 105 | |||
| 106 | result = headline_image | ||
| 107 | |||
| 108 | assert_match "Main Filing", result | ||
| 109 | assert_match "Supplementary Exhibit", result | ||
| 110 | assert_match "headline_document_card", result | ||
| 111 | assert_match "related_documents_list", result | ||
| 112 | end | ||
| 113 | |||
| 114 | test "headline_image lists attached PDFs even with no headline chosen" do | ||
| 115 | node = Node.root.children.create!(:slug => "headline_image_pdf_no_headline_test") | ||
| 116 | pdf_a = Asset.create!(:name => "Document A", :upload_content_type => "application/pdf") | ||
| 117 | pdf_b = Asset.create!(:name => "Document B", :upload_content_type => "application/pdf") | ||
| 118 | node.draft.assets << pdf_a | ||
| 119 | node.draft.assets << pdf_b | ||
| 120 | @page = node.draft | ||
| 121 | |||
| 122 | result = headline_image | ||
| 123 | |||
| 124 | assert_no_match "headline_document_card", result | ||
| 125 | assert_match "Document A", result | ||
| 126 | assert_match "Document B", result | ||
| 127 | end | ||
| 82 | end | 128 | end |
diff --git a/test/models/node_attach_asset_test.rb b/test/models/node_attach_asset_test.rb new file mode 100644 index 0000000..cb5b60f --- /dev/null +++ b/test/models/node_attach_asset_test.rb | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | require "test_helper" | ||
| 2 | |||
| 3 | class NodeAttachAssetTest < ActiveSupport::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | @user = users(:quentin) | ||
| 7 | @other = users(:aaron) | ||
| 8 | @node = Node.root.children.create!(:slug => "attach_asset_test") | ||
| 9 | @image = create_image_asset | ||
| 10 | end | ||
| 11 | |||
| 12 | test "attaches to a draft-only node" do | ||
| 13 | result = @node.attach_asset!(@image, :user => @user) | ||
| 14 | assert_equal 1, result[:attached] | ||
| 15 | assert_includes @node.draft.assets, @image | ||
| 16 | end | ||
| 17 | |||
| 18 | test "attaches to head when no draft is pending" do | ||
| 19 | @node.publish_draft!(@user) | ||
| 20 | result = @node.attach_asset!(@image, :user => @user) | ||
| 21 | assert_equal 1, result[:attached] | ||
| 22 | assert_includes @node.head.assets, @image | ||
| 23 | end | ||
| 24 | |||
| 25 | test "attaches to head and pending draft alike" do | ||
| 26 | @node.publish_draft!(@user) | ||
| 27 | @node.lock_for_editing!(@user) | ||
| 28 | @node.create_new_draft(@user) | ||
| 29 | result = @node.attach_asset!(@image, :user => @user) | ||
| 30 | assert_equal 2, result[:attached] | ||
| 31 | assert_includes @node.head.assets, @image | ||
| 32 | assert_includes @node.draft.assets, @image | ||
| 33 | end | ||
| 34 | |||
| 35 | test "attaches to all three lifecycle rows" do | ||
| 36 | @node.publish_draft!(@user) | ||
| 37 | @node.lock_for_editing!(@user) | ||
| 38 | @node.create_new_draft(@user) | ||
| 39 | @node.autosave!({ :title => "wip" }, @user) | ||
| 40 | result = @node.attach_asset!(@image, :user => @user) | ||
| 41 | assert_equal 3, result[:attached] | ||
| 42 | [@node.head, @node.draft, @node.autosave].each do |row| | ||
| 43 | assert_includes row.assets, @image | ||
| 44 | end | ||
| 45 | end | ||
| 46 | |||
| 47 | test "skips rows that already carry the asset" do | ||
| 48 | @node.draft.related_assets.create!(:asset => @image) | ||
| 49 | @node.publish_draft!(@user) | ||
| 50 | @node.lock_for_editing!(@user) | ||
| 51 | @node.create_new_draft(@user) | ||
| 52 | result = @node.attach_asset!(@image, :user => @user) | ||
| 53 | assert_equal 0, result[:attached] | ||
| 54 | assert_equal 2, result[:already] | ||
| 55 | assert_equal 1, @node.draft.related_assets.where(:asset_id => @image.id).count | ||
| 56 | end | ||
| 57 | |||
| 58 | test "refuses when another user holds the lock and writes nothing" do | ||
| 59 | @node.lock_for_editing!(@other) | ||
| 60 | assert_raises(LockedByAnotherUser) { @node.attach_asset!(@image, :user => @user) } | ||
| 61 | assert_empty @node.draft.assets.reload | ||
| 62 | end | ||
| 63 | |||
| 64 | test "proceeds when the attaching user holds the lock" do | ||
| 65 | @node.lock_for_editing!(@user) | ||
| 66 | result = @node.attach_asset!(@image, :user => @user) | ||
| 67 | assert_equal 1, result[:attached] | ||
| 68 | end | ||
| 69 | |||
| 70 | test "sets the headline when none exists" do | ||
| 71 | result = @node.attach_asset!(@image, :user => @user, :headline => true) | ||
| 72 | assert_equal :set, result[:headline] | ||
| 73 | assert_equal @image, @node.draft.headline_asset | ||
| 74 | end | ||
| 75 | |||
| 76 | test "keeps an existing headline and reports it" do | ||
| 77 | incumbent = create_image_asset | ||
| 78 | @node.draft.related_assets.create!(:asset => incumbent, :headline => true) | ||
| 79 | result = @node.attach_asset!(@image, :user => @user, :headline => true) | ||
| 80 | assert_equal :kept_existing, result[:headline] | ||
| 81 | assert_equal incumbent, @node.draft.reload.headline_asset | ||
| 82 | assert_includes @node.draft.assets, @image | ||
| 83 | end | ||
| 84 | |||
| 85 | test "declines the headline flag for ineligible asset types" do | ||
| 86 | plain = create_plain_asset | ||
| 87 | result = @node.attach_asset!(plain, :user => @user, :headline => true) | ||
| 88 | assert_equal :not_eligible, result[:headline] | ||
| 89 | assert_includes @node.draft.assets.reload, plain | ||
| 90 | assert_nil @node.draft.headline_asset | ||
| 91 | end | ||
| 92 | |||
| 93 | test "refuses nodes in the Trash" do | ||
| 94 | @node.trash!(@user) | ||
| 95 | assert_raises(ActiveRecord::RecordInvalid) { @node.attach_asset!(@image, :user => @user) } | ||
| 96 | end | ||
| 97 | |||
| 98 | private | ||
| 99 | |||
| 100 | def create_image_asset | ||
| 101 | Asset.create!(:name => "attach test image", :upload_content_type => "image/png") | ||
| 102 | end | ||
| 103 | |||
| 104 | def create_plain_asset | ||
| 105 | Asset.create!(:name => "attach test note", :upload_content_type => "text/plain") | ||
| 106 | end | ||
| 107 | end | ||
diff --git a/test/models/node_test.rb b/test/models/node_test.rb index ab66f81..ba38340 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb | |||
| @@ -268,7 +268,6 @@ class NodeTest < ActiveSupport::TestCase | |||
| 268 | assert_equal "quentin", node.head.user.login | 268 | assert_equal "quentin", node.head.user.login |
| 269 | end | 269 | end |
| 270 | 270 | ||
| 271 | |||
| 272 | test "update?" do | 271 | test "update?" do |
| 273 | Node.root.descendants.delete_all | 272 | Node.root.descendants.delete_all |
| 274 | updates = Node.root.children.create!( :slug => "updates" ) | 273 | updates = Node.root.children.create!( :slug => "updates" ) |
| @@ -359,6 +358,16 @@ class NodeTest < ActiveSupport::TestCase | |||
| 359 | assert_equal @user1, node.reload.lock_owner | 358 | assert_equal @user1, node.reload.lock_owner |
| 360 | end | 359 | end |
| 361 | 360 | ||
| 361 | test "title reads from autosave when neither draft nor head exists yet" do | ||
| 362 | node = Node.root.children.create!(:slug => "title_autosave_only_test") | ||
| 363 | node.draft.destroy | ||
| 364 | node.update_column(:draft_id, nil) | ||
| 365 | node.lock_for_editing!(users(:quentin)) | ||
| 366 | node.autosave!({ :title => "autosave-only title" }, users(:quentin)) | ||
| 367 | |||
| 368 | assert_equal "autosave-only title", node.reload.title | ||
| 369 | end | ||
| 370 | |||
| 362 | test "revert! is a safe no-op on a fresh node with only a draft" do | 371 | test "revert! is a safe no-op on a fresh node with only a draft" do |
| 363 | node = create_node_with_draft | 372 | node = create_node_with_draft |
| 364 | node.lock_for_editing!(@user1) | 373 | node.lock_for_editing!(@user1) |
| @@ -537,30 +546,6 @@ class NodeTest < ActiveSupport::TestCase | |||
| 537 | assert result.index(mine) < result.index(someone_elses_newer) | 546 | assert result.index(mine) < result.index(someone_elses_newer) |
| 538 | end | 547 | end |
| 539 | 548 | ||
| 540 | test "recently_changed includes a node whose head was recently published" do | ||
| 541 | node = Node.root.children.create!(:slug => "recent_changed_published") | ||
| 542 | find_or_create_draft(node, @user1) | ||
| 543 | node.autosave!({:title => "v1"}, @user1) | ||
| 544 | node.save_draft!(@user1) | ||
| 545 | node.publish_draft! | ||
| 546 | |||
| 547 | assert_includes Node.recently_changed, node | ||
| 548 | end | ||
| 549 | |||
| 550 | test "recently_changed excludes a node only touched by locking or unlocking after an old publish" do | ||
| 551 | node = Node.root.children.create!(:slug => "recent_changed_lock_only") | ||
| 552 | find_or_create_draft(node, @user1) | ||
| 553 | node.autosave!({:title => "v1"}, @user1) | ||
| 554 | node.save_draft!(@user1) | ||
| 555 | node.publish_draft! | ||
| 556 | node.head.update_column(:updated_at, 20.days.ago) | ||
| 557 | |||
| 558 | node.lock_for_editing!(@user1) | ||
| 559 | node.unlock! | ||
| 560 | |||
| 561 | assert_not_includes Node.recently_changed, node | ||
| 562 | end | ||
| 563 | |||
| 564 | test "autosave! carries over the current related assets to the newly created autosave row" do | 549 | test "autosave! carries over the current related assets to the newly created autosave row" do |
| 565 | node = Node.root.children.create!(:slug => "autosave_asset_carryover_test") | 550 | node = Node.root.children.create!(:slug => "autosave_asset_carryover_test") |
| 566 | user = User.find_by_login("quentin") | 551 | user = User.find_by_login("quentin") |
diff --git a/test/models/page_test.rb b/test/models/page_test.rb index 1f924f9..98a00d2 100644 --- a/test/models/page_test.rb +++ b/test/models/page_test.rb | |||
| @@ -309,11 +309,15 @@ class PageTest < ActiveSupport::TestCase | |||
| 309 | 309 | ||
| 310 | kept_asset = Asset.create!(:upload_file_name => "kept.png", :upload_content_type => "image/png", :upload_file_size => 1) | 310 | kept_asset = Asset.create!(:upload_file_name => "kept.png", :upload_content_type => "image/png", :upload_file_size => 1) |
| 311 | removed_asset = Asset.create!(:upload_file_name => "removed.pdf", :upload_content_type => "application/pdf", :upload_file_size => 1) | 311 | removed_asset = Asset.create!(:upload_file_name => "removed.pdf", :upload_content_type => "application/pdf", :upload_file_size => 1) |
| 312 | n.head.update_assets([kept_asset.id, removed_asset.id]) | 312 | n.head.related_assets.delete_all |
| 313 | n.head.related_assets.create!(:asset_id => kept_asset.id, :position => 1) | ||
| 314 | n.head.related_assets.create!(:asset_id => removed_asset.id, :position => 2) | ||
| 313 | 315 | ||
| 314 | d2 = find_or_create_draft(n, @user1) | 316 | d2 = find_or_create_draft(n, @user1) |
| 315 | added_asset = Asset.create!(:upload_file_name => "added.png", :upload_content_type => "image/png", :upload_file_size => 1) | 317 | added_asset = Asset.create!(:upload_file_name => "added.png", :upload_content_type => "image/png", :upload_file_size => 1) |
| 316 | d2.update_assets([kept_asset.id, added_asset.id]) | 318 | d2.related_assets.delete_all |
| 319 | d2.related_assets.create!(:asset_id => kept_asset.id, :position => 1) | ||
| 320 | d2.related_assets.create!(:asset_id => added_asset.id, :position => 2) | ||
| 317 | d2.save! | 321 | d2.save! |
| 318 | 322 | ||
| 319 | diff = d2.diff_against(n.head) | 323 | diff = d2.diff_against(n.head) |
diff --git a/test/models/related_asset_test.rb b/test/models/related_asset_test.rb index 710b4cc..bb86ddb 100644 --- a/test/models/related_asset_test.rb +++ b/test/models/related_asset_test.rb | |||
| @@ -11,15 +11,25 @@ class RelatedAssetTest < ActiveSupport::TestCase | |||
| 11 | assert related.valid? | 11 | assert related.valid? |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | test "headline cannot be set on a non-image asset" do | 14 | test "headline can be set on a PDF asset" do |
| 15 | node = Node.root.children.create!(:slug => "related_asset_headline_pdf_test") | 15 | node = Node.root.children.create!(:slug => "related_asset_headline_pdf_test") |
| 16 | asset = Asset.create!(:name => "programme", :upload_content_type => "application/pdf") | 16 | asset = Asset.create!(:name => "expert opinion", :upload_content_type => "application/pdf") |
| 17 | node.draft.assets << asset | ||
| 18 | related = node.draft.related_assets.find_by(:asset_id => asset.id) | ||
| 19 | |||
| 20 | related.headline = true | ||
| 21 | assert related.valid? | ||
| 22 | end | ||
| 23 | |||
| 24 | test "headline cannot be set on a non-image, non-PDF asset" do | ||
| 25 | node = Node.root.children.create!(:slug => "related_asset_headline_text_test") | ||
| 26 | asset = Asset.create!(:name => "programme", :upload_content_type => "text/plain") | ||
| 17 | node.draft.assets << asset | 27 | node.draft.assets << asset |
| 18 | related = node.draft.related_assets.find_by(:asset_id => asset.id) | 28 | related = node.draft.related_assets.find_by(:asset_id => asset.id) |
| 19 | 29 | ||
| 20 | related.headline = true | 30 | related.headline = true |
| 21 | assert_not related.valid? | 31 | assert_not related.valid? |
| 22 | assert_includes related.errors[:headline], "can only be set on image assets" | 32 | assert_includes related.errors[:headline], "can only be set on image or PDF assets" |
| 23 | end | 33 | end |
| 24 | 34 | ||
| 25 | test "the headline validation does not raise when asset is missing" do | 35 | test "the headline validation does not raise when asset is missing" do |
