debian-mirror-gitlab/spec/models/wiki_page_spec.rb

665 lines
20 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require "spec_helper"
2017-09-10 17:25:29 +05:30
describe WikiPage do
2018-10-15 14:42:47 +05:30
let(:project) { create(:project, :wiki_repo) }
2014-09-02 18:07:02 +05:30
let(:user) { project.owner }
let(:wiki) { ProjectWiki.new(project, user) }
2020-03-13 15:44:24 +05:30
let(:new_page) do
described_class.new(wiki).tap do |page|
page.attributes = { title: 'test page', content: 'test content' }
end
end
let(:existing_page) do
create_page('test page', 'test content')
wiki.find_page('test page')
end
subject { new_page }
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
describe '.group_by_directory' do
context 'when there are no pages' do
it 'returns an empty array' do
2017-09-10 17:25:29 +05:30
expect(described_class.group_by_directory(nil)).to eq([])
expect(described_class.group_by_directory([])).to eq([])
2017-08-17 22:00:37 +05:30
end
end
context 'when there are pages' do
before do
create_page('dir_1/dir_1_1/page_3', 'content')
2019-07-07 11:18:12 +05:30
create_page('page_1', 'content')
2017-08-17 22:00:37 +05:30
create_page('dir_1/page_2', 'content')
2019-07-07 11:18:12 +05:30
create_page('dir_2', 'page with dir name')
2017-08-17 22:00:37 +05:30
create_page('dir_2/page_5', 'content')
2019-07-07 11:18:12 +05:30
create_page('page_6', 'content')
2017-08-17 22:00:37 +05:30
create_page('dir_2/page_4', 'content')
end
2019-07-07 11:18:12 +05:30
2017-08-17 22:00:37 +05:30
let(:page_1) { wiki.find_page('page_1') }
2019-07-07 11:18:12 +05:30
let(:page_6) { wiki.find_page('page_6') }
let(:page_dir_2) { wiki.find_page('dir_2') }
2017-08-17 22:00:37 +05:30
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
2020-03-13 15:44:24 +05:30
describe "#list_pages" do
2019-07-31 22:56:46 +05:30
context 'sort by title' do
let(:grouped_entries) { described_class.group_by_directory(wiki.list_pages) }
let(:expected_grouped_entries) { [dir_1_1, dir_1, page_dir_2, dir_2, page_1, page_6] }
it 'returns an array with pages and directories' do
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)
expect(slugs).to match_array(expected_slugs)
end
2019-07-07 11:18:12 +05:30
end
end
2019-05-18 00:54:41 +05:30
2019-07-31 22:56:46 +05:30
context 'sort by created_at' do
let(:grouped_entries) { described_class.group_by_directory(wiki.list_pages(sort: 'created_at')) }
let(:expected_grouped_entries) { [dir_1_1, page_1, dir_1, page_dir_2, dir_2, page_6] }
2019-07-07 11:18:12 +05:30
2019-07-31 22:56:46 +05:30
it 'returns an array with pages and directories' do
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)
2019-07-07 11:18:12 +05:30
2019-07-31 22:56:46 +05:30
expect(slugs).to match_array(expected_slugs)
end
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
end
2019-07-31 22:56:46 +05:30
it 'returns an array with retained order with directories at the top' do
expected_order = ['dir_1/dir_1_1/page_3', 'dir_1/page_2', 'dir_2', 'dir_2/page_4', 'dir_2/page_5', 'page_1', 'page_6']
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
grouped_entries = described_class.group_by_directory(wiki.list_pages)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
actual_order =
2019-10-12 21:52:04 +05:30
grouped_entries.flat_map do |page_or_dir|
2019-07-31 22:56:46 +05:30
get_slugs(page_or_dir)
end
expect(actual_order).to eq(expected_order)
end
2017-08-17 22:00:37 +05:30
end
end
end
describe '.unhyphenize' do
it 'removes hyphens from a name' do
name = 'a-name--with-hyphens'
2017-09-10 17:25:29 +05:30
expect(described_class.unhyphenize(name)).to eq('a name with hyphens')
2017-08-17 22:00:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
describe "#initialize" do
2018-12-05 23:21:45 +05:30
context "when initialized with an existing page" do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2014-09-02 18:07:02 +05:30
it "sets the slug attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.slug).to eq("test-page")
2014-09-02 18:07:02 +05:30
end
it "sets the title attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.title).to eq("test page")
2014-09-02 18:07:02 +05:30
end
it "sets the formatted content attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.content).to eq("test content")
2014-09-02 18:07:02 +05:30
end
it "sets the format attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.format).to eq(:markdown)
2014-09-02 18:07:02 +05:30
end
it "sets the message attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.message).to eq("test commit")
2014-09-02 18:07:02 +05:30
end
it "sets the version attribute" do
2020-03-13 15:44:24 +05:30
expect(subject.version).to be_a Gitlab::Git::WikiPageVersion
2014-09-02 18:07:02 +05:30
end
end
end
describe "validations" do
it "validates presence of title" do
subject.attributes.delete(:title)
2020-03-13 15:44:24 +05:30
expect(subject).not_to be_valid
expect(subject.errors.keys).to contain_exactly(:title)
2014-09-02 18:07:02 +05:30
end
it "validates presence of content" do
subject.attributes.delete(:content)
2020-03-13 15:44:24 +05:30
expect(subject).not_to be_valid
expect(subject.errors.keys).to contain_exactly(:content)
end
describe '#validate_path_limits' do
let(:max_title) { described_class::MAX_TITLE_BYTES }
let(:max_directory) { described_class::MAX_DIRECTORY_BYTES }
where(:character) do
['a', 'ä', '🙈']
end
with_them do
let(:size) { character.bytesize.to_f }
let(:valid_title) { character * (max_title / size).floor }
let(:valid_directory) { character * (max_directory / size).floor }
let(:invalid_title) { character * ((max_title + 1) / size).ceil }
let(:invalid_directory) { character * ((max_directory + 1) / size).ceil }
it 'accepts page titles below the limit' do
subject.title = valid_title
expect(subject).to be_valid
end
it 'accepts directories below the limit' do
subject.title = valid_directory + '/foo'
expect(subject).to be_valid
end
it 'accepts a path with page title and directory below the limit' do
subject.title = "#{valid_directory}/#{valid_title}"
expect(subject).to be_valid
end
it 'rejects page titles exceeding the limit' do
subject.title = invalid_title
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
2020-04-08 14:13:33 +05:30
"exceeds the limit of #{max_title} bytes"
2020-03-13 15:44:24 +05:30
)
end
it 'rejects directories exceeding the limit' do
2020-04-08 14:13:33 +05:30
subject.title = "#{invalid_directory}/#{invalid_directory}2/foo"
2020-03-13 15:44:24 +05:30
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
2020-04-08 14:13:33 +05:30
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\"",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}2\""
2020-03-13 15:44:24 +05:30
)
end
it 'rejects a page with both title and directory exceeding the limit' do
subject.title = "#{invalid_directory}/#{invalid_title}"
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
2020-04-08 14:13:33 +05:30
"exceeds the limit of #{max_title} bytes",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\""
2020-03-13 15:44:24 +05:30
)
end
end
context 'with an existing page title exceeding the limit' do
subject do
title = 'a' * (max_title + 1)
create_page(title, 'content')
wiki.find_page(title)
end
it 'accepts the exceeding title length when unchanged' do
expect(subject).to be_valid
end
it 'rejects the exceeding title length when changed' do
subject.title = 'b' * (max_title + 1)
expect(subject).not_to be_valid
expect(subject.errors).to include(:title)
end
end
2014-09-02 18:07:02 +05:30
end
end
describe "#create" do
2020-03-13 15:44:24 +05:30
let(:attributes) do
2018-12-13 13:39:08 +05:30
{
title: "Index",
content: "Home Page",
format: "markdown",
message: 'Custom Commit Message'
}
end
2014-09-02 18:07:02 +05:30
context "with valid attributes" do
it "saves the wiki page" do
2020-03-13 15:44:24 +05:30
subject.create(attributes)
2015-04-26 12:48:37 +05:30
expect(wiki.find_page("Index")).not_to be_nil
2014-09-02 18:07:02 +05:30
end
it "returns true" do
2020-03-13 15:44:24 +05:30
expect(subject.create(attributes)).to eq(true)
2018-12-13 13:39:08 +05:30
end
it 'saves the wiki page with message' do
2020-03-13 15:44:24 +05:30
subject.create(attributes)
2018-12-13 13:39:08 +05:30
expect(wiki.find_page("Index").message).to eq 'Custom Commit Message'
2015-04-26 12:48:37 +05:30
end
end
end
describe "dot in the title" do
let(:title) { 'Index v1.2.3' }
describe "#create" do
2020-03-13 15:44:24 +05:30
let(:attributes) { { title: title, content: "Home Page", format: "markdown" } }
2015-04-26 12:48:37 +05:30
context "with valid attributes" do
it "saves the wiki page" do
2020-03-13 15:44:24 +05:30
subject.create(attributes)
2015-04-26 12:48:37 +05:30
expect(wiki.find_page(title)).not_to be_nil
end
it "returns true" do
2020-03-13 15:44:24 +05:30
expect(subject.create(attributes)).to eq(true)
2015-04-26 12:48:37 +05:30
end
end
end
describe "#update" do
2020-03-13 15:44:24 +05:30
subject do
2015-04-26 12:48:37 +05:30
create_page(title, "content")
2020-03-13 15:44:24 +05:30
wiki.find_page(title)
2015-04-26 12:48:37 +05:30
end
it "updates the content of the page" do
2020-03-13 15:44:24 +05:30
subject.update(content: "new content")
page = wiki.find_page(title)
expect(page.content).to eq('new content')
2015-04-26 12:48:37 +05:30
end
it "returns true" do
2020-03-13 15:44:24 +05:30
expect(subject.update(content: "more content")).to be_truthy
2014-09-02 18:07:02 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
describe '#create' do
2019-02-15 15:39:39 +05:30
context 'with valid attributes' do
it 'raises an error if a page with the same path already exists' do
create_page('New Page', 'content')
create_page('foo/bar', 'content')
2020-03-13 15:44:24 +05:30
2019-02-15 15:39:39 +05:30
expect { create_page('New Page', 'other content') }.to raise_error Gitlab::Git::Wiki::DuplicatePageError
expect { create_page('foo/bar', 'other content') }.to raise_error Gitlab::Git::Wiki::DuplicatePageError
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
it 'if the title is preceded by a / it is removed' do
create_page('/New Page', 'content')
2018-12-23 12:14:25 +05:30
2019-02-15 15:39:39 +05:30
expect(wiki.find_page('New Page')).not_to be_nil
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
2019-02-15 15:39:39 +05:30
end
2014-09-02 18:07:02 +05:30
2019-02-15 15:39:39 +05:30
describe "#update" do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
context "with valid attributes" do
it "updates the content of the page" do
new_content = "new content"
2020-03-13 15:44:24 +05:30
subject.update(content: new_content)
page = wiki.find_page('test page')
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(page.content).to eq("new content")
2017-09-10 17:25:29 +05:30
end
2019-02-15 15:39:39 +05:30
it "updates the title of the page" do
new_title = "Index v.1.2.4"
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
subject.update(title: new_title)
page = wiki.find_page(new_title)
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(page.title).to eq(new_title)
2019-02-15 15:39:39 +05:30
end
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
it "returns true" do
2020-03-13 15:44:24 +05:30
expect(subject.update(content: "more content")).to be_truthy
2014-09-02 18:07:02 +05:30
end
2019-02-15 15:39:39 +05:30
end
2014-09-02 18:07:02 +05:30
2019-02-15 15:39:39 +05:30
context 'with same last commit sha' do
it 'returns true' do
2020-03-13 15:44:24 +05:30
expect(subject.update(content: 'more content', last_commit_sha: subject.last_commit_sha)).to be_truthy
2017-09-10 17:25:29 +05:30
end
2019-02-15 15:39:39 +05:30
end
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
context 'with different last commit sha' do
it 'raises exception' do
2020-03-13 15:44:24 +05:30
expect { subject.update(content: 'more content', last_commit_sha: 'xxx') }.to raise_error(WikiPage::PageChangedError)
2017-09-10 17:25:29 +05:30
end
2019-02-15 15:39:39 +05:30
end
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
context 'when renaming a page' do
it 'raises an error if the page already exists' do
create_page('Existing Page', 'content')
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect { subject.update(title: 'Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq 'test page'
expect(subject.content).to eq 'new_content'
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
it 'updates the content and rename the file' do
new_title = 'Renamed Page'
new_content = 'updated content'
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.update(title: new_title, content: new_content)).to be_truthy
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
page = wiki.find_page(new_title)
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(page).not_to be_nil
expect(page.content).to eq new_content
2018-03-17 18:26:18 +05:30
end
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
context 'when moving a page' do
it 'raises an error if the page already exists' do
create_page('foo/Existing Page', 'content')
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect { subject.update(title: 'foo/Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq 'test page'
expect(subject.content).to eq 'new_content'
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
it 'updates the content and moves the file' do
new_title = 'foo/Other Page'
new_content = 'new_content'
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.update(title: new_title, content: new_content)).to be_truthy
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
page = wiki.find_page(new_title)
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
expect(page).not_to be_nil
expect(page.content).to eq new_content
end
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
context 'in subdir' do
2020-03-13 15:44:24 +05:30
subject do
2019-02-15 15:39:39 +05:30
create_page('foo/Existing Page', 'content')
2020-03-13 15:44:24 +05:30
wiki.find_page('foo/Existing Page')
2019-02-15 15:39:39 +05:30
end
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
it 'moves the page to the root folder if the title is preceded by /' do
2020-03-13 15:44:24 +05:30
expect(subject.slug).to eq 'foo/Existing-Page'
expect(subject.update(title: '/Existing Page', content: 'new_content')).to be_truthy
expect(subject.slug).to eq 'Existing-Page'
2018-03-17 18:26:18 +05:30
end
2019-02-15 15:39:39 +05:30
it 'does nothing if it has the same title' do
2020-03-13 15:44:24 +05:30
original_path = subject.slug
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.update(title: 'Existing Page', content: 'new_content')).to be_truthy
expect(subject.slug).to eq original_path
2018-03-17 18:26:18 +05:30
end
end
2019-02-15 15:39:39 +05:30
context 'in root dir' do
it 'does nothing if the title is preceded by /' do
2020-03-13 15:44:24 +05:30
original_path = subject.slug
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.update(title: '/test page', content: 'new_content')).to be_truthy
expect(subject.slug).to eq original_path
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
context "with invalid attributes" do
it 'aborts update if title blank' do
2020-03-13 15:44:24 +05:30
expect(subject.update(title: '', content: 'new_content')).to be_falsey
expect(subject.content).to eq 'new_content'
2018-12-23 12:14:25 +05:30
2020-03-13 15:44:24 +05:30
page = wiki.find_page('test page')
2019-02-15 15:39:39 +05:30
2020-03-13 15:44:24 +05:30
expect(page.content).to eq 'test content'
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
describe "#destroy" do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
it "deletes the page" do
2020-03-13 15:44:24 +05:30
subject.delete
2019-07-31 22:56:46 +05:30
expect(wiki.list_pages).to be_empty
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns true" do
2020-03-13 15:44:24 +05:30
expect(subject.delete).to eq(true)
2014-09-02 18:07:02 +05:30
end
end
describe "#versions" do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2014-09-02 18:07:02 +05:30
2019-02-15 15:39:39 +05:30
it "returns an array of all commits for the page" do
2020-03-13 15:44:24 +05:30
3.times { |i| subject.update(content: "content #{i}") }
2019-02-15 15:39:39 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.versions.count).to eq(4)
2019-01-03 12:48:30 +05:30
end
2019-02-15 15:39:39 +05:30
it 'returns instances of WikiPageVersion' do
2020-03-13 15:44:24 +05:30
expect(subject.versions).to all( be_a(Gitlab::Git::WikiPageVersion) )
2014-09-02 18:07:02 +05:30
end
end
2020-04-08 14:13:33 +05:30
describe '#title_changed?' do
using RSpec::Parameterized::TableSyntax
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
let(:untitled_page) { described_class.new(wiki) }
let(:directory_page) do
create_page('parent directory/child page', 'test content')
wiki.find_page('parent directory/child page')
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
where(:page, :title, :changed) do
:untitled_page | nil | false
:untitled_page | 'new title' | true
:new_page | nil | true
:new_page | 'test page' | true
:new_page | 'new title' | true
:existing_page | nil | false
:existing_page | 'test page' | false
:existing_page | 'test-page' | false
:existing_page | '/test page' | false
:existing_page | '/test-page' | false
:existing_page | ' test page ' | true
:existing_page | 'new title' | true
:existing_page | 'new-title' | true
:directory_page | nil | false
:directory_page | 'parent directory/child page' | false
:directory_page | 'parent-directory/child page' | false
:directory_page | 'parent-directory/child-page' | false
:directory_page | 'child page' | false
:directory_page | 'child-page' | false
:directory_page | '/child page' | true
:directory_page | 'parent directory/other' | true
:directory_page | 'parent-directory/other' | true
:directory_page | 'parent-directory / child-page' | true
:directory_page | 'other directory/child page' | true
:directory_page | 'other-directory/child page' | true
end
with_them do
it 'returns the expected value' do
subject = public_send(page)
subject.title = title if title
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
expect(subject.title_changed?).to be(changed)
end
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
2019-10-31 01:37:42 +05:30
describe '#path' do
it 'returns the path when persisted' do
2020-04-08 14:13:33 +05:30
expect(existing_page.path).to eq('test-page.md')
2019-10-31 01:37:42 +05:30
end
it 'returns nil when not persisted' do
2020-04-08 14:13:33 +05:30
expect(new_page.path).to be_nil
2019-10-31 01:37:42 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe '#directory' do
context 'when the page is at the root directory' do
2020-04-08 14:13:33 +05:30
subject { existing_page }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
it 'returns an empty string' do
expect(subject.directory).to eq('')
2017-08-17 22:00:37 +05:30
end
end
context 'when the page is inside an actual directory' do
2020-03-13 15:44:24 +05:30
subject do
2017-08-17 22:00:37 +05:30
create_page('dir_1/dir_1_1/file', 'content')
2020-03-13 15:44:24 +05:30
wiki.find_page('dir_1/dir_1_1/file')
end
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
it 'returns the full directory hierarchy' do
expect(subject.directory).to eq('dir_1/dir_1_1')
2017-08-17 22:00:37 +05:30
end
end
end
2016-04-02 18:10:28 +05:30
describe '#historical?' do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
let(:old_version) { subject.versions.last.id }
let(:old_page) { wiki.find_page(subject.title, old_version) }
let(:latest_version) { subject.versions.first.id }
let(:latest_page) { wiki.find_page(subject.title, latest_version) }
2016-04-02 18:10:28 +05:30
2020-03-13 15:44:24 +05:30
before do
3.times { |i| subject.update(content: "content #{i}") }
2016-04-02 18:10:28 +05:30
end
it 'returns true when requesting an old version' do
2018-12-13 13:39:08 +05:30
expect(old_page.historical?).to be_truthy
2016-04-02 18:10:28 +05:30
end
it 'returns false when requesting latest version' do
2018-12-13 13:39:08 +05:30
expect(latest_page.historical?).to be_falsy
2016-04-02 18:10:28 +05:30
end
it 'returns false when version is nil' do
2018-12-13 13:39:08 +05:30
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 }
2016-04-02 18:10:28 +05:30
2018-12-13 13:39:08 +05:30
expect(old_page.historical?).to be_falsy
2016-04-02 18:10:28 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe '#to_partial_path' do
it 'returns the relative path to the partial to be used' do
2020-03-13 15:44:24 +05:30
expect(subject.to_partial_path).to eq('projects/wikis/wiki_page')
2017-08-17 22:00:37 +05:30
end
end
describe '#==' do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2017-08-17 22:00:37 +05:30
it 'returns true for identical wiki page' do
2020-03-13 15:44:24 +05:30
expect(subject).to eq(subject)
2017-08-17 22:00:37 +05:30
end
it 'returns false for updated wiki page' do
2020-03-13 15:44:24 +05:30
subject.update(content: "Updated content")
updated_page = wiki.find_page('test page')
expect(updated_page).not_to be_nil
expect(updated_page).not_to eq(subject)
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
describe '#last_commit_sha' do
2020-03-13 15:44:24 +05:30
subject { existing_page }
2017-09-10 17:25:29 +05:30
it 'returns commit sha' do
2020-03-13 15:44:24 +05:30
expect(subject.last_commit_sha).to eq subject.last_version.sha
2017-09-10 17:25:29 +05:30
end
it 'is changed after page updated' do
2020-03-13 15:44:24 +05:30
last_commit_sha_before_update = subject.last_commit_sha
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
subject.update(content: "new content")
page = wiki.find_page('test page')
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(page.last_commit_sha).not_to eq last_commit_sha_before_update
2017-09-10 17:25:29 +05:30
end
end
2018-11-18 11:00:15 +05:30
describe '#hook_attrs' do
it 'adds absolute urls for images in the content' do
2020-03-13 15:44:24 +05:30
subject.attributes[:content] = 'test![WikiPage_Image](/uploads/abc/WikiPage_Image.png)'
2018-11-18 11:00:15 +05:30
2020-03-13 15:44:24 +05:30
expect(subject.hook_attrs['content']).to eq("test![WikiPage_Image](#{Settings.gitlab.url}/uploads/abc/WikiPage_Image.png)")
2018-11-18 11:00:15 +05:30
end
end
2014-09-02 18:07:02 +05:30
private
def remove_temp_repo(path)
FileUtils.rm_rf path
end
def commit_details
2018-10-15 14:42:47 +05:30
Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "test commit")
2014-09-02 18:07:02 +05:30
end
def create_page(name, content)
wiki.wiki.write_page(name, :markdown, content, commit_details)
end
2017-08-17 22:00:37 +05:30
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
2014-09-02 18:07:02 +05:30
end