From 50efff371741949e377f8f9c11b724b1981f373a Mon Sep 17 00:00:00 2001 From: Tim Pierce Date: Thu, 30 Oct 2014 10:22:02 -0400 Subject: [PATCH] 4088: use filterable.js to filter on client side Implemented client-side filtering via filterable.js: * Designated filename rows as "filterable" * Designated filename pattern input field as "filterable-control" * Added "Select all" and "Unselect all" buttons with matching select_all_files() and unselect_all_files() actions * Limit selection actions (compare, move, copy etc) to visible page elements only. * Added integration tests "Filtering collection files by regexp" and "Creating collection from list of filtered files". --- .../app/assets/javascripts/selection.js.erb | 2 +- .../views/collections/_show_files.html.erb | 59 ++++---- .../test/integration/collections_test.rb | 133 ++++++++++++++++-- 3 files changed, 147 insertions(+), 47 deletions(-) diff --git a/apps/workbench/app/assets/javascripts/selection.js.erb b/apps/workbench/app/assets/javascripts/selection.js.erb index 59e6425698..ca4dce3d9b 100644 --- a/apps/workbench/app/assets/javascripts/selection.js.erb +++ b/apps/workbench/app/assets/javascripts/selection.js.erb @@ -183,7 +183,7 @@ function dispatch_selection_action() { } $(this). closest('.selection-action-container'). - find(':checkbox:checked'). + find(':checkbox:checked:visible'). each(function() { data.push({name: param_name, value: $(this).val()}); }); diff --git a/apps/workbench/app/views/collections/_show_files.html.erb b/apps/workbench/app/views/collections/_show_files.html.erb index 9cd77b02e1..6f26bda0d9 100644 --- a/apps/workbench/app/views/collections/_show_files.html.erb +++ b/apps/workbench/app/views/collections/_show_files.html.erb @@ -1,3 +1,24 @@ + +
<% if !defined? no_checkboxes or !no_checkboxes %>
@@ -15,50 +36,24 @@ 'data-toggle' => 'dropdown' %> + +
-
- <%= form_tag collection_path(@object.uuid), {method: 'get'} do %> -
- - - - -
- <% end %> +
+

<% end %> -<% file_regex = nil %> -<% if params[:file_regex] %> - <% begin %> - <% file_regex = Regexp.new(params[:file_regex]) %> - <% rescue RegexpError %> - <% # If the pattern is not a valid regex, quote it %> - <% # (i.e. use it as a simple substring search) %> -

-

The search term <%= params[:file_regex] %> could not be parsed as a regular expression.

-

Searching for files named <%= params[:file_regex] %> instead.

-
- <% file_regex = Regexp.new(Regexp.quote(params[:file_regex])) %> - <% end %> -<% end %> - <% file_tree = @object.andand.files_tree %> <% if file_tree.nil? or file_tree.empty? %>

This collection is empty.

