-- Panagsubok para iti [[Modulo:Dokumentasion]]. Pinduten ti tungtunganna a panid tapno maipataray ti panagsubok.

local doc = require('Modulo:Dokumentasion/pagipadasan')
local ScribuntoUnit = require('Modulo:ScribuntoUnit')
local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------------------
-- Test case helper functions
--------------------------------------------------------------------------------------------

local function getEnv(page)
	-- Gets an env table using the specified page.
	return doc.getEnvironment{page = page}
end

--------------------------------------------------------------------------------------------
-- Test helper functions
--------------------------------------------------------------------------------------------

function suite:testMessage()
	self:assertEquals('pagipadasan', doc.message('sandbox-subpage'))
	self:assertEquals('Dagiti subpanid daytoy a foobar', doc.message('subpages-link-display', {'foobar'}))
	self:assertEquals(true, doc.message('display-print-category', nil, 'boolean'))
end

function suite:testMakeToolbar()
	self:assertEquals(nil, doc.makeToolbar())
	self:assertEquals('<small style="font-style: normal;">(Foo)</small>', doc.makeToolbar('Foo'))
	self:assertEquals('<small style="font-style: normal;">(Foo &#124; Bar)</small>', doc.makeToolbar('Foo', 'Bar'))
end

function suite:testMakeWikilink()
	self:assertEquals('[[Foo]]', doc.makeWikilink('Foo'))
	self:assertEquals('[[Foo|Bar]]', doc.makeWikilink('Foo', 'Bar'))
end

function suite:testMakeCategoryLink()
	self:assertEquals('[[Kategoria:Foo]]', doc.makeCategoryLink('Foo'))
	self:assertEquals('[[Kategoria:Foo|Bar]]', doc.makeCategoryLink('Foo', 'Bar'))
end

function suite:testMakeUrlLink()
	self:assertEquals('[Foo Bar]', doc.makeUrlLink('Foo', 'Bar'))
end

--------------------------------------------------------------------------------------------
-- Test env table
--------------------------------------------------------------------------------------------

function suite:assertEnvFieldEquals(expected, page, field)
	local env = getEnv(page)
	self:assertEquals(expected, env[field])
end	

function suite:assertEnvTitleEquals(expected, page, titleField)
	local env = getEnv(page)
	local title = env[titleField]
	self:assertEquals(expected, title.prefixedText)
end	

function suite:testEnvTitle()
	self:assertEnvTitleEquals('Wikipedia:Pagipadasan', 'Wikipedia:Pagipadasan', 'title')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pagipadasan', 'Plantilia:Kaspagarigan/pagipadasan', 'title')
end

function suite:testEnvBadTitle()
	local env = doc.getEnvironment{page = 'Bad[]Title'}
	local title = env.title
	self:assertEquals(nil, title)
end

function suite:testEnvTemplateTitle()
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan', 'Plantilia:Kaspagarigan', 'templateTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan', 'Plantilia tungtungan:Kaspagarigan', 'templateTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan', 'Plantilia:Kaspagarigan/pagipadasan', 'templateTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan', 'Plantilia tungtungan:Kaspagarigan/pagipadasan', 'templateTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan', 'Plantilia:Kaspagarigan/pangsubok', 'templateTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/foo', 'Plantilia:Kaspagarigan/foo', 'templateTitle')
	self:assertEnvTitleEquals('Papeles:Example', 'Papeles tungtungan:Example', 'templateTitle')
	self:assertEnvTitleEquals('Papeles:Example', 'Papeles tungtungan:Example/pagipadasan', 'templateTitle')
end

function suite:testEnvDocTitle()
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/dok', 'Plantilia:Kaspagarigan', 'docTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/dok', 'Plantilia tungtungan:Kaspagarigan', 'docTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/dok', 'Plantilia:Kaspagarigan/pagipadasan', 'docTitle')
	self:assertEnvTitleEquals('Tungtungan:Kaspagarigan/dok', 'Kaspagarigan', 'docTitle')
	self:assertEnvTitleEquals('Papeles tungtungan:Example.png/dok', 'Papeles:Example.png', 'docTitle')
	self:assertEnvTitleEquals('Papeles tungtungan:Example.png/dok', 'Papeles tungtungan:Example.png/pagipadasan', 'docTitle')
end

function suite:testEnvSandboxTitle()
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pagipadasan', 'Plantilia:Kaspagarigan', 'sandboxTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pagipadasan', 'Plantilia tungtungan:Kaspagarigan', 'sandboxTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pagipadasan', 'Plantilia:Kaspagarigan/pagipadasan', 'sandboxTitle')
	self:assertEnvTitleEquals('Tungtungan:Kaspagarigan/pagipadasan', 'Kaspagarigan', 'sandboxTitle')
	self:assertEnvTitleEquals('Papeles tungtungan:Example.png/pagipadasan', 'Papeles:Example.png', 'sandboxTitle')
end

