@@ -1,207 +1,203 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
4 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; either version 2 |
|
8 | # as published by the Free Software Foundation; either version 2 | |
9 | # of the License, or (at your option) any later version. |
|
9 | # of the License, or (at your option) any later version. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 |
|
19 | |||
20 | require File.expand_path('../../test_helper', __FILE__) |
|
20 | require File.expand_path('../../test_helper', __FILE__) | |
21 |
|
21 | |||
22 | class SearchTest < ActiveSupport::TestCase |
|
22 | class SearchTest < ActiveSupport::TestCase | |
23 | fixtures :users, |
|
23 | fixtures :users, | |
24 | :members, |
|
24 | :members, | |
25 | :member_roles, |
|
25 | :member_roles, | |
26 | :projects, |
|
26 | :projects, | |
27 | :projects_trackers, |
|
27 | :projects_trackers, | |
28 | :roles, |
|
28 | :roles, | |
29 | :enabled_modules, |
|
29 | :enabled_modules, | |
30 | :issues, |
|
30 | :issues, | |
31 | :trackers, |
|
31 | :trackers, | |
32 | :issue_statuses, |
|
32 | :issue_statuses, | |
33 | :enumerations, |
|
33 | :enumerations, | |
34 | :journals, |
|
34 | :journals, | |
35 | :journal_details, |
|
35 | :journal_details, | |
36 | :repositories, |
|
36 | :repositories, | |
37 | :changesets |
|
37 | :changesets | |
38 |
|
38 | |||
39 | def setup |
|
39 | def setup | |
40 | @project = Project.find(1) |
|
40 | @project = Project.find(1) | |
41 | @issue_keyword = '%unable to print recipes%' |
|
41 | @issue_keyword = '%unable to print recipes%' | |
42 | @issue = Issue.find(1) |
|
42 | @issue = Issue.find(1) | |
43 | @changeset_keyword = '%very first commit%' |
|
43 | @changeset_keyword = '%very first commit%' | |
44 | @changeset = Changeset.find(100) |
|
44 | @changeset = Changeset.find(100) | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def test_search_by_anonymous |
|
47 | def test_search_by_anonymous | |
48 | User.current = nil |
|
48 | User.current = nil | |
49 |
|
49 | |||
50 | r = Issue.search_results(@issue_keyword) |
|
50 | r = Issue.search_results(@issue_keyword) | |
51 | assert r.include?(@issue) |
|
51 | assert r.include?(@issue) | |
52 | r = Changeset.search_results(@changeset_keyword) |
|
52 | r = Changeset.search_results(@changeset_keyword) | |
53 | assert r.include?(@changeset) |
|
53 | assert r.include?(@changeset) | |
54 |
|
54 | |||
55 | # Removes the :view_changesets permission from Anonymous role |
|
55 | # Removes the :view_changesets permission from Anonymous role | |
56 | remove_permission Role.anonymous, :view_changesets |
|
56 | remove_permission Role.anonymous, :view_changesets | |
57 | User.current = nil |
|
57 | User.current = nil | |
58 |
|
58 | |||
59 | r = Issue.search_results(@issue_keyword) |
|
59 | r = Issue.search_results(@issue_keyword) | |
60 | assert r.include?(@issue) |
|
60 | assert r.include?(@issue) | |
61 | r = Changeset.search_results(@changeset_keyword) |
|
61 | r = Changeset.search_results(@changeset_keyword) | |
62 | assert !r.include?(@changeset) |
|
62 | assert !r.include?(@changeset) | |
63 |
|
63 | |||
64 | # Make the project private |
|
64 | # Make the project private | |
65 | @project.update_attribute :is_public, false |
|
65 | @project.update_attribute :is_public, false | |
66 | r = Issue.search_results(@issue_keyword) |
|
66 | r = Issue.search_results(@issue_keyword) | |
67 | assert !r.include?(@issue) |
|
67 | assert !r.include?(@issue) | |
68 | r = Changeset.search_results(@changeset_keyword) |
|
68 | r = Changeset.search_results(@changeset_keyword) | |
69 | assert !r.include?(@changeset) |
|
69 | assert !r.include?(@changeset) | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def test_search_by_user |
|
72 | def test_search_by_user | |
73 | User.current = User.find_by_login('rhill') |
|
73 | User.current = User.find_by_login('rhill') | |
74 | assert User.current.memberships.empty? |
|
74 | assert User.current.memberships.empty? | |
75 |
|
75 | |||
76 | r = Issue.search_results(@issue_keyword) |
|
76 | r = Issue.search_results(@issue_keyword) | |
77 | assert r.include?(@issue) |
|
77 | assert r.include?(@issue) | |
78 | r = Changeset.search_results(@changeset_keyword) |
|
78 | r = Changeset.search_results(@changeset_keyword) | |
79 | assert r.include?(@changeset) |
|
79 | assert r.include?(@changeset) | |
80 |
|
80 | |||
81 | # Removes the :view_changesets permission from Non member role |
|
81 | # Removes the :view_changesets permission from Non member role | |
82 | remove_permission Role.non_member, :view_changesets |
|
82 | remove_permission Role.non_member, :view_changesets | |
83 | User.current = User.find_by_login('rhill') |
|
83 | User.current = User.find_by_login('rhill') | |
84 |
|
84 | |||
85 | r = Issue.search_results(@issue_keyword) |
|
85 | r = Issue.search_results(@issue_keyword) | |
86 | assert r.include?(@issue) |
|
86 | assert r.include?(@issue) | |
87 | r = Changeset.search_results(@changeset_keyword) |
|
87 | r = Changeset.search_results(@changeset_keyword) | |
88 | assert !r.include?(@changeset) |
|
88 | assert !r.include?(@changeset) | |
89 |
|
89 | |||
90 | # Make the project private |
|
90 | # Make the project private | |
91 | @project.update_attribute :is_public, false |
|
91 | @project.update_attribute :is_public, false | |
92 | r = Issue.search_results(@issue_keyword) |
|
92 | r = Issue.search_results(@issue_keyword) | |
93 | assert !r.include?(@issue) |
|
93 | assert !r.include?(@issue) | |
94 | r = Changeset.search_results(@changeset_keyword) |
|
94 | r = Changeset.search_results(@changeset_keyword) | |
95 | assert !r.include?(@changeset) |
|
95 | assert !r.include?(@changeset) | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | def test_search_by_allowed_member |
|
98 | def test_search_by_allowed_member | |
99 | User.current = User.find_by_login('jsmith') |
|
99 | User.current = User.find_by_login('jsmith') | |
100 | assert User.current.projects.include?(@project) |
|
100 | assert User.current.projects.include?(@project) | |
101 |
|
101 | |||
102 | r = Issue.search_results(@issue_keyword) |
|
102 | r = Issue.search_results(@issue_keyword) | |
103 | assert r.include?(@issue) |
|
103 | assert r.include?(@issue) | |
104 | r = Changeset.search_results(@changeset_keyword) |
|
104 | r = Changeset.search_results(@changeset_keyword) | |
105 | assert r.include?(@changeset) |
|
105 | assert r.include?(@changeset) | |
106 |
|
106 | |||
107 | # Make the project private |
|
107 | # Make the project private | |
108 | @project.update_attribute :is_public, false |
|
108 | @project.update_attribute :is_public, false | |
109 | r = Issue.search_results(@issue_keyword) |
|
109 | r = Issue.search_results(@issue_keyword) | |
110 | assert r.include?(@issue) |
|
110 | assert r.include?(@issue) | |
111 | r = Changeset.search_results(@changeset_keyword) |
|
111 | r = Changeset.search_results(@changeset_keyword) | |
112 | assert r.include?(@changeset) |
|
112 | assert r.include?(@changeset) | |
113 | end |
|
113 | end | |
114 |
|
114 | |||
115 | def test_search_by_unallowed_member |
|
115 | def test_search_by_unallowed_member | |
116 | # Removes the :view_changesets permission from user's and non member role |
|
116 | # Removes the :view_changesets permission from user's and non member role | |
117 | remove_permission Role.find(1), :view_changesets |
|
117 | remove_permission Role.find(1), :view_changesets | |
118 | remove_permission Role.non_member, :view_changesets |
|
118 | remove_permission Role.non_member, :view_changesets | |
119 |
|
119 | |||
120 | User.current = User.find_by_login('jsmith') |
|
120 | User.current = User.find_by_login('jsmith') | |
121 | assert User.current.projects.include?(@project) |
|
121 | assert User.current.projects.include?(@project) | |
122 |
|
122 | |||
123 | r = Issue.search_results(@issue_keyword) |
|
123 | r = Issue.search_results(@issue_keyword) | |
124 | assert r.include?(@issue) |
|
124 | assert r.include?(@issue) | |
125 | r = Changeset.search_results(@changeset_keyword) |
|
125 | r = Changeset.search_results(@changeset_keyword) | |
126 | assert !r.include?(@changeset) |
|
126 | assert !r.include?(@changeset) | |
127 |
|
127 | |||
128 | # Make the project private |
|
128 | # Make the project private | |
129 | @project.update_attribute :is_public, false |
|
129 | @project.update_attribute :is_public, false | |
130 | r = Issue.search_results(@issue_keyword) |
|
130 | r = Issue.search_results(@issue_keyword) | |
131 | assert r.include?(@issue) |
|
131 | assert r.include?(@issue) | |
132 | r = Changeset.search_results(@changeset_keyword) |
|
132 | r = Changeset.search_results(@changeset_keyword) | |
133 | assert !r.include?(@changeset) |
|
133 | assert !r.include?(@changeset) | |
134 | end |
|
134 | end | |
135 |
|
135 | |||
136 | def test_search_issue_with_multiple_hits_in_journals |
|
136 | def test_search_issue_with_multiple_hits_in_journals | |
137 | issue = Issue.find(1) |
|
137 | issue = Issue.find(1) | |
138 | assert_equal 2, issue.journals.where("notes LIKE '%notes%'").count |
|
138 | assert_equal 2, issue.journals.where("notes LIKE '%notes%'").count | |
139 |
|
139 | |||
140 | r = Issue.search_results('%notes%') |
|
140 | r = Issue.search_results('%notes%') | |
141 | assert_equal 1, r.size |
|
141 | assert_equal 1, r.size | |
142 | assert_equal issue, r.first |
|
142 | assert_equal issue, r.first | |
143 | end |
|
143 | end | |
144 |
|
144 | |||
145 | def test_search_should_be_case_insensitive |
|
145 | def test_search_should_be_case_insensitive | |
146 | issue = Issue.generate!(:subject => "AzerTY") |
|
146 | issue = Issue.generate!(:subject => "AzerTY") | |
147 |
|
147 | |||
148 | r = Issue.search_results('AZERty') |
|
148 | r = Issue.search_results('AZERty') | |
149 | assert_include issue, r |
|
149 | assert_include issue, r | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def test_search_should_be_case_insensitive_with_accented_characters |
|
152 | def test_search_should_be_case_insensitive_with_accented_characters | |
153 | unless sqlite? |
|
153 | unless sqlite? | |
154 | issue1 = Issue.generate!(:subject => "Special chars: ÖÖ") |
|
154 | issue1 = Issue.generate!(:subject => "Special chars: ÖÖ") | |
155 | issue2 = Issue.generate!(:subject => "Special chars: Öö") |
|
155 | issue2 = Issue.generate!(:subject => "Special chars: Öö") | |
156 |
|
||||
157 | r = Issue.search_results('ÖÖ') |
|
156 | r = Issue.search_results('ÖÖ') | |
158 | assert_include issue1, r |
|
157 | assert_include issue1, r | |
159 | assert_include issue2, r |
|
158 | assert_include issue2, r | |
160 | end |
|
159 | end | |
161 | end |
|
160 | end | |
162 |
|
161 | |||
163 | def test_search_should_be_case_and_accent_insensitive_with_mysql |
|
162 | def test_search_should_be_case_and_accent_insensitive_with_mysql | |
164 | if mysql? |
|
163 | if mysql? | |
165 | issue1 = Issue.generate!(:subject => "OO") |
|
164 | issue1 = Issue.generate!(:subject => "OO") | |
166 | issue2 = Issue.generate!(:subject => "oo") |
|
165 | issue2 = Issue.generate!(:subject => "oo") | |
167 |
|
||||
168 | r = Issue.search_results('ÖÖ') |
|
166 | r = Issue.search_results('ÖÖ') | |
169 | assert_include issue1, r |
|
167 | assert_include issue1, r | |
170 | assert_include issue2, r |
|
168 | assert_include issue2, r | |
171 | end |
|
169 | end | |
172 | end |
|
170 | end | |
173 |
|
171 | |||
174 | def test_search_should_be_case_and_accent_insensitive_with_postgresql_and_noaccent_extension |
|
172 | def test_search_should_be_case_and_accent_insensitive_with_postgresql_and_noaccent_extension | |
175 | if postgresql? |
|
173 | if postgresql? | |
176 | skip unless Redmine::Database.postgresql_version >= 90000 |
|
174 | skip unless Redmine::Database.postgresql_version >= 90000 | |
177 | # Extension will be rollbacked with the test transaction |
|
175 | # Extension will be rollbacked with the test transaction | |
178 | ActiveRecord::Base.connection.execute("CREATE EXTENSION IF NOT EXISTS unaccent") |
|
176 | ActiveRecord::Base.connection.execute("CREATE EXTENSION IF NOT EXISTS unaccent") | |
179 | Redmine::Database.reset |
|
177 | Redmine::Database.reset | |
180 | assert Redmine::Database.postgresql_unaccent? |
|
178 | assert Redmine::Database.postgresql_unaccent? | |
181 |
|
||||
182 | issue1 = Issue.generate!(:subject => "OO") |
|
179 | issue1 = Issue.generate!(:subject => "OO") | |
183 | issue2 = Issue.generate!(:subject => "oo") |
|
180 | issue2 = Issue.generate!(:subject => "oo") | |
184 |
|
||||
185 | r = Issue.search_results('ÖÖ') |
|
181 | r = Issue.search_results('ÖÖ') | |
186 | assert_include issue1, r |
|
182 | assert_include issue1, r | |
187 | assert_include issue2, r |
|
183 | assert_include issue2, r | |
188 | end |
|
184 | end | |
189 | ensure |
|
185 | ensure | |
190 | Redmine::Database.reset |
|
186 | Redmine::Database.reset | |
191 | end |
|
187 | end | |
192 |
|
188 | |||
193 | def test_fetcher_should_handle_accents_in_phrases |
|
189 | def test_fetcher_should_handle_accents_in_phrases | |
194 | f = Redmine::Search::Fetcher.new('No special chars "in a phrase"', User.anonymous, %w(issues), Project.all) |
|
190 | f = Redmine::Search::Fetcher.new('No special chars "in a phrase"', User.anonymous, %w(issues), Project.all) | |
195 | assert_equal ['No', 'special', 'chars', 'in a phrase'], f.tokens |
|
191 | assert_equal ['No', 'special', 'chars', 'in a phrase'], f.tokens | |
196 |
|
192 | |||
197 | f = Redmine::Search::Fetcher.new('Special chars "in a phrase Öö"', User.anonymous, %w(issues), Project.all) |
|
193 | f = Redmine::Search::Fetcher.new('Special chars "in a phrase Öö"', User.anonymous, %w(issues), Project.all) | |
198 | assert_equal ['Special', 'chars', 'in a phrase Öö'], f.tokens |
|
194 | assert_equal ['Special', 'chars', 'in a phrase Öö'], f.tokens | |
199 | end |
|
195 | end | |
200 |
|
196 | |||
201 | private |
|
197 | private | |
202 |
|
198 | |||
203 | def remove_permission(role, permission) |
|
199 | def remove_permission(role, permission) | |
204 | role.permissions = role.permissions - [ permission ] |
|
200 | role.permissions = role.permissions - [ permission ] | |
205 | role.save |
|
201 | role.save | |
206 | end |
|
202 | end | |
207 | end |
|
203 | end |
General Comments 0
You need to be logged in to leave comments.
Login now