Files
danbooru/test/unit/wiki_page_test.rb
evazion 754e3a2ee2 tests: fix forum post votes & wiki page updater tests.
* Move forum post vote tests from test/controllers to test/functional.
* Fix forum post vote tests to work with new routes.
* Fix obsolete wiki page tests dealing with updater_id.
2019-10-28 02:13:10 -05:00

121 lines
3.9 KiB
Ruby

require 'test_helper'
class WikiPageTest < ActiveSupport::TestCase
setup do
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A wiki page" do
context "that is locked" do
should "not be editable by a member" do
CurrentUser.user = FactoryBot.create(:moderator_user)
@wiki_page = FactoryBot.create(:wiki_page, :is_locked => true)
CurrentUser.user = FactoryBot.create(:user)
@wiki_page.update(body: "hello")
assert_equal(["Is locked and cannot be updated"], @wiki_page.errors.full_messages)
end
should "be editable by a moderator" do
CurrentUser.user = FactoryBot.create(:moderator_user)
@wiki_page = FactoryBot.create(:wiki_page, :is_locked => true)
CurrentUser.user = FactoryBot.create(:moderator_user)
@wiki_page.update(body: "hello")
assert_equal([], @wiki_page.errors.full_messages)
end
end
context "updated by a moderator" do
setup do
@user = FactoryBot.create(:moderator_user)
CurrentUser.user = @user
@wiki_page = FactoryBot.create(:wiki_page)
end
should "allow the is_locked attribute to be updated" do
@wiki_page.update(is_locked: true)
@wiki_page.reload
assert_equal(true, @wiki_page.is_locked?)
end
end
context "updated by a regular user" do
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
@wiki_page = FactoryBot.create(:wiki_page, :title => "HOT POTATO", :other_names => "foo*bar baz")
end
should "not allow the is_locked attribute to be updated" do
@wiki_page.update(is_locked: true)
assert_equal(["Is locked and cannot be updated"], @wiki_page.errors.full_messages)
@wiki_page.reload
assert_equal(false, @wiki_page.is_locked?)
end
should "normalize its title" do
assert_equal("hot_potato", @wiki_page.title)
end
should "normalize its other names" do
@wiki_page.update(:other_names => "foo*bar baz baz 加賀(艦これ)")
assert_equal(%w[foo*bar baz 加賀(艦これ)], @wiki_page.other_names)
end
should "search by title" do
matches = WikiPage.titled("hot potato")
assert_equal(1, matches.count)
assert_equal("hot_potato", matches.first.title)
end
should "search other names with wildcards" do
matches = WikiPage.search(other_names_match: "fo*")
assert_equal([@wiki_page.id], matches.map(&:id))
end
should "create versions" do
assert_difference("WikiPageVersion.count") do
@wiki_page = FactoryBot.create(:wiki_page, :title => "xxx")
end
assert_difference("WikiPageVersion.count") do
@wiki_page.title = "yyy"
travel(1.day) do
@wiki_page.save
end
end
end
should "revert to a prior version" do
@wiki_page.title = "yyy"
travel(1.day) do
@wiki_page.save
end
version = WikiPageVersion.first
@wiki_page.revert_to!(version)
@wiki_page.reload
assert_equal("hot_potato", @wiki_page.title)
end
should "update its dtext links" do
@wiki_page.update!(body: "[[long hair]]")
assert_equal(1, @wiki_page.dtext_links.size)
assert_equal("wiki_link", @wiki_page.dtext_links.first.link_type)
assert_equal("long_hair", @wiki_page.dtext_links.first.link_target)
@wiki_page.update!(body: "http://www.google.com")
assert_equal(1, @wiki_page.dtext_links.size)
assert_equal("external_link", @wiki_page.dtext_links.first.link_type)
assert_equal("http://www.google.com", @wiki_page.dtext_links.first.link_target)
@wiki_page.update!(body: "nothing")
assert_equal(0, @wiki_page.dtext_links.size)
end
end
end
end