function suite:testEnvTestcasesTitle()
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pangsubok', 'Plantilia:Kaspagarigan', 'testcasesTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pangsubok', 'Plantilia tungtungan:Kaspagarigan', 'testcasesTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/pangsubok', 'Plantilia:Kaspagarigan/pangsubok', 'testcasesTitle')
	self:assertEnvTitleEquals('Tungtungan:Kaspagarigan/pangsubok', 'Kaspagarigan', 'testcasesTitle')
	self:assertEnvTitleEquals('Papeles tungtungan:Example.png/pangsubok', 'Papeles:Example.png', 'testcasesTitle')
end

function suite:testEnvPrintTitle()
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/Maimaldit', 'Plantilia:Kaspagarigan', 'printTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/Maimaldit', 'Plantilia tungtungan:Kaspagarigan', 'printTitle')
	self:assertEnvTitleEquals('Plantilia:Kaspagarigan/Maimaldit', 'Plantilia:Kaspagarigan/pagipadasan', 'printTitle')
end

function suite:testEnvProtectionLevels()
	local pipeEnv = getEnv('Plantilia:!')
	self:assertEquals('sysop', pipeEnv.protectionLevels.edit[1])
	local sandboxEnv = getEnv('Wikipedia:Pagipadasan')
	local sandboxEditLevels = sandboxEnv.protectionLevels.edit
	if sandboxEditLevels then -- sandboxEditLevels may also be nil if the page is unprotected
		self:assertEquals(nil, sandboxEditLevels[1])
	else
		self:assertEquals(nil, sandboxEditLevels)
	end
end

function suite:testEnvSubjectSpace()
	self:assertEnvFieldEquals(10, 'Plantilia:Pagipadasan', 'subjectSpace')
	self:assertEnvFieldEquals(10, 'Plantilia tungtungan:Pagipadasan', 'subjectSpace')
	self:assertEnvFieldEquals(0, 'Foo', 'subjectSpace')
	self:assertEnvFieldEquals(0, 'Tungtungan:Foo', 'subjectSpace')
end

function suite:testEnvDocSpace()
	self:assertEnvFieldEquals(10, 'Plantilia:Pagipadasan', 'docSpace')
	self:assertEnvFieldEquals(828, 'Modulo:Pagipadasan', 'docSpace')
	self:assertEnvFieldEquals(1, 'Foo', 'docSpace')
	self:assertEnvFieldEquals(7, 'Papeles:Example.png', 'docSpace')
	self:assertEnvFieldEquals(9, 'MediaWiki:Watchlist-details', 'docSpace')
	self:assertEnvFieldEquals(15, 'Kategoria:Dagiti Wikipedista', 'docSpace')
end

function suite:testEnvDocpageBase()
	self:assertEnvFieldEquals('Plantilia:Kaspagarigan', 'Plantilia:Kaspagarigan', 'docpageBase')
	self:assertEnvFieldEquals('Plantilia:Kaspagarigan', 'Plantilia:Kaspagarigan/pagipadasan', 'docpageBase')
	self:assertEnvFieldEquals('Plantilia:Kaspagarigan', 'Plantilia tungtungan:Kaspagarigan', 'docpageBase')
	self:assertEnvFieldEquals('Papeles tungtungan:Example.png', 'Papeles:Example.png', 'docpageBase')
	self:assertEnvFieldEquals('Papeles tungtungan:Example.png', 'Papeles tungtungan:Example.png', 'docpageBase')
	self:assertEnvFieldEquals('Papeles tungtungan:Example.png', 'Papeles tungtungan:Example.png/pagipadasan', 'docpageBase')
end

function suite:testEnvCompareUrl()
	-- We use "Template:Edit protected" rather than "Template:Example" here as it has a space in the title.
	local expected = '//ilo.wikipedia.org/w/index.php?title=Espesial%3AComparePages&page1=Plantilia%3ANasalakniban&page2=Plantilia%3ANasalakniban%2Fpagipadasan'
	self:assertEnvFieldEquals(expected, 'Plantilia:Nasalakniban', 'compareUrl') 
	self:assertEnvFieldEquals(expected, 'Plantilia:Nasalakniban/pagipadasan', 'compareUrl')
	self:assertEnvFieldEquals(nil, 'Plantilia:Non-existent template adsfasdg', 'compareUrl')
	self:assertEnvFieldEquals(nil, 'Plantilia:Fact', 'compareUrl') -- Exists but doesn't have a sandbox.
end

--------------------------------------------------------------------------------------------
-- Test sandbox notice
--------------------------------------------------------------------------------------------

