To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / ca / ca9a94f4269bc05500b823a9f42b210c9322360b.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (8.48 KB)
| 1 | 1296:038ba2d95de8 | Chris | require File.expand_path('../../test_helper', __FILE__)
|
|---|---|---|---|
| 2 | require 'search_controller' |
||
| 3 | |||
| 4 | # Re-raise errors caught by the controller. |
||
| 5 | class SearchController; def rescue_action(e) raise e end; end |
||
| 6 | |||
| 7 | class SearchControllerTest < ActionController::TestCase |
||
| 8 | fixtures :projects, :enabled_modules, :roles, :users, :members, :member_roles, |
||
| 9 | :issues, :trackers, :issue_statuses, :enumerations, |
||
| 10 | :custom_fields, :custom_values, |
||
| 11 | :repositories, :changesets |
||
| 12 | |||
| 13 | def setup |
||
| 14 | @controller = SearchController.new |
||
| 15 | @request = ActionController::TestRequest.new |
||
| 16 | @response = ActionController::TestResponse.new |
||
| 17 | User.current = nil |
||
| 18 | end |
||
| 19 | |||
| 20 | def test_search_for_projects |
||
| 21 | get :index |
||
| 22 | assert_response :success |
||
| 23 | assert_template 'index' |
||
| 24 | |||
| 25 | get :index, :q => "cook" |
||
| 26 | assert_response :success |
||
| 27 | assert_template 'index' |
||
| 28 | assert assigns(:results).include?(Project.find(1)) |
||
| 29 | end |
||
| 30 | |||
| 31 | def test_search_all_projects |
||
| 32 | get :index, :q => 'recipe subproject commit', :all_words => '' |
||
| 33 | assert_response :success |
||
| 34 | assert_template 'index' |
||
| 35 | |||
| 36 | assert assigns(:results).include?(Issue.find(2)) |
||
| 37 | assert assigns(:results).include?(Issue.find(5)) |
||
| 38 | assert assigns(:results).include?(Changeset.find(101)) |
||
| 39 | assert_tag :dt, :attributes => { :class => /issue/ },
|
||
| 40 | :child => { :tag => 'a', :content => /Add ingredients categories/ },
|
||
| 41 | :sibling => { :tag => 'dd', :content => /should be classified by categories/ }
|
||
| 42 | |||
| 43 | assert assigns(:results_by_type).is_a?(Hash) |
||
| 44 | assert_equal 5, assigns(:results_by_type)['changesets'] |
||
| 45 | assert_tag :a, :content => 'Changesets (5)' |
||
| 46 | end |
||
| 47 | |||
| 48 | def test_search_issues |
||
| 49 | get :index, :q => 'issue', :issues => 1 |
||
| 50 | assert_response :success |
||
| 51 | assert_template 'index' |
||
| 52 | |||
| 53 | assert_equal true, assigns(:all_words) |
||
| 54 | assert_equal false, assigns(:titles_only) |
||
| 55 | assert assigns(:results).include?(Issue.find(8)) |
||
| 56 | assert assigns(:results).include?(Issue.find(5)) |
||
| 57 | assert_tag :dt, :attributes => { :class => /issue closed/ },
|
||
| 58 | :child => { :tag => 'a', :content => /Closed/ }
|
||
| 59 | end |
||
| 60 | |||
| 61 | def test_search_issues_should_search_notes |
||
| 62 | Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword') |
||
| 63 | |||
| 64 | get :index, :q => 'searchkeyword', :issues => 1 |
||
| 65 | assert_response :success |
||
| 66 | assert_include Issue.find(2), assigns(:results) |
||
| 67 | end |
||
| 68 | |||
| 69 | def test_search_issues_with_multiple_matches_in_journals_should_return_issue_once |
||
| 70 | Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword') |
||
| 71 | Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword') |
||
| 72 | |||
| 73 | get :index, :q => 'searchkeyword', :issues => 1 |
||
| 74 | assert_response :success |
||
| 75 | assert_include Issue.find(2), assigns(:results) |
||
| 76 | assert_equal 1, assigns(:results).size |
||
| 77 | end |
||
| 78 | |||
| 79 | def test_search_issues_should_search_private_notes_with_permission_only |
||
| 80 | Journal.create!(:journalized => Issue.find(2), :notes => 'Private notes with searchkeyword', :private_notes => true) |
||
| 81 | @request.session[:user_id] = 2 |
||
| 82 | |||
| 83 | Role.find(1).add_permission! :view_private_notes |
||
| 84 | get :index, :q => 'searchkeyword', :issues => 1 |
||
| 85 | assert_response :success |
||
| 86 | assert_include Issue.find(2), assigns(:results) |
||
| 87 | |||
| 88 | Role.find(1).remove_permission! :view_private_notes |
||
| 89 | get :index, :q => 'searchkeyword', :issues => 1 |
||
| 90 | assert_response :success |
||
| 91 | assert_not_include Issue.find(2), assigns(:results) |
||
| 92 | end |
||
| 93 | |||
| 94 | def test_search_all_projects_with_scope_param |
||
| 95 | get :index, :q => 'issue', :scope => 'all' |
||
| 96 | assert_response :success |
||
| 97 | assert_template 'index' |
||
| 98 | assert assigns(:results).present? |
||
| 99 | end |
||
| 100 | |||
| 101 | def test_search_my_projects |
||
| 102 | @request.session[:user_id] = 2 |
||
| 103 | get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => '' |
||
| 104 | assert_response :success |
||
| 105 | assert_template 'index' |
||
| 106 | assert assigns(:results).include?(Issue.find(1)) |
||
| 107 | assert !assigns(:results).include?(Issue.find(5)) |
||
| 108 | end |
||
| 109 | |||
| 110 | def test_search_my_projects_without_memberships |
||
| 111 | # anonymous user has no memberships |
||
| 112 | get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => '' |
||
| 113 | assert_response :success |
||
| 114 | assert_template 'index' |
||
| 115 | assert assigns(:results).empty? |
||
| 116 | end |
||
| 117 | |||
| 118 | def test_search_project_and_subprojects |
||
| 119 | get :index, :id => 1, :q => 'recipe subproject', :scope => 'subprojects', :all_words => '' |
||
| 120 | assert_response :success |
||
| 121 | assert_template 'index' |
||
| 122 | assert assigns(:results).include?(Issue.find(1)) |
||
| 123 | assert assigns(:results).include?(Issue.find(5)) |
||
| 124 | end |
||
| 125 | |||
| 126 | def test_search_without_searchable_custom_fields |
||
| 127 | CustomField.update_all "searchable = #{ActiveRecord::Base.connection.quoted_false}"
|
||
| 128 | |||
| 129 | get :index, :id => 1 |
||
| 130 | assert_response :success |
||
| 131 | assert_template 'index' |
||
| 132 | assert_not_nil assigns(:project) |
||
| 133 | |||
| 134 | get :index, :id => 1, :q => "can" |
||
| 135 | assert_response :success |
||
| 136 | assert_template 'index' |
||
| 137 | end |
||
| 138 | |||
| 139 | def test_search_with_searchable_custom_fields |
||
| 140 | get :index, :id => 1, :q => "stringforcustomfield" |
||
| 141 | assert_response :success |
||
| 142 | results = assigns(:results) |
||
| 143 | assert_not_nil results |
||
| 144 | assert_equal 1, results.size |
||
| 145 | assert results.include?(Issue.find(7)) |
||
| 146 | end |
||
| 147 | |||
| 148 | def test_search_all_words |
||
| 149 | # 'all words' is on by default |
||
| 150 | get :index, :id => 1, :q => 'recipe updating saving', :all_words => '1' |
||
| 151 | assert_equal true, assigns(:all_words) |
||
| 152 | results = assigns(:results) |
||
| 153 | assert_not_nil results |
||
| 154 | assert_equal 1, results.size |
||
| 155 | assert results.include?(Issue.find(3)) |
||
| 156 | end |
||
| 157 | |||
| 158 | def test_search_one_of_the_words |
||
| 159 | get :index, :id => 1, :q => 'recipe updating saving', :all_words => '' |
||
| 160 | assert_equal false, assigns(:all_words) |
||
| 161 | results = assigns(:results) |
||
| 162 | assert_not_nil results |
||
| 163 | assert_equal 3, results.size |
||
| 164 | assert results.include?(Issue.find(3)) |
||
| 165 | end |
||
| 166 | |||
| 167 | def test_search_titles_only_without_result |
||
| 168 | get :index, :id => 1, :q => 'recipe updating saving', :titles_only => '1' |
||
| 169 | results = assigns(:results) |
||
| 170 | assert_not_nil results |
||
| 171 | assert_equal 0, results.size |
||
| 172 | end |
||
| 173 | |||
| 174 | def test_search_titles_only |
||
| 175 | get :index, :id => 1, :q => 'recipe', :titles_only => '1' |
||
| 176 | assert_equal true, assigns(:titles_only) |
||
| 177 | results = assigns(:results) |
||
| 178 | assert_not_nil results |
||
| 179 | assert_equal 2, results.size |
||
| 180 | end |
||
| 181 | |||
| 182 | def test_search_content |
||
| 183 | Issue.update_all("description = 'This is a searchkeywordinthecontent'", "id=1")
|
||
| 184 | |||
| 185 | get :index, :id => 1, :q => 'searchkeywordinthecontent', :titles_only => '' |
||
| 186 | assert_equal false, assigns(:titles_only) |
||
| 187 | results = assigns(:results) |
||
| 188 | assert_not_nil results |
||
| 189 | assert_equal 1, results.size |
||
| 190 | end |
||
| 191 | |||
| 192 | def test_search_with_offset |
||
| 193 | get :index, :q => 'coo', :offset => '20080806073000' |
||
| 194 | assert_response :success |
||
| 195 | results = assigns(:results) |
||
| 196 | assert results.any? |
||
| 197 | assert results.map(&:event_datetime).max < '20080806T073000'.to_time |
||
| 198 | end |
||
| 199 | |||
| 200 | def test_search_previous_with_offset |
||
| 201 | get :index, :q => 'coo', :offset => '20080806073000', :previous => '1' |
||
| 202 | assert_response :success |
||
| 203 | results = assigns(:results) |
||
| 204 | assert results.any? |
||
| 205 | assert results.map(&:event_datetime).min >= '20080806T073000'.to_time |
||
| 206 | end |
||
| 207 | |||
| 208 | def test_search_with_invalid_project_id |
||
| 209 | get :index, :id => 195, :q => 'recipe' |
||
| 210 | assert_response 404 |
||
| 211 | assert_nil assigns(:results) |
||
| 212 | end |
||
| 213 | |||
| 214 | def test_quick_jump_to_issue |
||
| 215 | # issue of a public project |
||
| 216 | get :index, :q => "3" |
||
| 217 | assert_redirected_to '/issues/3' |
||
| 218 | |||
| 219 | # issue of a private project |
||
| 220 | get :index, :q => "4" |
||
| 221 | assert_response :success |
||
| 222 | assert_template 'index' |
||
| 223 | end |
||
| 224 | |||
| 225 | def test_large_integer |
||
| 226 | get :index, :q => '4615713488' |
||
| 227 | assert_response :success |
||
| 228 | assert_template 'index' |
||
| 229 | end |
||
| 230 | |||
| 231 | def test_tokens_with_quotes |
||
| 232 | get :index, :id => 1, :q => '"good bye" hello "bye bye"' |
||
| 233 | assert_equal ["good bye", "hello", "bye bye"], assigns(:tokens) |
||
| 234 | end |
||
| 235 | |||
| 236 | def test_results_should_be_escaped_once |
||
| 237 | assert Issue.find(1).update_attributes(:subject => '<subject> escaped_once', :description => '<description> escaped_once') |
||
| 238 | get :index, :q => 'escaped_once' |
||
| 239 | assert_response :success |
||
| 240 | assert_select '#search-results' do |
||
| 241 | assert_select 'dt.issue a', :text => /<subject>/ |
||
| 242 | assert_select 'dd', :text => /<description>/ |
||
| 243 | end |
||
| 244 | end |
||
| 245 | |||
| 246 | def test_keywords_should_be_highlighted |
||
| 247 | assert Issue.find(1).update_attributes(:subject => 'subject highlighted', :description => 'description highlighted') |
||
| 248 | get :index, :q => 'highlighted' |
||
| 249 | assert_response :success |
||
| 250 | assert_select '#search-results' do |
||
| 251 | assert_select 'dt.issue a span.highlight', :text => 'highlighted' |
||
| 252 | assert_select 'dd span.highlight', :text => 'highlighted' |
||
| 253 | end |
||
| 254 | end |
||
| 255 | end |