Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

replace legacy spec

ulferts9c0bb4b0d865
diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb
index 74e444663f3..b23bd62c7ef 100644
--- a/spec/models/wiki_page_spec.rb
+++ b/spec/models/wiki_page_spec.rb
@@ -54,7 +54,7 @@ describe WikiPage, type: :model do
let!(:wiki_page2) { create(:wiki_page, wiki: wiki2, title: 'asdf') }
it 'scopes the slug correctly' do
- pages = WikiPage.where(title: 'asdf')
+ pages = described_class.where(title: 'asdf')
expect(pages.count).to eq(2)
expect(pages.first.slug).to eq('asdf')
expect(pages.last.slug).to eq('asdf')
@@ -120,6 +120,23 @@ describe WikiPage, type: :model do
end
describe '#destroy' do
+ context 'for a page with content (which is always the case)' do
+ let!(:wiki_content) { create(:wiki_content, page: wiki_page) }
+
+ before do
+ # to have the wiki_content properly hooked up
+ wiki_page.reload
+ end
+
+ it 'destroys the wiki content as well' do
+ expect { wiki_page.destroy }.to change(WikiContent, :count).from(1).to(0)
+ end
+
+ it 'destroys the wiki content\'s journals as well' do
+ expect { wiki_page.destroy }.to change(Journal, :count).from(1).to(0)
+ end
+ end
+
context 'when the only wiki page is destroyed' do
before do
wiki_page.destroy
@@ -127,7 +144,7 @@ describe WikiPage, type: :model do
it 'ensures there is still a wiki menu item' do
expect(wiki.wiki_menu_items).to be_one
- expect(wiki.wiki_menu_items.first.is_main_item?).to be_truthy
+ expect(wiki.wiki_menu_items.first).to be_is_main_item
end
end
@@ -139,7 +156,22 @@ describe WikiPage, type: :model do
it 'ensures that there is still a wiki menu item named like the wiki start page' do
expect(wiki.wiki_menu_items).to be_one
- expect(wiki.wiki_menu_items.first.name).to eq WikiPage.slug(wiki.start_page)
+ expect(wiki.wiki_menu_items.first.name).to eq described_class.slug(wiki.start_page)
+ end
+ end
+
+ context 'when destroying a parent' do
+ let!(:child_wiki_page) do
+ create(:wiki_page, parent_id: wiki_page.id, wiki:, project:)
+ end
+
+ before do
+ wiki_page.destroy
+ end
+
+ it 'keeps the child but nils the parent_id' do
+ expect(child_wiki_page.reload.parent_id)
+ .to be_nil
end
end
end
@@ -170,6 +202,84 @@ describe WikiPage, type: :model do
end
end
+ describe '#parent_title' do
+ let(:child_wiki_page) do
+ create(:wiki_page, parent_id: wiki_page.id, wiki:, project:)
+ end
+
+ it 'is empty for a page without a parent' do
+ expect(wiki_page.parent_title)
+ .to be_nil
+ end
+
+ it 'is the name of the parent page if set' do
+ expect(child_wiki_page.parent_title)
+ .to eq wiki_page.title
+ end
+ end
+
+ describe '#parent_title=' do
+ let(:other_wiki_page) do
+ create(:wiki_page, wiki:, project:)
+ end
+
+ let(:other_wiki_page_child) do
+ create(:wiki_page, parent: other_wiki_page, wiki:, project:)
+ end
+
+ context 'when setting it to the name of a wiki page' do
+ it 'sets the parent to that wiki page' do
+ wiki_page.parent_title = other_wiki_page.title
+ wiki_page.save
+
+ expect(wiki_page.reload.parent)
+ .to eql(other_wiki_page)
+ end
+ end
+
+ context 'when setting to an empty string' do
+ let(:child_wiki_page) do
+ create(:wiki_page, parent_id: wiki_page.id, wiki:, project:)
+ end
+
+ it 'unsets the parent' do
+ child_wiki_page.parent_title = ''
+ child_wiki_page.save
+
+ expect(child_wiki_page.reload.parent)
+ .to be_nil
+ end
+ end
+
+ context 'when setting to a child' do
+ let(:child_wiki_page) do
+ create(:wiki_page, parent_id: wiki_page.id, wiki:, project:)
+ end
+
+ it 'causes an error' do
+ wiki_page.parent_title = child_wiki_page.title
+
+ expect(wiki_page.save)
+ .to be false
+
+ expect(wiki_page.errors[:parent_title])
+ .to eq [I18n.t('activerecord.errors.messages.circular_dependency')]
+ end
+ end
+
+ context 'when setting to a itself' do
+ it 'causes an error' do
+ wiki_page.parent_title = wiki_page.title
+
+ expect(wiki_page.save)
+ .to be false
+
+ expect(wiki_page.errors[:parent_title])
+ .to eq [I18n.t('activerecord.errors.messages.circular_dependency')]
+ end
+ end
+ end
+
describe '.visible' do
let(:other_project) { create(:project).reload }
let(:other_wiki) { project.wiki }
@@ -182,7 +292,7 @@ describe WikiPage, type: :model do
end
it 'returns all pages for which the user has the \'view_wiki_pages\' permission' do
- expect(WikiPage.visible(user))
+ expect(described_class.visible(user))
.to match_array [wiki_page]
end
end
diff --git a/spec/models/wiki_spec.rb b/spec/models/wiki_spec.rb
index 233b282ce46..a7e54a2f535 100644
--- a/spec/models/wiki_spec.rb
+++ b/spec/models/wiki_spec.rb
@@ -131,4 +131,48 @@ describe Wiki, type: :model do
end
end
end
+
+ describe '#find_or_new_page' do
+ let(:title) { 'Übersicht' }
+ let!(:wiki_page) { create(:wiki_page, wiki:, title:) }
+
+ subject { wiki.find_or_new_page(search_string) }
+
+ context 'when using the title of an existing page' do
+ let(:search_string) { title }
+
+ it 'returns that page' do
+ expect(subject)
+ .to eq wiki_page
+ end
+ end
+
+ context 'when using the title in a different case' do
+ let(:search_string) { 'ÜBERSICHT' }
+
+ it 'finds the page' do
+ expect(subject)
+ .to eq wiki_page
+ end
+ end
+
+ context 'when using a different title' do
+ let(:search_string) { title + title }
+
+ it 'returns a wiki page' do
+ expect(subject)
+ .to be_a WikiPage
+ end
+
+ it 'returns an unpersisted record' do
+ expect(subject)
+ .to be_new_record
+ end
+
+ it 'set the title of the new wiki page' do
+ expect(subject.title)
+ .to eq search_string
+ end
+ end
+ end
end
diff --git a/spec_legacy/unit/wiki_page_spec.rb b/spec_legacy/unit/wiki_page_spec.rb
deleted file mode 100644
index 02f3bd40863..00000000000
--- a/spec_legacy/unit/wiki_page_spec.rb
+++ /dev/null
@@ -1,114 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-require_relative '../legacy_spec_helper'
-
-describe WikiPage, type: :model do
- fixtures :all
-
- before do
- @wiki = Wiki.find(1)
- @page = @wiki.pages.first
- end
-
- it 'finds or new page' do
- page = @wiki.find_or_new_page('CookBook documentation')
- assert_kind_of WikiPage, page
- assert !page.new_record?
-
- page = @wiki.find_or_new_page('Non existing page')
- assert_kind_of WikiPage, page
- assert page.new_record?
- end
-
- it 'parents title' do
- page = WikiPage.find_by(title: 'Another page')
- assert_nil page.parent_title
-
- page = WikiPage.find_by(title: 'Page with an inline image')
- assert_equal 'CookBook documentation', page.parent_title
- end
-
- it 'assigns parent' do
- page = WikiPage.find_by(title: 'Another page')
- page.parent_title = 'CookBook documentation'
- assert page.save
- page.reload
- assert_equal WikiPage.find_by(title: 'CookBook documentation'), page.parent
- end
-
- it 'unassigns parent' do
- page = WikiPage.find_by(title: 'Page with an inline image')
- page.parent_title = ''
- assert page.save
- page.reload
- assert_nil page.parent
- end
-
- it 'parents validation' do
- page = WikiPage.find_by(title: 'CookBook documentation')
-
- # A child page
- page.parent_title = 'Page with an inline image'
- assert !page.save
- assert_includes page.errors[:parent_title], I18n.t('activerecord.errors.messages.circular_dependency')
- # The page itself
- page.parent_title = 'CookBook documentation'
- assert !page.save
- assert_includes page.errors[:parent_title], I18n.t('activerecord.errors.messages.circular_dependency')
-
- page.parent_title = 'Another page'
- assert page.save
- end
-
- it 'destroys' do
- page = WikiPage.find(1)
- content_ids = WikiContent.where(page_id: 1).map(&:id)
- page.destroy
- assert_nil WikiPage.find_by(id: 1)
- # make sure that page content and its history are deleted
- assert WikiContent.where(page_id: 1).empty?
- content_ids.each do |wiki_content_id|
- assert Journal.where(journable_type: 'WikiContent',
- journable_id: wiki_content_id)
- end
- end
-
- it 'destroys should not nullify children' do
- page = WikiPage.find(2)
- child_ids = page.child_ids
- assert child_ids.any?
- page.destroy
- assert_nil WikiPage.find_by(id: 2)
-
- children = WikiPage.where(id: child_ids)
- assert_equal child_ids.size, children.size
- children.each do |child|
- assert_nil child.parent_id
- end
- end
-end