function suite.getSandboxNoticeTestData(page)
	local env = getEnv(page)
	local templatePage = page:match('^(.*)/pagipadasan$')
	local image = '[[File:Sandbox.svg|50px|alt=|link=]]'
	local templateBlurb = 'Daytoy ket isu ti panid a [[Wikipedia:Dagiti pagsubokan a kaso ti plantilia|pagipadasan ti plantilia]] para iti [[' .. templatePage .. ']]'
	local moduleBlurb = 'Daytoy ket isu ti panid a [[Wikipedia:Dagiti pagsubokan a kaso ti plantilia|pagipadasan ti modulo]] para iti [[' .. templatePage .. ']]'
	local otherBlurb = 'Daytoy ket isu ti panid a pagipadasan para iti [[' .. templatePage .. ']]'
	local diff = '[//ilo.wikipedia.org/w/index.php?title=Espesial%3AComparePages&page1=' .. mw.uri.encode(templatePage) .. '&page2=' .. mw.uri.encode(page) .. ' dip]'
	local testcasesBlurb = 'Kitaen pay ti kakuyog a panid para iti [[' .. templatePage .. '/pangsubok|pangsubok a kaskaso]].'
	local category = '[[Kategoria:Dagiti pagipadasan ti plantilia]]'
	local clear = '<div style="clear: both;"></div>'
	return env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category, clear
end	

function suite:testSandboxNoticeNotSandbox()
	local env = getEnv('Plantilia:Kaspagarigan')
	local notice = doc.sandboxNotice({}, env)
	self:assertEquals(nil, notice)
end

