Commit c16351a7 authored by Alex Kalderimis's avatar Alex Kalderimis

WikiPage: Expose user, support identity equality

Exposes `@user` as `user`, so that the current page author can be
identified.

Change equality to match other DB objects: pages with equal slugs are
equal.
parent 88e65526
......@@ -19,10 +19,11 @@ class ProjectWiki
DIRECTION_DESC = 'desc'
DIRECTION_ASC = 'asc'
attr_reader :project, :user
# Returns a string describing what went wrong after
# an operation fails.
attr_reader :error_message
attr_reader :project
def initialize(project, user = nil)
@project = project
......@@ -196,9 +197,9 @@ class ProjectWiki
def commit_details(action, message = nil, title = nil)
commit_message = message.presence || default_message(action, title)
git_user = Gitlab::Git::User.from_gitlab(@user)
git_user = Gitlab::Git::User.from_gitlab(user)
Gitlab::Git::Wiki::CommitDetails.new(@user.id,
Gitlab::Git::Wiki::CommitDetails.new(user.id,
git_user.username,
git_user.name,
git_user.email,
......@@ -206,7 +207,7 @@ class ProjectWiki
end
def default_message(action, title)
"#{@user.username} #{action} page: #{title}"
"#{user.username} #{action} page: #{title}"
end
def update_project_activity
......
......@@ -21,6 +21,14 @@ class WikiPage
ActiveModel::Name.new(self, nil, 'wiki')
end
def eql?(other)
return false unless other.present? && other.is_a?(self.class)
slug == other.slug && wiki.project == other.wiki.project
end
alias_method :==, :eql?
# Sorts and groups pages by directory.
#
# pages - an array of WikiPage objects.
......@@ -58,6 +66,7 @@ class WikiPage
# The GitLab ProjectWiki instance.
attr_reader :wiki
delegate :project, to: :wiki
# The raw Gitlab::Git::WikiPage instance.
attr_reader :page
......@@ -70,6 +79,10 @@ class WikiPage
Gitlab::HookData::WikiPageBuilder.new(self).build
end
# Construct a new WikiPage
#
# @param [ProjectWiki] wiki
# @param [Gitlab::Git::WikiPage] page
def initialize(wiki, page = nil)
@wiki = wiki
@page = page
......
......@@ -12,6 +12,10 @@ module Gitlab
'content' => absolute_image_urls(wiki_page.content)
)
end
def uploads_prefix
''
end
end
end
end
......@@ -606,12 +606,36 @@ describe WikiPage do
expect(subject).to eq(subject)
end
it 'returns false for updated wiki page' do
it 'returns true for updated wiki page' do
subject.update(content: "Updated content")
updated_page = wiki.find_page('test page')
updated_page = wiki.find_page(existing_page.slug)
expect(updated_page).not_to be_nil
expect(updated_page).not_to eq(subject)
expect(updated_page).to eq(subject)
end
it 'returns false for a completely different wiki page' do
other_page = create(:wiki_page)
expect(subject.slug).not_to eq(other_page.slug)
expect(subject.project).not_to eq(other_page.project)
expect(subject).not_to eq(other_page)
end
it 'returns false for page with different slug on same project' do
other_page = create(:wiki_page, project: subject.project)
expect(subject.slug).not_to eq(other_page.slug)
expect(subject.project).to eq(other_page.project)
expect(subject).not_to eq(other_page)
end
it 'returns false for page with the same slug on a different project' do
other_page = create(:wiki_page, title: existing_page.slug)
expect(subject.slug).to eq(other_page.slug)
expect(subject.project).not_to eq(other_page.project)
expect(subject).not_to eq(other_page)
end
end
......@@ -640,6 +664,30 @@ describe WikiPage do
end
end
describe '#meta' do
it 'returns an appropriate metadata object' do
wiki_page = create(:wiki_page)
expect(wiki_page.meta).to have_attributes(
valid?: true,
canonical_slug: wiki_page.slug,
title: wiki_page.title,
project: wiki_page.project,
slugs: include(have_attributes(slug: wiki_page.slug))
)
end
it 'is up-to-date after updates' do
wiki_page = create(:wiki_page)
old_title = wiki_page.title
new_title = FFaker::Lorem.sentence
expect do
wiki_page.update(title: new_title)
end.to change { wiki_page.meta.title }.from(old_title).to(new_title)
end
end
private
def remove_temp_repo(path)
......
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