##// END OF EJS Templates
Fixed: Wiki#find_page should not be case sensitive because page title uniqueness is not (#6987)....
Jean-Philippe Lang -
r4316:97140f6a7828
parent child
Show More
@@ -1,85 +1,85
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Wiki < ActiveRecord::Base
18 class Wiki < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title'
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title'
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
22
22
23 acts_as_watchable
23 acts_as_watchable
24
24
25 validates_presence_of :start_page
25 validates_presence_of :start_page
26 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
26 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
27
27
28 def visible?(user=User.current)
28 def visible?(user=User.current)
29 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
29 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
30 end
30 end
31
31
32 # Returns the wiki page that acts as the sidebar content
32 # Returns the wiki page that acts as the sidebar content
33 # or nil if no such page exists
33 # or nil if no such page exists
34 def sidebar
34 def sidebar
35 @sidebar ||= find_page('Sidebar', :with_redirect => false)
35 @sidebar ||= find_page('Sidebar', :with_redirect => false)
36 end
36 end
37
37
38 # find the page with the given title
38 # find the page with the given title
39 # if page doesn't exist, return a new page
39 # if page doesn't exist, return a new page
40 def find_or_new_page(title)
40 def find_or_new_page(title)
41 title = start_page if title.blank?
41 title = start_page if title.blank?
42 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
42 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
43 end
43 end
44
44
45 # find the page with the given title
45 # find the page with the given title
46 def find_page(title, options = {})
46 def find_page(title, options = {})
47 title = start_page if title.blank?
47 title = start_page if title.blank?
48 title = Wiki.titleize(title)
48 title = Wiki.titleize(title).downcase
49 page = pages.find_by_title(title)
49 page = pages.first(:conditions => ["LOWER(title) LIKE ?", title])
50 if !page && !(options[:with_redirect] == false)
50 if !page && !(options[:with_redirect] == false)
51 # search for a redirect
51 # search for a redirect
52 redirect = redirects.find_by_title(title)
52 redirect = redirects.first(:conditions => ["LOWER(title) LIKE ?", title])
53 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
53 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
54 end
54 end
55 page
55 page
56 end
56 end
57
57
58 # Finds a page by title
58 # Finds a page by title
59 # The given string can be of one of the forms: "title" or "project:title"
59 # The given string can be of one of the forms: "title" or "project:title"
60 # Examples:
60 # Examples:
61 # Wiki.find_page("bar", project => foo)
61 # Wiki.find_page("bar", project => foo)
62 # Wiki.find_page("foo:bar")
62 # Wiki.find_page("foo:bar")
63 def self.find_page(title, options = {})
63 def self.find_page(title, options = {})
64 project = options[:project]
64 project = options[:project]
65 if title.to_s =~ %r{^([^\:]+)\:(.*)$}
65 if title.to_s =~ %r{^([^\:]+)\:(.*)$}
66 project_identifier, title = $1, $2
66 project_identifier, title = $1, $2
67 project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
67 project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
68 end
68 end
69 if project && project.wiki
69 if project && project.wiki
70 page = project.wiki.find_page(title)
70 page = project.wiki.find_page(title)
71 if page && page.content
71 if page && page.content
72 page
72 page
73 end
73 end
74 end
74 end
75 end
75 end
76
76
77 # turn a string into a valid page title
77 # turn a string into a valid page title
78 def self.titleize(title)
78 def self.titleize(title)
79 # replace spaces with _ and remove unwanted caracters
79 # replace spaces with _ and remove unwanted caracters
80 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
80 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
81 # upcase the first letter
81 # upcase the first letter
82 title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
82 title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
83 title
83 title
84 end
84 end
85 end
85 end
@@ -1,73 +1,74
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class WikiRedirectTest < ActiveSupport::TestCase
20 class WikiRedirectTest < ActiveSupport::TestCase
21 fixtures :projects, :wikis
21 fixtures :projects, :wikis, :wiki_pages
22
22
23 def setup
23 def setup
24 @wiki = Wiki.find(1)
24 @wiki = Wiki.find(1)
25 @original = WikiPage.create(:wiki => @wiki, :title => 'Original title')
25 @original = WikiPage.create(:wiki => @wiki, :title => 'Original title')
26 end
26 end
27
27
28 def test_create_redirect
28 def test_create_redirect
29 @original.title = 'New title'
29 @original.title = 'New title'
30 assert @original.save
30 assert @original.save
31 @original.reload
31 @original.reload
32
32
33 assert_equal 'New_title', @original.title
33 assert_equal 'New_title', @original.title
34 assert @wiki.redirects.find_by_title('Original_title')
34 assert @wiki.redirects.find_by_title('Original_title')
35 assert @wiki.find_page('Original title')
35 assert @wiki.find_page('Original title')
36 assert @wiki.find_page('ORIGINAL title')
36 end
37 end
37
38
38 def test_update_redirect
39 def test_update_redirect
39 # create a redirect that point to this page
40 # create a redirect that point to this page
40 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
41 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
41
42
42 @original.title = 'New title'
43 @original.title = 'New title'
43 @original.save
44 @original.save
44 # make sure the old page now points to the new page
45 # make sure the old page now points to the new page
45 assert_equal 'New_title', @wiki.find_page('An old page').title
46 assert_equal 'New_title', @wiki.find_page('An old page').title
46 end
47 end
47
48
48 def test_reverse_rename
49 def test_reverse_rename
49 # create a redirect that point to this page
50 # create a redirect that point to this page
50 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
51 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
51
52
52 @original.title = 'An old page'
53 @original.title = 'An old page'
53 @original.save
54 @original.save
54 assert !@wiki.redirects.find_by_title_and_redirects_to('An_old_page', 'An_old_page')
55 assert !@wiki.redirects.find_by_title_and_redirects_to('An_old_page', 'An_old_page')
55 assert @wiki.redirects.find_by_title_and_redirects_to('Original_title', 'An_old_page')
56 assert @wiki.redirects.find_by_title_and_redirects_to('Original_title', 'An_old_page')
56 end
57 end
57
58
58 def test_rename_to_already_redirected
59 def test_rename_to_already_redirected
59 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Other_page')
60 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Other_page')
60
61
61 @original.title = 'An old page'
62 @original.title = 'An old page'
62 @original.save
63 @original.save
63 # this redirect have to be removed since 'An old page' page now exists
64 # this redirect have to be removed since 'An old page' page now exists
64 assert !@wiki.redirects.find_by_title_and_redirects_to('An_old_page', 'Other_page')
65 assert !@wiki.redirects.find_by_title_and_redirects_to('An_old_page', 'Other_page')
65 end
66 end
66
67
67 def test_redirects_removed_when_deleting_page
68 def test_redirects_removed_when_deleting_page
68 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
69 assert WikiRedirect.create(:wiki => @wiki, :title => 'An_old_page', :redirects_to => 'Original_title')
69
70
70 @original.destroy
71 @original.destroy
71 assert !@wiki.redirects.find(:first)
72 assert !@wiki.redirects.find(:first)
72 end
73 end
73 end
74 end
@@ -1,65 +1,74
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # redMine - project management software
3 # redMine - project management software
4 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
20 require File.dirname(__FILE__) + '/../test_helper'
21
21
22 class WikiTest < ActiveSupport::TestCase
22 class WikiTest < ActiveSupport::TestCase
23 fixtures :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
23 fixtures :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
24
24
25 def test_create
25 def test_create
26 wiki = Wiki.new(:project => Project.find(2))
26 wiki = Wiki.new(:project => Project.find(2))
27 assert !wiki.save
27 assert !wiki.save
28 assert_equal 1, wiki.errors.count
28 assert_equal 1, wiki.errors.count
29
29
30 wiki.start_page = "Start page"
30 wiki.start_page = "Start page"
31 assert wiki.save
31 assert wiki.save
32 end
32 end
33
33
34 def test_update
34 def test_update
35 @wiki = Wiki.find(1)
35 @wiki = Wiki.find(1)
36 @wiki.start_page = "Another start page"
36 @wiki.start_page = "Another start page"
37 assert @wiki.save
37 assert @wiki.save
38 @wiki.reload
38 @wiki.reload
39 assert_equal "Another start page", @wiki.start_page
39 assert_equal "Another start page", @wiki.start_page
40 end
40 end
41
41
42 def test_find_page
43 wiki = Wiki.find(1)
44 page = WikiPage.find(2)
45
46 assert_equal page, wiki.find_page('Another_page')
47 assert_equal page, wiki.find_page('Another page')
48 assert_equal page, wiki.find_page('ANOTHER page')
49 end
50
42 def test_titleize
51 def test_titleize
43 assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES')
52 assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES')
44 assert_equal 'テスト', Wiki.titleize('テスト')
53 assert_equal 'テスト', Wiki.titleize('テスト')
45 end
54 end
46
55
47 context "#sidebar" do
56 context "#sidebar" do
48 setup do
57 setup do
49 @wiki = Wiki.find(1)
58 @wiki = Wiki.find(1)
50 end
59 end
51
60
52 should "return nil if undefined" do
61 should "return nil if undefined" do
53 assert_nil @wiki.sidebar
62 assert_nil @wiki.sidebar
54 end
63 end
55
64
56 should "return a WikiPage if defined" do
65 should "return a WikiPage if defined" do
57 page = @wiki.pages.new(:title => 'Sidebar')
66 page = @wiki.pages.new(:title => 'Sidebar')
58 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
67 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
59 page.save!
68 page.save!
60
69
61 assert_kind_of WikiPage, @wiki.sidebar
70 assert_kind_of WikiPage, @wiki.sidebar
62 assert_equal 'Sidebar', @wiki.sidebar.title
71 assert_equal 'Sidebar', @wiki.sidebar.title
63 end
72 end
64 end
73 end
65 end
74 end
General Comments 0
You need to be logged in to leave comments. Login now