function suite:testSandboxNoticeStaticVals()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category, clear = suite.getSandboxNoticeTestData('Plantilia:Kaspagarigan/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertStringContains('^' .. clear, notice, false)
	self:assertStringContains(image, notice, true)
	self:assertStringContains(category, notice, true)
end

function suite:testSandboxNoticeTemplateBlurb()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Plantilia:Kaspagarigan/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertStringContains(templateBlurb, notice, true)
end

function suite:testSandboxNoticeModuleBlurb()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Modulo:Math/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertStringContains(moduleBlurb, notice, true)
end

function suite:testSandboxNoticeOtherBlurb()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Agar-aramat:Lam-ang/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertStringContains(otherBlurb, notice, true)
end

function suite:testSandboxNoticeBlurbDiff()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Plantilia:Lam-ang/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	if mw.title.getCurrentTitle().isTalk then
		-- This test doesn't work in the debug console due to the use of frame:preprocess({{REVISIONID}}).
		-- The frame test doesn't seem to be working for now, so adding a namespace hack.
		self:assertStringContains(diff, notice, true)
	end
end

function suite:testSandboxNoticeBlurbDiffNoBasePage()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Modulo:Agar-aramat:Lam-ang/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	if mw.title.getCurrentTitle().isTalk then
		-- This test doesn't work in the debug console due to the use of frame:preprocess({{REVISIONID}}).
		-- The frame test doesn't seem to be working for now, so adding a namespace hack.
		self:assertNotStringContains(diff, notice, true)
	end
end

function suite:testSandboxNoticeTestcases()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Plantilia:Nasalakniban/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertStringContains(testcasesBlurb, notice, true)
end

function suite:testSandboxNoticeNoTestcases()
	local env, image, templateBlurb, moduleBlurb, otherBlurb, diff, testcasesBlurb, category = suite.getSandboxNoticeTestData('Plantilia:Kaspagarigan/pagipadasan')
	local notice = doc.sandboxNotice({}, env)
	self:assertNotStringContains(testcasesBlurb, notice, true)
end

--------------------------------------------------------------------------------------------
-- Test protection template
-- 
-- There's not much we can do with this until {{pp-meta}} gets rewritten in Lua. At the
-- moment the protection detection only works for the current page, and the testcases pages
-- will be unprotected.
--------------------------------------------------------------------------------------------

function suite:testProtectionTemplateUnprotectedTemplate()
	local env = getEnv('Plantilia:Kaspagarigan')
	self:assertEquals(nil, doc.protectionTemplate(env))
end

function suite:testProtectionTemplateProtectedTemplate()
	local env = getEnv('Plantilia:!')
	-- Test whether there is some content. We don't care what the content is, as the protection level
	-- detected will be for the current page, not the template.
	self:assertTrue(doc.protectionTemplate(env))
end

function suite:testProtectionTemplateUnprotectedModule()
	local env = getEnv('Modulo:Kumusta')
	self:assertEquals(nil, doc.protectionTemplate(env))
end

function suite:testProtectionTemplateProtectedModule()
	local env = getEnv('Modulo:Wensaan')
	-- Test whether there is some content. We don't care what the content is, as the protection level
	-- detected will be for the current page, not the template.
	self:assertTrue(doc.protectionTemplate(env))
end

--------------------------------------------------------------------------------------------
-- Test _startBox
--------------------------------------------------------------------------------------------

function suite:testStartBoxContentArg()
	local pattern = '<div style="padding%-bottom:3px;border%-bottom:1px solid #aaa;margin%-bottom:1ex;">\n<span style="font%-weight:bold;font%-size:125%%;">.-</span></div>'
	local startBox = doc._startBox({content = 'kas pagarigan a dokumentasion'}, getEnv('Plantilia:Kaspagarigan'))
	self:assertStringContains(pattern, startBox)
end

function suite:testStartBoxTemplateHtml()
	self:assertStringContains(
		'<div style="padding%-bottom:3px;border%-bottom:1px solid #aaa;margin%-bottom:1ex;">\n<span style="font%-weight:bold;font%-size:125%%;">.-</span><span class="doc%-editsection%-right plainlinks" id="doc_editlinks">.-</span></div>',
		doc._startBox({}, getEnv('Plantilia:Kaspagarigan'))
	)
end

function suite:testStartBoxMainHtml()
	self:assertStringContains(
		'<div style="padding%-bottom:3px;border%-bottom:1px solid #aaa;margin%-bottom:1ex;">\n<span style="font%-size:150%%;">.-</span><span class="doc%-editsection%-right plainlinks" id="doc_editlinks">.-</span></div>',
		doc._startBox({}, getEnv('Foo'))
	)
end

--------------------------------------------------------------------------------------------
-- Test makeStartBoxLinksData
--------------------------------------------------------------------------------------------

function suite:testMakeStartBoxLinksData()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxLinksData({}, env)
	self:assertEquals('Plantilia:Kaspagarigan', data.title.prefixedText)
	self:assertEquals('Plantilia:Kaspagarigan/dok', data.docTitle.prefixedText)
	self:assertEquals('kitaen', data.viewLinkDisplay)
	self:assertEquals('urnosen', data.editLinkDisplay)
	self:assertEquals('pakasaritaan', data.historyLinkDisplay)
	self:assertEquals('purgaen', data.purgeLinkDisplay)
	self:assertEquals('agpartuat', data.createLinkDisplay)
end

function suite:testMakeStartBoxLinksDataFilePreload()
	local env = getEnv('Papeles:Example.png')
	local data = doc.makeStartBoxLinksData({}, env)
	self:assertEquals('Plantilia:Dokumentasion/pangikarga-filespace', data.preload)
end

function suite:testMakeStartBoxLinksDataTemplatePreload()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxLinksData({}, env)
	self:assertEquals('Plantilia:Dokumentasion/pangikarga', data.preload)
end

function suite:testMakeStartBoxLinksDataArgsPreload()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxLinksData({preload = 'Ti naisangayan a pangikargak'}, env)
	self:assertEquals('Ti naisangayan a pangikargak', data.preload)
end

--------------------------------------------------------------------------------------------
-- Test renderStartBoxLinks
--------------------------------------------------------------------------------------------

function suite.makeExampleStartBoxLinksData(exists)
	-- Makes a data table to be used with testRenderStartBoxLinksExists and testRenderStartBoxLinksDoesntExist.
	local data = {}
	if exists then
		data.title = mw.title.new('Plantilia:!')
		data.docTitle = mw.title.new('Plantilia:!/dok')
	else
		data.title = mw.title.new('Plantilia:NonExistentTemplate')
		data.docTitle = mw.title.new('Plantilia:NonExistentTemplate/dok')
	end
	data.viewLinkDisplay = 'kitaen'
	data.editLinkDisplay = 'urnosen'
	data.historyLinkDisplay = 'pakasaritaan'
	data.purgeLinkDisplay = 'purgaen'
	data.createLinkDisplay = 'agpartuat'
	data.preload = 'Plantilia:MyPreload'
	return data
end

function suite:testRenderStartBoxLinksExists()
	local data = suite.makeExampleStartBoxLinksData(true)
	local expected = '&#91;[[Plantilia:!/dok|kitaen]]&#93; &#91;[//ilo.wikipedia.org/w/index.php?title=Plantilia:!/dok&action=edit urnosen]&#93; &#91;[//ilo.wikipedia.org/w/index.php?title=Plantilia:!/dok&action=history pakasaritaan]&#93; &#91;[//ilo.wikipedia.org/w/index.php?title=Plantilia:!&action=purge purgaen]&#93;'
	self:assertEquals(expected, doc.renderStartBoxLinks(data))
end

function suite:testRenderStartBoxLinksDoesntExist()
	local data = suite.makeExampleStartBoxLinksData(false)
	local expected = '&#91;[//ilo.wikipedia.org/w/index.php?title=Plantilia:NonExistentTemplate/dok&action=edit&preload=Plantilia%3AMyPreload agpartuat]&#93;'
	self:assertEquals(expected, doc.renderStartBoxLinks(data))
end

--------------------------------------------------------------------------------------------
-- Test makeStartBoxData
--------------------------------------------------------------------------------------------

function suite:testStartBoxDataBlankHeading()
	local data = doc.makeStartBoxData({heading = ''}, {})
	self:assertEquals(nil, data)
end

function suite:testStartBoxDataHeadingTemplate()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env)
	local expected = '[[File:Test Template Info-Icon.svg|50px|link=|alt=Simbolo ti dokumentasion]] Dokumentasion ti plantilia'
	self:assertEquals(expected, data.heading)
