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