Commit d66066f7 authored by Nick Thomas's avatar Nick Thomas

Harden the wiki against missing last_versions

Currently, we assume a "last_version" always exists for a wiki page.
In production, this is not always true. So, guard uses of it with a
null check.
parent e657c9a9
...@@ -160,7 +160,9 @@ class WikiPage ...@@ -160,7 +160,9 @@ class WikiPage
# Returns boolean True or False if this instance # Returns boolean True or False if this instance
# is an old version of the page. # is an old version of the page.
def historical? def historical?
@page.historical? && last_version.sha != version.sha return false unless last_commit_sha && version
@page.historical? && last_commit_sha != version.sha
end end
# Returns boolean True or False if this instance # Returns boolean True or False if this instance
......
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
= link_to wiki_page.title, project_wiki_path(@project, wiki_page) = link_to wiki_page.title, project_wiki_path(@project, wiki_page)
%small (#{wiki_page.format}) %small (#{wiki_page.format})
.float-right .float-right
%small= (s_("Last edited %{date}") % { date: time_ago_with_tooltip(wiki_page.last_version.authored_date) }).html_safe - if wiki_page.last_version
%small= (s_("Last edited %{date}") % { date: time_ago_with_tooltip(wiki_page.last_version.authored_date) }).html_safe
...@@ -11,8 +11,9 @@ ...@@ -11,8 +11,9 @@
.nav-text .nav-text
%h2.wiki-page-title= @page.title.capitalize %h2.wiki-page-title= @page.title.capitalize
%span.wiki-last-edit-by %span.wiki-last-edit-by
= (_("Last edited by %{name}") % { name: "<strong>#{@page.last_version.author_name}</strong>" }).html_safe - if @page.last_version
#{time_ago_with_tooltip(@page.last_version.authored_date)} = (_("Last edited by %{name}") % { name: "<strong>#{@page.last_version.author_name}</strong>" }).html_safe
#{time_ago_with_tooltip(@page.last_version.authored_date)}
.nav-controls .nav-controls
= render 'main_links' = render 'main_links'
......
---
title: Fix a bug displaying certain wiki pages
merge_request: 22377
author:
type: fixed
...@@ -457,6 +457,12 @@ describe WikiPage do ...@@ -457,6 +457,12 @@ describe WikiPage do
end end
describe '#historical?' do describe '#historical?' do
let(:page) { wiki.find_page('Update') }
let(:old_version) { page.versions.last.id }
let(:old_page) { wiki.find_page('Update', old_version) }
let(:latest_version) { page.versions.first.id }
let(:latest_page) { wiki.find_page('Update', latest_version) }
before do before do
create_page('Update', 'content') create_page('Update', 'content')
@page = wiki.find_page('Update') @page = wiki.find_page('Update')
...@@ -468,23 +474,27 @@ describe WikiPage do ...@@ -468,23 +474,27 @@ describe WikiPage do
end end
it 'returns true when requesting an old version' do it 'returns true when requesting an old version' do
old_version = @page.versions.last.id expect(old_page.historical?).to be_truthy
old_page = wiki.find_page('Update', old_version)
expect(old_page.historical?).to eq true
end end
it 'returns false when requesting latest version' do it 'returns false when requesting latest version' do
latest_version = @page.versions.first.id expect(latest_page.historical?).to be_falsy
latest_page = wiki.find_page('Update', latest_version)
expect(latest_page.historical?).to eq false
end end
it 'returns false when version is nil' do it 'returns false when version is nil' do
latest_page = wiki.find_page('Update', nil) expect(latest_page.historical?).to be_falsy
end
it 'returns false when the last version is nil' do
expect(old_page).to receive(:last_version) { nil }
expect(old_page.historical?).to be_falsy
end
it 'returns false when the version is nil' do
expect(old_page).to receive(:version) { nil }
expect(latest_page.historical?).to eq false expect(old_page.historical?).to be_falsy
end end
end end
......
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