end

function suite:testStartBoxDataHeadingModule()
	local env = getEnv('Modulo:Kumusta')
	local data = doc.makeStartBoxData({}, env)
	local expected = '[[File:Lua-logo-nolabel.svg|30px|link=|alt=Simbolo ti dokumentasion]] Dokumentasion ti modulo'
	self:assertEquals(expected, data.heading)
end

function suite:testStartBoxDataHeadingFile()
	local env = getEnv('Papeles:Example.png')
	local data = doc.makeStartBoxData({}, env)
	local expected = 'Pakabuklan'
	self:assertEquals(expected, data.heading)
end

function suite:testStartBoxDataHeadingOther()
	local env = getEnv('Agar-aramat:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env)
	local expected = 'Dokumentasion'
	self:assertEquals(expected, data.heading)
end

function suite:testStartBoxDataHeadingStyle()
	local data = doc.makeStartBoxData({['heading-style'] = 'foo:bar'}, {})
	self:assertEquals('foo:bar', data.headingStyleText)
	self:assertEquals(nil, data.headingFontSize)
	self:assertEquals(nil, data.headingFontWeight)
end

function suite:testStartBoxDataHeadingStyleTemplate()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env)
	self:assertEquals('bold', data.headingFontWeight)
	self:assertEquals('125%', data.headingFontSize)
	self:assertEquals(nil, data.headingStyleText)
end

function suite:testStartBoxDataHeadingStyleOther()
	local env = getEnv('Agar-aramat:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env)
	self:assertEquals('150%', data.headingFontSize)
	self:assertEquals(nil, data.headingFontWeight)
	self:assertEquals(nil, data.headingStyleText)
end

function suite:testStartBoxDataLinks()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env, 'some links')
	self:assertEquals('some links', data.links)
	self:assertEquals('doc-editsection-right plainlinks', data.linksClass)
	self:assertEquals('doc_editlinks', data.linksId)
end

function suite:testStartBoxDataNoLinks()
	local env = getEnv('Plantilia:Kaspagarigan')
	local data = doc.makeStartBoxData({}, env)
	self:assertEquals(nil, data.links)
	self:assertEquals(nil, data.linksClass)
	self:assertEquals(nil, data.linksId)
end

--------------------------------------------------------------------------------------------
-- Test renderStartBox
--------------------------------------------------------------------------------------------

function suite:testRenderStartBox()
	local expected = '<div style="padding-bottom:3px;border-bottom:1px solid #aaa;margin-bottom:1ex;">\n<span></span></div>'
	self:assertEquals(expected, doc.renderStartBox{})
end

function suite:testRenderStartBoxHeadingStyleText()
	self:assertStringContains('\n<span style="foo:bar;">', doc.renderStartBox{headingStyleText = 'foo:bar'}, true)
end

function suite:testRenderStartBoxHeadingFontWeight()
	self:assertStringContains('\n<span style="font-weight:bold;">', doc.renderStartBox{headingFontWeight = 'bold'}, true)
end

function suite:testRenderStartBoxHeadingFontSize()
	self:assertStringContains('\n<span style="font-size:120%;">', doc.renderStartBox{headingFontSize = '120%'}, true)
end

function suite:testRenderStartBoxHeading()
	self:assertStringContains('\n<span>Foobar</span>', doc.renderStartBox{heading = 'Foobar'}, true)
end

function suite:testRenderStartBoxLinks()
	self:assertStringContains('<span>list of links</span>', doc.renderStartBox{links = 'list of links'}, true)
end

function suite:testRenderStartBoxLinksClass()
	self:assertStringContains('<span class="linksclass">list of links</span>', doc.renderStartBox{linksClass = 'linksclass', links = 'list of links'}, true)
	self:assertNotStringContains('linksclass', doc.renderStartBox{linksClass = 'linksclass'}, true)
end

function suite:testRenderStartBoxLinksId()
	self:assertStringContains('<span id="linksid">list of links</span>', doc.renderStartBox{linksId = 'linksid', links = 'list of links'}, true)
	self:assertNotStringContains('linksid', doc.renderStartBox{linksId = 'linksid'}, true)
end

--------------------------------------------------------------------------------------------
-- Test _content
--------------------------------------------------------------------------------------------

function suite:testContentArg()
	self:assertEquals('\nsome documentation\n', doc._content({content = 'some documentation'}, {}))
end

function suite:testContentNoContent()
	local env = getEnv('Plantilia:This is a non-existent template agauchvaiu')
	self:assertEquals('\n\n', doc._content({}, env))
end

function suite:testContentExists()
	local env = doc.getEnvironment{'Plantilia:Dokumentasion/pangsubok/test3'}
	local docs = mw.getCurrentFrame():preprocess('{{Plantilia:Dokumentasion/pangsubok/test3}}')
	local expected = '\n' .. docs .. '\n'
	self:assertEquals(expected, doc._content({}, env))
end

