Commit 1d85e5ff authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'fix-dots-in-wiki-title' into 'master'

Fix dots in Wiki slug causing errors

### What does this MR do?

When a user enters in dots into the Wiki slug, an error occurs:

```
NoMethodError (undefined method `escaped_url_path' for nil:NilClass):
  app/models/wiki_page.rb:172:in `set_attributes'
  app/models/wiki_page.rb:191:in `save'
  app/models/wiki_page.rb:155:in `update'
  app/controllers/projects/wikis_controller.rb:49:in `update'
```

This MR fixes this problem.

### Are there points in the code the reviewer needs to double check?

See the problem below.

### Why was this MR needed?

The issue is that the `save` method gets called differently:

```ruby
def create(attr = {})
....
    save :create_page, title, content, format, message
```

or

```ruby
def update(new_content = "", format = :markdown, message = nil)
...
    save :update_page, @page, content, format, message
```

In the create case, title is the slug entered in by the user (e.g. `path/how-to-write-wiki-pages`).

In the update case, originally `@page.page` included the format extension (e.g.`path/how-to-write-wiki-pages.Markdown`). The method `page_title_and_dir` was trying to handle both cases and not doing the right thing. For example, calling `page_title_and_dir('test-1.2.3')` would result in:

```
path_title = test-1.2
path_dir = 3
```

### What are the relevant issue numbers / [Feature requests](http://feedback.gitlab.com/)?

Issues #1263, #431

This replaces !156

See merge request !419
parents 75105366 59d5c779
......@@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
v 7.10.0 (unreleased)
- Fix "Import projects from" button to show the correct instructions (Stan Hu)
- Fix dots in Wiki slugs causing errors (Stan Hu)
- Update poltergeist to version 1.6.0 to support PhantomJS 2.0 (Zeger-Jan van de Weg)
- Fix cross references when usernames, milestones, or project names contain underscores (Stan Hu)
- enable line wrapping per default and remove the checkbox to toggle it (Hannes Rosenögger)
......
......@@ -104,7 +104,7 @@ class ProjectWiki
def page_title_and_dir(title)
title_array = title.split("/")
title = title_array.pop
[title.gsub(/\.[^.]*$/, ""), title_array.join("/")]
[title, title_array.join("/")]
end
def search_files(query)
......
......@@ -179,7 +179,8 @@ class WikiPage
if valid? && project_wiki.send(method, *args)
page_details = if method == :update_page
@page.path
# Use url_path instead of path to omit format extension
@page.url_path
else
title
end
......
......@@ -78,6 +78,47 @@ describe WikiPage do
end
end
describe "dot in the title" do
let(:title) { 'Index v1.2.3' }
before do
@wiki_attr = {title: title, content: "Home Page", format: "markdown"}
end
describe "#create" do
after do
destroy_page(title)
end
context "with valid attributes" do
it "saves the wiki page" do
subject.create(@wiki_attr)
expect(wiki.find_page(title)).not_to be_nil
end
it "returns true" do
expect(subject.create(@wiki_attr)).to eq(true)
end
end
end
describe "#update" do
before do
create_page(title, "content")
@page = wiki.find_page(title)
end
it "updates the content of the page" do
@page.update("new content")
@page = wiki.find_page(title)
end
it "returns true" do
expect(@page.update("more content")).to be_truthy
end
end
end
describe "#update" do
before do
create_page("Update", "content")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment