2013-03-04 03:43:52 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
2015-12-09 09:50:51 +00:00
|
|
|
describe WikiPage, models: true do
|
2014-07-31 16:24:53 +00:00
|
|
|
let(:project) { create(:empty_project) }
|
2013-03-04 03:43:52 +00:00
|
|
|
let(:user) { project.owner }
|
2014-04-09 11:35:58 +00:00
|
|
|
let(:wiki) { ProjectWiki.new(project, user) }
|
2013-03-04 03:43:52 +00:00
|
|
|
|
|
|
|
subject { WikiPage.new(wiki) }
|
|
|
|
|
2016-12-17 18:38:26 +00:00
|
|
|
describe '.group_by_directory' do
|
2016-12-16 04:12:21 +00:00
|
|
|
context 'when there are no pages' do
|
2016-12-26 22:12:15 +00:00
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(WikiPage.group_by_directory(nil)).to eq([])
|
|
|
|
expect(WikiPage.group_by_directory([])).to eq([])
|
2016-12-16 04:12:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are pages' do
|
2016-12-17 18:38:26 +00:00
|
|
|
before do
|
2016-12-19 04:34:35 +00:00
|
|
|
create_page('dir_1/dir_1_1/page_3', 'content')
|
2016-12-17 18:38:26 +00:00
|
|
|
create_page('dir_1/page_2', 'content')
|
2016-12-19 04:34:35 +00:00
|
|
|
create_page('dir_2/page_5', 'content')
|
|
|
|
create_page('dir_2/page_4', 'content')
|
|
|
|
create_page('page_1', 'content')
|
2016-12-17 18:38:26 +00:00
|
|
|
end
|
2016-12-26 22:12:15 +00:00
|
|
|
let(:page_1) { wiki.find_page('page_1') }
|
|
|
|
let(:dir_1) do
|
|
|
|
WikiDirectory.new('dir_1', [wiki.find_page('dir_1/page_2')])
|
|
|
|
end
|
|
|
|
let(:dir_1_1) do
|
|
|
|
WikiDirectory.new('dir_1/dir_1_1', [wiki.find_page('dir_1/dir_1_1/page_3')])
|
|
|
|
end
|
|
|
|
let(:dir_2) do
|
|
|
|
pages = [wiki.find_page('dir_2/page_5'),
|
|
|
|
wiki.find_page('dir_2/page_4')]
|
|
|
|
WikiDirectory.new('dir_2', pages)
|
|
|
|
end
|
2016-12-17 18:38:26 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
it 'returns an array with pages and directories' do
|
|
|
|
expected_grouped_entries = [page_1, dir_1, dir_1_1, dir_2]
|
2016-12-17 18:38:26 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
grouped_entries = WikiPage.group_by_directory(wiki.pages)
|
2016-12-17 18:38:26 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
grouped_entries.each_with_index do |page_or_dir, i|
|
|
|
|
expected_page_or_dir = expected_grouped_entries[i]
|
|
|
|
expected_slugs = get_slugs(expected_page_or_dir)
|
|
|
|
slugs = get_slugs(page_or_dir)
|
2016-12-16 04:12:21 +00:00
|
|
|
|
2016-12-17 18:38:26 +00:00
|
|
|
expect(slugs).to match_array(expected_slugs)
|
|
|
|
end
|
2016-12-16 04:12:21 +00:00
|
|
|
end
|
2016-12-19 04:34:35 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
it 'returns an array sorted by alphabetical position' do
|
|
|
|
# Directories and pages within directories are sorted alphabetically.
|
|
|
|
# Pages at root come before everything.
|
|
|
|
expected_order = ['page_1', 'dir_1/page_2', 'dir_1/dir_1_1/page_3',
|
|
|
|
'dir_2/page_4', 'dir_2/page_5']
|
2016-12-19 04:34:35 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
grouped_entries = WikiPage.group_by_directory(wiki.pages)
|
2016-12-19 04:34:35 +00:00
|
|
|
|
2016-12-26 22:12:15 +00:00
|
|
|
actual_order =
|
|
|
|
grouped_entries.map do |page_or_dir|
|
|
|
|
get_slugs(page_or_dir)
|
|
|
|
end.
|
|
|
|
flatten
|
|
|
|
expect(actual_order).to eq(expected_order)
|
2016-12-19 04:34:35 +00:00
|
|
|
end
|
2016-12-16 04:12:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-30 03:07:31 +00:00
|
|
|
describe '.unhyphenize' do
|
|
|
|
it 'removes hyphens from a name' do
|
|
|
|
name = 'a-name--with-hyphens'
|
|
|
|
|
|
|
|
expect(WikiPage.unhyphenize(name)).to eq('a name with hyphens')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
describe "#initialize" do
|
|
|
|
context "when initialized with an existing gollum page" do
|
|
|
|
before do
|
|
|
|
create_page("test page", "test content")
|
|
|
|
@page = wiki.wiki.paged("test page")
|
|
|
|
@wiki_page = WikiPage.new(wiki, @page, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the slug attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.slug).to eq("test-page")
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the title attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.title).to eq("test page")
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the formatted content attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.content).to eq("test content")
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the format attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.format).to eq(:markdown)
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the message attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.message).to eq("test commit")
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the version attribute" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@wiki_page.version).to be_a Gollum::Git::Commit
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validations" do
|
|
|
|
before do
|
2015-06-22 18:41:00 +00:00
|
|
|
subject.attributes = { title: 'title', content: 'content' }
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "validates presence of title" do
|
|
|
|
subject.attributes.delete(:title)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(subject.valid?).to be_falsey
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "validates presence of content" do
|
|
|
|
subject.attributes.delete(:content)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(subject.valid?).to be_falsey
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2015-06-22 18:41:00 +00:00
|
|
|
@wiki_attr = { title: "Index", content: "Home Page", format: "markdown" }
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
after do
|
|
|
|
destroy_page("Index")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with valid attributes" do
|
|
|
|
it "saves the wiki page" do
|
|
|
|
subject.create(@wiki_attr)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(wiki.find_page("Index")).not_to be_nil
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(subject.create(@wiki_attr)).to eq(true)
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-22 01:04:29 +00:00
|
|
|
describe "dot in the title" do
|
|
|
|
let(:title) { 'Index v1.2.3' }
|
|
|
|
|
|
|
|
before do
|
2015-06-22 18:41:00 +00:00
|
|
|
@wiki_attr = { title: title, content: "Home Page", format: "markdown" }
|
2015-03-22 01:04:29 +00:00
|
|
|
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
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
describe "#update" do
|
|
|
|
before do
|
|
|
|
create_page("Update", "content")
|
|
|
|
@page = wiki.find_page("Update")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page("Update")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with valid attributes" do
|
|
|
|
it "updates the content of the page" do
|
|
|
|
@page.update("new content")
|
|
|
|
@page = wiki.find_page("Update")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@page.update("more content")).to be_truthy
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
before do
|
|
|
|
create_page("Delete Page", "content")
|
|
|
|
@page = wiki.find_page("Delete Page")
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "deletes the page" do
|
2013-03-04 03:43:52 +00:00
|
|
|
@page.delete
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(wiki.pages).to be_empty
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "returns true" do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@page.delete).to eq(true)
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#versions" do
|
|
|
|
before do
|
|
|
|
create_page("Update", "content")
|
|
|
|
@page = wiki.find_page("Update")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page("Update")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array of all commits for the page" do
|
|
|
|
3.times { |i| @page.update("content #{i}") }
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@page.versions.count).to eq(4)
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-09 05:14:16 +00:00
|
|
|
describe "#title" do
|
|
|
|
before do
|
|
|
|
create_page("Title", "content")
|
|
|
|
@page = wiki.find_page("Title")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page("Title")
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it "replaces a hyphen to a space" do
|
2014-04-09 05:14:16 +00:00
|
|
|
@page.title = "Import-existing-repositories-into-GitLab"
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@page.title).to eq("Import existing repositories into GitLab")
|
2014-04-09 05:14:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-18 23:22:20 +00:00
|
|
|
describe '#directory' do
|
|
|
|
context 'when the page is at the root directory' do
|
2016-12-27 04:05:53 +00:00
|
|
|
it 'returns an empty string' do
|
2016-12-18 23:22:20 +00:00
|
|
|
create_page('file', 'content')
|
|
|
|
page = wiki.find_page('file')
|
|
|
|
|
2016-12-27 04:05:53 +00:00
|
|
|
expect(page.directory).to eq('')
|
2016-12-18 23:22:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the page is inside an actual directory' do
|
|
|
|
it 'returns the full directory hierarchy' do
|
|
|
|
create_page('dir_1/dir_1_1/file', 'content')
|
|
|
|
page = wiki.find_page('dir_1/dir_1_1/file')
|
|
|
|
|
2016-12-27 04:05:53 +00:00
|
|
|
expect(page.directory).to eq('dir_1/dir_1_1')
|
2016-12-18 23:22:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-27 13:42:31 +00:00
|
|
|
describe '#historical?' do
|
|
|
|
before do
|
|
|
|
create_page('Update', 'content')
|
|
|
|
@page = wiki.find_page('Update')
|
|
|
|
3.times { |i| @page.update("content #{i}") }
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
destroy_page('Update')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when requesting an old version' do
|
|
|
|
old_version = @page.versions.last.to_s
|
|
|
|
old_page = wiki.find_page('Update', old_version)
|
|
|
|
|
|
|
|
expect(old_page.historical?).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when requesting latest version' do
|
|
|
|
latest_version = @page.versions.first.to_s
|
|
|
|
latest_page = wiki.find_page('Update', latest_version)
|
|
|
|
|
|
|
|
expect(latest_page.historical?).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when version is nil' do
|
|
|
|
latest_page = wiki.find_page('Update', nil)
|
|
|
|
|
|
|
|
expect(latest_page.historical?).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 01:51:34 +00:00
|
|
|
describe '#to_partial_path' do
|
|
|
|
it 'returns the relative path to the partial to be used' do
|
|
|
|
page = build(:wiki_page)
|
|
|
|
|
|
|
|
expect(page.to_partial_path).to eq('projects/wikis/wiki_page')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-30 13:26:30 +00:00
|
|
|
describe '#==' do
|
|
|
|
let(:original_wiki_page) { create(:wiki_page) }
|
|
|
|
|
|
|
|
it 'returns true for identical wiki page' do
|
|
|
|
expect(original_wiki_page).to eq(original_wiki_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for updated wiki page' do
|
|
|
|
updated_wiki_page = original_wiki_page.update("Updated content")
|
|
|
|
expect(original_wiki_page).not_to eq(updated_wiki_page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-31 16:24:53 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def remove_temp_repo(path)
|
|
|
|
FileUtils.rm_rf path
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_details
|
2015-10-03 06:48:54 +00:00
|
|
|
{ name: user.name, email: user.email, message: "test commit" }
|
2014-07-31 16:24:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_page(name, content)
|
|
|
|
wiki.wiki.write_page(name, :markdown, content, commit_details)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_page(title)
|
|
|
|
page = wiki.wiki.paged(title)
|
|
|
|
wiki.wiki.delete_page(page, commit_details)
|
|
|
|
end
|
2016-12-26 22:12:15 +00:00
|
|
|
|
|
|
|
def get_slugs(page_or_dir)
|
|
|
|
if page_or_dir.is_a? WikiPage
|
|
|
|
[page_or_dir.slug]
|
|
|
|
else
|
|
|
|
page_or_dir.pages.present? ? page_or_dir.pages.map(&:slug) : []
|
|
|
|
end
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|