--------------------------------------------------------------------------------------------
-- Test _endBox
--------------------------------------------------------------------------------------------

function suite:testEndBoxLinkBoxOff()
	local env = getEnv()
	self:assertEquals(nil, doc._endBox({['link box'] = 'off'}, env))
end

function suite:testEndBoxNoDocsOtherNs()
	local env = {
		subjectSpace = 4,
		docTitle = {
			exists = false
		}
	}
	self:assertEquals(nil, doc._endBox({}, env))
end

function suite:testEndBoxAlwaysShowNs()
	self:assertTrue(doc._endBox({}, getEnv('Plantilia:Non-existent template asdfalsdhaw')))
	self:assertTrue(doc._endBox({}, getEnv('Modulo:Non-existent module asdhewbydcyg')))
	self:assertTrue(doc._endBox({}, getEnv('Agar-aramat:Non-existent user ahfliwebalisyday')))
end

function suite:testEndBoxStyles()
	local env = getEnv('Plantilia:Kaspagarigan')
	local endBox = doc._endBox({}, env)
	self:assertStringContains('id="documentation-meta-data"', endBox, true)
	self:assertStringContains('style="background-color: #F0F8FF;"', endBox, true)
	self:assertStringContains('style="font-style: italic;">', endBox, true)
	self:assertNotStringContains('[[File:', endBox, true)
	self:assertNotStringContains('[[Image:', endBox, true)
end

function suite:testEndBoxSandboxImage()
	local env = getEnv('Plantilia:Kaspagarigan/pagipadasan')
	local endBox = doc._endBox({}, env)
	self:assertNotStringContains('[[File:', endBox, true)
	self:assertNotStringContains('[[Image:', endBox, true)
end

function suite:testEndBoxLinkBoxArg()
	local env = getEnv()
	self:assertStringContains('Custom link box', doc._endBox({['link box'] = 'Custom link box'}, env))
end

function suite:testEndBoxExperimentBlurbValidNs()
	local expected = 'Mabalin dagiti editor ti agipadas kadagiti panid ti.-<br />'
	self:assertStringContains(expected, doc._endBox({}, getEnv('Plantilia:Kaspagarigan')))
	self:assertStringContains(expected, doc._endBox({}, getEnv('Modulo:Kumusta')))
	self:assertStringContains(expected, doc._endBox({}, getEnv('Agar-aramat:Kaspagarigan')))
end

function suite:testEndBoxExperimentBlurbInvalidNs()
	local expected = 'Mabalin dagiti editor ti agipadas kadagiti panid ti.-<br />'
	self:assertNotStringContains(expected, doc._endBox({}, getEnv('Wikipedia:Twinkle'))) -- Wikipedia:Twinkle has an existing /doc subpage
end

function suite:testEndBoxCategoriesBlurb()
	local expected = 'Pangngaasi nga ikabil dagiti kategoria idiay %[%[.-|/dok%]%] a subpanid% ken dagiti interwiki idiay [[WP:Wikidata|Wikidata]].'
	self:assertStringContains(expected, doc._endBox({}, getEnv('Plantilia:Kaspagarigan')))
	self:assertStringContains(expected, doc._endBox({}, getEnv('Modulo:Kumusta')))
	self:assertStringContains(expected, doc._endBox({}, getEnv('Agar-aramat:Kaspagarigan')))
	self:assertNotStringContains(expected, doc._endBox({[1] = 'Foo'}, getEnv('Plantilia:Kaspagarigan')))
	self:assertNotStringContains(expected, doc._endBox({content = 'Bar'}, getEnv('Plantilia:Kaspagarigan')))
	self:assertNotStringContains(expected, doc._endBox({}, getEnv('Wikipedia:Pagipadasan')))
end

function suite:testEndBoxPrintBlurb()
	local expected = '<br />Ti %[%[Tulong:Liblibro/para kadagiti eksperto#Panagpasayaat iti tabas ti libro|maimaldit a bersion%]%]'
		.. ' iti daytoy a plantilia ket adda idiay %[%[.-|/Maimaldit%]%]%.'
		.. ' No baliwam daytoy a plantilia, pangngaasi a baliwan met ti maimaldit a bersion%.'
	self:assertStringContains(expected, doc._endBox({}, getEnv('Plantilia:Expand list'))) -- Has a /Print subpage
	self:assertNotStringContains(expected, doc._endBox({}, getEnv('Plantilia:!'))) -- Doesn't have a /Print subpage, and is not likely to.
end

--------------------------------------------------------------------------------------------
-- Test makeDocPageBlurb
--------------------------------------------------------------------------------------------

function suite:testDocPageBlurbError()
	self:assertEquals(nil, doc.makeDocPageBlurb({}, {}))
end

