diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 083317b65..312833c6c 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -1111,3 +1111,7 @@ td.day .calendar-text { display: flex; justify-content: center; } + +.tag-topic, .tag-operation { + text-decoration: none; +} diff --git a/app/helpers/materials_helper.rb b/app/helpers/materials_helper.rb index 9ccd964d3..b1d245014 100644 --- a/app/helpers/materials_helper.rb +++ b/app/helpers/materials_helper.rb @@ -71,17 +71,15 @@ def display_attribute(resource, attribute, show_label: true, title: nil, markdow ].any? value = resource.send(attribute) - if markdown - value = render_markdown(value) - end + value = render_markdown(value) if markdown if value.present? - if list - value = value.map do |v| - html_escape(block_given? ? yield(v) : v) - end - else - value = html_escape(block_given? ? yield(value) : value) - end + value = if list + value.map do |v| + html_escape(block_given? ? yield(v) : v) + end + else + html_escape(block_given? ? yield(value) : value) + end end string = "
" unless value.blank? || value.try(:strip) == 'License Not Specified' @@ -119,18 +117,31 @@ def embed_youtube(material) def keywords_and_topics(resource, limit: nil) tags = [] - %i[scientific_topic_names operation_names keywords].each do |field| - tags |= resource.send(field) if resource.respond_to?(field) + if resource.respond_to?(:scientific_topics) + tags += resource.scientific_topics.map do |term| + content_tag(:span, class: 'label label-info tag-topic') do + content_tag(:i, '', class: 'fa fa-flask') + ' ' + term.preferred_label + end + end end - limit_exceeded = limit && (tags.length > limit) - tags = tags.first(limit) if limit + if resource.respond_to?(:operations) + tags += resource.operations.map do |term| + content_tag(:span, class: 'label label-info tag-operation') do + content_tag(:i, '', class: 'fa fa-cogs') + ' ' + term.preferred_label + end + end + end - elements = tags.map do |tag| - content_tag(:span, tag, class: 'label label-info') + if resource.respond_to?(:keywords) + tags += resource.keywords.map do |tag| + content_tag(:span, tag, class: 'label label-info tag-keyword') + end end - elements << '…' if limit_exceeded - elements.join(' ').html_safe + limit_exceeded = limit && (tags.length > limit) + tags = tags.first(limit) if limit + tags << '…'.html_safe if limit_exceeded + safe_join(tags, ' ') end end diff --git a/app/views/common/_extra_metadata.html.erb b/app/views/common/_extra_metadata.html.erb index d32155ece..92ed02ddf 100644 --- a/app/views/common/_extra_metadata.html.erb +++ b/app/views/common/_extra_metadata.html.erb @@ -63,7 +63,9 @@ <%= display_attribute(resource, :contributors) { |values| values.join(', ') } if resource.respond_to?(:contributors) %> <%= display_attribute(resource, :remote_created_date) if resource.respond_to?(:remote_created_date) %> <%= display_attribute(resource, :remote_updated_date) if resource.respond_to?(:remote_updated_date) %> -<%= display_attribute(resource, :scientific_topics) { |values| values.map { |x| x.preferred_label }.join(', ') } %> +<%= display_attribute(resource, :scientific_topics) { + |values| safe_join(values.map { |x| link_to(x.preferred_label, x.uri, target: '_blank') }, ', ') +} %> <% if resource.is_a?(LearningPath) %> <%= display_attribute(resource, :status) { |value| material_status_title_for_label(value) } %> @@ -73,5 +75,7 @@ <% end %> <% if resource.respond_to?(:operations) -%> - <%= display_attribute(resource, :operations) { |values| values.map { |x| x.preferred_label }.join(', ') } %> + <%= display_attribute(resource, :operations) { + |values| safe_join(values.map { |x| link_to(x.preferred_label, x.uri, target: '_blank') }, ', ') + } %> <% end %> diff --git a/test/controllers/learning_paths_controller_test.rb b/test/controllers/learning_paths_controller_test.rb index fa2f34358..1cf40a752 100644 --- a/test/controllers/learning_paths_controller_test.rb +++ b/test/controllers/learning_paths_controller_test.rb @@ -12,7 +12,7 @@ class LearningPathsControllerTest < ActionController::TestCase description: 'New description' } end - #INDEX TESTS + # INDEX TESTS test 'should get index' do get :index assert_response :success @@ -49,7 +49,7 @@ class LearningPathsControllerTest < ActionController::TestCase assert_equal learning_paths_path, body['links']['self'] end - #NEW TESTS + # NEW TESTS test 'should get new page for curators and admins only' do get :new @@ -72,14 +72,14 @@ class LearningPathsControllerTest < ActionController::TestCase assert_response :success end - #EDIT TESTS + # EDIT TESTS test 'should not get edit page for not logged in users' do - #Not logged in = Redirect to login + # Not logged in = Redirect to login get :edit, params: { id: @learning_path } assert_redirected_to new_user_session_path end - #logged in but insufficient permissions = ERROR + # logged in but insufficient permissions = ERROR test 'should get edit for learning_path owner' do sign_in @learning_path.user get :edit, params: { id: @learning_path } @@ -93,20 +93,20 @@ class LearningPathsControllerTest < ActionController::TestCase end test 'should get edit for admin' do - #Owner of learning_path logged in = SUCCESS + # Owner of learning_path logged in = SUCCESS sign_in users(:admin) get :edit, params: { id: @learning_path } assert_response :success end test 'should not get edit page for regular user' do - #Administrator = SUCCESS + # Administrator = SUCCESS sign_in users(:another_regular_user) get :edit, params: { id: @learning_path } assert :forbidden end - #CREATE TEST + # CREATE TEST test 'should create learning_path for curator' do sign_in users(:curator) assert_difference('LearningPath.count') do @@ -146,7 +146,7 @@ class LearningPathsControllerTest < ActionController::TestCase assert_redirected_to new_user_session_path end - #SHOW TEST + # SHOW TEST test 'should show learning_path' do get :show, params: { id: @learning_path } assert_response :success @@ -183,21 +183,22 @@ class LearningPathsControllerTest < ActionController::TestCase assert_equal learning_path_path(assigns(:learning_path)), body['data']['links']['self'] end - #UPDATE TEST + # UPDATE TEST test 'should update learning_path' do sign_in @learning_path.user patch :update, params: { id: @learning_path, learning_path: @updated_learning_path } assert_redirected_to learning_path_path(assigns(:learning_path)) end - test "should add topics to learning_path" do + test 'should add topics to learning_path' do sign_in @learning_path.user learning_path = learning_paths(:two) assert_no_difference('LearningPathTopic.count') do assert_difference('LearningPathTopicLink.count', 1) do assert_difference('learning_path.topics.count', 1) do patch :update, params: { learning_path: { - topic_links_attributes: { '1': { topic_id: learning_path_topics(:goblet_things).id, order: 300 } } }, + topic_links_attributes: { '1': { topic_id: learning_path_topics(:goblet_things).id, order: 300 } } + }, id: learning_path.id } end end @@ -211,14 +212,15 @@ class LearningPathsControllerTest < ActionController::TestCase assert_equal learning_path_topics(:goblet_things), links[1].topic end - test "should remove topic from learning_path" do + test 'should remove topic from learning_path' do sign_in @learning_path.user learning_path = learning_paths(:two) assert_no_difference('LearningPathTopic.count') do assert_difference('LearningPathTopicLink.count', -1) do assert_difference('learning_path.topics.count', -1) do patch :update, params: { learning_path: { - topic_links_attributes: { '1': { id: learning_path.topic_link_ids.first, _destroy: '1' } } }, + topic_links_attributes: { '1': { id: learning_path.topic_link_ids.first, _destroy: '1' } } + }, id: learning_path.id } end end @@ -227,7 +229,7 @@ class LearningPathsControllerTest < ActionController::TestCase assert_empty assigns(:learning_path).topic_links end - test "should modify items in learning_path" do + test 'should modify items in learning_path' do sign_in @learning_path.user l1 = @learning_path.topic_links[0] @@ -248,7 +250,7 @@ class LearningPathsControllerTest < ActionController::TestCase end end - #DESTROY TEST + # DESTROY TEST test 'should destroy learning_path owned by user' do sign_in @learning_path.user assert_difference('LearningPath.count', -1) do @@ -273,9 +275,8 @@ class LearningPathsControllerTest < ActionController::TestCase assert_response :forbidden end - - #CONTENT TESTS - #BREADCRUMBS + # CONTENT TESTS + # BREADCRUMBS test 'breadcrumbs for learning_paths index' do get :index assert_response :success @@ -326,7 +327,7 @@ class LearningPathsControllerTest < ActionController::TestCase end end - #OTHER CONTENT + # OTHER CONTENT test 'learning path lists topics' do sign_in(users(:regular_user)) @@ -344,8 +345,8 @@ class LearningPathsControllerTest < ActionController::TestCase get :show, params: { id: @learning_path } assert_response :success - assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 #No Edit - assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 #No Edit + assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 # No Edit + assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 # No Edit end test 'do not show action buttons when not owner or admin' do @@ -353,8 +354,8 @@ class LearningPathsControllerTest < ActionController::TestCase get :show, params: { id: @learning_path } - assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 #No Edit - assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 #No Edit + assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 # No Edit + assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 # No Edit end test 'show action buttons when learning_path_curator' do @@ -375,7 +376,7 @@ class LearningPathsControllerTest < ActionController::TestCase assert_select 'a.btn[href=?]', learning_path_path(@learning_path), text: 'Delete', count: 1 end - #API Actions + # API Actions test 'should not allow access to private learning_paths' do sign_in users(:regular_user) get :show, params: { id: learning_paths(:in_development_learning_path) } @@ -453,11 +454,11 @@ class LearningPathsControllerTest < ActionController::TestCase assert_difference('LearningPathTopicLink.count', -1) do patch :update, params: { learning_path: { - topic_links_attributes: { '1': { id: @learning_path.topic_link_ids.first, _destroy: '1' } } }, + topic_links_attributes: { '1': { id: @learning_path.topic_link_ids.first, _destroy: '1' } } + }, id: @learning_path.id } assert_redirected_to learning_path_path(assigns(:learning_path)) end - end test 'should not allow non-collaborator to edit' do @@ -511,10 +512,10 @@ class LearningPathsControllerTest < ActionController::TestCase end response_materials = body.dig('data', 'relationships', 'materials', 'data') - assert_equal [materials[1].id, materials[0].id, materials[2].id], response_materials.map { |m| m['id'].to_i } + assert_equal([materials[1].id, materials[0].id, materials[2].id], response_materials.map { |m| m['id'].to_i }) response_events = body.dig('data', 'relationships', 'events', 'data') - assert_equal [events[1].id, events[0].id], response_events.map { |e| e['id'].to_i } + assert_equal([events[1].id, events[0].id], response_events.map { |e| e['id'].to_i }) end test 'should include back-reference to learning path in item links' do diff --git a/test/controllers/materials_controller_test.rb b/test/controllers/materials_controller_test.rb index e41945e12..bf37993a5 100644 --- a/test/controllers/materials_controller_test.rb +++ b/test/controllers/materials_controller_test.rb @@ -24,10 +24,10 @@ class MaterialsControllerTest < ActionController::TestCase @monitor = @failing_material.create_link_monitor(url: @failing_material.url, code: 404, fail_count: 5) end - #Tests + # Tests # INDEX, NEW, EDIT, CREATE, SHOW, BREADCRUMBS, TABS, API CHECKS - #INDEX TESTS + # INDEX TESTS test 'should get index' do get :index assert_response :success @@ -121,7 +121,7 @@ class MaterialsControllerTest < ActionController::TestCase assert_select '.broken-link-notice', text: /this material's URL.+since 2 April 2023/ end - #NEW TESTS + # NEW TESTS test 'should get new' do sign_in users(:regular_user) get :new @@ -129,11 +129,11 @@ class MaterialsControllerTest < ActionController::TestCase end test 'should get new page for logged in users only' do - #Redirect to login if not logged in + # Redirect to login if not logged in get :new assert_response :redirect sign_in users(:regular_user) - #Success for everyone else + # Success for everyone else get :new assert_response :success sign_in users(:admin) @@ -147,14 +147,14 @@ class MaterialsControllerTest < ActionController::TestCase assert_response :forbidden end - #EDIT TESTS + # EDIT TESTS test 'should not get edit page for not logged in users' do - #Not logged in = Redirect to login + # Not logged in = Redirect to login get :edit, params: { id: @material } assert_redirected_to new_user_session_path end - #logged in but insufficient permissions = ERROR + # logged in but insufficient permissions = ERROR test 'should get edit for material owner' do sign_in users(:regular_user) get :edit, params: { id: @material } @@ -162,7 +162,7 @@ class MaterialsControllerTest < ActionController::TestCase end test 'should get edit for admin' do - #Owner of material logged in = SUCCESS + # Owner of material logged in = SUCCESS sign_in users(:admin) get :edit, params: { id: @material } assert_response :success @@ -181,7 +181,7 @@ class MaterialsControllerTest < ActionController::TestCase end test 'should not get edit page for non-owner user' do - #Administrator = SUCCESS + # Administrator = SUCCESS sign_in users(:another_regular_user) get :edit, params: { id: @material } assert :forbidden @@ -194,7 +194,7 @@ class MaterialsControllerTest < ActionController::TestCase assert_response :success end - #CREATE TEST + # CREATE TEST test 'should create material for user' do sign_in users(:regular_user) assert_difference('Material.count') do @@ -268,8 +268,7 @@ class MaterialsControllerTest < ActionController::TestCase format: :json, material: { title: test_title, url: test_url, - content_provider_id: test_provider.id - } + content_provider_id: test_provider.id } } assert_response :success assert_equal 'application/json; charset=utf-8', response.content_type, 'response content type not matched' @@ -283,13 +282,13 @@ class MaterialsControllerTest < ActionController::TestCase assert_equal test_material.keywords, JSON.parse(response.body)['keywords'], 'keywords not matched' assert_equal test_material.status, JSON.parse(response.body)['status'], 'status not matched' - #optional attributes + # optional attributes assert_equal test_material.doi, JSON.parse(response.body)['doi'], 'doi not matched.' assert_equal test_provider.id, JSON.parse(response.body)['content_provider_id'], 'provider not matched' assert_equal test_material.resource_type, JSON.parse(response.body)['resource_type'], 'resource_type not matched' assert_equal test_material.version, JSON.parse(response.body)['version'], 'version not matched' assert_equal test_material.other_types, JSON.parse(response.body)['other_types'], 'other_types not matched' - #assert_equal test_material.events, JSON.parse(response.body)['events'], 'events not matched' + # assert_equal test_material.events, JSON.parse(response.body)['events'], 'events not matched' assert_equal test_material.target_audience, JSON.parse(response.body)['target_audience'], 'target audience not matched' assert_equal test_material.authors, JSON.parse(response.body)['authors'], 'authors not matched' assert_equal test_material.contributors, JSON.parse(response.body)['contributors'], 'contributors not matched' @@ -350,7 +349,7 @@ class MaterialsControllerTest < ActionController::TestCase assert_redirected_to new_user_session_path end - #SHOW TEST + # SHOW TEST test 'should show material' do get :show, params: { id: @material } assert_response :success @@ -421,7 +420,7 @@ class MaterialsControllerTest < ActionController::TestCase assert_equal material_url(assigns(:material), host: TeSS::Config.base_url), body['@id'] end - #UPDATE TEST + # UPDATE TEST test 'should update material' do sign_in @material.user patch :update, params: { id: @material, material: @updated_material } @@ -487,7 +486,7 @@ class MaterialsControllerTest < ActionController::TestCase assert_nil material.reload.edit_suggestion end - #DESTROY TEST + # DESTROY TEST test 'should destroy material owned by user' do sign_in users(:regular_user) assert_difference('Material.count', -1) do @@ -540,26 +539,26 @@ class MaterialsControllerTest < ActionController::TestCase assert_redirected_to materials_path end - #CONTENT TESTS - #BREADCRUMBS + # CONTENT TESTS + # BREADCRUMBS test 'breadcrumbs for materials index' do get :index assert_response :success - assert_select 'div.breadcrumbs', :text => /Home/, :count => 1 do - assert_select 'a[href=?]', root_path, :count => 1 - assert_select 'li[class=active]', :text => /Materials/, :count => 1 + assert_select 'div.breadcrumbs', text: /Home/, count: 1 do + assert_select 'a[href=?]', root_path, count: 1 + assert_select 'li[class=active]', text: /Materials/, count: 1 end end test 'breadcrumbs for showing material' do get :show, params: { id: @material } assert_response :success - assert_select 'div.breadcrumbs', :text => /Home/, :count => 1 do - assert_select 'a[href=?]', root_path, :count => 1 - assert_select 'li', :text => /Materials/, :count => 1 do - assert_select 'a[href=?]', materials_url, :count => 1 + assert_select 'div.breadcrumbs', text: /Home/, count: 1 do + assert_select 'a[href=?]', root_path, count: 1 + assert_select 'li', text: /Materials/, count: 1 do + assert_select 'a[href=?]', materials_url, count: 1 end - assert_select 'li[class=active]', :text => /#{@material.title}/, :count => 1 + assert_select 'li[class=active]', text: /#{@material.title}/, count: 1 end end @@ -567,15 +566,15 @@ class MaterialsControllerTest < ActionController::TestCase sign_in users(:regular_user) get :edit, params: { id: @material } assert_response :success - assert_select 'div.breadcrumbs', :text => /Home/, :count => 1 do - assert_select 'a[href=?]', root_path, :count => 1 - assert_select 'li', :text => /Materials/, :count => 1 do - assert_select 'a[href=?]', materials_url, :count => 1 + assert_select 'div.breadcrumbs', text: /Home/, count: 1 do + assert_select 'a[href=?]', root_path, count: 1 + assert_select 'li', text: /Materials/, count: 1 do + assert_select 'a[href=?]', materials_url, count: 1 end - assert_select 'li', :text => /#{@material.title}/, :count => 1 do - assert_select 'a[href=?]', material_url(@material), :count => 1 + assert_select 'li', text: /#{@material.title}/, count: 1 do + assert_select 'a[href=?]', material_url(@material), count: 1 end - assert_select 'li[class=active]', :text => /Edit/, :count => 1 + assert_select 'li[class=active]', text: /Edit/, count: 1 end end @@ -583,66 +582,65 @@ class MaterialsControllerTest < ActionController::TestCase sign_in users(:regular_user) get :new assert_response :success - assert_select 'div.breadcrumbs', :text => /Home/, :count => 1 do - assert_select 'a[href=?]', root_path, :count => 1 - assert_select 'li', :text => /Materials/, :count => 1 do - assert_select 'a[href=?]', materials_url, :count => 1 + assert_select 'div.breadcrumbs', text: /Home/, count: 1 do + assert_select 'a[href=?]', root_path, count: 1 + assert_select 'li', text: /Materials/, count: 1 do + assert_select 'a[href=?]', materials_url, count: 1 end - assert_select 'li[class=active]', :text => /New/, :count => 1 + assert_select 'li[class=active]', text: /New/, count: 1 end end - #OTHER CONTENT + # OTHER CONTENT test 'material has correct layout' do get :show, params: { id: @material } assert_response :success - assert_select 'h2', :text => @material.title #Has Title + assert_select 'h2', text: @material.title # Has Title assert_select 'a.btn', text: 'View material', count: 1 do assert_select 'a[href=?]', @material.url, count: 1 end - #Should not show when not logged in - assert_select 'a.btn[href=?]', edit_material_path(@material), :count => 0 #No Edit - assert_select 'a.btn[href=?]', material_path(@material), :count => 0 #No Edit + # Should not show when not logged in + assert_select 'a.btn[href=?]', edit_material_path(@material), count: 0 # No Edit + assert_select 'a.btn[href=?]', material_path(@material), count: 0 # No Edit end test 'do not show action buttons when not owner or admin' do sign_in users(:another_regular_user) get :show, params: { id: @material } - assert_select 'a.btn[href=?]', edit_material_path(@material), :count => 0 #No Edit - assert_select 'a.btn[href=?]', material_path(@material), :count => 0 #No Edit + assert_select 'a.btn[href=?]', edit_material_path(@material), count: 0 # No Edit + assert_select 'a.btn[href=?]', material_path(@material), count: 0 # No Edit end test 'show action buttons when owner' do sign_in users(:regular_user) get :show, params: { id: @material } - assert_select 'a.btn[href=?]', edit_material_path(@material), :count => 1 - assert_select 'a.btn[href=?]', material_path(@material), :text => 'Delete', :count => 1 + assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1 + assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1 end test 'show action buttons when admin' do sign_in users(:admin) get :show, params: { id: @material } - assert_select 'a.btn[href=?]', edit_material_path(@material), :count => 1 - assert_select 'a.btn[href=?]', material_path(@material), :text => 'Delete', :count => 1 + assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1 + assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1 end test 'show action buttons when approved editor' do @material.content_provider.add_editor users(:another_regular_user) sign_in users(:another_regular_user) get :show, params: { id: @material } - assert_select 'a.btn[href=?]', edit_material_path(@material), :count => 1 - assert_select 'a.btn[href=?]', material_path(@material), :text => 'Delete', :count => 1 + assert_select 'a.btn[href=?]', edit_material_path(@material), count: 1 + assert_select 'a.btn[href=?]', material_path(@material), text: 'Delete', count: 1 end - #API Actions + # API Actions test 'should find existing material by title and content provider' do post :check_exists, params: { format: :json, material: { title: @material.title, url: 'whatever.com', - content_provider_id: @material.content_provider_id - } + content_provider_id: @material.content_provider_id } } assert_response :success assert_equal(JSON.parse(response.body)['id'], @material.id) @@ -702,13 +700,13 @@ class MaterialsControllerTest < ActionController::TestCase test 'should display filters on index' do get :index - assert_select 'h4.nav-heading', :text => /Content provider/, :count => 0 - assert_select 'li.masonry-brick', :count => Material.count + assert_select 'h4.nav-heading', text: /Content provider/, count: 0 + assert_select 'li.masonry-brick', count: Material.count end test 'should create new material through API' do scraper_role = Role.fetch('scraper_user') - scraper_user = User.where(:role_id => scraper_role.id).first + scraper_user = User.where(role_id: scraper_role.id).first material_title = 'horse' assert scraper_user assert_difference('Material.count') do @@ -722,7 +720,7 @@ class MaterialsControllerTest < ActionController::TestCase contact: 'default contact', doi: 'https://doi.org/10.1001/RSE.2.190', licence: 'CC-BY-4.0', - keywords: ['scraped', 'through', 'api'], + keywords: %w[scraped through api], status: 'active' }, format: 'json' @@ -733,7 +731,7 @@ class MaterialsControllerTest < ActionController::TestCase test 'should not create new material without valid authentication token' do scraper_role = Role.fetch('scraper_user') - scraper_user = User.where(:role_id => scraper_role.id).first + scraper_user = User.where(role_id: scraper_role.id).first assert scraper_user assert_no_difference('Material.count') do @@ -747,7 +745,7 @@ class MaterialsControllerTest < ActionController::TestCase contact: 'default contact', doi: 'https://doi.org/10.1001/RSE.2.190', licence: 'CC-BY-4.0', - keywords: %{ invalid authtoken }, + keywords: %( invalid authtoken ), status: 'active' }, format: 'json' @@ -760,7 +758,7 @@ class MaterialsControllerTest < ActionController::TestCase user = users(:scraper_user) material = materials(:scraper_user_material) - new_title = "totally new title" + new_title = 'totally new title' assert_no_difference('Material.count') do patch :update, params: { user_token: user.authentication_token, @@ -783,7 +781,7 @@ class MaterialsControllerTest < ActionController::TestCase other_user = users(:another_regular_user) material = user.materials.first - new_title = "totally new title" + new_title = 'totally new title' assert_no_difference('Material.count') do patch :update, params: { user_token: other_user.authentication_token, @@ -848,7 +846,7 @@ class MaterialsControllerTest < ActionController::TestCase description: 'New description', url: 'http://new.url.com', content_provider_id: ContentProvider.first.id, - external_resources_attributes: { "1" => { title: 'Cool link', url: 'https://tess.elixir-uk.org/', _destroy: '0' } } + external_resources_attributes: { '1' => { title: 'Cool link', url: 'https://tess.elixir-uk.org/', _destroy: '0' } } } } end @@ -873,7 +871,7 @@ class MaterialsControllerTest < ActionController::TestCase description: 'New description', url: 'http://new.url.com', content_provider_id: ContentProvider.first.id, - external_resources_attributes: { "0" => { id: resource.id, _destroy: '1' } } + external_resources_attributes: { '0' => { id: resource.id, _destroy: '1' } } } } end @@ -895,7 +893,7 @@ class MaterialsControllerTest < ActionController::TestCase description: 'New description', url: 'http://new.url.com', content_provider_id: ContentProvider.first.id, - external_resources_attributes: { "1" => { id: resource.id, title: 'Cool link', + external_resources_attributes: { '1' => { id: resource.id, title: 'Cool link', url: 'http://www.reddit.com', _destroy: '0' } } } } @@ -907,19 +905,17 @@ class MaterialsControllerTest < ActionController::TestCase end # TODO: SOLR tests will not run on TRAVIS. Explore stratergy for testing solr -=begin - test 'should return matching materials' do - get 'index', :format => :json, :q => 'training' - assert_response :success - assert response.body.size > 0 - end - - test 'should return no matching materials' do - get 'index', :format => :json, :q => 'kdfsajfklasdjfljsdfljdsfjncvmn' - assert_response :success - assert_equal(response.body,'[]') - end -=end + # test 'should return matching materials' do + # get 'index', :format => :json, :q => 'training' + # assert_response :success + # assert response.body.size > 0 + # end + # + # test 'should return no matching materials' do + # get 'index', :format => :json, :q => 'kdfsajfklasdjfljsdfljdsfjncvmn' + # assert_response :success + # assert_equal(response.body,'[]') + # end test 'finds multiple preferred labels' do topic_one = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0154') topic_two = Edam::Ontology.instance.lookup('http://edamontology.org/topic_0078') @@ -966,10 +962,9 @@ class MaterialsControllerTest < ActionController::TestCase url: 'http://www.example.com/sanity/0', doi: 'https://doi.org/10.1100/RSE.2019.23', licence: 'CC-BY-4.0', - keywords: ['insanity', 'sanitized', 'sanitary'], + keywords: %w[insanity sanitized sanitary], contact: 'default contact', - status: 'development' - } + status: 'development' } } end @@ -1075,7 +1070,7 @@ class MaterialsControllerTest < ActionController::TestCase test 'can lock fields' do sign_in @material.user assert_difference('FieldLock.count', 2) do - patch :update, params: { id: @material, material: { title: 'hi', locked_fields: ['title', 'description'] } } + patch :update, params: { id: @material, material: { title: 'hi', locked_fields: %w[title description] } } end assert_redirected_to material_path(assigns(:material)) @@ -1254,10 +1249,9 @@ class MaterialsControllerTest < ActionController::TestCase url: 'http://example.com/dodgy-event', doi: 'https://doi.org/10.10067/SEA.2019.22', licence: 'CC-BY-4.0', - keywords: %w{ dodgy event }, + keywords: %w[dodgy event], contact: 'default contact', - status: 'archived' - } + status: 'archived' } } end end @@ -1272,7 +1266,7 @@ class MaterialsControllerTest < ActionController::TestCase title: @material.title, url: 'http://example.com/dodgy-event', doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', licence: 'CC-BY-4.0', contact: 'default contact', - keywords: %w{ dodgy event unverified user }) + keywords: %w[dodgy event unverified user]) assert_enqueued_jobs 0 do assert_difference('Material.count') do @@ -1281,8 +1275,7 @@ class MaterialsControllerTest < ActionController::TestCase title: @material.title, url: 'http://example.com/dodgy-event-2', licence: 'CC-BY-4.0', contact: 'default contact', status: 'active', doi: 'https://doi.org/10.10067/SEA.2019.22', - keywords: %w{ another dodgy event } - } + keywords: %w[another dodgy event] } } end end @@ -1322,7 +1315,7 @@ class MaterialsControllerTest < ActionController::TestCase contact: 'default contact', doi: 'https://doi.org/10.1001/RSE.2.190', licence: 'CC-BY-4.0', - keywords: ['scraped', 'through', 'api'], + keywords: %w[scraped through api], status: 'active' }, format: 'json' @@ -1395,11 +1388,11 @@ class MaterialsControllerTest < ActionController::TestCase end test 'should hide fields' do - with_settings(feature: { materials_disabled: ['licence', 'scientific_topics', 'resource_type'] }) do + with_settings(feature: { materials_disabled: %w[licence scientific_topics resource_type] }) do sign_in users(:regular_user) get :new assert_response :success - assert_select 'div.hidden' do |div| + assert_select 'div.hidden' do |_div| assert_select 'label', text: 'Licence', count: 1 assert_select 'label', text: 'Status', count: 0 end @@ -1435,8 +1428,7 @@ class MaterialsControllerTest < ActionController::TestCase description: 'hey', external_resources_attributes: [ { title: 'A tool perhaps', url: 'https://bio.tools/some_tool' } - ] - }} + ] } } assert_response :success assert_select 'h2', text: 'Potential material' @@ -1460,10 +1452,10 @@ class MaterialsControllerTest < ActionController::TestCase test 'should show unverified users material to themselves' do sign_in users(:unverified_user) material = users(:unverified_user).materials.create!(description: @material.description, - title: @material.title, url: 'http://example.com/dodgy-event', - doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', - licence: 'CC-BY-4.0', contact: 'default contact', - keywords: %w{ dodgy event unverified user }) + title: @material.title, url: 'http://example.com/dodgy-event', + doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', + licence: 'CC-BY-4.0', contact: 'default contact', + keywords: %w[dodgy event unverified user]) get :show, params: { id: material } @@ -1474,10 +1466,10 @@ class MaterialsControllerTest < ActionController::TestCase test 'should show unverified users material to admin' do material = users(:unverified_user).materials.create!(description: @material.description, - title: @material.title, url: 'http://example.com/dodgy-event', - doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', - licence: 'CC-BY-4.0', contact: 'default contact', - keywords: %w{ dodgy event unverified user }) + title: @material.title, url: 'http://example.com/dodgy-event', + doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', + licence: 'CC-BY-4.0', contact: 'default contact', + keywords: %w[dodgy event unverified user]) sign_in users(:admin) get :show, params: { id: material } @@ -1489,10 +1481,10 @@ class MaterialsControllerTest < ActionController::TestCase test 'should not show unverified users material anon user' do material = users(:unverified_user).materials.create!(description: @material.description, - title: @material.title, url: 'http://example.com/dodgy-event', - doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', - licence: 'CC-BY-4.0', contact: 'default contact', - keywords: %w{ dodgy event unverified user }) + title: @material.title, url: 'http://example.com/dodgy-event', + doi: 'https://doi.org/10.10067/SEA.2019.22', status: 'active', + licence: 'CC-BY-4.0', contact: 'default contact', + keywords: %w[dodgy event unverified user]) get :show, params: { id: material } assert_response :forbidden @@ -1604,4 +1596,16 @@ class MaterialsControllerTest < ActionController::TestCase assert_equal plant_space, assigns(:material).space end end + + test 'should render scientific topics and operations as links in extra metadata' do + @material.scientific_topic_uris = ['http://edamontology.org/topic_0622'] # Genomics + @material.operation_uris = ['http://edamontology.org/operation_0292'] # Sequence alignment + @material.save! + + get :show, params: { id: @material } + assert_response :success + + assert_select 'p.scientific_topics a[href="http://edamontology.org/topic_0622"]', text: 'Genomics' + assert_select 'p.operations a[href="http://edamontology.org/operation_0292"]', text: 'Sequence alignment' + end end diff --git a/test/controllers/stars_controller_test.rb b/test/controllers/stars_controller_test.rb index 49762dfe6..f54365ce8 100644 --- a/test/controllers/stars_controller_test.rb +++ b/test/controllers/stars_controller_test.rb @@ -1,10 +1,9 @@ require 'test_helper' class StarsControllerTest < ActionController::TestCase - include Devise::Test::ControllerHelpers - test "can star a resource" do + test 'can star a resource' do sign_in users(:regular_user) material = materials(:good_material) @@ -15,7 +14,7 @@ class StarsControllerTest < ActionController::TestCase assert_response :success end - test "does not add duplicate star to resource" do + test 'does not add duplicate star to resource' do user = users(:regular_user) sign_in user material = materials(:good_material) @@ -28,7 +27,7 @@ class StarsControllerTest < ActionController::TestCase assert_response :success end - test "can un-star resource" do + test 'can un-star resource' do user = users(:regular_user) sign_in user material = materials(:good_material) @@ -41,7 +40,7 @@ class StarsControllerTest < ActionController::TestCase assert_response :success end - test "cannot star a resource if not logged in" do + test 'cannot star a resource if not logged in' do material = materials(:good_material) assert_no_difference('Star.count') do @@ -51,7 +50,7 @@ class StarsControllerTest < ActionController::TestCase assert_response :unauthorized end - test "cannot create bad star" do + test 'cannot create bad star' do user = users(:regular_user) sign_in user @@ -62,7 +61,7 @@ class StarsControllerTest < ActionController::TestCase assert_response :unprocessable_entity end - test "can view list of stars" do + test 'can view list of stars' do user = users(:regular_user) sign_in user material = materials(:good_material) @@ -71,7 +70,7 @@ class StarsControllerTest < ActionController::TestCase get :index assert_response :success - assert_select '#materials div.search-results-count', text: "1 Material", count: 1 + assert_select '#materials div.search-results-count', text: '1 Material', count: 1 assert_select '#materials li.masonry-brick a.link-overlay[href=?]', material_path(material), count: 1 end end diff --git a/test/helpers/materials_helper_test.rb b/test/helpers/materials_helper_test.rb index 91a6c3f0a..f348b4fd1 100644 --- a/test/helpers/materials_helper_test.rb +++ b/test/helpers/materials_helper_test.rb @@ -1,12 +1,46 @@ require 'test_helper' class MaterialsHelperTest < ActionView::TestCase - - test "edam file loaded successfully" do + test 'edam file loaded successfully' do topics = scientific_topic_names_for_autocomplete assert_equal topics.class, Array assert_not_empty topics assert_includes topics, 'Metabolomics' end + test 'keywords_and_topics generates spans with css classes for scientific topics and operations' do + topic_term = OpenStruct.new(preferred_label: 'Genomics', uri: 'http://edamontology.org/topic_0622') + operation_term = OpenStruct.new(preferred_label: 'Sequence alignment', uri: 'http://edamontology.org/operation_0292') + resource = OpenStruct.new( + scientific_topics: [topic_term], + operations: [operation_term], + keywords: %w[keyword1 keyword2] + ) + result = keywords_and_topics(resource) + + assert_includes result, 'Genomics' + assert_includes result, 'tag-topic' + assert_includes result, 'Sequence alignment' + assert_includes result, 'tag-operation' + assert_includes result, 'keyword1' + assert_includes result, 'keyword2' + end + + test 'keywords_and_topics handles missing attributes' do + resource = OpenStruct.new + + result = keywords_and_topics(resource) + assert_equal '', result + end + + test 'keywords_and_topics with limit' do + topic_term = OpenStruct.new(preferred_label: 'Genomics', uri: 'http://edamontology.org/topic_0622') + resource = OpenStruct.new( + scientific_topics: [topic_term], + keywords: %w[keyword1 keyword2 keyword3] + ) + result = keywords_and_topics(resource, limit: 2) + + assert_includes result, '…' + end end