<% else %> @@ -73,7 +68,7 @@ <% else %> <% link_params = {controller: 'collections', action: 'show_file', uuid: @object.portable_data_hash, file: file_path, size: size} %> -
+
<%= raw(human_readable_bytes_html(size)) %> <% disable_search = (Rails.configuration.filename_suffixes_with_view_icon.include? file_path.split('.')[-1]) ? false : true %> diff --git a/apps/workbench/test/integration/collections_test.rb b/apps/workbench/test/integration/collections_test.rb index 3abbf6f6ac..68ab0abf5f 100644 --- a/apps/workbench/test/integration/collections_test.rb +++ b/apps/workbench/test/integration/collections_test.rb @@ -202,26 +202,26 @@ class CollectionsTest < ActionDispatch::IntegrationTest end test "Filtering collection files by regexp" do + headless = Headless.new + headless.start + Capybara.current_driver = :selenium col = api_fixture('collections', 'multilevel_collection_1') visit page_with_token('active', "/collections/#{col['uuid']}") - # Test when only some files match the regex + # Filter file list to some but not all files in the collection page.find_field('file_regex').set('file[12]') - find('button#file_regex_submit').click assert page.has_text?("file1") assert page.has_text?("file2") assert page.has_no_text?("file3") - # Test all files matching the regex - page.find_field('file_regex').set('file[123]') - find('button#file_regex_submit').click + # Filter file list with a regex matching all files + page.find_field('file_regex').set('.*') assert page.has_text?("file1") assert page.has_text?("file2") assert page.has_text?("file3") - # Test no files matching the regex + # Filter file list to a regex matching no files page.find_field('file_regex').set('file9') - find('button#file_regex_submit').click assert page.has_no_text?("file1") assert page.has_no_text?("file2") assert page.has_no_text?("file3") @@ -230,13 +230,118 @@ class CollectionsTest < ActionDispatch::IntegrationTest assert page.has_text?("multilevel_collection_1") assert page.has_text?(col['portable_data_hash']) - # Syntactically invalid regex - # Page loads, but does not match any files + # Set filename filter to a syntactically invalid regex + # Page loads, but stops filtering after the last valid regex parse page.find_field('file_regex').set('file[2') - find('button#file_regex_submit').click - assert page.has_text?('could not be parsed as a regular expression') - assert page.has_no_text?("file1") - assert page.has_no_text?("file2") - assert page.has_no_text?("file3") + assert page.has_text?("multilevel_collection_1") + assert page.has_text?(col['portable_data_hash']) + assert page.has_text?("file1") + assert page.has_text?("file2") + assert page.has_text?("file3") + + # Test the "Select all" button + + # Note: calling .set('') on a Selenium element is not sufficient + # to reset the field for this test, as it does not send any key + # events to the browser. To clear the field, we must instead send + # a backspace character. + # See https://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html#clear-instance_method + page.find_field('file_regex').set("\b") # backspace + find('button#select-all').click + page.all('input[type=checkbox]').each do |checkbox| + assert checkbox.checked? + end + + # Test the "Unselect all" button + page.find_field('file_regex').set("\b") # backspace + find('button#unselect-all').click + page.all('input[type=checkbox]').each do |checkbox| + refute checkbox.checked? + end + + # Filter files, then "select all", then unfilter + page.find_field('file_regex').set("\b") # backspace + find('button#unselect-all').click + page.find_field('file_regex').set('file[12]') + find('button#select-all').click + page.find_field('file_regex').set("\b") # backspace + + # all "file1" and "file2" checkboxes must be selected + # all "file3" checkboxes must be clear + assert page.has_selector?('[value*="file1"]') + page.all('[value*="file1"]').each do |checkbox| + assert checkbox.checked?, 'checkboxes for file1 should be selected after filtering' + end + assert page.has_selector?('[value*="file2"]') + page.all('[value*="file2"]').each do |checkbox| + assert checkbox.checked?, 'checkboxes for file2 should be selected after filtering' + end + assert page.has_selector?('[value*="file3"]') + page.all('[value*="file3"]').each do |checkbox| + refute checkbox.checked?, 'checkboxes for file3 should be clear after filtering' + end + + # Select all files, then filter, then "unselect all", then unfilter + page.find_field('file_regex').set("\b") # backspace + find('button#select-all').click + page.find_field('file_regex').set('file[12]') + find('button#unselect-all').click + page.find_field('file_regex').set("\b") # backspace + + # all "file1" and "file2" checkboxes must be clear + # all "file3" checkboxes must be selected + assert page.has_selector?('[value*="file1"]') + page.all('[value*="file1"]').each do |checkbox| + refute checkbox.checked?, 'checkboxes for file1 should be clear after filtering' + end + assert page.has_selector?('[value*="file2"]') + page.all('[value*="file2"]').each do |checkbox| + refute checkbox.checked?, 'checkboxes for file2 should be clear after filtering' + end + assert page.has_selector?('[value*="file3"]') + page.all('[value*="file3"]').each do |checkbox| + assert checkbox.checked?, 'checkboxes for file3 should be selected after filtering' + end + end + + test "Creating collection from list of filtered files" do + headless = Headless.new + headless.start + Capybara.current_driver = :selenium + + col = api_fixture('collections', 'collection_with_files_in_subdir') + visit page_with_token('user1_with_load', "/collections/#{col['uuid']}") + assert page.has_text?('file_in_subdir1'), 'expected file_in_subdir1 not found' + assert page.has_text?('file1_in_subdir3'), 'expected file1_in_subdir3 not found' + assert page.has_text?('file2_in_subdir3'), 'expected file2_in_subdir3 not found' + assert page.has_text?('file1_in_subdir4'), 'expected file1_in_subdir4 not found' + assert page.has_text?('file2_in_subdir4'), 'expected file2_in_subdir4 not found' + + # Select all files but then filter them to files in subdir1, subdir2 or subdir3 + find('button#select-all').click + page.find_field('file_regex').set('_in_subdir[123]') + assert page.has_text?('file_in_subdir1'), 'expected file_in_subdir1 not in filtered files' + assert page.has_text?('file1_in_subdir3'), 'expected file1_in_subdir3 not in filtered files' + assert page.has_text?('file2_in_subdir3'), 'expected file2_in_subdir3 not in filtered files' + refute page.has_text?('file1_in_subdir4'), 'file1_in_subdir4 found in filtered files' + refute page.has_text?('file2_in_subdir4'), 'file2_in_subdir4 found in filtered files' + + # Create a new collection + click_button 'Selection...' + within('.selection-action-container') do + click_link 'Create new collection with selected files' + end + + # now in the newly created collection page + assert page.has_text?('Content hash:'), 'not on new collection page' + refute page.has_text?(col['uuid']), 'new collection page has old collection uuid' + refute page.has_text?(col['portable_data_hash']), 'new collection page has old portable_data_hash' + + # must have files in subdir1 and subdir3 but not subdir4 + assert page.has_text?('file_in_subdir1'), 'file_in_subdir1 missing from new collection' + assert page.has_text?('file1_in_subdir3'), 'file1_in_subdir3 missing from new collection' + assert page.has_text?('file2_in_subdir3'), 'file2_in_subdir3 missing from new collection' + refute page.has_text?('file1_in_subdir4'), 'file1_in_subdir4 found in new collection' + refute page.has_text?('file2_in_subdir4'), 'file2_in_subdir4 found in new collection' end end -- 2.30.2