function suite:testDocPageBlurbTemplateDocExists()
	local env = getEnv('Plantilia:Dokumentasion')
	local expected = 'Ti adda dita ngato a [[Wikipedia:Dokumentasion ti plantilia|dokumentasion]] ket [[Wikipedia:Panagilak-am|nailak-am]] manipud idiay [[Plantilia:Dokumentasion/dok]]. <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Plantilia:Dokumentasion/dok&action=edit urnosen] &#124; [//ilo.wikipedia.org/w/index.php?title=Plantilia:Dokumentasion/dok&action=history pakasaritaan])</small><br />'
	self:assertEquals(expected, doc.makeDocPageBlurb({}, env))
end

function suite:testDocPageBlurbTemplateDocDoesntExist()
	local env = getEnv('Plantilia:Non-existent template ajlkfdsa')
	self:assertEquals(nil, doc.makeDocPageBlurb({}, env))
end

function suite:testDocPageBlurbModuleDocExists()
	local env = getEnv('Modulo:Kumusta')
	local expected = 'Ti adda dita ngato a [[Wikipedia:Dokumentasion ti plantilia|dokumentasion]] ket [[Wikipedia:Panagilak-am|nailak-am]] manipud idiay [[Modulo:Kumusta/dok]]. <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Modulo:Kumusta/dok&action=edit urnosen] &#124; [//ilo.wikipedia.org/w/index.php?title=Modulo:Kumusta/dok&action=history pakasaritaan])</small><br />'
	self:assertEquals(expected, doc.makeDocPageBlurb({}, env))
end

function suite:testDocPageBlurbModuleDocDoesntExist()
	local env = getEnv('Modulo:Non-existent module ajlkfdsa')
	local expected = 'Mabalinmo ti [//ilo.wikipedia.org/w/index.php?title=Modulo:Non-existent_module_ajlkfdsa/dok&action=edit&preload=Plantilia%3ADokumentasion%2Fpangikarga-modulo-dok agpartuat] iti panid ti dokumentasion para iti daytoy a [[Wikipedia:Lua|modulo ti Scribunto]].<br />'
	self:assertEquals(expected, doc.makeDocPageBlurb({}, env))
end

--------------------------------------------------------------------------------------------
-- Test makeExperimentBlurb
--------------------------------------------------------------------------------------------

function suite:testExperimentBlurbTemplate()
	local env = getEnv('Plantilia:Kaspagarigan')
	self:assertStringContains("Mabalin dagiti editor ti agipadas kadagiti panid ti .- ken .- iti daytoy a plantilia.", doc.makeExperimentBlurb({}, env))
end

function suite:testExperimentBlurbModule()
	local env = getEnv('Modulo:Kumusta')
	self:assertStringContains("Mabalin dagiti editor ti agipadas kadagiti panid ti .- ken .- iti daytoy a modulo.", doc.makeExperimentBlurb({}, env))
end

function suite:testExperimentBlurbSandboxExists()
	local env = getEnv('Plantilia:Urnosen ti nasalakniban')
	local pattern = '[[Plantilia:Urnosen ti nasalakniban/pagipadasan|pagipadsan]] <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Plantilia:Urnosen_ti_nasalakniban/pagipadasan&action=edit urnosen] &#124; [//ilo.wikipedia.org/w/index.php?title=Special%3AComparePages&page1=Plantilia%3AUrnosen_ti_nasalakniban&page2=Plantilia%3AUrnosen_ti_nasalakniban%2Fpagipadasan dip])</small>'
	self:assertStringContains(pattern, doc.makeExperimentBlurb({}, env), true)
end

function suite:testExperimentBlurbSandboxDoesntExist()
	local env = getEnv('Plantilia:Non-existent template sajdfasd')
	local pattern = 'pagipadasan <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Plantilia:Non-existent_template_sajdfasd/pagipadasan&action=edit&preload=Plantilia%3ADokumentasion%2Fpangikarga-pagipadasan agpartuat] &#124; [//ilo.wikipedia.org/w/index.php?title=Plantilia:Non-existent_template_sajdfasd/pagipadasan&pangikarga=Plantilia%3ANon-existent+template+sajdfasd&action=edit&summary=Agpartuat+ti+pagipadasan+a+bersion+ti+%5B%5BPlantilia%3ANon-existent+template+sajdfasd%5D%5D kapada])</small>'
	self:assertStringContains(pattern, doc.makeExperimentBlurb({}, env), true)
end

function suite:testExperimentBlurbTestcasesExist()
	local env = getEnv('Plantilia:Urnosen ti nasalakniban')
	local pattern = '[[Plantilia:Urnosen ti nasalakniban/pangsubok|pangsubok]] <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Plantilia:Urnosen_ti_nasalakniban/pangsubok&action=edit urnosen])</small>'
	self:assertStringContains(pattern, doc.makeExperimentBlurb({}, env), true)
end

function suite:testExperimentBlurbTestcasesDontExist()
	local env = getEnv('Plantilia:Non-existent template sajdfasd')
	local pattern = 'subokan <small style="font-style: normal;">([//ilo.wikipedia.org/w/index.php?title=Plantilia:Non-existent_template_sajdfasd/pangsubok&action=edit&preload=Plantilia%3ADokumentasion%2Fpangikarga-pangsubok agpartuat])</small>'
	self:assertStringContains(pattern, doc.makeExperimentBlurb({}, env), true)
end

--------------------------------------------------------------------------------------------
-- Test makeCategoriesBlurb
--------------------------------------------------------------------------------------------

function suite:testMakeCategoriesBlurb()
	local env = getEnv('Plantilia:Kaspagarigan')
	self:assertEquals('Pangngaasi nga ikabil dagiti kategoria idiay [[Plantilia:Kaspagarigan/dok|/dok]] a subpanid ken dagiti interwiki idiay [[WP:Wikidata|Wikidata]].', doc.makeCategoriesBlurb({}, env))
end

--------------------------------------------------------------------------------------------
-- Test makeSubpagesBlurb
--------------------------------------------------------------------------------------------

function suite:testMakeSubpagesBlurbTemplate()
	local env = getEnv('Plantilia:Kaspagarigan')
	self:assertEquals('[[Special:PrefixIndex/Plantilia:Kaspagarigan/|Dagiti subpanid daytoy a plantilia]].', doc.makeSubpagesBlurb({}, env))
end

function suite:testMakeSubpagesBlurbModule()
	local env = getEnv('Modulo:Kumusta')
	self:assertEquals('[[Special:PrefixIndex/Modulo:Kumusta/|Dagiti subpanid daytoy a modulo]].', doc.makeSubpagesBlurb({}, env))
end

function suite:testMakeSubpagesBlurbOther()
	local env = getEnv('Papeles:Example.png')
	self:assertEquals('[[Special:PrefixIndex/Papeles:Example.png/|Dagiti subpanid daytoy a panid]].', doc.makeSubpagesBlurb({}, env))
end

--------------------------------------------------------------------------------------------
-- Test makePrintBlurb
--------------------------------------------------------------------------------------------

function suite:testMakePrintBlurbExists()
	local env = {
		printTitle = {
			exists = true,
			prefixedText = 'Plantilia:Kaspagarigan/Maimaldit'
		}
	}
	local expected = 'Ti [[Tulong:Liblibro/para kadagiti eksperto#Panagpasayaat ti tabas ti libro|maimaldit a bersion]]'
		.. ' iti daytoy a plantilia ket adda idiay [[Plantilia:Kaspagarigan/Maimaldit|/Maimaldit]].'
		.. ' No baliwam daytoy a plantilia, pangngaasi a baliwan met ti maimaldit a bersion.'
		.. '[[Kategoria:Dagiti plantilia nga addaan kadagiti maimaldit a bersion]]'
	self:assertEquals(expected, doc.makePrintBlurb({}, env))
end

function suite:testMakePrintBlurbDoesntExist()
	local env = {
		printTitle = {
			exists = false,
			prefixedText = 'Plantilia:Kaspagarigan/Maimaldit'
		}
	}
	self:assertEquals(nil, doc.makePrintBlurb({}, env))
end

--------------------------------------------------------------------------------------------
-- Test addTrackingCategories
--------------------------------------------------------------------------------------------

function suite.getStrangeUsageCat()
	return '[[Kategoria:Dagiti panid ti Wikipedia nga addaan kadagiti nakakaskasdaaw a panagusar ti ((dokumentasion))]]'
end

function suite:testAddTrackingCategoriesTemplatePage()
	local env = getEnv('Plantilia:Kaspagarigan')
	self:assertEquals('', doc.addTrackingCategories(env))
end

function suite:testAddTrackingCategoriesDocPage()
	local env = getEnv('Plantilia:Kaspagarigan/dok')
	self:assertEquals(self.getStrangeUsageCat(), doc.addTrackingCategories(env))
end

function suite:testAddTrackingCategoriesTestcasesPage()
	local env = getEnv('Plantilia:Kaspagarigan/pangsubok')
	self:assertEquals(self.getStrangeUsageCat(), doc.addTrackingCategories(env))
end

function suite:testAddTrackingCategoriesModuleDoc()
	local env = getEnv('Modulo:Math/dok')
	self:assertEquals(self.getStrangeUsageCat(), doc.addTrackingCategories(env))
end

function suite:testAddTrackingCategoriesModuleTestcases()
	local env = getEnv('Modulo:Math/pangsubok')
	self:assertEquals('', doc.addTrackingCategories(env))
end

function suite:testAddTrackingCategoriesInvalidTitle()
	local env = getEnv('Plantilia:Foo[]Bar')
	self:assertEquals(nil, doc.addTrackingCategories(env))
end

--------------------------------------------------------------------------------------------
-- Whole-module tests
--------------------------------------------------------------------------------------------

function suite:testSandboxImageBug()
	self:assertNotStringContains('<table id="documentation%-meta%-data".-%[%[%w+:Sandbox.svg', doc.main{page = 'Plantilia:Kaspagarigan/pagipadasan'})
end

return suite